Keywords and Identifiers in C

Further, we will learn about keywords and identifiers in C programming language in detail.

Every word that we use in C programming language is classified as either a keyword or an identifier.

Now we will understand the definition and the rules of using keywords and identifiers.

What are Keywords in C programming?

C programming language has a set of 32 keywords that have fixed meanings which cannot be changed at any time during the execution of a program. Keywords have fixed meaning in C programming language and these meanings cannot be changed over time.

  • The keywords play the role of basic building blocks in a statement of a C program.
  • As C is a case sensitive language, keywords must be written in lowercase.
  • Some compilers may have additional keywords.

List of Keywords used in C

The list of all keywords used in C programming language are listed in the table below:

In the above code, int is a keyword and score is a variable of type int (integer). score is the name of a variable hence it is an identifier.

How to use keywords in a program?

Below is an example to show how we can use keywords in a program:

On line 4, 5 and 6 we can see highlighted text int, char, and float respectively. These are called keywords. They are declared before the variable names in each line. It indicates the type of data these variables will hold during the execution of the program i.e. int (integer) type, char (character) type, and float type respectively.

Now let’s take one more example to understand the use of keywords in C.

Example: Program to add two numbers.

#include<stdio.h>
int main()
{
int Add,a,b;
printf("Enter 1st number: ");
scanf("%d",&a);
printf("Enter 2nd number: ");
scanf("%d",&b);
Add=a+b;
printf("Sum of %d and %d is %d",a,b,Add);
return 0;
}

Output:

Enter 1st number: 2                                                                                                                                                                                                                          
Enter 2nd number: 4                                                                                                                                                                                                                                       
Sum of 2 and 4 is 6

Explanation:

In line 2 of the above code, int indicates that the main() function is of integer type.

Also, in line 4 of the above code, int indicates that the variables Add, a and b are of integer type.

As we know int is a keyword, so its meaning remains the same throughout the program.

Similarly, in line 11 of the above code, return is also a keyword, so its meaning will remain the same throughout the program.

What are Identifiers in C programming?

Identifiers are the names we use for different entities in a program like variables, arrays, functions, structure, union, etc. Identifiers are user-defined names which allow users to name a variable of their own choice. It helps in writing a meaningful and purpose-specific program that can be understood easily by the user in case of writing a complex program.

  • Identifiers are the name of variables, functions, and arrays, etc.
  • Identifiers are user-defined names.
  • Identifiers allow sequencing of letters and digits, with a letter as a first character.
  • Keywords cannot be used as Identifiers.
  • Identifiers do not permit the use of special characters like the semicolon, comma, slash, whitespaces, etc.
  • Identifiers can be written in both uppercase and lowercase.
  • Identifiers can also have underscore characters.

How to use identifiers in a program?

Below is an example to show how we can use identifiers in a program:

In line 2 of the above code, the function name main is an identifier whereas int is a keyword.

[Remember? : We have learnt that identifiers can be the name of variables, functions, and arrays, etc.]

In line 4, 5 and 6 , variable names Sum, a, and b are identifiers and int is a keyword.

Let’s understand the use of identifiers in another example given below.

Example: Program to print a number sequence.

#include<stdio.h>
int main()
{
int Seq=0,n,i;
printf("Enter a number:");
scanf("%d",&n);
printf("Sequence: ");
    for(i=1;i<=n;i++)
    {
        Seq=Seq+1;
        printf("%d ",Seq);
    }
    return 0;
}

Output:

Enter a number: 3
Sequence: 1 2 3

Explanation:

In line 4 of the above code, Seq is a variable name which is an identifier assigned in the program with initial value zero but from line 8, after each iteration the value of Seq changes until the condition of for loop becomes false.

Looping is another useful concept present in C programming language.

In looping, a sequence of statements are executed until some particular conditions are met which results in the total termination of the loop. Loops in C language are of three types: while loop, do loop, and for loop.

We will learn about loop control structures, types of loop and their uses in more detail in the coming sections.