Chapter 04Beginner
Keywords in C
Learn all 32 reserved words in C that have special meaning. These words are reserved by C and cannot be used as variable names.
12 min readUpdated 2024-12-16
keywordsreserved wordsintfloatifwhileforreturn
What You Will Learn
- ✓Know all 32 C keywords
- ✓Understand why keywords cannot be used as names
- ✓Recognize keywords by category (data types, control, storage)
01What are Keywords?
📝 Definition
Keywords (also called reserved words) are predefined words in C that have special meanings. They are part of C's syntax and cannot be used as identifiers (variable names, function names, etc.).
❌ Invalid
main.c
C
int int = 5; // ERROR!float return; // ERROR!char if = 'a'; // ERROR!✅ Valid
main.c
C
int number = 5; // OKfloat result; // OKchar initial = 'a'; // OK💡 Key Points
- • C has 32 keywords (C89/C90 standard)
- • All keywords are in lowercase
- • Keywords are case-sensitive (if ≠ IF)
- • Cannot be used as variable/function names
02All 32 Keywords in C
📋 Complete List of C 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
📝 Note: C99 & C11 Added More
C99 added: inline, restrict, _Bool, _Complex, _Imaginary
C11 added: _Alignas, _Alignof, _Atomic, _Generic, etc.
03Data Type Keywords
These keywords define the type of data a variable can hold.
| Keyword | Description | Example |
|---|---|---|
| int | Integer (whole numbers) | int age = 25; |
| float | Single-precision decimal | float pi = 3.14f; |
| double | Double-precision decimal | double pi = 3.14159; |
| char | Single character | char grade = 'A'; |
| void | No type / empty | void printHello(); |
| short | Short integer | short num = 100; |
| long | Long integer | long big = 100000L; |
| signed | Can be negative (default) | signed int x = -5; |
| unsigned | Only positive (0+) | unsigned int x = 5; |
data_type_keywords.c
C
1#include <stdio.h>23int main() {4 // Data type keywords in action5 int age = 25;6 float salary = 50000.50f;7 double pi = 3.14159265359;8 char grade = 'A';9 short small = 100;10 long large = 1000000L;11 unsigned int positive = 42;12 13 printf("Age: %d\n", age);14 printf("Salary: %.2f\n", salary);15 return 0;16}04Control Flow Keywords
These keywords control the flow of execution in your program.
🔀 Decision Making
if- Execute if trueelse- Execute if falseswitch- Multi-way branchcase- Switch case labeldefault- Default switch case🔄 Loops
for- Count-controlled loopwhile- Pre-test loopdo- Post-test loop (do-while)break- Exit loop/switchcontinue- Skip to next iterationgoto- Jump to labelcontrol_flow_keywords.c
C
1#include <stdio.h>23int main() {4 int x = 10;5 6 // if-else7 if (x > 5) {8 printf("x is greater than 5\n");9 } else {10 printf("x is 5 or less\n");11 }12 13 // for loop14 for (int i = 0; i < 3; i++) {15 printf("i = %d\n", i);16 }17 18 // while loop19 while (x > 5) {20 printf("x = %d\n", x);21 x--;22 }23 24 // switch25 switch (x) {26 case 5:27 printf("x is 5\n");28 break;29 default:30 printf("x is not 5\n");31 }32 33 return 0;34}05Storage Class Keywords
These keywords define where a variable is stored and how long it exists.
| Keyword | Description | Lifetime |
|---|---|---|
| auto | Automatic (default for locals) | Block scope |
| static | Persists between calls | Program lifetime |
| extern | Defined in another file | Program lifetime |
| register | Hint: store in CPU register | Block scope |
storage_class_keywords.c
C
1void counter() {2 static int count = 0; // Persists between calls3 count++;4 printf("Called %d times\n", count);5}67extern int globalVar; // Defined elsewhere89register int fast = 0; // Hint for faster access06User-Defined Type Keywords
| Keyword | Description | Example |
|---|---|---|
| struct | Group of related variables | struct Person { ... } |
| union | Share memory location | union Data { ... } |
| enum | Named integer constants | enum Color { RED, GREEN } |
| typedef | Create type alias | typedef int Integer; |
user_defined_types.c
C
1// struct - group related data2struct Person {3 char name[50];4 int age;5};67// enum - named constants8enum Color { RED, GREEN, BLUE };910// typedef - create alias11typedef unsigned int uint;1213// union - shared memory14union Data {15 int i;16 float f;17};07Other Important Keywords
| Keyword | Description | Example |
|---|---|---|
| return | Exit function, return value | return 0; |
| sizeof | Get size in bytes | sizeof(int) → 4 |
| const | Make read-only | const int PI = 3; |
| volatile | Can change unexpectedly | volatile int flag; |
other_keywords.c
C
1#include <stdio.h>23int add(int a, int b) {4 return a + b; // return: exit with value5}67int main() {8 const int MAX = 100; // const: cannot change9 10 printf("int size: %zu bytes\n", sizeof(int));11 printf("Sum: %d\n", add(5, 3));12 13 return 0; // return success14}08Keywords by Category
📊Data Types (9)
intfloatdoublecharvoidshortlongsignedunsigned
🔀Control Flow (12)
ifelseswitchcasedefaultforwhiledobreakcontinuegotoreturn
📦Storage Classes (4)
autostaticexternregister
🏗️User-Defined Types (4)
structunionenumtypedef
🔧Other Keywords (3)
sizeofconstvolatile
09Common Mistakes
❌ Using Keywords as Variable Names
int for = 5; // ERROR: 'for' is a keyword❌ Wrong Case
IF (x > 5) // ERROR: should be lowercase 'if'❌ Misspelling Keywords
swicth (x) // ERROR: should be 'switch'10Summary
🎯 Key Takeaways
- •C has 32 reserved keywords
- •All keywords are lowercase
- •Keywords cannot be used as identifiers
- •Categories: Data Types, Control Flow, Storage Classes, User Types
09Next Steps
Now learn about data types and variables in C: