Keywords
Keywords are reserved words that have predefined meanings in the C programming language. In this lesson, you will learn the 32 ANSI C keywords, their categories, and the rules for using them.
Introduction
When learning any programming language, you will come across certain words that have a special meaning. In C, these words are called keywords. Keywords are reserved by the C language to perform specific tasks. They are part of the language itself and cannot be changed or redefined by the programmer. Understanding keywords is important because they form the foundation of every C program.
What are Keywords?
Keywords are reserved words that have predefined meanings in the C programming language. Each keyword is designed to perform a specific operation, such as declaring variables, defining data types, controlling program flow, or creating functions. Since these words already have a special purpose, they cannot be used as identifiers, such as variable names, function names, or array names.
Real-World Analogy
Imagine a school. Certain rooms already have fixed names like Principal Office, Library, Laboratory, and Staff Room. You cannot rename the Library as Classroom because it already has a predefined purpose. Similarly, in C, words like int, if, return, and while already have predefined meanings. You cannot use them for any other purpose.
In a School
- Library (Fixed purpose)
- Laboratory (Fixed purpose)
- Staff Room (Fixed purpose)
In C Programming
- int (Fixed purpose)
- if (Fixed purpose)
- return (Fixed purpose)
Characteristics of Keywords
- They have predefined meanings.
- They are part of the C language.
- They cannot be used as variable names.
- They cannot be modified.
- They are recognized automatically by the compiler.
- They are always written in lowercase.
Number of Keywords
The original ANSI C language contains 32 keywords. Modern C standards have introduced additional keywords, but beginners should first learn the original 32 keywords because they are used in almost every C program.
List of ANSI C Keywords
| Keyword | Purpose | Keyword | Purpose |
|---|---|---|---|
| auto | Automatic variables | break | Terminates loop/switch |
| case | Case in switch | char | Character data type |
| const | Constant values | continue | Skips loop iteration |
| default | Default case | do | Do-while loop |
| double | Double precision | else | Alternative condition |
| enum | Enumeration | extern | External variable |
| float | Floating-point | for | For loop |
| goto | Transfer control | if | Conditional statement |
| int | Integer data type | long | Long integer |
| register | Register storage | return | Returns value |
| short | Short integer | signed | Signed data type |
| sizeof | Returns memory size | static | Static variable |
| struct | Structure | switch | Multi-way selection |
| typedef | New type name | union | Union |
| unsigned | Unsigned type | void | No return value |
| volatile | Prevents optimization | while | While loop |
Categories of Keywords
Instead of memorizing all keywords together, it is easier to understand them by grouping them into categories:
Data Type Keywords
Define the type of data stored in a variable.
- int, char, float, double
- void, short, long
- signed, unsigned
Decision Making
Help the program make decisions based on conditions.
- if, else
- switch, case, default
Looping Keywords
Repeat a block of code multiple times.
- for, while, do
Jump Keywords
Change the normal flow of program execution.
- break, continue
- goto, return
Storage Class
Define the lifetime and scope of variables.
- auto, register
- static, extern
User-Defined Types
Help create custom data types.
- struct, union
- enum, typedef
Why Can't Keywords Be Used as Variable Names?
Since keywords already have predefined meanings, using them as variable names would confuse the compiler. For example, int if = 10; makes the compiler unable to determine whether if represents a variable or a conditional statement. Instead, use int marks = 10;.
Case Sensitivity
C is a case-sensitive language. This means int and INT are completely different. Only lowercase int is a keyword. Similarly, IF, If, and iF are not keywords.
How the Compiler Recognizes Keywords
When the compiler reads your program, it checks every word. If the word matches a reserved keyword, it performs the corresponding operation. Otherwise, it treats the word as an identifier.
int age;
// Compiler interpretation:
// int → Keyword
// age → IdentifierCommonly Used Keywords
As a beginner, you will frequently use these keywords. They appear in almost every C program:
int, char, float, if, else, for, while, do, return, break, continue, void
Common Beginner Mistakes
// Incorrect:
int while = 10;
// Correct:
int count = 10;// Incorrect:
INT age;
// Correct:
int age;// Incorrect:
innt age;
// Correct:
int age;// Incorrect:
integer age;
// Correct:
int age;Best Practices
- Always write keywords in lowercase.
- Do not memorize all keywords at once. Learn them as you encounter them.
- Never use keywords as identifiers.
- Use meaningful variable names instead.
Frequently Asked Questions
What is a keyword?
A keyword is a reserved word with a predefined meaning in the C programming language.
Can I create my own keyword?
No. Only the C language defines keywords.
Can keywords be used as variable names?
No. Keywords are reserved by the compiler and cannot be used as identifiers.
How many keywords are there in C?
The original ANSI C language contains 32 keywords. Newer C standards introduce additional keywords, but beginners should first focus on the original set.
Is INT the same as int?
No. C is case-sensitive. Only int is a valid keyword.
Key Takeaways
- Keywords are reserved words in C.
- Each keyword has a predefined meaning.
- Keywords cannot be used as identifiers.
- C keywords are case-sensitive.
- Learning keywords is essential because they form the building blocks of C programs.
Summary
Keywords are the reserved words that define the syntax and behavior of the C programming language. They help declare data types, control program flow, manage memory, and create user-defined data structures. Since keywords have predefined meanings, they cannot be used as variable or function names. As you continue learning C, you will gradually use many of these keywords in real programs.