C programming language includes a set of reserved words known as keywords. These keywords have predefined meanings in the language and cannot be used as identifiers (e.g., variable names, function names). This article by The Coding College explains C keywords in detail, their categories, and their usage with examples to help you master C programming.
What Are C Keywords?
Keywords in C are reserved words that are part of the language syntax. They are used to perform specific operations and control the flow of the program. C has 32 keywords defined in its standard, which are case-sensitive and written in lowercase.
List of C Keywords
Here’s a categorized list of all C keywords:
Data Types
char
double
float
int
void
Control Statements
if
else
switch
case
default
Loops
for
while
do
break
continue
Storage Class Specifiers
auto
extern
register
static
Constants
const
volatile
Pointers and Memory Management
sizeof
typedef
Functions
return
void
Others
goto
struct
enum
union
Understanding C Keywords with Examples
1. int
Used to declare integer variables.
#include <stdio.h>
int main() {
int num = 10;
printf("Value: %d\n", num);
return 0;
}
2. if
and else
Control flow based on conditions.
#include <stdio.h>
int main() {
int age = 18;
if (age >= 18) {
printf("Eligible to vote.\n");
} else {
printf("Not eligible to vote.\n");
}
return 0;
}
3. for
Used for looping.
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("Iteration: %d\n", i + 1);
}
return 0;
}
4. typedef
Defines new data types.
#include <stdio.h>
typedef unsigned int uint;
int main() {
uint num = 100;
printf("Value: %u\n", num);
return 0;
}
5. struct
Groups variables into a single entity.
#include <stdio.h>
struct Student {
char name[50];
int age;
};
int main() {
struct Student s1 = {"Alice", 20};
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
return 0;
}
Best Practices for Using Keywords
- Avoid Conflicts: Do not use keywords as variable or function names.
- Consistency: Always use lowercase for keywords since C is case-sensitive.
- Comment Usage: Include comments to clarify how and why specific keywords are used.
Common Errors with C Keywords
Misusing Keywords
Incorrect:
int float = 10; // Error: float is a keyword
Correct:
int value = 10;
Case Sensitivity
Incorrect:
Int num = 10; // Error: 'Int' is not a valid keyword
Correct:
int num = 10;
Learn More with The Coding College
Visit The Coding College for in-depth tutorials, examples, and hands-on exercises on C programming. Stay updated with the latest programming trends and enhance your skills with our curated content.