LAST UPDATED ON: SEPTEMBER 17, 2024
C Keywords and Identifiers
-
Keywords and Identifiers are the building blocks of any program in C programming.
-
Keywords are reserved words, that have some special meaning in C programming.
-
Identifiers are names, given to variables, functions, pointers, etc. in a program.
-
There are 32 keywords in the C programming language.
-
Keywords cannot be used as Identifiers in C programming.
C Keywords
-
In C programming language there are some reserved words that are used internally and have some special purpose and meaning, such words are called Keywords or just Reserved Keywords.
-
The meaning and purpose of the keywords cannot be changed.
-
Each Keyword is unique.
-
There are only 32 keywords in C programming.
-
You cannot use keywords as name for variables, functions, pointers, etc. because if you do so, the compiler will get confused and give you an error.
In the last tutorial, where we learned C language syntax, we learned that everything in the C program is a Token.
Yes, Keywords are also tokens.
Code Example
Let's see a simple C program in which keywords are used,
int main() {
int num = 7;
char ch = 'Z';
float pi = 3.14;
return 0;
}
In the above code example, int
, char
, float
, and return
are keywords.
Common Keywords
Some of the most commonly used and seen keywords in C programs are:
-
int
- represents integer datatype
-
char
- represents character datatype
-
float
- represents floating-point value datatype
-
return
- Used to return a result from a function
-
if
and else
- Used in conditional statements
-
for
, while
, and do-while
- Used to implement Loops
-
void
, etc
All C Keywords
Here are all the C keywords:
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
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
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 lowercase. Apart from some C99 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.
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
-
Identifiers are names used for variables, functions, structures, pointers, etc. in a program.
-
Identifiers are not-predefined. You define them. The names you give to the variables, functions, etc. in C programs are identifiers.
-
Just like you have a name, similarly in C programming, 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.
Code Example
Here is a code example in which we have used some identifiers:
int main() {
int count = 25;
float price = 78.5;
return 0;
}
In the above code example, count and price are identifiers.
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.
Naming Conventions for Identifiers
You can follow the above rules and use any name, but here are some conventions that C programmers follow:
-
You should use meaningful names, instead of single-character names.
-
For multi-word names, you can use an underscore, like product_code.
-
You should use lowercase characters only.
Using Identifiers and Keywords:
Let's see a few examples where we'll use keywords and identifiers. We will define a C variable and name it using an identifier.
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.
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 use to call you, while you are a human being.
Q2. Are Identifiers case-sensitive?
Yes, identifiers are case-sensitive. Everything in C programming is case-sensitive. Age and age are considered as different identifiers.
Q3. Can I use special characters in Identifiers?
You can only use an underscore _
special character in identifiers. You cannot use other special characters like #
, $
, %
, &
, etc.
Q4. Can I use a number at the start of an Identifier?
No, you cannot use a number as the first character in an identifier. You can only use an alphabet or an underscore _
symbol.