-2

Here is a program in C language that simply displays variables that have not been initialized,

#include <stdio.h>

int main()
{
int a, b;
printf("%d%d",a,b);

return 0;
}

Output: 00

I have started learning c programming 20 minutes ago for the first time in my life, can you tell me why does it show the output as 00, now since we're supposed to initialize out variables with 0 in most cases to avoid seeing the garbage value in their place so I just wanted to know why does it happen ? Why don't we get a garbage value instead and not the plain '0' which has its own ASCII value?

sam
  • 15
  • 2
  • 5
    What would you expect it to show? Have you covered the *undefined behavior* concept in these 20 minutes yet? – Eugene Sh. Jan 12 '18 at 17:25
  • UB UB.......... – user2736738 Jan 12 '18 at 17:26
  • 1
    I thought it would show the ASCII values of a and b – sam Jan 12 '18 at 17:27
  • @sam why would it do that? Also, try running your code under a debugger like Visual Studio and see what happens. It might be different. You might get some interesting values. –  Jan 12 '18 at 17:27
  • What, like `9798`? What do variable names have to do with their contents? – melpomene Jan 12 '18 at 17:28
  • @sam, get thee to a C tutorial and start at the beginning. –  Jan 12 '18 at 17:28
  • change your code to `int a=1,b=2;` and see what happens – pm100 Jan 12 '18 at 17:28
  • Then you have enough time to go over the concepts slowly and thoroughly. – Eugene Sh. Jan 12 '18 at 17:29
  • it did show the contents of a and b, they both happen to contains 0 - purely by accident – pm100 Jan 12 '18 at 17:29
  • btw I'm 9 so not late eh? – sam Jan 12 '18 at 17:29
  • The memory just happens to be 0. Don't rely on that. If you run it enough times you might encounter a different result – mdatsev Jan 12 '18 at 17:31
  • @jdv why would you tell someone, who doesn't even know what a variable is, to use a debugger? – lpares12 Jan 12 '18 at 17:32
  • 1
    @lpares12 1. it wasn't clear that this person didn't know _any_ programming. The question says specifically C. Maybe they know Ruby, or Forth, or COBOL? 2. It wasn't clear this person was a complete novice until after the comment. 3. As a learning exercise, novice coders should learn IDEs and debugging techniques early and often. And once the OP understands variables, debugging results may not only be interesting, but illuminating. –  Jan 12 '18 at 17:34
  • Knows of ASCII so not a novice programmer. Do you cover ASCII in the first 20 minutes? – Weather Vane Jan 12 '18 at 17:35
  • @WeatherVane Looks like *happened to hear* the term "ASCII"... – Eugene Sh. Jan 12 '18 at 17:36
  • 3
    I'm voting to close this question as off-topic because after learning a new language for 20 minutes, your first impulse when hitting something unexpected should be to read a textbook to familiarize yourself with the language concepts. – Jens Jan 12 '18 at 17:48
  • Why couldn't it give garbage values – sam Jan 13 '18 at 00:55
  • They are garbage values, they just happen to be garbage values of value zero. For what you know, they could next time be your ATM pin and your birthday. – Yunnosch Jan 13 '18 at 07:44
  • I have reworded the problem so in case if you have some faith in me then please remove the hold !! – sam Jan 13 '18 at 08:09

3 Answers3

3
int x = 0;
printf("%d", x); // -> 0

shows the value of x as an integer in memory. However, uninitialized variables cannot be trusted to have a value of zero every time. See What happens to a declared, uninitialized variable in C? Does it have a value?

If you want the character representation of that integer, use

int x = 99;
printf("%c", x); // -> c

instead. It will give the ASCII representation. int x = 99; means 'c')

Felix
  • 2,548
  • 19
  • 48
1

variable not assigned doesn't mean that it has no value, it just means that the value is not defined (or known if your prefer). But be careful, reading a variable that has never been initialised is undefined behaviour.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
0

By default C does not give a value to uninitialized variables, you have to tell it what to store in each variable. If you do attempt to use these variables without giving them a value, the output will be unpredictable.

For example when I ran this on my pc I go the value: 41944324050944

Do not do this. If you do your program will throw unexpected results when you run it later. See this.

The proper way you would do this is to define a and b like this:

int a = 0;
int b = 0;

In the case of defining static storage the default value would be 0 (always) see this web page for more information.

This program will return 00 as expected:

#include <stdio.h>

int main()
{
    static int a, b;
    printf("%d%d",a,b);

    return 0;
}

even-though a and b are not assigned a value the use static storage and therefore have a value 0.

Also if you want ASCII characters printed use %c instead of %d. See here. Also note that 00 in ASCII is null see here for printing null variables.

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • "*By default C does not give a value to uninitialized variables*" ... unless they have static storage; then they default to 0. – melpomene Jan 12 '18 at 17:42
  • @melpomene See my updated answer – Xantium Jan 12 '18 at 18:06
  • In the early days, the value of an uninitialized object would be whatever happened to be in the memory allocated for it. But C moved beyond that decades ago. The behavior of C is now defined in an abstract model, and compilers do not implement C initializations, assignments, and expressions with simple direct translations to machine code. C source code is translated to an intermediate language. In that intermediate form, mathematical transformations are performed (largely for optimization purposes). Those transformations keep **defined** behavior of the language (and other behavior the… – Eric Postpischil Jan 12 '18 at 18:48
  • … implementors choose), but they may change anything else about the program. Only after the transformations is machine code produced. Objects may no longer correspond to single regions of memory. C permits uninitialized objects to appear to have different values at different times, and to cause other problems. So, no, an uninitialized object does not have whatever value happened to be in memory at a place allocated for it. – Eric Postpischil Jan 12 '18 at 18:49
  • @EricPostpischil Thank you I did not know that. Although I will remove the "garbage" part I am unsure what to put back so I'll probably do some research. Thank you for explaining the reason for the down-vote, taking the time to explain everything and correcting me when I go wrong. – Xantium Jan 12 '18 at 19:02
  • @simon I think it will show the garbage value ? – sam Jan 13 '18 at 06:56
  • @sam yes the value will be unpredictable but not necessarily what is stored in that part of the memory. If you try it again you will probably get a different value – Xantium Jan 13 '18 at 07:41