Keywords and Identifiers in C
Every language has words with a fixed meaning. In C, these are called keywords, like int, if and return.
The names you choose, for variables and functions, are called identifiers. In this lesson you will learn all 32 keywords in an easy-to-remember way, and the rules for making valid identifiers.
What You Will Learn
- What keywords are, and the list of 32 keywords in ANSI C.
- What identifiers are.
- The rules for naming identifiers.
- How to spot valid and invalid names.
- The difference between a keyword and an identifier.
Keywords in C
Exam definition: Keywords are reserved words that have a fixed meaning in C. They cannot be used as identifiers. ANSI C has 32 keywords, and all of them are written in lowercase.
All 32 keywords, in 4 easy groups
| Group | Keywords | Count |
|---|---|---|
| Data types | char int float double void short long signed unsigned | 9 |
| Control flow | if else switch case default for while do break continue goto return | 12 |
| Storage classes | auto register static extern | 4 |
| Others | const volatile sizeof typedef struct union enum | 7 |
Memory trick: 9 + 12 + 4 + 7 = 32. Remember the 4 groups: Types, Control, Storage, Others. Storage is the easiest: "A R S E" = auto, register, static, extern.
Newer standards added a few more keywords (C99 added inline, restrict and _Bool, for example), but exams usually ask for the 32 keywords of ANSI C.
Identifiers in C
Exam definition: An identifier is the name given to a variable, function, array or other user-defined item in a program.
Rules for naming identifiers
- It can contain letters (a–z, A–Z), digits (0–9) and the underscore
_only. - It must start with a letter or an underscore, never with a digit.
- No spaces and no special characters like
@,#,-,$. - It cannot be a keyword.
- It is case-sensitive:
marks,MarksandMARKSare three different names. - Keep it reasonably short. Compilers are only required to treat the first 31 characters as important (63 in C99 and later).
Memory trick: "L-D-U, start with L or U": only Letters, Digits and Underscore, and the first character must be a Letter or Underscore.
Valid or invalid?
| Name | Valid? | Reason |
|---|---|---|
marks | Valid | Only letters |
total_marks | Valid | Underscore is allowed |
_count | Valid | Can start with an underscore |
marks2 | Valid | Digits allowed after the first character |
2marks | Invalid | Starts with a digit |
total marks | Invalid | Contains a space |
total-marks | Invalid | - is not allowed |
int | Invalid | It is a keyword |
Int | Valid | Different from int (case-sensitive), but confusing; avoid it |
Keyword vs Identifier
| Keyword | Identifier |
|---|---|
| Fixed meaning, given by C | Name chosen by the programmer |
| Cannot be changed or reused | Can be any valid name |
| Always lowercase | Can use upper and lower case |
| 32 in ANSI C | Unlimited |
Example: int, while | Example: age, total_marks |
Example 1: Good Identifiers in a C Program
#include <stdio.h>
int main(void)
{
int student_age = 19;
float cgpa = 8.4f;
char grade = 'A';
printf("Age: %d\n", student_age);
printf("CGPA: %.1f\n", cgpa);
printf("Grade: %c\n", grade);
return 0;
}
Output
Age: 19 CGPA: 8.4 Grade: A
Code Explanation
- Keywords used here:
int,float,char,void,return. - Identifiers used here:
main,student_age,cgpa,grade(andprintf, a name from the library). - The names describe what they store.
student_ageis much clearer thanx.
How the Compiler Uses Them
- When the compiler reads your code, it checks each word against its list of keywords.
- If the word is a keyword, it uses its fixed meaning (for example,
intmeans "integer type"). - Otherwise, it treats the word as an identifier and checks that it was declared before use.
- That is why a keyword can never be an identifier: the compiler would not know which meaning you want.
Example 2: What Happens with an Invalid Identifier
#include <stdio.h>
int main(void)
{
int 2marks = 50;
printf("%d\n", 2marks);
return 0;
}
Output (GCC compiler error)
error: invalid suffix "marks" on integer constant
Other compilers word it differently (Clang says expected identifier), but it is the same problem. The compiler reads 2 as a number, then gets confused by marks. Rename it to marks2 or second_marks and it works.
Important Points to Remember
- ANSI C has 32 keywords, all in lowercase.
- Identifiers use only letters, digits and underscore.
- An identifier cannot start with a digit.
- A keyword cannot be used as an identifier.
- C is case-sensitive:
ageandAgeare different.
Common Mistakes Beginners Make
| Mistake | Example | Fix |
|---|---|---|
| Starting with a digit | 1st_rank | rank_1st or first_rank |
| Using a keyword | int float = 2; | int price = 2; |
| Using a space or hyphen | roll-no | roll_no |
| Wrong case when using it | Declared total, used Total | Use exactly the same spelling |
| Meaningless names | a1, xx | marks, average |
Keywords and Identifiers Practice Questions
- What is a keyword? How many keywords are there in ANSI C?
- Write the four storage class keywords.
- Write any four rules for naming an identifier.
- Which are valid:
roll_no,9lives,_temp,float,my name? - Write two differences between a keyword and an identifier.
Show answers
- A reserved word with a fixed meaning. ANSI C has 32 keywords.
auto,register,static,extern.- Only letters, digits and underscore; must start with a letter or underscore; no keywords; no spaces or special characters (also: case-sensitive).
- Valid:
roll_no,_temp. Invalid:9lives(starts with digit),float(keyword),my name(space). - A keyword has a fixed meaning given by C, an identifier is chosen by the programmer. Keywords cannot be reused; identifiers can be any valid name.
Coding Exercise
Write a program that stores a student's roll number (45), percentage (82.5) and section ('B') in three variables with meaningful, valid names, and prints them. Then list which words in your program are keywords.
Show solution
#include <stdio.h>
int main(void)
{
int roll_number = 45;
float percentage = 82.5f;
char section = 'B';
printf("Roll number: %d\n", roll_number);
printf("Percentage: %.1f\n", percentage);
printf("Section: %c\n", section);
return 0;
}
Roll number: 45 Percentage: 82.5 Section: B
Keywords used: int, void, float, char, return.
Quick Quiz: Keywords and Identifiers (MCQ)
-
How many keywords are there in ANSI C?
A) 24 B) 30 C) 32 D) 48
-
Which is a valid identifier?
A)
3totalB)total_3C)total-3D)total 3 -
Which of these is NOT a keyword?
A)
staticB)sizeofC)mainD)volatile -
An identifier can start with:
A) A digit B) A letter or underscore C)
@D)# -
Are
sumandSumthe same identifier in C?A) Yes B) No C) Only in C99 D) Only for variables
Show answers
- C: 32.
- B:
total_3. - C:
mainis an identifier (a function name), not a keyword. - B: a letter or underscore.
- B: no, C is case-sensitive.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Keyword | Reserved word with a fixed meaning |
|---|---|
| Number of keywords | 32 (ANSI C), all lowercase |
| Groups | Data types 9 + Control 12 + Storage 4 + Others 7 |
| Storage keywords | auto, register, static, extern |
| Identifier | Name of a variable, function or array |
| Allowed characters | Letters, digits, underscore |
| First character | Letter or underscore (never a digit) |
Is main a keyword? | No, it is an identifier |
Frequently asked questions
- Can a keyword be used as a variable name?
- No. Keywords are reserved and have a fixed meaning.
- Is main a keyword in C?
- No.
mainis an identifier: the name of a function. - Why can't an identifier start with a digit?
- The compiler would read it as a number, like
2marks→2+marks. - Is printf a keyword?
- No. It is a library function declared in
stdio.h. - Are keywords case-sensitive?
- Yes. Keywords are always lowercase:
intis a keyword,INTis not.
Remember
- Keyword count: 9 + 12 + 4 + 7 = 32.
- Identifier rule: L-D-U, start with L or U.
Next Topic
Next lesson: Tokens in C. You will learn the smallest building blocks of every C program and how to count them.