C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - General Questions (Q.No. 8)
8.
Is the following statement a declaration or definition?
extern int i;
Declaration
Definition
Function
Error
Answer: Option
Explanation:

Declaring is the way a programmer tells the compiler to expect a particular type, be it a variable, class/struct/union type, a function type (prototype) or a particular object instance. (ie. extern int i)

Declaration never reserves any space for the variable or instance in the program's memory; it simply a "hint" to the compiler that a use of the variable or instance is expected in the program. This hinting is technically called "forward reference".

Discussion:
38 comments Page 2 of 4.

Vasavi said:   1 decade ago
int i;----is declaration.
int i=0;--is definition.
extern int i;--declaration.

Why because this statement declares that int i is already defined somewhere.

Geetha said:   1 decade ago
Hey,whetr int i; is declaration or definition.
Dn, extern int i; is declaration or definition

Rahul Chauhan said:   9 years ago
Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them.

Definition: We define a variable/function, apart from the role of the declaration, it also allocates memory for that variable/function.

Initialization: When we assigned the value of a variable.

Eg.

// This is the only declaration. y is not allocated memory by this statement
extern int y;

// This is both declaration and definition, memory to x is allocated by this statement.
int x;

// This is initialization.
x = 94;

Sumanta Kundu said:   9 years ago
int i=0 is a declaration.

But if we write
extern int i;
Then, it becomes a definition because this extern variable reserves space for 'i' and initializes 'i' to zero.

The answer given is wrong. So,the correct answer will be option B.

Abul Asad said:   9 years ago
What is the difference between the following two declarations of a function myfunction?

a) void myfunction (int a[ ]);
b) void myfunction (int *a);

Rishav Jain said:   9 years ago
@Abul Asad.

Both are same, address of a is passed as an argument.
In both cases, function will receive the address of a.
In the case of array a[], address of a[0] will be automatically initialised.

Hirak said:   8 years ago
What is the difference between the following two declarations of a function myfunction?

a) void myfunction (int a[ ]);
b) void myfunction (int *a);

Rohith said:   8 years ago
Please tell me exact explanation of declaration and definition.

Mrinmoy said:   6 years ago
This is a statement I think which is telling the compiler that x is an external variable.

The declaration is "int x;" which is declared anywhere in the program & without "int x;" line "extern int x;" will do nothing with x because this line itself is not a declaration.

Wikiok said:   1 decade ago
Declaration: no memory is used. You can not declare a non user defined data type: "int a" is a definition. But you can declare any user defined data type: "struct mystruct{int a; };" It hasn't use any memory. In this view function prototypes are declarations too. "void myfunction(int a);"

Definition: Memory has been used in code or data segment. "int a;" "struct mystruct myvariable;" void myfunction(int a) {printf("%d",a); }


Post your comments here:

Your comments will be displayed after verification.