A variable is an identifier that a C program uses to hold a value. Every variable has a type, which determines how much memory is allotted to it and how it is organized. There are several predefined types available in C such as char, int, float, etc. With typedef, we can even build our own data types. Each variable can be assigned a unique name which must adhere to the C language’s identifier rules.
Rules for Naming Identifiers
There are several rules for naming identifiers in C language, which are as follows:
- The underscore (_) sign, letters, numbers, and only alphanumeric characters (a-z, A-Z, 0-9) are permitted in an identifier.
- Identifier names must be unique.
- The first character must be an underscore or a letter.
- A keyword cannot serve as an identifier.
- There is significance only in the first thirty-one (31) characters.
- There cannot be any blank spaces in it.
- Case matters when using identifiers.
- A unique identifier shouldn’t exceed 31 characters.
- No blank spaces or commas are permitted in an identifier.
- An identifier can either be in lower case or upper case or both. Camel case is preferred in naming identifiers.
An example for identifiers in C++ is:
if ( marks != 0 )
printf( "Marks are not zero.\n" );
In the above code, we are using a ‘marks’ identifier to store a value 3 in it, and then the output is based on this value.
Types of Identifiers
There are two types of identifiers in C language.
1: Internal Identifiers
Internal identifiers are terms that are used to refer to variables, functions, or other programming constructs inside of a C program. Usually established by the programmer, these identifiers are hidden from view outside of the program code. Variable, function, and class names are a few examples of internal identifiers.
2: External Identifiers
On the other hand, names that are used to identify things or entities outside of the program or system are referred to as external identifiers. Often, other sources like operating systems, libraries, or other applications establish these IDs. The names of files, database tables, and network addresses are a few examples of external identifiers.
The identifiers are the names given to user-defined elements in a C program. They are declared using valid C language syntax, must adhere to the C language’s identifier rules and can have a maximum length of 31 characters. Identifiers are used for variables, functions, arrays, structures, unions, pointers, and typedefs.