Page Content

Tutorials

What Are The C Keywords And Identifiers?

C Keywords and Identifiers

The basic building blocks of a program in C and C++ are known as tokens. In essence, these tokens are the smallest discrete components that the compiler can identify. Two significant groups of these tokens are IDs and keywords.

Keywords

Words with predefined, precise meanings in the C and C++ programming languages are called keywords. Program statements use them as basic building components. The compiler already knows what keywords signify, thus they can’t be used as variable names or identifiers. A keyword cannot be used as a variable name since doing so would attempt to give the word a new, contradictory meaning, which is prohibited. Lowercase letters are typically used for writing keywords.

Standard C Language contains thirty-two keywords. Those include auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, and while In addition to these C keywords, C++ adds its own.

Identifiers

Identifiers are the names assigned to various program objects, including arrays, variables, constants, functions, and unions. The programmer creates them to give software objects distinctive names.

When creating IDs, certain guidelines must be followed:

  • A letter (A-Z or a-z) or an underscore (_) must appear at the start of an identifier.
  • Following the initial character, IDs may include underscores, letters, and numbers (0–9).
  • You can’t use keywords as IDs.
  • Uppercase and lowercase letters are treated as separate in C (and C++), which is a case-sensitive language. For instance, Madam and Madam are regarded as different identifiers.
  • Starting identifiers with an underscore is generally discouraged since it may cause conflicts with system-defined names.

To make a program easier to read and self-document, use meaningful identifiers, such tax_rate rather than just tr. A programmer would typically not redefine certain identifiers, such as printf and scanf, which are names of standard library functions that are already familiar to the C system.

This straightforward C code example illustrates keywords and identifiers:

#include <stdio.h> // #include is a preprocessor directive. stdio.h is a header file name 
int main() { // int is a keyword, main is an identifier/function name
    // This is a comment - Comments are ignored by the compiler 
    int number1; // int is a keyword, number1 is an identifier 
    int number2; // int is a keyword, number2 is an identifier 
    int sum;     // int is a keyword, sum is an identifier 
    printf("Enter first integer: "); // printf is an identifier (standard library function name)
    scanf("%d", &number1);        // scanf is an identifier (standard library function name)
    printf("Enter second integer: ");
    scanf("%d", &number2);
    sum = number1 + number2; // sum, number1, number2 are identifiers. = and + are operators.
    printf("Sum is %d\n", sum); // printf is an identifier. %d is a format specifier.
    // Example using keywords 'if' and 'for'
    if (sum > 10) { // if is a keyword. sum is an identifier. > is an operator.
        printf("Sum is greater than 10.\n");
    }
    for (int i = 0; i < 3; i++) { // for is a keyword, int is a keyword. i is an identifier.
        printf("Loop iteration %d\n", i + 1);
    }
    return 0; // return is a keyword. 0 is an integer constant/literal.
}

As an illustration:

  • The keywords are return, for, if, and int. The language has defined and reserved meanings for them.
  • The following are identifiers: main, number1, number2, sum, my. The programmer gave these names to variables and functions.
  • The system’s standard library functions are printf and scanf, which are also identifiers.

To build readable and syntactically correct C/C++ programs, you must understand identifiers and keywords.

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index