1

Why does the following snippet cause random numbers to print to the screen with printf, but putchar always outputs 1?

#include <stdio.h>

int main() {
    char c;

    printf("%d\n", c );
    putchar(c);
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Daniel
  • 1,284
  • 3
  • 12
  • 17

2 Answers2

4

According to C99 standard, this is undefined behavior. Let's see why:

Section 6.7.8.9 says that

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

This applies to your variable c, because it has automatic storage duration, and it is not initialized explicitly.

Section J.2 says that

The behavior is undefined in the following circumstances:

...

The value of an object with automatic storage duration is used while it is indeterminate

This applies to your code as well, because you read c when you pass it as a parameter to both printf and putchar, and the value of c is still indeterminate, because it has not been assigned.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

1) The c variable first has a random value(default/garbage value) to itself as you declared-but-did-not-initialize your char c to any defined letter or value of ur interest(character).
2) Next you tried to printf the %d(digit/decimal/numerical value) of the char c, so now it is giving you a converted value of the garbage which was earlier assigned to c when you declared the char c in the first place.
3) Finally you tried to use putchar(c), which again behaves similarly because your char c is uninitialized and is still being read thereby re-trying to manage with an undetermined value to be printed onto the screen. (since the same un-initialized character variable c is being passed to both kind of printing as a parameter).

Yes these 3 statements are a bit clumsy to understand but they are as layman as it can get to help speed-up some understanding regarding this query of yours.

Pay attention to the 1st comment response to your question by @bluemoon. Those 3 words alone litterally have a huge amount of sensibility and meaningfull-ness to them, to a point that it also tells you what you have done erroneous in your own code(your actions)."UNDEFINED"(try relating the same with UNINITIALIZED).

Code Man
  • 105
  • 1
  • 2
  • 14
  • also i failed to understand why your very question was not an answer to itself in the first place?... unless you went through some basic information on variables behaviour and printing output on screen which are like the few "must-dos" before you move on to chapter 2s of any language. These things are the first lessons for most language learning. However, having said that i am open-minded to weird patterns of learning as it sometimes(as seldom as it may be) can produce unexpectedly interesting observations. hence i answered anyway, but this one killed my hopes for the same. – Code Man Sep 24 '15 at 20:55
  • 1
    I have been coding for a while. I am just now learning C though. I did this on purpose because I wanted to know what would happen, and thought maybe the compiler would throw an error. When it didn't, I turned to the web for an answer. – Daniel Sep 25 '15 at 04:39
  • @Daniel me too m a beginner for C/C++...what i can share as an experience is .....trying variety of permutations and combinations with the unknown/doubtful things often gives you a definite pattern, and that "definite pattern" is what is called as the word "definition"(answers), for certain things. Newaz i hope the answer helped your understanding is what i concern with at the end of the day. – Code Man Sep 25 '15 at 04:54