C, a powerful and versatile programming language, forms the backbone of many modern software systems. At its core lies a set of special words known as keywords or reserved words. These keywords are the building blocks of C programming, each serving a specific purpose and playing a crucial role in the language's syntax and functionality.

What are C Keywords?

C keywords are predefined, reserved words that have special meanings and purposes within the C programming language. These words cannot be used as identifiers (such as variable names, function names, or labels) because they are reserved for specific uses in C syntax.

🔑 Key Point: C keywords are case-sensitive and must be written in lowercase.

There are 32 keywords in standard C (C89/C90), with a few additional keywords introduced in later versions of the language. Let's dive deep into these keywords, exploring their uses and providing practical examples for each.

List of C Keywords

Here's a comprehensive list of C keywords:

Category Keywords
Data Types int, float, double, char, void
Type Modifiers short, long, signed, unsigned
Storage Classes auto, register, static, extern
Control Flow if, else, switch, case, default, break, continue, goto
Loops for, while, do
Function-related return
Structures and Unions struct, union
Other const, volatile, sizeof, typedef, enum

Let's explore each of these keywords in detail with practical examples.

Data Type Keywords

1. int

The int keyword is used to declare integer variables.

#include <stdio.h>

int main() {
    int age = 25;
    int year = 2023;

    printf("Age: %d\n", age);
    printf("Year: %d\n", year);

    return 0;
}

Output:

Age: 25
Year: 2023

2. float

float is used for declaring single-precision floating-point numbers.

#include <stdio.h>

int main() {
    float pi = 3.14159f;
    float radius = 5.0f;
    float area = pi * radius * radius;

    printf("Area of circle: %.2f\n", area);

    return 0;
}

Output:

Area of circle: 78.54

3. double

double is used for declaring double-precision floating-point numbers.

#include <stdio.h>

int main() {
    double avogadro = 6.02214076e23;

    printf("Avogadro's number: %.2e\n", avogadro);

    return 0;
}

Output:

Avogadro's number: 6.02e+23

4. char

char is used to declare character variables.

#include <stdio.h>

int main() {
    char grade = 'A';
    char newline = '\n';

    printf("Grade: %c%c", grade, newline);

    return 0;
}

Output:

Grade: A

5. void

void is used to indicate that a function doesn't return a value or to declare a generic pointer.

#include <stdio.h>

void greet() {
    printf("Hello, World!\n");
}

int main() {
    greet();
    return 0;
}

Output:

Hello, World!

Type Modifier Keywords

6. short

short is used to declare short integer variables.

#include <stdio.h>

int main() {
    short int small_number = 32767;

    printf("Small number: %hd\n", small_number);

    return 0;
}

Output:

Small number: 32767

7. long

long is used to declare long integer or double variables.

#include <stdio.h>

int main() {
    long int big_number = 2147483647L;
    long double pi = 3.141592653589793238L;

    printf("Big number: %ld\n", big_number);
    printf("Pi (long double): %.15Lf\n", pi);

    return 0;
}

Output:

Big number: 2147483647
Pi (long double): 3.141592653589793

8. signed

signed is used to declare signed variables (can hold both positive and negative values).

#include <stdio.h>

int main() {
    signed int temperature = -10;

    printf("Temperature: %d°C\n", temperature);

    return 0;
}

Output:

Temperature: -10°C

9. unsigned

unsigned is used to declare unsigned variables (can hold only non-negative values).

#include <stdio.h>

int main() {
    unsigned int positive_number = 42;

    printf("Positive number: %u\n", positive_number);

    return 0;
}

Output:

Positive number: 42

Storage Class Keywords

10. auto

auto is the default storage class for local variables. It's rarely used explicitly as it's the default behavior.

#include <stdio.h>

int main() {
    auto int x = 10;  // 'auto' is optional here

    printf("x: %d\n", x);

    return 0;
}

Output:

x: 10

11. register

register is used to suggest that a variable should be stored in a CPU register for faster access.

#include <stdio.h>

int main() {
    register int counter;

    for(counter = 1; counter <= 5; counter++) {
        printf("%d ", counter);
    }

    return 0;
}

Output:

1 2 3 4 5

12. static

static is used to declare variables that retain their value between function calls or to limit the scope of a global variable to the current file.

#include <stdio.h>

void increment_counter() {
    static int counter = 0;
    counter++;
    printf("Counter: %d\n", counter);
}

int main() {
    increment_counter();
    increment_counter();
    increment_counter();

    return 0;
}

Output:

Counter: 1
Counter: 2
Counter: 3

13. extern

extern is used to declare a variable or function that is defined in another file.

// File: main.c
#include <stdio.h>

extern int global_variable;

int main() {
    printf("Global variable: %d\n", global_variable);
    return 0;
}

// File: global.c
int global_variable = 42;

Output (when compiled and run):

Global variable: 42

Control Flow Keywords

14. if

if is used for conditional execution of code.

#include <stdio.h>

int main() {
    int age = 18;

    if (age >= 18) {
        printf("You are eligible to vote.\n");
    }

    return 0;
}

Output:

You are eligible to vote.

15. else

else is used in conjunction with if to specify an alternative code block to execute when the if condition is false.

#include <stdio.h>

int main() {
    int number = 7;

    if (number % 2 == 0) {
        printf("%d is even.\n", number);
    } else {
        printf("%d is odd.\n", number);
    }

    return 0;
}

Output:

7 is odd.

16. switch

switch is used for multi-way branching based on the value of an expression.

#include <stdio.h>

int main() {
    char grade = 'B';

    switch(grade) {
        case 'A':
            printf("Excellent!\n");
            break;
        case 'B':
            printf("Good job!\n");
            break;
        case 'C':
            printf("Average performance.\n");
            break;
        default:
            printf("Need improvement.\n");
    }

    return 0;
}

Output:

Good job!

17. case

case is used within a switch statement to specify a particular case to match against.

(See the example for switch)

18. default

default is used within a switch statement to specify the code to execute if no case matches.

(See the example for switch)

19. break

break is used to exit from a loop or switch statement.

#include <stdio.h>

int main() {
    int i;

    for(i = 1; i <= 10; i++) {
        if(i == 6) {
            break;
        }
        printf("%d ", i);
    }

    return 0;
}

Output:

1 2 3 4 5

20. continue

continue is used to skip the rest of the current iteration in a loop and continue with the next iteration.

#include <stdio.h>

int main() {
    int i;

    for(i = 1; i <= 5; i++) {
        if(i == 3) {
            continue;
        }
        printf("%d ", i);
    }

    return 0;
}

Output:

1 2 4 5

21. goto

goto is used to transfer control to a labeled statement within the same function.

#include <stdio.h>

int main() {
    int i = 0;

start:
    if(i < 5) {
        printf("%d ", i);
        i++;
        goto start;
    }

    return 0;
}

Output:

0 1 2 3 4

⚠️ Note: While goto exists in C, it's generally discouraged in modern programming practices as it can make code harder to understand and maintain.

Loop Keywords

22. for

for is used to create a loop that executes a specified number of times.

#include <stdio.h>

int main() {
    int i;

    for(i = 1; i <= 5; i++) {
        printf("%d ", i);
    }

    return 0;
}

Output:

1 2 3 4 5

23. while

while is used to create a loop that continues as long as a specified condition is true.

#include <stdio.h>

int main() {
    int count = 1;

    while(count <= 5) {
        printf("%d ", count);
        count++;
    }

    return 0;
}

Output:

1 2 3 4 5

24. do

do is used in conjunction with while to create a loop that executes at least once before checking the condition.

#include <stdio.h>

int main() {
    int num = 1;

    do {
        printf("%d ", num);
        num++;
    } while(num <= 5);

    return 0;
}

Output:

1 2 3 4 5

25. return

return is used to exit a function and optionally return a value to the calling code.

#include <stdio.h>

int sum(int a, int b) {
    return a + b;
}

int main() {
    int result = sum(5, 3);
    printf("Sum: %d\n", result);

    return 0;
}

Output:

Sum: 8

Structure and Union Keywords

26. struct

struct is used to define a structure, which is a user-defined data type that groups related variables of different data types.

#include <stdio.h>

struct Person {
    char name[50];
    int age;
};

int main() {
    struct Person john = {"John Doe", 30};

    printf("Name: %s\n", john.name);
    printf("Age: %d\n", john.age);

    return 0;
}

Output:

Name: John Doe
Age: 30

27. union

union is used to define a union, which is a special data type that allows storing different data types in the same memory location.

#include <stdio.h>

union Data {
    int i;
    float f;
    char str[20];
};

int main() {
    union Data data;

    data.i = 10;
    printf("data.i: %d\n", data.i);

    data.f = 220.5;
    printf("data.f: %.2f\n", data.f);

    strcpy(data.str, "C Programming");
    printf("data.str: %s\n", data.str);

    return 0;
}

Output:

data.i: 10
data.f: 220.50
data.str: C Programming

Other Keywords

28. const

const is used to declare constants, which are variables whose values cannot be changed after initialization.

#include <stdio.h>

int main() {
    const double PI = 3.14159;
    double radius = 5.0;
    double area = PI * radius * radius;

    printf("Area of circle: %.2f\n", area);

    // PI = 3.14; // This would cause a compilation error

    return 0;
}

Output:

Area of circle: 78.54

29. volatile

volatile is used to declare variables that can be changed by external factors (like hardware) and should not be optimized by the compiler.

#include <stdio.h>

int main() {
    volatile int sensor_value = 0;

    // Simulating external change
    sensor_value = 42;

    printf("Sensor value: %d\n", sensor_value);

    return 0;
}

Output:

Sensor value: 42

30. sizeof

sizeof is used to determine the size in bytes of a variable or data type.

#include <stdio.h>

int main() {
    int num = 10;
    double pi = 3.14159;

    printf("Size of int: %zu bytes\n", sizeof(int));
    printf("Size of num: %zu bytes\n", sizeof(num));
    printf("Size of double: %zu bytes\n", sizeof(double));
    printf("Size of pi: %zu bytes\n", sizeof(pi));

    return 0;
}

Output:

Size of int: 4 bytes
Size of num: 4 bytes
Size of double: 8 bytes
Size of pi: 8 bytes

31. typedef

typedef is used to create an alias name for existing data types.

#include <stdio.h>

typedef unsigned long ulong;

int main() {
    ulong big_number = 1234567890UL;

    printf("Big number: %lu\n", big_number);

    return 0;
}

Output:

Big number: 1234567890

32. enum

enum is used to define an enumeration, which is a set of named integer constants.

#include <stdio.h>

enum Days {SUN, MON, TUE, WED, THU, FRI, SAT};

int main() {
    enum Days today = WED;

    printf("Today is day number %d\n", today);

    return 0;
}

Output:

Today is day number 3

Conclusion

C keywords form the foundation of the C programming language. Understanding these reserved words and their proper usage is crucial for writing efficient and effective C programs. Each keyword serves a specific purpose, from defining data types and controlling program flow to creating complex data structures.

By mastering these keywords, you'll be well-equipped to tackle a wide range of programming challenges in C. Remember that while knowing these keywords is important, the art of programming lies in how you combine them to create elegant and efficient solutions.

🚀 Pro Tip: Practice using these keywords in various programming scenarios to solidify your understanding and improve your C programming skills.

As you continue your journey in C programming, keep exploring and experimenting with these keywords. They are your tools for crafting powerful and efficient C programs. Happy coding!