Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

C Keywords and Identifiers

Keywords and Identifiers in the C language are the building block of any program. Keywords are predefined, which means the C language has a list of words that are Keywords, while an Identifier is user-defined, which means you while writing the C language program can specify identifiers. Let's see what these two are.

What are Keywords?

Every programming language has some reserved words that are used internally and have some meaning for the language, such words are called Reserved Keywords or just Keywords.

Taking an example from real-world, when someone says, Do you have my Laptop? You immediately picture a Laptop computer, what if the person asking for his/her Laptop, has a dog named Laptop(and is asking about the Dog), you won't understand until and unless you know that he/she has a dog named Laptop. This can lead to confusion, right.

Hence, the words that any programming language like the C language uses internally for some specific purpose, are not allowed to be used other than that purpose, otherwise, the language compiler will get confused.

In the last tutorial, where we learned C language syntax, we learned that everything in the C program is a Token.

Hence we can say that the words or Keywords are also tokens.

What are Identifiers?

Identifiers are words or texts used to identify anything in the C language.

Just like you have a name, using which everyone calls you, it can be John, Ron, Scarlett, Monica, etc., similarly in the C language, when we define a variable or a function, or a structure, etc. we name them so that it becomes easier for us to identify them and use them whenever required. These names are called Identifiers.

Identifiers are also tokens. Again, because every meaningful symbol, word, etc. in the C language are C Tokens, hence Identifiers are also tokens.

C keywords and Tokens

C Keywords

Now we know, that keywords are reserved words that have special meaning in the C language.

The meaning of C language keywords has already been described in the C compiler, which means the C compiler knows what these words mean and what it has to do with them.

These meanings cannot be changed. Hence, keywords cannot be used as a variable name or a function name (in short as an Identifier) because that would try to change the existing meaning of the keyword, which is not allowed.

You cannot use Keywords as Identifiers. This means you cannot name your variable, function, structure or pointer, or anything else, using a keyword.

There are a total of 32 keywords in the C language.

List of Keywords in C:

The following is a table of keywords available in the C language.

auto double int struct
break else long switch
case enum register typedef
const extern return union
char float short unsigned
continue for signed volatile
default goto sizeof void
do if static while

All these keywords were defined in the C89 standard, which was the first version of the C language, while some more keywords were added in the later C99 standard.

The keywords added by C99 are:

_Bool _Imaginary restrict
_Complex inline  

Each keyword has its own meaning attached to it, for example, the int keyword is used to define an integer datatype value, if/else are used for defining conditional statements, and so on. We will learn most of them as we progress in this tutorial.

Important Points to Note:

Here are some useful points to remember about Keywords.

  • A keyword can not be used as an Identifier (remember this always).

  • Keywords must be written in lower case. Apart from some C99 based keywords, all other keywords are in lowercase.

  • Keywords hold special meaning for the C compiler, so respect that, otherwise you will get a compile-time error.

The main() is not a keyword, but you should treat it as a keyword only. Because this function is a special function. If you name any variable as main, you may confuse the Compiler.

C Identifiers

In the C language identifiers are the names given to variables, constants, functions, structures, pointers, or any other user-defined data, to identify them.

Because it's up to the user to define the Identifiers, hence there are some rules that one has to follow, which are defined by the C language, to avoid unnecessary errors in the compiler.

Rules for defining an Identifier:

  • An identifier can only have alphanumeric characters(a-z , A-Z , 0-9) and underscore(_).

  • The first character of an identifier can only contain alphabet(a-z, A-Z) or underscore (_).

  • Identifiers are case-sensitive in the C language. For example, name and Name will be treated as two different identifiers.

  • Keywords are not allowed to be used as Identifiers.

  • No special characters, such as a semicolon, period, whitespaces, slash, or comma are permitted to be used in or as an Identifier.

Using Identifiers and Keywords:

Let's see a few examples where we'll use keywords and identifiers. We will define a C variable, so do not get confused, we will be learning about variables very soon.

When we declare a variable or any function in any C language program, to use it we must provide a name to it, that name is then used throughout the program, for example:

int myvariable = "Studytonight";

Here myvariable is the name or identifier for the variable which stores the value "Studytonight" in it. And int is the keyword. And all of them are tokens.

Let's have another example,

int money;
double salary;

Example of Valid Identifiers:

Here are some valid identifiers,

total, avg1, difference_1;

Example of Invalid Identifiers:

Here are some invalid identifiers,

$myvar; // incorrect
x!y;  // again incorrect

Using Keywords as Identifiers

What happens when we use keywords as identifiers?

#include <stdio.h>

int main() {
   // using void as name of variable (identifier)
   int void = 0;

   return 0;
}


error: two or more data types in declaration specifiers
int void = 0;
^

Run Code →

The C language compiler will think that we have mistakenly used two data types together and will give an error. Because void is also a data type in the C language(hence it is a keyword).

Frequently Asked Questions

Here are some frequently asked questions related to keywords and identifiers.

Q1. What is the difference between Identifier and Variable?

The identifier is just the name given to the variable, while a variable represents a memory field where some data is stored based on its data type. It's just like what is the difference between you and your name. Your name is just a word that people call you with, while you are a human being.

Conclusion

This tutorial was all about keywords and identifiers in C language. They both are parts of the token. Tokens in C are defined as the smallest individual element in C and are the basic building blocks of creating a program. We have covered keywords and identifiers in detail along with their valid syntax.