user12341234 user12341234 - 1 year ago 47
C Question

Declare and use variable in same statement

In C is it valid to use a variable in the same statement in which it is declared?

In both gcc 4.9 and clang 3.5 the following program compiles and runs without error:

#include "stdio.h"

int main() {
int x = x;
printf("%d\n", x);

In gcc it outputs
and in clang
(which is largest positive 2-byte integer value).

Why does this not cause a compilation error? Is this valid in any particular C specification? Is its behavior explicitly undefined?

Answer Source
int x = x;

This is "valid" in the sense that it doesn't violate a constraint or syntax rule, so no compile-time diagnostic is required. The name x is visible within the initializer, and refers to the object being declared. This allows for things like:

int x = 10, y = x + 1;

But the declaration has undefined behavior, because the initializer refers to an object that hasn't been initialized.

The explicit statement that the behavior is undefined is in N1570 paragraph 2, which describes the "conversion" of an lvalue (an expression that designates an object) to the value stored in that object.

Except when [list of cases that don't apply here, an lvalue that does not have array type is converted to the value stored in the designated object (and is no longer an lvalue); this is called lvalue conversion.
If the lvalue designates an object of automatic storage duration that could have been declared with the register storage class (never had its address taken), and that object is uninitialized (not declared with an initializer and no assignment to it has been performed prior to use), the behavior is undefined.

The object in question is x, referenced in the initializer. At that point, no value has been assigned to x, so the expression has undefined behavior.

In practice, you'll probably get a compile-time warning if you enable a high enough warning level. The actual behavior might be the same as if you had omitted the initializer:

int x;

but don't count on it.