0
extern int i;// is a declaration
int j; //definition

the author has given that first statement is a declaration and second as a definition.i think second statement is a declaration and first as a definition.

i went through the complete reference C by Herbert Schildt

Yu Hao
  • 111,229
  • 40
  • 211
  • 267

4 Answers4

2
extern int i

...is a variable declaration, since it only tells the compiler "there exists a variable called i, but it's defined somewhere else".

int i

...is a variable definition, since it tells the compiler to create the actual variable.

Joachim Isaksson
  • 163,843
  • 22
  • 249
  • 272
0

The keyword extern is used to declare external variables, so the book is right.

There is one exception, if an initializer is added, then it's a definition:

extern int i = 42;
Yu Hao
  • 111,229
  • 40
  • 211
  • 267
0

Firstly, neither the first not the second is a "statement". In C language declarations are not statements. Declarations are declarations, statements are statements - in C these are two independent non-intersecting worlds.

Secondly, every definition is a declaration at the same time. Definition is just a particular kind of declaration. So it is not correct to contrapose declarations and definitions is such a mutually exclusive way.

Thirdly, what the comments say is correct and you are wrong. The first is a non-defining declaration. The second one is a definition.

Fourthly, the second one is a so called tentative definition - a C-specific feature. It has some peculiar properties. In general case, it does not necessarily define a variable with external linkage. The linkage of the variable it defines might depend on context. In your specific example, taken literally, it does indeed define a variable with external linkage. You can search on the term tentative definition to learn more about it.

Fifthly, Shildt's books are ripe with massive amount of terminological errors (and not just terminological ones). This is actually what they are mostly known for.

AnT
  • 291,388
  • 39
  • 487
  • 734
0

declaration : variable just declared

Ex: int i; 

definition : variable declaration+its initialization with value.

Ex: int i=10;

In your case

extern int i;// is a declaration    
//because you did not initialize value to i here.

Assuming j is Global variable. then it is initialized with ZERO at the time of declaration.
Here declaration+initialization =definition

int j; //definition  

if j is local then it is declaration only.

Gangadhar
  • 9,510
  • 3
  • 27
  • 48