Open In App

C Identifiers

Last Updated : 06 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In C programming language, identifiers are the building blocks of a program. Identifiers are unique names that are assigned to variables, structs, functions, and other entities. They are used to uniquely identify the entity within the program. In the below example “section” is an identifier assigned to the string type value.

char section = 'A';

For the naming of identifiers, we have a set of rules in C to be followed for valid identifier names.

Rules to Name an Identifier in C

A programmer has to follow certain rules while naming variables. For the valid identifier, we must follow the given below set of rules.

  1. An identifier can include letters (a-z or A-Z), and digits (0-9).
  2. An identifier cannot include special characters except the ‘_’ underscore. 
  3. Spaces are not allowed while naming an identifier.
  4. An identifier can only begin with an underscore or letters.
  5. We cannot name identifiers the same as keywords because they are reserved words to perform a specific task. For example, printf, scanf, int, char, struct, etc. If we use a keyword’s name as an identifier the compiler will throw an error.
  6. The identifier must be unique in its namespace.
  7. C language is case-sensitive so, ‘name’ and ‘NAME’ are different identifiers.

The below image shows some valid and invalid identifiers in C language.

identifiers in  C

Valid and Invalid Identifiers in C

Examples of Identifiers in C

In the below code, identifiers are named by following all the rules for valid identifiers. We use identifiers to name a structure, function, character data type, double data type, etc. If you are not aware of structures and functions in C then don’t worry you will learn them soon. The below code ran successfully without throwing any errors as we have followed all the rules.

C




// C program to illustrate the identifiers
#include <stdio.h>
  
// here student identifier is used to refer the below structure
struct _student {
    int id;
    int class;
    char section;
};
  
// isEven identifier is used to call the below
// function
void isEven(int num)
{
    if(num%2==0){
      printf("It is an Even Number");
    }
      else{
      printf("It is not an Even Number");
    }
}
  
void main()
{
    // identifiers used as variable names.
    int studentAge = 20;
    double Marks = 349.50;
      
    // Calling isEven function.
    isEven(453);
  
}


Output

It is not an Even Number

What happens if we use a keyword as an Identifier in C?

In the below code, we have used const as an identifier which is a keyword in C. This will result in an error in the output.

C




#include <stdio.h>
  
int main() {
      
    // used keyword as an identifier
      int const = 90;
    
    return 0;
}


Output

​./Solution.c: In function 'main':
./Solution.c:5:14: error: expected identifier or '(' before '=' token
int const = 90;
^

Related Articles:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads