In the C programming language, keywords are reserved words that have a special, predefined meaning to the compiler. These words are the fundamental building blocks of the C language and are used to construct program statements.
You can think of keywords as the verbs and nouns of the C language; they define the structure and actions of a program.
Key Properties
路聽聽聽聽聽聽聽聽 Reserved: Keywords are reserved by the language and cannot be used for any other purpose.
路聽聽聽聽聽聽聽聽 Predefined Meaning: The compiler knows exactly what each keyword is supposed to do.
路聽聽聽聽聽聽聽聽 Lowercase: All keywords in C must be written in lowercase.
Most importantly, you cannot use a keyword as the name for a variable, function, or any other identifier.
List of ANSI C Keywords
The ANSI C standard (C89/C90) defines 32 keywords, which are often grouped by their function:
Category |
Keywords |
Data Types |
|
User-Defined Types |
|
Control Flow |
|
Jumping |
|
Storage Classes |
|
Type Qualifiers |
|
Miscellaneous |
|
聽
Note:
Newer standards of C (like C99 and C11) have added a few more keywords (e.g., _Bool
, inline
), but the 32 listed above are the most fundamental.
Example of Keywords in a Program
This example demonstrates how several keywords are used together to create a simple program.
C
#
聽
int main() {
聽聽聽
// 'const' and 'int' are keywords
聽聽聽
const
int MAX_VALUE =
10;
聽聽聽
聽聽聽聽
// 'for' is a keyword for creating a loop
聽聽聽
for (
int i =
1; i <= MAX_VALUE; i++) {
聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
// 'if' and 'else' are keywords for decision making
聽聽聽聽聽聽聽
if (i %
2 ==
0) {
聽聽聽聽聽聽聽聽聽聽聽
printf(
"%d is an even number.\n", i);
聽聽聽聽聽聽聽 }
else {
聽聽聽聽聽聽聽聽聽聽聽
printf(
"%d is an odd number.\n", i);
聽聽聽聽聽聽聽 }
聽聽聽 }
聽聽聽
聽聽聽聽
// 'return' is a keyword to exit the function
聽聽聽
return
0;
}
Code Breakdown:
路聽聽聽聽聽聽聽聽
int
: A data type keyword used to declare integer
variables (MAX_VALUE
and i
).
路聽聽聽聽聽聽聽聽
const
: A type qualifier keyword that makes the variable MAX_VALUE
a read-only constant.
路聽聽聽聽聽聽聽聽
for
: A control flow keyword used to create a loop that
iterates from 1 to 10.
路聽聽聽聽聽聽聽聽
if...else
: Control flow keywords used to check a condition. The
program prints one message if a number is even and another if it's odd.
路聽聽聽聽聽聽聽聽
return
: A jump keyword that ends the main
function and returns the value 0
to the operating system.
Trying to use a keyword as a variable name will result in a compilation error:
C
// THIS IS WRONG AND WILL CAUSE AN ERROR
int
if =
5;
// Error: 'if' is a reserved keyword.
聽