1

I was playing with the single line declaration and initialization of multiple variables and notices something out of ordinary in C

#include <stdio.h>

int main() {
    int a, c, d, e = 0;
    printf("a = %d, c = %d, d = %d, e = %d", a, c, d, e);
}

So the output of the above program was

a = 2961408, c = 0, d = 4200720, e = 0

Easy since e is assigned 0 and rest all may be garbage value but c has the value 0. I changed the code to

#include <stdio.h>

int main() {
    int a, b, c, d, e = 0;
    printf("a = %d, b = %d, c = %d, d = %d, e = %d", a, b, c, d, e);
}

and this time the output was:

a = 2297856, b = 0, c = 4200736, d = 4200848, e = 0

Again the second variable was initialized as 0 [Here b, previously c] along with e and rest all are garbage.

And the same thing was noticed for 3, 4, 5, 6, 7, and so on variables every time the second variable in the declaration is initialized the value 0 along with the last variable.

This is not the case of initialization of last variable but if I assign any variable after the second variable with value 0. My second variable get 0 as it's initialization value. Is it something wrong with my compiler or is there some logic behind this in C?

#include <stdio.h>

int main() {
    int a, b, c, d, e, g = 10, f;
    printf("a = %d, b = %d, c = %d, d = %d, e = %d, g = %d, f = %d", a, b, c, d, e, g, f);
}

Output:

a = 3702784, b = 0, c = 4200752, d = 4200864, e = 6422400, g = 10, f = 4200752

[P.S. My C compiler is gcc (MinGW.org GCC Build-2) 9.2.0 ]

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Aniket Thomas
  • 343
  • 2
  • 9
  • 1
    Zero is also one of the possible garbage values. (Garbage doesn't mean "looks weird", it means "uninitialized, therefore I cannot know its value".) – M Oehm Jun 02 '21 at 08:16
  • But every time it is initialised as 0 whereas all other value is changing the garbage value – Aniket Thomas Jun 02 '21 at 08:22
  • See: [Undefined, unspecified and implementation-defined behavior](https://stackoverflow.com/q/2397984/3422102) and [What is indeterminate behavior in C++ ? How is it different from undefined behavior?](https://stackoverflow.com/q/11240484/3422102) and [Undefined behavior](https://en.cppreference.com/w/c/language/behavior) and ... [nasal demons: n.](http://www.catb.org/jargon/html/N/nasal-demons.html) Basically *Undefined* means undefined -- like a Box of Chocolates -- you never know what you are going to get... – David C. Rankin Jun 02 '21 at 08:23
  • The specific case is [C11 Standard - J.2 Undefined Behavior](http://port70.net/~nsz/c/c11/n1570.html#J.2) *"The value of an object with automatic storage duration is used while it is indeterminate (6.2.4, 6.7.9, 6.8)."* – David C. Rankin Jun 02 '21 at 08:25
  • 1
    `c` is **not** initialised. It also has a garbage value. There is no value judement about garbage. Being a familiar number does make it "not garbage". But what happens to be in uninitialised memory is related to activities that took place earlier, and if that activity was the same each time, then the values are not "random". They are still garbage though. – Weather Vane Jun 02 '21 at 08:36
  • 1
    Even if an undefined behavior seems reproducible in a given program compiled with a given compiler and run on a given computer doesn't mean that it will always be the case. Do not write a program that rely on **any** undefined behavior. Some day when context changed, your program will be broken. – fpiette Jun 02 '21 at 08:40
  • My neighbour throws away an empty cereal box every Friday at 9.00am. I throw away random items at rantom times. Is his garbage less garbage than my garbage just because it happens to be the same every time and looks familiar? – n. m. could be an AI Jun 02 '21 at 08:51
  • 1
    Can't resist saying that one person's garbage is another's gold mine. – Weather Vane Jun 02 '21 at 08:51
  • 1
    Trying to find a pattern in the uninitialized values of automatic variables may seem entertaining, but it's not going to help you with programming. (As Weather Vane points out, garbage values often come from previous activities. Zero is a frequent value, so you will find it in the garbage often.) – M Oehm Jun 02 '21 at 08:58
  • @TomKarzes: The C standard does not define anything to be unpredictable. – Eric Postpischil Jun 02 '21 at 10:54

1 Answers1

3

Is it something wrong with my compiler

No.

or is there some logic behind this in C?

No.

Reading uninitialized local values give you indeterminate values. This is so-called undefined behavior - the first thing you need to understand is what that means. What is undefined behavior and how does it work? As I wrote there:

It is important to investigate the cause of the undefined behavior - not so much the symptoms. Beginners often ask why a certain behavior occurred when they did something undefined, but there is often not much to learn from investigating the outcome. Time is better spent on learning what caused the undefined behavior.

Now the reason why this is undefined behavior is actually because you didn't take the address of those uninitialized local variables, see this answer: (Why) is using an uninitialized variable undefined behavior? So the program might as well just crash.

That issue aside, on many systems indeterminate values manifest themselves as printing whatever happened to be stored at some random locations in RAM. The way RAM memory works, we never "delete" or "erase" anything from it. We just keep track of which locations that are used, then when they aren't used any longer, whatever happened to be stored there remains.

But there are no guarantees of anything so an optimizing compiler might just as well print the contents of some random CPU register. Again, there's nothing meaningful to learn from this. You might as well toss some dice and then try to find a reason why you got certain values.

Some bad compilers utilize a "debug build" where the whole RAM is set to zero, so on such systems you'll get all zeroes. This is bad since it hides away bugs until you switch from debug to release.

Anyway, the only thing important to learn from this is gcc/mingw would have whined about uninitalized variables in case you used -Wall. As a beginner, make a habit of compiling with:

gcc -std=c11 -pedantic-errors -Werror -Wall -Wextra
Lundin
  • 195,001
  • 40
  • 254
  • 396