Introduction
Keywords in C are reserved words that have predefined meanings and cannot be used as variable names, function names, or any other identifiers. These words are fundamental building blocks of the C language and serve specific purposes in program syntax and semantics. Understanding keywords is essential for writing correct C programs.
Key Concepts
Reserved Words: Words with special meaning that cannot be redefined Data Type Keywords: Keywords that define data types Control Flow Keywords: Keywords that control program execution Storage Class Keywords: Keywords that define variable storage and scope Qualifier Keywords: Keywords that modify variable properties Case Sensitivity: All C keywords are lowercase
Complete List of C Keywords
Standard C (C89/C90) Keywords - 32 Total
auto break case char
const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while
C99 Additional Keywords
inline restrict _Bool _Complex _Imaginary
C11 Additional Keywords
_Alignas _Alignof _Atomic _Static_assert
_Noreturn _Thread_local _Generic
Data Type Keywords
Basic Data Types
#include <stdio.h>
int main() {
// Integer types
char character = 'A'; // 8-bit character
short shortInt = 32767; // Short integer
int integer = 2147483647; // Standard integer
long longInt = 2147483647L; // Long integer
// Floating-point types
float floatNum = 3.14f; // Single precision
double doubleNum = 3.14159; // Double precision
// Modifiers
signed int signedInt = -100; // Signed integer (default)
unsigned int unsignedInt = 100U; // Unsigned integer
printf("Character: %c\n", character);
printf("Short: %hd\n", shortInt);
printf("Integer: %d\n", integer);
printf("Long: %ld\n", longInt);
printf("Float: %.2f\n", floatNum);
printf("Double: %.5f\n", doubleNum);
printf("Signed: %d\n", signedInt);
printf("Unsigned: %u\n", unsignedInt);
return 0;
}
Void and Advanced Types
#include <stdio.h>
// Function returning void
void displayMessage(void) {
printf("This function returns nothing\n");
}
// Function with void pointer
void printSize(void *ptr, char type) {
switch (type) {
case 'i':
printf("Integer value: %d\n", *(int*)ptr);
break;
case 'f':
printf("Float value: %.2f\n", *(float*)ptr);
break;
case 'c':
printf("Character value: %c\n", *(char*)ptr);
break;
}
}
int main() {
int num = 42;
float val = 3.14;
char ch = 'X';
displayMessage();
printSize(&num, 'i');
printSize(&val, 'f');
printSize(&ch, 'c');
return 0;
}
Control Flow Keywords
Conditional Statements
#include <stdio.h>
int main() {
int score = 85;
// if-else statement
if (score >= 90) {
printf("Grade: A\n");
} else if (score >= 80) {
printf("Grade: B\n");
} else if (score >= 70) {
printf("Grade: C\n");
} else {
printf("Grade: F\n");
}
// switch-case statement
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Other day\n");
break;
}
return 0;
}
Loop Keywords
#include <stdio.h>
int main() {
printf("For loop:\n");
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
printf("While loop:\n");
int j = 1;
while (j <= 5) {
printf("%d ", j);
j++;
}
printf("\n");
printf("Do-while loop:\n");
int k = 1;
do {
printf("%d ", k);
k++;
} while (k <= 5);
printf("\n");
// Break and continue
printf("Break and continue example:\n");
for (int i = 1; i <= 10; i++) {
if (i == 3) {
continue; // Skip 3
}
if (i == 8) {
break; // Stop at 8
}
printf("%d ", i);
}
printf("\n");
return 0;
}
Jump Statements
#include <stdio.h>
int factorial(int n) {
if (n <= 1) {
return 1; // Return keyword
}
return n * factorial(n - 1);
}
void demonstrateGoto() {
int i = 0;
start: // Label for goto
printf("%d ", i);
i++;
if (i < 5) {
goto start; // Jump to label
}
printf("\n");
}
int main() {
printf("Factorial of 5: %d\n", factorial(5));
printf("Goto demonstration: ");
demonstrateGoto();
return 0;
}
Storage Class Keywords
Auto and Register
#include <stdio.h>
void demonstrateAuto() {
auto int localVar = 10; // auto is default for local variables
printf("Auto variable: %d\n", localVar);
}
void demonstrateRegister() {
register int counter; // Suggest storing in CPU register
for (counter = 0; counter < 5; counter++) {
printf("Register counter: %d\n", counter);
}
// printf("Address: %p\n", &counter); // Error: can't get address
}
int main() {
demonstrateAuto();
demonstrateRegister();
return 0;
}
Static Keyword
#include <stdio.h>
// Static global variable (file scope only)
static int globalCounter = 0;
void incrementCounter() {
static int localCounter = 0; // Retains value between calls
localCounter++;
globalCounter++;
printf("Local static: %d, Global static: %d\n",
localCounter, globalCounter);
}
// Static function (internal linkage)
static void internalFunction() {
printf("This function is only visible in this file\n");
}
int main() {
for (int i = 0; i < 3; i++) {
incrementCounter();
}
internalFunction();
return 0;
}
Extern Keyword
// In global scope or header file
extern int sharedVariable; // Declaration (defined elsewhere)
extern void externalFunction(void); // Function declaration
// Example usage in main
int sharedVariable = 100; // Definition
void externalFunction(void) {
printf("External function called\n");
printf("Shared variable: %d\n", sharedVariable);
}
int main() {
externalFunction();
return 0;
}
Type Qualifier Keywords
Const Qualifier
#include <stdio.h>
int main() {
const int MAX_SIZE = 100; // Constant integer
const float PI = 3.14159; // Constant float
const int *ptr1; // Pointer to constant int
int *const ptr2 = &MAX_SIZE; // Constant pointer
const int *const ptr3 = &MAX_SIZE; // Constant pointer to constant
printf("Constants: %d, %.5f\n", MAX_SIZE, PI);
// MAX_SIZE = 200; // Error: cannot modify const variable
return 0;
}
Volatile Qualifier
#include <stdio.h>
volatile int hardwareRegister = 0; // May change unexpectedly
void checkHardware() {
while (hardwareRegister == 0) {
// Compiler won't optimize this loop away
// because hardwareRegister is volatile
printf("Waiting for hardware...\n");
}
printf("Hardware ready!\n");
}
int main() {
printf("Volatile example (conceptual)\n");
hardwareRegister = 1; // Simulate hardware change
checkHardware();
return 0;
}
Structure and Union Keywords
Struct Examples
#include <stdio.h>
struct Student {
int id;
char name[50];
float marks;
};
typedef struct {
int x;
int y;
} Point;
int main() {
struct Student student1 = {101, "John Doe", 85.5};
Point point1 = {10, 20};
printf("Student: ID=%d, Name=%s, Marks=%.1f\n",
student1.id, student1.name, student1.marks);
printf("Point: (%d, %d)\n", point1.x, point1.y);
return 0;
}
Union Examples
#include <stdio.h>
union Data {
int integer;
float floating;
char character;
};
int main() {
union Data data;
data.integer = 100;
printf("Integer: %d\n", data.integer);
data.floating = 3.14; // Overwrites integer value
printf("Float: %.2f\n", data.floating);
data.character = 'A'; // Overwrites float value
printf("Character: %c\n", data.character);
printf("Union size: %zu bytes\n", sizeof(union Data));
return 0;
}
Enum Keywords
#include <stdio.h>
enum Color {
RED,
GREEN,
BLUE
};
enum Status {
SUCCESS = 0,
ERROR = -1,
WARNING = 1
};
int main() {
enum Color favoriteColor = BLUE;
enum Status operationResult = SUCCESS;
printf("Favorite color code: %d\n", favoriteColor);
printf("Operation status: %d\n", operationResult);
switch (favoriteColor) {
case RED:
printf("Color is red\n");
break;
case GREEN:
printf("Color is green\n");
break;
case BLUE:
printf("Color is blue\n");
break;
}
return 0;
}
Special Keywords
Sizeof Operator
#include <stdio.h>
int main() {
printf("Size of char: %zu bytes\n", sizeof(char));
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of float: %zu bytes\n", sizeof(float));
printf("Size of double: %zu bytes\n", sizeof(double));
int array[10];
printf("Size of array: %zu bytes\n", sizeof(array));
printf("Number of elements: %zu\n", sizeof(array)/sizeof(array[0]));
struct Example {
int a;
char b;
double c;
};
printf("Size of struct: %zu bytes\n", sizeof(struct Example));
return 0;
}
Typedef Keyword
#include <stdio.h>
// Creating type aliases
typedef int Integer;
typedef float Real;
typedef char* String;
// Complex type alias
typedef struct {
int hours;
int minutes;
int seconds;
} Time;
// Function pointer typedef
typedef int (*CompareFunc)(int a, int b);
int compare(int a, int b) {
return (a > b) ? 1 : (a < b) ? -1 : 0;
}
int main() {
Integer number = 42;
Real pi = 3.14159;
String message = "Hello, World!";
Time currentTime = {14, 30, 45};
CompareFunc comparePtr = compare;
printf("Number: %d\n", number);
printf("Pi: %.5f\n", pi);
printf("Message: %s\n", message);
printf("Time: %02d:%02d:%02d\n",
currentTime.hours, currentTime.minutes, currentTime.seconds);
printf("Comparison of 10 and 5: %d\n", comparePtr(10, 5));
return 0;
}
Best Practices
- Never use keywords as identifiers - they have reserved meanings
- Remember case sensitivity - all keywords are lowercase
- Use appropriate storage classes for variable scope and lifetime
- Apply const qualifier for values that shouldn’t change
- Use typedef to create meaningful type names
- Understand keyword context - some keywords have multiple uses
- Choose descriptive variable names that don’t conflict with keywords
- Stay updated with new keywords in C standards
Common Mistakes
Keyword Misuse
// WRONG - Using keywords as identifiers
// int int = 5; // Error: 'int' is a keyword
// float return = 3.14; // Error: 'return' is a keyword
// char while = 'A'; // Error: 'while' is a keyword
// CORRECT - Using proper identifiers
int integer = 5;
float returnValue = 3.14;
char whileChar = 'A';
Summary
C keywords are reserved words with specific meanings that form the foundation of the language syntax. They include data type keywords (int, char, float), control flow keywords (if, while, for), storage class keywords (static, extern), and type qualifiers (const, volatile). Understanding these keywords and their proper usage is fundamental to writing correct C programs. Keywords cannot be used as identifiers and are case-sensitive, always written in lowercase.
Part of BCA Programming with C Course (UGCOA22J201)