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 1 of 4.

Rahul Chauhan said:   8 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;

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); }

Nancy said:   1 decade ago
int a;
its a definition not declaration.
actually a variable is said to be defined if memory is allocated to that variable.
and when we write, int a;
a is assigned 2 bytes of memory and may vary according to compliler.
int a; / its a definition of variable
a=10;
if we carefully analyse second statement, value is assigned to a variable,its called initialization of a variable
a=10; / initialization of variable

Venky said:   1 decade ago
Let's consider file name is a1.c :

#include<stdio.h>
{
int i=10;
}


Then let us consider another file a2.c :

#include"a1.c"
#include<stdio.h>
main()
{
extern int i;
printf("%d\n",i);
}


Now this will print output is 10.

This is the right way how to use extern keyword.

Sagarborse said:   1 decade ago
When we are declaring any function after main() function then we declare it before its use which is declaration

and what that method is going to invoke that code is its definition

Example:

void show(); //Declaration
void main(){
show(); //Call to method
}
void show(){ //Definition
printf("Hello!");
}

But what is declaration and definition in context of Variable?

Sandeep said:   1 decade ago
When you want all your program variables and functions to use a particular defined value then that could be declared as EXTERN.

More clearly...When many functions or variables need to access a particular variable whose value should be the same for all those which refer it (even if the defined value is changed. All those should access the same new value) then "extern" is used.

Prathyusha said:   1 decade ago
HELLO Sagarborse,
u asked wat is declaration and definition in the context of a variale.
here i am with a program demonstrating this,
#include<stdio.h>
void main()
{
int a; // variable "a" declaration
a=10; // variable "a" definition
printf("value of a is %d",a);
}

output:value of a is 10


correct me if i am wrong plz!....

Ragaveni said:   1 decade ago
If only name of variable or function is provided then it is declaration.

If a value is provided to variable and body is provided to function it is definition(same as common english word).

//variable
int i;//declaring
i=5;//defining
//method
void sum();//declaring
void sum()//defining
{
int a,b
printf("%f",a+b);
}

Studinstru said:   1 decade ago
What about static int a;.

As static int means its default value is consider as 0/zero only.

Then above statement is declaration or definition ?

This variable is stored in BSS section of memory.

Kernel make this variable as zero before program execution for arithmetic purpose.

Mrinmoy said:   5 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.


Post your comments here:

Your comments will be displayed after verification.