Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaration Versus Definition in c

Tags:

c

Recently while learning about c programming i noticed something that i found interesting. I had read that a statement like int i=0; is the only way to force a definition while a statement like extern int i; implies a forced declaration. A statement like int i; would be context dependent. But what happens when i combine the the extern with initialization like extern int i=13;. Compiler generates a warning. But what is this rule governing this?

like image 322
Bazooka Avatar asked Nov 08 '11 17:11

Bazooka


People also ask

What is difference between declaration and definition in C?

Declaration means that variable is only declared and memory is allocated, but no value is set. However, definition means the variables has been initialized. The same works for variables, arrays, collections, etc.

What is difference between declaration and definition?

i.e., declaration gives details about the properties of a variable. Whereas, Definition of a variable says where the variable gets stored. i.e., memory for the variable is allocated during the definition of the variable.

What is declaration and definition of function in C?

A function consist of two parts: Declaration: the function's name, return type, and parameters (if any) Definition: the body of the function (code to be executed)

What's the difference between function declaration and definition?

Definition. Function declaration is a prototype that specifies the function name, return types and parameters without the function body. Function Definition, on the other hand, refers to the actual function that specifies the function name, return types and parameters with the function body.


1 Answers

This is a Coding style warning.
The argument for this is the code is valid, but extremely unidiomatic for C since "extern" is generally expected to mean that the declaration is not providing a definition of the object.

extern int i=13;  

declares and defines i, While:

extern int i;      

just declares the variable i.

A specific bug 45977 has been raised on GCC on the same but it is still shows Unconfirmed Status.

The bug report points out that the code is syntactically as per the C standard. And it has an discussion which discusses this in detail.


For Standerdese Fans:
Relevant Section References are:
ansi c99 Standard 6.2.2: Linkage Of Identifiers and
ansi c99 Standard 6.9.2.4

like image 53
Alok Save Avatar answered Oct 13 '22 20:10

Alok Save