Keywords in C
Sunny Bhaskar
10/29/20241 min read
Keywords in C are predefined reserved words that have a special meaning to the compiler. These words are part of the C language's syntax and cannot be used for any other purpose, such as naming variables or functions. Keywords help define the structure and behavior of a C program.
Key Points about Keywords
They are reserved by the language, so you can't use them as variable or function names.
All keywords in C are written in lowercase.
Each keyword has a specific purpose or function in the program.
List of Common Keywords in C
Here’s a list of some of the important keywords in C
1. Data Type Keywords
- `int` – Defines integer variables (whole numbers).
- `float` – Defines floating-point variables (numbers with decimals).
- `char` – Defines character variables (single characters).
- `double` – Defines double-precision floating-point variables (larger decimal numbers).
- `void` – Specifies no value or type (often used for functions).
2. Control Flow Keywords
- `if`, `else` – Used for conditional statements.
- `switch`, `case` – Used for switch-case statements.
- `for`, `while`, `do` – Used for loops (repeating code).
3. Storage Class Keywords
- `auto` – Default storage class for local variables.
- `static` – Preserves the value of a variable between function calls.
- `extern` – Declares a variable that is defined in another file.
- `register` – Suggests to the compiler to store the variable in a register for faster access.
4. Other Keywords
- `return` – Used in functions to return a value to the caller.
- `break` – Exits loops or switch statements.
- `continue` – Skips the current iteration of a loop and moves to the next one.
- `sizeof` – Returns the size of a data type or variable.
- `const` – Declares a variable as constant (its value cannot be changed).
- `typedef` – Used to give a new name to a data type.
Example of Keywords in Use
#include <stdio.h>
int main() {
int number = 10; // 'int' is a keyword for declaring an integer variable.
if (number > 5) { // 'if' and 'else' are keywords for conditional statements.
printf("Number is greater than 5.\n");
} else {
printf("Number is 5 or less.\n");
}
return 0; // 'return' is a keyword for ending a function and returning a value.
}
Summary
Keywords are reserved words with specific functions in the C language.
You cannot use them for naming your variables, functions, or any other user-defined elements.
They help define data types, control the flow of the program, manage loops, and perform many other essential tasks.