-4

Why is my code output showing 4200848 before entering the diameter?

I have tried restarting VS Code and restarting my laptop too. After checking my code countless number of times and doing the afforementioned things, I thought that my program would run smoothly but it didn't actually.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
  • 5
    Please edit your question and post the code as text in the question itself. – VLL May 31 '23 at 05:53
  • 2
    Local variables are not initialized. Their values are *indeterminate*. Using indeterminate values could lead to *undefined behavior*. And your code prints the indeterminate value of the uninitialized `diameter` variable. – Some programmer dude May 31 '23 at 05:58
  • 4
    Please don't post pictures of code. Post actual code as text. I've updated your question to inline the image, but that doesn't mean you shouldn't correct it to just be text. – selbie May 31 '23 at 05:58
  • @Someprogrammerdude What do you mean "could"? – Yunnosch May 31 '23 at 05:58
  • 2
    @Yunnosch IIRC UB only happens if the value happens to be a trap representation. – Some programmer dude May 31 '23 at 05:59
  • 1
    Please provide a textual [mre] to demonstrate your observations. – Yunnosch May 31 '23 at 05:59
  • 2
    My psychic powers suggest that you didn't intend to end the printf with `...%d", diamater);` Maybe just `printf("Enter the diameter\n");` will do. – selbie May 31 '23 at 06:02
  • 2
    What other number did you expect when you print that variable? And why? – Gerhardh May 31 '23 at 06:03
  • What output do you expect there? Why at all do you output any value there? What is the purpose, the intetion of asking "What diameter should I use? Oh by the way, here is a diameter I happen to have here, no relevance, but I thought you might enjoy seeing it." – Yunnosch May 31 '23 at 06:03
  • Or to put it differently: Are you more surprised by the specific value? Or are you actually surprised by the fact that any value at all is appearing there? The answers are very different. The answer for the specific value is more or less given in the first few comments: "undefined behaviour for use of uninitialised values". The answer for "Why at all a value?" is simply "Because you programmed it." – Yunnosch May 31 '23 at 06:05
  • I suggest that you read this: [Why not upload images of code/data/errors?](https://meta.stackoverflow.com/q/285551/12149471) – Andreas Wenzel May 31 '23 at 06:08

2 Answers2

1

You are not initializing diameter, but you are trying to print it with printf. When you don't initialize a variable, the value inside it is undefined i.e it can be anything depending on what the memory contains at runtime.

int diameter; // undefined value: 4200848 in your case
printf("Enter the diameter of the circle %d", diameter) // prints diameter

Instead, you should first get the diameter, then print it.

int diameter = 0;
printf("Enter the diameter of the circle: ");
scanf("%d", &diameter);
printf("The diameter is %d", diameter);
  • 1
    The code should also check that the `scanf()` call was successful. – Jonathan Leffler May 31 '23 at 06:29
  • There is [an answer by the knowledgeable Jens Gustedt](https://stackoverflow.com/a/11965368/3150802) discussing that accessing uninitialized automatic variables which may be in registers is undefined behavior. Taking an address of a variable prevents it from being in a register (hence no UB), and common sense suggests that this is true even if the address is taken *after* the access; but I'm not sure whether the standard dictates that ;-). – Peter - Reinstate Monica May 31 '23 at 06:59
  • @Peter-ReinstateMonica Correct - this is not undefined behavior but unspecified behavior. The value of `diameter` is indeterminate. It may in theory be a trap representation, but real-world computers don't use trap representations for plain integers. So pedantically, the value is not "undefined" but _indeterminate_. – Lundin May 31 '23 at 07:33
0

Update the variable with input from the user and then try to output it to the Console. If you try to print the diameter variable before assigning it with value enter by the user, you might get a crazy number which is undefined.

#include <stdio.h>
#include <math.h>

int main() {
//declare the diameter variable
int diameter ;
//ask the user to enter a value
printf("%s","Enter the diameter of the circle");
//use scanf to capture input from the user 
scanf(&diameter);
//now you can print the content of the diameter entered 
printf("%s %d","The diameter entered is ", diameter);
//use the diameter for computing the area
} 
Son of Man
  • 1,213
  • 2
  • 7
  • 27