Identifier/Variable naming conventions in C language [Rules and Recommendations]

In C programming language, all variables which are using in the program must be declared before their usage. Variable should declare in the declaration section of the function scope (it may be main or other user define function). Declaration section starts at the beginning of any function just after the opening curly brace.

Rules and recommendations through the compilers for identifier/variable naming conventions

Here are the rules and recommendations through the compilers for identifier/variable naming conventions, all rules must be followed while declaring an identifier/variable.

1) An identifier/variable name must be start with an alphabet or underscore (_) only, no other special characters, digits are allowed as first character of the identifier/variable name.

Valid examples:

product_name, age, _gender

2) Any space cannot be used between two words of an identifier/variable; you can use underscore (_) instead of space.

Valid examples:

product_name, my_age, gross_salary

3) If there are two or more words in an identifier/variable - you can also use "camel case" style to declare a variable. In the "camel case" style – first character of first word is either a lowercase alphabet or underscore (_) and first character of second word is a uppercase alphabet.

Valid examples:

productName, empName, grossSalary, _MyAge

When a variable name starts with underscore, next alphabet may uppercase alphabet.

4) An identifier/variable may contain only characters, digits and underscores only. No other special characters are allowed, and we cannot use digit as first character of an identifier/variable name (as written in point 1).

Valid examples:

length1, length2, _City_1

5) There are only 32 characters including digits, underscores are allowed in an identifier/variable name.

6) An identifier/variable name should be of short length, meaningful and descriptive (here, I wrote short length, a short length variable can be easily remembered). For example, if you want to declare a variable to store gross salary of an employee, you can use gross_salary, grossSalary, gSalary, emp_gross_salary, empGrossSalary. But I would recommend using gross_salary or emp_gross_salary as these variable names are meaningful and descriptive.

7) All constants and macros should be in uppercase, (this is a suggestion only)

Valid examples:

const float PI=3.14f;
#define MAX_LENGTH=100;

8) Description of each variable should be written with the declarations, so that other programmers or you (when, you see/change the code again) could understand the use of the variables. For this you should declare each variable in a separate line and write comment after the declaration.

Valid examples:

char empName[30]={0}; /*to store employee name*/
int empAge=0; /*to store employee age*/
float gross_salary=0.0f; /*to store gross salary*/

9) Maintain indentation while declaring/defining an identifier/variable, in the last point declared variables are not in a proper indentation, you should write like this:

Valid examples:

char	empName[30]	={0}; 	/*to store employee name*/
int 	empAge		=0; 	/*to store employee age*/
float 	gross_salary	=0.0f; 	/*to store gross salary*/

10) Each variable should be initialized with a default/initial value, an uninitialized variable may dangerous to use further without assigning a value (as they can be initialized with any random/garbage value).

11) Use initialization instead of assignment while declaring a variable

An assignment has following form:

int number; /*to store an integer number*/

An initialization has following form:

int number=0; /*to store an integer number*/

Initialization with variable declaration is better as it clears declaration and default/initial value.

Some other rules to remember

  1. There are some words which are reserved in the compiler, we call them keywords. Any of the keywords like int, char, float, if, else, while, for, do, break etc cannot be used as an identifier/variable name.
  2. We cannot declare more than one variable with same name in a scope, but variables having same name can be declared in another scope.
  3. Global and Local variables name may have same name. [Read: How to access global variable in a function when local variables name are same.]

C Language Tutorial »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.