0

I'm running C, and I'm wondering why the output for this shows "I am 89 years old". I get that the variable currentyr and birthyr should come before age, but that still doesn't explain to me how the code still runs without error and produces 89. I'm coming from racket, where if something is wrong all it does is give me an error rather than give me a random output so I'm a little confused.

int age;
int currentyr;
int birthyr;

age = currentyr - birthyr;
currentyr = 2018;
birthyr = 2000;

printf("I am %d years old", age);
ming
  • 229
  • 1
  • 9
  • 2
    uninitialized variables contains indeterminate values. – kiran Biradar Dec 25 '18 at 05:01
  • I mentioned already that I know what the "error" is, that I used undefined variables in my age calculation, but I just want to understand what is going on in my code that leads to an output of 89. I'm used to the program just shutting down and saying there's an error if something is wrong with the code, but I don't understand why it continues and I don't get why 89 is produced – ming Dec 25 '18 at 05:23
  • Just put `age = currentyr - birthyr;` after `birthyr = 2000;`. `89` output is because of some garbage value. – kiner_shah Dec 25 '18 at 06:39
  • Again, I know the error, but how did the garbage value get there? Is it just some random number/ – ming Dec 27 '18 at 04:28

3 Answers3

2

You should initialize the values of the current and birth year before computing the age:

currentyr = 2018;
birthyr = 2000;
age = currentyr - birthyr;

printf("I am %d years old", age);

Most likely what is happening now is that your age difference is being computed with undefined or possibly random values.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I mentioned already that I know what the "error" is, that I used undefined variables in my age calculation, but I just want to understand what is going on in my code that leads to an output of 89. I'm used to the program just shutting down and saying there's an error if something is wrong with the code, but I don't understand why it continues and I don't get why 89 is produces – ming Dec 25 '18 at 05:23
  • @ming The value could be _anything_ other than 89 as well. Run your code on a dozen different C compilers, and you would probably see at least a handful of different values. The correct answer is the only thing with which you should really be concerned here. – Tim Biegeleisen Dec 25 '18 at 05:24
  • @ming _I know what the "error" is, that I used undefined variables in my age calculation ?_ Then you shouldn't proceed further. Compile your code with `-Wall -Werror` which will say `error: ‘currentyr’ is used uninitialized in this function [-Werror=uninitialized]` and compilation will stops. – Achal Dec 25 '18 at 05:36
0

but that still doesn't explain to me how the code still runs without error and produces 89...

The C compilers provide set of options which you can use to request or suppress warning messages. For e.g. if you are using the gcc compiler then it provides the set of options which you can use while compiling your code. One of the gcc option is -Wall which enables all the warning about constructions. If you compile your program with this -Wall then compiler report the warning about the use of uninitialized variable in your program:

# gcc -Wall  prg.c 
prg.c:8:7: warning: variable 'currentyr' is uninitialized when used here [-Wuninitialized]
age = currentyr - birthyr;
      ^~~~~~~~~
prg.c:5:14: note: initialize the variable 'currentyr' to silence this warning
int currentyr;
             ^
              = 0
prg.c:8:19: warning: variable 'birthyr' is uninitialized when used here [-Wuninitialized]
age = currentyr - birthyr;
                  ^~~~~~~
prg.c:6:12: note: initialize the variable 'birthyr' to silence this warning
int birthyr;
           ^
            = 0
2 warnings generated.

See the warning messages related to variables uninitialized when used.
Also, you can convert these warning messages to error by using -Werror option of gcc compiler.

From C Standards#6.7.9p10:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

On your system the calculation using those indeterminate values is producing result 89. You may not get result 89 every time when you run your program. On my system, I am getting -

I am 218173908 years old
H.S.
  • 11,654
  • 2
  • 15
  • 32
-1

Maybe this question can give you the answer.

Why does it continue? Why does it produce 89? It's the result of C language standard and the compiler.

wall e
  • 1
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Vasilisa Dec 25 '18 at 07:16