0

I'm new to C, this is My code. Maybe this is too easy for someone else.

#include <stdio.h> int main() //Assign value to identifiers { int a,b,c,d=6; printf("a:%d\n",a); printf("b:%d\n",b); printf("c:%d\n",c); printf("d:%d",d); return 0; } 

Why is a=16, b=0, and c=12522400?

a:16 b:0 c:12522400 d:6 
7
  • 1
    By chance. Values of uninitialized non-static local variables are indeterminate. If you want to know the reason of this specific result, you will need deep inspection of generated code and some other information of your environment. Commented Oct 30, 2020 at 23:45
  • 1
    Maybe take a look at this: stackoverflow.com/questions/6838408/… Commented Oct 30, 2020 at 23:45
  • 1
    What value did you expect them to have? Commented Oct 30, 2020 at 23:48
  • Try int a=6, b=6, c=6, d=6; instead. Commented Oct 30, 2020 at 23:51
  • @klutt I expect them all to be 6. Commented Oct 30, 2020 at 23:57

1 Answer 1

1

Because those variables are not initialized. You always have to initialize a local, automatic variable to avoid undefined behaviour.

For more details look here: https://en.wikipedia.org/wiki/Uninitialized_variable

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.