-2

I saw the following code in the wild and I don't know what to make of it:

more or less:

int main(void)
{
   int a = 0, v;
   printf("%d\n", v); 
} 

This code with gcc will print out 0. At first I though, oh well that's because initialized local variables are assigned 0, but in this case I never declared the type of v...so what gives?

  • 5
    *initialized local variables are assigned 0* - No, it have garbage. And the type of `v` is `int`. – Maroun Dec 02 '13 at 06:18
  • 4
    The type of v is `int`. And the behavior is still undefined. `v`'s value is [*indeterminate*](http://stackoverflow.com/a/17394924/1322972). – WhozCraig Dec 02 '13 at 06:18
  • 1
    On my machine I get 1664045150 and not 0. You just happened to get 0. – Ray Toal Dec 02 '13 at 06:18
  • 4
    FWIW I think a better title for this question would be "Value of unassigned local variables" because as it stands your program does not have any assignment to `v` nor can you call `v` unreferenced, because you ... clearly referenced it! #Pedantic :) – Ray Toal Dec 02 '13 at 06:20
  • Reading an uninitialized variable results in undefined behavior. – Ed S. Dec 02 '13 at 06:23
  • @EdgarAroutiounian Instead of guessing, I recommend that you read over the basics of C more carefully first. The answers are helpful too. – Xiaolei Zhu Dec 02 '13 at 06:26

5 Answers5

4
int a = 0, v;

is equivalent to:

int a = 0;
int v;

So you did declare the type of v, just not explicitly. It's an int.

Anyway, like all uninitialized local variables, v's value isn't guaranteed to be anything. Accessing it is still undefined behavior; you just happened to get 0.

Community
  • 1
  • 1
Maxpm
  • 24,113
  • 33
  • 111
  • 170
1

What do you think the comma does in a declaration statement?

In C - C99 and C++, an initializer is an optional part of a declarator. The init-declarator-list is a comma-separated sequence of declarators, each of which can have additional type information, or an initializer, or both.

As such, your expression int a = 0, v; does declare v as a an int.

Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

Type of v is int. Please read the syntax of variable declaration.

You are just lucky that it is printing 0. Value of v is garbage.

doptimusprime
  • 9,115
  • 6
  • 52
  • 90
1

From ISO/IEC 9899:TC2 section 6.7.8 Semantics

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.
If an object that has static storage duration is not initialized explicitly, then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules;
— if it is a union, the first named member is initialized (recursively) according to these rules.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
0

From Wiki,

Multiple variables can be declared with one statement, like this:

int anumber, anothernumber, yetanothernumber;
Abhineet
  • 5,320
  • 1
  • 25
  • 43