C Programming - Declarations and Initializations - Discussion

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

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".

38 comments Page 1 of 4.

Antony said:   1 decade ago
What actually here mean by "extern" ?

Venki said:   1 decade ago
extern here refers to type of storage class in c . If you have 2 files say A and B and you want to access the global variable declared in A to be appearing in B then u culd do so by adding extern keyword . Refer to google if not satisfied .

Rahul said:   1 decade ago
Why we 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


void show(); //Declaration
void main(){
show(); //Call to method
void show(){ //Definition

But what is declaration and definition in context of Variable?

Atul said:   1 decade ago
Sagarborse tells us about prototype not for extern.

Extern is use to get access power to use the data member which is store in other file.

Sureshkumar said:   1 decade ago
Where should I use extern? can you tell little deeper.

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.

Appu said:   1 decade ago
int i=1;

This statement is definition or declaration ?

Anybody help me?

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,
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!....

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
if we carefully analyse second statement, value is assigned to a variable,its called initialization of a variable
a=10; / initialization of variable

Post your comments here:

Your comments will be displayed after verification.