Declaration of a variable in c


Declaration of variables in c:

Declaration of variables means to acknowledge the compiler only about variable name and its data type with its modifiers but compiler doesn’t reserve any memory for the variables.

In c we can declared any variable with help of extern keyword while it has not initialized. Example of declaration:

(1) extern int a;

(2)extern struct student{
    char * name;
    int roll;
    double marks;
};

Important points about declaration variables in c:

(1) Since declaration variable doesn’t get any memory space so we cannot assign any value to variable. For example:

#include<stdio.h>
extern int a;
int main(){
    a=100;
    printf("%d",a);
    return 0;
}

Output: Compilation error

(2) We cannot use any operator with a variable which has only declared.

(3)We can declare any variable either globally or locally.

(4)A same variable can be declared many times.


Definition of variables in c:

A c statement in which a variable gets a memory is known as definition of variable. All auto, register, static and initialized extern variable are example of definition of variables. For example:

(a)

int a; //Definition of variable a
static int a; //Definition of variable a
register int a; //Definition of variable a
extern int a=5; //Definition of variable a

Note: In the above c statement all variables has been declared and defined at the same time.

(b)

#include<stdio.h>
extern int a; //Declaration of variable a
extern int a; //Again declaration of variable a
int a=5;    //Definition of variable a

int main(){
    ptintf("%d",a);
    return 0;
}

Output: 5

Important points about definition of variable:

(1)If any variable has not declared then it declaration occurs at the time of definition.

(2)We can use any operator after the definition of variable.

(3)Definition of variables can be globally or locally.

(4)A register variable gets CPU instead of memory in definition.

(5)A static or extern variable gets memory at the compile time while auto and register variables get memory or CPU at the run time.