C Identifiers and its naming rule, you should know

In this blog post, you will learn about C Identifiers and their naming rules. I have already discussed the character set of C, storage class, and linkage.  So let’s first understand, what is an identifier in C programming.

What is an identifier in C?

“Identifiers” are the names you refer to for variables, types, functions, and labels in your program. Identifiers must be unique and differ in spelling and case from any keywords. Keywords can not use as an identifier in C programming.

An identifier is a sequence of digits, underscores, lowercase and uppercase Latin letters, and most Unicode characters. The first character of an identifier name must be a non-digit (including the underscore _ , the lowercase and uppercase Latin letters, and other characters).

Note:  Lowercase and uppercase letters are distinct because C is a case-sensitive language.

Example,

All mentioned below identifiers are different because C is a case-sensitive language.

int ABC;

int aBC;

int abc;

int AbC;

int abC;

 

 

You create an identifier by specifying it in the declaration of a variable, type, or function. In the below example, aticleworld is an identifier for an integer variable, and main is identifier names for functions.

#include <stdio.h>

int main()
{
    int aticleworld;
    
    return 0;
}

 

Note: There is no specific limit on the maximum length of an identifier.

 

Predefined Identifiers:

Identifiers are generally created by the programmer during the implementation but there are some predefined identifiers that are inbuilt in programming.

The identifier __func__  is a predefined identifier. According to the C standard, “The identifier __func__   shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration appeared,

static const char __func__[] = “function-name”;

where function-name is the name of the lexically-enclosing function.”

 

Let’s see an example code to understand the __func__   identifier.

#include <stdio.h>

int main()
{
    printf("%s\n", __func__);

    return 0;
}

Output: main

 

Note: Since the name__func__  is reserved for any use by the implementation, if any other the identifier is explicitly declared using the name __func__  , the behavior is undefined.

 

Rules for naming identifiers:

1.   A valid identifier is a sequence of digits, underscores, lowercase, and uppercase Latin letters, and most Unicode characters.

Example,

int data;

int dat2a;

int _data;

int _Da2ta;

 

 

2. The first character of an identifier name must be a non-digit (including the underscore _, the lowercase and uppercase Latin letters, and other characters).

Example,

int 2data; //Invalid

float 9test; // Invalid
int abc; //Valid

int Abc; //Valid

 

 

 3. Keywords can not use as an identifier in C programming.

Example,

int while;  //invalid

float if;   //invalid

int goto;   //invalid

 

Recommended Post

Leave a Reply

Your email address will not be published. Required fields are marked *