Identifier naming rule in c


In c any name is called identifier. This name can be variable name, function name, enum constant name, micro constant name, goto label name, any other data type name like structure, union, enum names or typedef name.


Rule 1:  Name of identifier includes alphabets, digit   and underscore.



Valid name: world, addition23, sum_of_number etc.


Invalid name: factorial#, avg value, display*number etc.

Rule 2: First character of any identifier must be either alphabets or underscore.

Valid name:  _calulate, _5,a_, __ etc.

Invalid name: 5_, 10_function, 123 etc.

Rule 3: Name of identifier cannot be any keyword of c program.

Invalid name: interrupt, float, asm, enum etc.

Example:

#include<stdio.h>
int main(){
    int huge=5;
    printf("%d"”,huge);
    return 0;
}

Output: Compilation error

Rule 4: Name of function cannot be global identifier.

Valid name: __TOTAL__, __NAME__  , __TINY__etc.

Invalid name: __TIME__ ,__DATE__ , __FILE__ ,__LINE__ ,__STDC__, __TINY__, __SMALL__, __COMPACT__, __LARGE__, __HUHE__, __CDECL__, __PASCAL__, __MSDOS__, __TURBOC__

For example:

(a)

#include<stdio.h>
int main(){
    double __TIME__=5.5;
    printf("%f",__TIME__);
    return 0;
}

Output: Compilation error

(b)

#include<stdio.h>
int main(){
    double __CPP__=5.5;
    printf("%f",__CPP__);
    return 0;
}

Output: 5.500000

Note: It is good practice to not write the variable name in the above format.

Rule 5: Name of identifier cannot be register Pseudo variables

Register Pseudo variables are:

Example:

#include<stdio.h>
int main(){
    long int _AH=5;
    printf("%ld",_AH);
    return 0;
}

Output: Compilation error

Rule 6: Name of identifier cannot be exactly same as of name of function within the scope of the function.

Example:

(q) What will be output of following program?

#include<stdio.h>
float lata();
int main(){
    float lata;
    lata=lata();
    printf("%f",lata);
    return 0;
}

float lata(){
    return 4.5f;
}

Output: Compiler error

Rule 7: Name of identifier is case sensitive i.e. num and Num are two different variables.

Rule 8: Only first 32 characters are significant of   identifier name.

Example:

abcdefghijklmnopqrstuvwxyz123456aaa, 

abcdefghijklmnopqrstuvwxyz123456bbb, 

abcdefghijklmnopqrstuvwxyz123456cad

All three identifier name are same because only first 32 characters has meaning. Rest has not any importance.

Rule 9: Identifier name cannot be exactly same as constant name which have been declared in header file of c and you have included that header files. For example:

(a)

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

Output: 25

(b)

#include<math.h>
#include<stdio.h>
int main(){
    int M_PI=25;
    printf("%d",M_PI);
   return 0;
}

Output: Compilation error

Explanation: M_PI is constant name which has been defined in header file math.h hence it cannot be variable in c.

Variable name cannot be exactly same as function name which have been declared any header file of c and we have included that header file in our program and used that function also in the program. For example:

(a)

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

Output: 25

(b)

#include<math.h>
#include<stdio.h>
int main(){
    int sqrt=25;
    printf("%d",sqrt);
    return 0;
}

Output: 25

(c)

#include<math.h>
#include<stdio.h>
int main(){
    int sqrt=25;//Invalid variable name
    printf("%f",sqrt(sqrt));
   return 0;
}

Output: Compilation error

Note: Header file stdio.h or conio.h by default it has included every c program .If you have not included that header explicitly then c complier will include that file. To make any program as c program save it as with .c extension.

(d)

int main(){
    int scanf=45;
    printf("%d",scanf);
   return 0;
}

Output: 45

(e)

int main(){
    int scanf=45,b;
    scanf("%d",&b);
    printf("%d %d",scanf,b);
    return 0;
}

Output: Compilation error

Identifier name in c can be exactly same as data type which has been declared in any header of c. For example

(a)

#include<stdio.h>
int main(){
    FILE *ptr;
    int FILE =11;
    printf("%d",FILE);
    return 0;
}

Output: 11

(b)

#include<stdio.h>
#include<time.h>
int main(){
    clock_t clock_t=80
    printf("%d",clock_t);
    return 0;

}

10 comments:

Unknown said...

good info.....

dinhpq said...

Thanks.

Unknown said...

Thanks,good info.

Unknown said...

1.why is the output 25 not 5?
#include
#include
int main(){
int sqrt=25;
printf("%d",sqrt);
return 0;
}

Output: 25


2.what is difernce between following two codes:
(d)

int main(){
int scanf=45;
printf("%d",scanf);
return 0;
}

Output: 45

(e)

int main(){
int scanf=45,b;
scanf("%d",&b);
printf("%d %d",scanf,b);
return 0;
}

Output: Compilation error

Unknown said...

Name of identifier cannot be exactly same as of name of function
read carefully the rules of identifier which are given above

Unknown said...

Name of identifier cannot be exactly same as of name of function
read carefully the rules of identifier which are given above

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...

in above RuLE 4....there is __TINY__ global identifier is in both valid as well as invalid global identifier....
which is true??? is __TINY__ valid or invalid gloal identifier???

Raghava said...

identfier name can be same as data type, i thini its not right for all the data dypes for example:

#include
#include
int main(){

int float=10
printf("%d\n",int);
return 0;

}

This gives ERROR