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; // OK
float result; // OK
char 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.

KeywordDescriptionExample
intInteger (whole numbers)int age = 25;
floatSingle-precision decimalfloat pi = 3.14f;
doubleDouble-precision decimaldouble pi = 3.14159;
charSingle characterchar grade = 'A';
voidNo type / emptyvoid printHello();
shortShort integershort num = 100;
longLong integerlong big = 100000L;
signedCan be negative (default)signed int x = -5;
unsignedOnly positive (0+)unsigned int x = 5;
data_type_keywords.c
C
1#include <stdio.h>
2
3int main() {
4 // Data type keywords in action
5 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 true
else- Execute if false
switch- Multi-way branch
case- Switch case label
default- Default switch case

🔄 Loops

for- Count-controlled loop
while- Pre-test loop
do- Post-test loop (do-while)
break- Exit loop/switch
continue- Skip to next iteration
goto- Jump to label
control_flow_keywords.c
C
1#include <stdio.h>
2
3int main() {
4 int x = 10;
5
6 // if-else
7 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 loop
14 for (int i = 0; i < 3; i++) {
15 printf("i = %d\n", i);
16 }
17
18 // while loop
19 while (x > 5) {
20 printf("x = %d\n", x);
21 x--;
22 }
23
24 // switch
25 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.

KeywordDescriptionLifetime
autoAutomatic (default for locals)Block scope
staticPersists between callsProgram lifetime
externDefined in another fileProgram lifetime
registerHint: store in CPU registerBlock scope
storage_class_keywords.c
C
1void counter() {
2 static int count = 0; // Persists between calls
3 count++;
4 printf("Called %d times\n", count);
5}
6
7extern int globalVar; // Defined elsewhere
8
9register int fast = 0; // Hint for faster access

06User-Defined Type Keywords

KeywordDescriptionExample
structGroup of related variablesstruct Person { ... }
unionShare memory locationunion Data { ... }
enumNamed integer constantsenum Color { RED, GREEN }
typedefCreate type aliastypedef int Integer;
user_defined_types.c
C
1// struct - group related data
2struct Person {
3 char name[50];
4 int age;
5};
6
7// enum - named constants
8enum Color { RED, GREEN, BLUE };
9
10// typedef - create alias
11typedef unsigned int uint;
12
13// union - shared memory
14union Data {
15 int i;
16 float f;
17};

07Other Important Keywords

KeywordDescriptionExample
returnExit function, return valuereturn 0;
sizeofGet size in bytessizeof(int) → 4
constMake read-onlyconst int PI = 3;
volatileCan change unexpectedlyvolatile int flag;
other_keywords.c
C
1#include <stdio.h>
2
3int add(int a, int b) {
4 return a + b; // return: exit with value
5}
6
7int main() {
8 const int MAX = 100; // const: cannot change
9
10 printf("int size: %zu bytes\n", sizeof(int));
11 printf("Sum: %d\n", add(5, 3));
12
13 return 0; // return success
14}

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: