0

Sample code:

void f(void)
{
    int x;
    int x;
}

Invocations:

$ gcc t0.c -std=c11 -pedantic -Wall -Wextra
t0.c: In function 'f':
t0.c:4:9: error: redeclaration of 'x' with no linkage
    4 |     int x;
      |         ^
t0.c:3:9: note: previous declaration of 'x' with type 'int'
    3 |     int x;
      |         ^

$ clang t0.c -std=c11 -pedantic -Wall -Wextra
t0.c:4:9: error: redefinition of 'x'
    int x;
        ^
t0.c:3:9: note: previous definition is here
    int x;
        ^

$ cl t0.c /std:c11 /Za
t0.c(4): error C2086: 'int x': redefinition
t0.c(3): note: see declaration of 'x'

As I understand int x; is a definition of an identifier x. Is that correct?

pmor
  • 5,392
  • 4
  • 17
  • 36
  • 3
    `int x;` inside a function is a definition of `x`. It is also a declaration of `x`. All the compiler phrasings shown are correct. – Eric Postpischil Mar 04 '22 at 15:52
  • Every definition is also a declaration; some declarations are not definitions. See also [What is the difference between a declaration and a definition?](https://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration) – Jonathan Leffler Mar 04 '22 at 16:38
  • See also the C11 standard [§6.7 Declarations ¶5](http://port70.net/~nsz/c/c11/n1570.html#6.7p5): _A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that: • for an object, causes storage to be reserved for that object;_ – Jonathan Leffler Mar 04 '22 at 16:59

0 Answers0