Keywords in C
Keywords in C are predefined, reserved words used in programming that have special meanings to the compiler. These keywords help define the syntax and structure of the C programming language.
They cannot be used as variable names, function names, or any other identifiers. Keywords are the foundation of any C program, and understanding them is crucial for coding in C.
Below is a list of C keywords and their descriptions:
- Data Types
- Control Statements
- Storage Classes
- Operators
- Others
1) Data Types
C provides several built-in data types such as int, float, char, and double, which are used to define variables and return types of functions. These keywords define the type and size of data.
2) Control Statements
Control statements include keywords like if, else, switch, case, for, while, do. These are used to control the flow of execution in a program.
3) Storage Classes
Storage classes determine the scope, visibility, and lifetime of variables. Examples include auto, register, static, extern.
4) Operators
Keywords like sizeof, typeof are used for specific operations in C. These keywords help perform operations efficiently in the C environment.
5) Others
Other reserved keywords like return, goto, typedef, void have unique purposes within the C programming framework.
Reserved Keywords in C
The following table lists all the reserved keywords in C:
Keyword | Description |
---|---|
int | Defines integer data type |
float | Defines floating-point data type |
return | Returns a value from a function |
if | Starts a conditional statement |
else | Defines an alternative path in a conditional statement |
while | Starts a loop that runs while a condition is true |
for | Starts a loop with a counter |
switch | Starts a multi-case conditional statement |
case | Defines a single condition in a switch statement |
default | Defines a fallback in a switch statement |
void | Defines a function that does not return a value |
static | Defines a static variable or function |
extern | Declares an external variable or function |
Example Programs Using C Keywords
1. Basic Arithmetic
#include <stdio.h>
int main()
{
int a = 15, b = 10, result; // 'int' is a keyword
result = a - b;
printf("Result of subtraction: %d\n", result);
return 0;
}
Output
2. Conditional Statement
#include <stdio.h>
int main()
{
int age = 20;
if (age >= 18) // 'if' and 'else' are keywords
{
printf("You are an adult\n");
}
else
{
printf("You are a minor\n");
}
return 0;
}
Output
3. Switch Statement
#include <stdio.h>
int main()
{
int day = 3;
switch (day) // 'switch' and 'case' are keywords
{
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
Output
4. Loop Example
#include <stdio.h>
int main()
{
for (int i = 1; i <= 5; i++) // 'for' is a keyword
{
printf("Number: %d\n", i);
}
return 0;
}
Output
Number: 2
Number: 3
Number: 4
Number: 5
5. Using Enumerations
#include <stdio.h>
enum week {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; // 'enum' is a keyword
int main()
{
enum week today;
today = Wednesday;
printf("Day of the week: %d\n", today);
return 0;
}