List of Keywords in C
In C programming, keywords are reserved words that have specific meanings and cannot be used as identifiers such as variable names or function names. Understanding each keyword’s purpose is essential for mastering C programming.
Keyword | Description |
---|---|
1. auto | Declares automatic local variables (default storage class). |
2. break | Exits from a loop or a switch statement. |
3. case | Defines a case in a switch statement. |
4. char | Declares a variable of type character. |
5. const | Declares a constant variable whose value cannot be changed. |
6. continue | Skips the current iteration of a loop and moves to the next iteration. |
7. default | Specifies the default case in a switch statement. |
8. do | Used to create a do-while loop. |
9. double | Declares a variable of type double (double-precision floating-point). |
10. else | Specifies the alternative block of code in an if statement. |
11. enum | Declares an enumeration (a set of named integer constants). |
12. extern | Declares a variable or function that is defined in another file or scope. |
13. float | Declares a variable of type float (single-precision floating-point). |
14. for | Creates a loop that repeats a block of code a specified number of times. |
15. goto | Performs an unconditional jump to a labeled statement. |
16. if | Specifies a conditional statement. |
17. int | Declares a variable of type integer. |
18. long | Declares a variable of type long integer. |
19. register | Suggests storing the variable in a CPU register for faster access. |
20. return | Exits from a function and optionally returns a value. |
21. short | Declares a variable of type short integer. |
22. signed | Declares a signed data type, which can hold negative values. |
23. sizeof | Returns the size of a data type or object in bytes. |
24. static | Declares a static variable that retains its value between function calls. |
25. struct | Declares a structure, a collection of variables of different types. |
26. switch | Creates a switch statement for multi-way branching. |
27. typedef | Creates an alias for a data type. |
28. union | Declares a union, which allows different data types to share the same memory location. |
29. unsigned | Declares an unsigned data type, which cannot hold negative values. |
30. void | Specifies that a function returns no value or declares a pointer to no type. |
31. volatile | Prevents the compiler from optimizing the variable. |
32. while | Creates a loop that repeats a block of code as long as a condition is true. |
1. auto
- Purpose: Declares automatic variables. This is the default storage class for local variables.
- Usage:
auto
is rarely used since all local variables are automatically consideredauto
.
auto int x = 10; // Same as int x = 10;
2. break
- Purpose: Terminates the execution of a loop (
for
,while
, ordo-while
) or aswitch
statement.
for (int i = 0; i < 5; i++) {
if (i == 3) break; // Exits the loop when i equals 3
}
3. case
- Purpose: Defines individual cases within a
switch
statement. Each case represents a different possible value of the expression being evaluated.
int day = 2;
switch (day) {
case 1: printf("Sunday\n"); break;
case 2: printf("Monday\n"); break;
default: printf("Invalid day\n");
}
4. char
- Purpose: Declares a variable of type character, which stores a single character (1 byte) value.
char letter = 'A';
5. const
- Purpose: Declares a constant variable whose value cannot be changed once it is initialized.
const int pi = 3.14; // pi cannot be modified
6. continue
- Purpose: Skips the rest of the current loop iteration and moves to the next iteration.
for (int i = 0; i < 5; i++) {
if (i == 2) continue; // Skip when i equals 2
printf("%d ", i);
}
7. default
- Purpose: Specifies the default case in a
switch
statement when none of thecase
values match.
switch (day) {
case 1: printf("Sunday\n"); break;
default: printf("Invalid day\n");
}
8. do
- Purpose: Used in
do-while
loops. Executes the block of code at least once before checking the condition.
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);
9. double
- Purpose: Declares a variable of type double, which holds a double-precision floating-point number.
double pi = 3.14159;
10. else
- Purpose: Specifies the alternative block of code in an
if-else
statement, executed when theif
condition is false.
if (x > 10) {
printf("Greater\n");
} else {
printf("Smaller\n");
}
11. enum
- Purpose: Defines a set of named integer constants.
enum Day { Sunday, Monday, Tuesday };
enum Day today = Monday;
12. extern
- Purpose: Declares a variable or function that is defined in another file or scope. It is often used in multi-file programs.
extern int x; // Variable x is defined elsewhere
13. float
- Purpose: Declares a variable of type float, which holds a single-precision floating-point number.
float pi = 3.14f;
14. for
- Purpose: Creates a loop that repeats a block of code a fixed number of times.
for (int i = 0; i < 5; i++) {
printf("%d ", i);
}
15. goto
- Purpose: Performs an unconditional jump to a labeled statement. It is generally discouraged because it can make code difficult to follow.
goto label;
printf("This will be skipped\n");
label:
printf("Jumped to label\n");
16. if
- Purpose: Specifies a conditional statement that executes a block of code if the condition is true.
if (x > 10) {
printf("Greater than 10\n");
}
17. int
- Purpose: Declares a variable of type integer.
int age = 25;
18. long
- Purpose: Declares a variable of type long integer, which can store larger integer values than a regular
int
.
long largeNumber = 1234567890;
19. register
- Purpose: Suggests that the variable should be stored in a CPU register for faster access. It is a hint to the compiler, and the compiler may ignore it.
register int counter = 0;
20. return
- Purpose: Exits from a function and optionally returns a value.
int sum(int a, int b) {
return a + b;
}
21. short
- Purpose: Declares a variable of type short integer, which takes up less memory than a regular
int
.
short smallNumber = 100;
22. signed
- Purpose: Declares a signed data type, meaning it can store both negative and positive values. It is often used with
char
orint
.
signed int num = -5;
23. sizeof
- Purpose: Returns the size of a data type or object in bytes.
printf("Size of int: %zu bytes\n", sizeof(int));
24. static
- Purpose: Declares a variable that retains its value between function calls, or limits the scope of a variable to a file (in case of global variables).
static int count = 0;
25. struct
- Purpose: Declares a structure, which is a collection of variables of different types.
struct Person {
char name[20];
int age;
};
26. switch
- Purpose: Creates a
switch
statement for multi-way branching based on the value of an expression.
switch (day) {
case 1: printf("Sunday\n"); break;
case 2: printf("Monday\n"); break;
default: printf("Invalid day\n");
}
27. typedef
- Purpose: Creates an alias for a data type.
typedef unsigned int uint;
uint age = 25;
28. union
- Purpose: Declares a union, which is a data structure where all members share the same memory location, meaning only one member can be used at a time.
union Data {
int i;
float f;
char str[20];
};
29. unsigned
- Purpose: Declares an unsigned data type, which cannot store negative values.
unsigned int age = 30;
30. void
- Purpose: Declares a function that does not return any value or specifies a pointer to no type.
void greet() {
printf("Hello, World!\n");
}
31. volatile
- Purpose: Prevents the compiler from optimizing the variable. This is useful when a variable can be changed unexpectedly, such as in hardware or interrupts.
volatile int sensor_data;
32. while
- Purpose: Creates a
while
loop that repeats a block of code as long as the condition is true.
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}
Understanding the purpose of each keyword in C is crucial for writing efficient, correct, and maintainable code. Keywords serve as the backbone of the C language,
enabling everything from basic variable declarations to complex control structures.
At SamagraCS Educational Technology, we encourage you to practice using these keywords in real programs to gain a deeper understanding of how they work. If you have any questions or need further assistance, feel free to reach out to Pawan & Pooja, or the team. Keep learning and happy coding!