Keywords · learncode.live

Keywords

Keywords are reserved words in C that have a special meaning to the compiler. You cannot use them as variable names, function names, or any other kind of identifier. Think of them as the command words of the language - just like you can’t name a variable if because if is how you tell the computer to make a decision.

C has 32 keywords (as per the C89/C90 standard), and later standards added a few more. Every keyword is written in lowercase.

// ✅ These are keywords used the right way:
if (score > 50) { }
int count = 10;
return 0;

// ❌ These would cause errors:
int if = 5;      // 'if' is a keyword
float return;    // 'return' is a keyword

What Happens if We Use a Variable Name Same as a Keyword?

The compiler will throw an error and refuse to compile your program. Keywords are locked by the language - you cannot repurpose them.

#include <stdio.h>

int main() {
    int float = 10;    // ❌ ERROR: 'float' is a keyword
    int for = 5;       // ❌ ERROR: 'for' is a keyword
    int while = 1;     // ❌ ERROR: 'while' is a keyword

    printf("%d", float);
    return 0;
}

If you try to compile this, the compiler will say something like:

error: expected identifier or '(' before 'float'

Fix: Use a variation - float_value, for_loop, while_condition.

C Keywords List

Here are the 32 keywords from the original C standard, grouped by what they do.

Data Type Keywords

These keywords are used to declare the type of data a variable will hold.

KeywordPurposeExample
intInteger (whole number)int age = 25;
charSingle characterchar grade = 'A';
floatDecimal number (single precision)float pi = 3.14;
doubleDecimal number (double precision)double price = 99.99;
voidNo type / emptyvoid greet() { }
structGroup related variablesstruct Student { };
unionOverlapping memory for different typesunion Data { };
enumNamed integer constantsenum Color { RED, GREEN };
signedSigned integer (default)signed int x = -5;
unsignedUnsigned integer (no negatives)unsigned int x = 10;
shortSmaller integershort int x = 5;
longLarger integerlong int x = 100000;
#include <stdio.h>

int main() {
    int age = 25;
    char grade = 'A';
    float pi = 3.14f;
    double price = 199.99;
    unsigned int count = 100;

    printf("Age: %d\n", age);
    printf("Grade: %c\n", grade);
    printf("Pi: %.2f\n", pi);
    printf("Price: %.2lf\n", price);
    printf("Count: %u\n", count);

    return 0;
}

Control Flow Keywords

These keywords control the order in which statements are executed.

KeywordPurpose
ifExecute code if a condition is true
elseExecute code when if is false
switchSelect one of many code blocks
caseA specific value in a switch
defaultFallback case in a switch
whileRepeat while a condition is true
doExecute once, then repeat while true
forLoop with initialization, condition, increment
breakExit a loop or switch early
continueSkip current iteration, go to next
returnExit a function and optionally return a value
gotoJump to another labeled line (rarely used)
#include <stdio.h>

int main() {
    int score = 85;

    // if-else
    if (score >= 90) {
        printf("Grade: A\n");
    } else if (score >= 75) {
        printf("Grade: B\n");
    } else {
        printf("Grade: C\n");
    }

    // for loop
    for (int i = 1; i <= 3; i++) {
        printf("Count: %d\n", i);
    }

    // while loop with break
    int n = 0;
    while (1) {
        n++;
        if (n == 3) break;
        printf("n = %d\n", n);
    }

    // switch-case
    char grade = 'B';
    switch (grade) {
        case 'A':
            printf("Excellent!\n");
            break;
        case 'B':
            printf("Good job!\n");
            break;
        default:
            printf("Keep trying!\n");
    }

    return 0;
}

Storage Class Keywords

These keywords define the scope (where a variable is accessible) and lifetime (how long it lives) of a variable.

KeywordPurpose
autoDefault storage for local variables (rarely used explicitly)
registerSuggest storing the variable in a CPU register for speed
staticPreserve value between function calls; limit scope to the file
externDeclare a variable defined in another file
#include <stdio.h>

void counter() {
    static int count = 0;  // Keeps its value between calls
    count++;
    printf("Called %d times\n", count);
}

int main() {
    counter();  // Called 1 times
    counter();  // Called 2 times
    counter();  // Called 3 times

    return 0;
}

Without static, count would reset to 0 every time counter() is called.

User-defined Type Keywords

These keywords let you create your own data types - one of C’s most powerful features.

KeywordPurposeExample
structGroup different types into one recordstruct Student { char name[50]; int age; };
unionStore different types in the same memory locationunion Value { int i; float f; };
enumCreate a set of named integer constantsenum Weekday { MON, TUE, WED };
typedefCreate an alias for an existing typetypedef unsigned long ulong;
#include <stdio.h>

// struct: group related data
struct Student {
    char name[50];
    int age;
    float marks;
};

// enum: named constants
enum Weekday { MON = 1, TUE, WED, THU, FRI };

// typedef: create a shorter alias
typedef unsigned long ulong;

int main() {
    struct Student s1 = {"Alice", 20, 92.5};
    printf("%s is %d years old\n", s1.name, s1.age);

    enum Weekday today = WED;
    printf("Day number: %d\n", today);   // 3

    ulong bigNumber = 1000000;
    printf("%lu\n", bigNumber);

    return 0;
}

Type Qualifier Keywords

These keywords modify how a variable can be accessed or optimized.

KeywordPurpose
constValue cannot be changed after initialization
volatileTell the compiler the value may change unexpectedly (e.g., hardware registers)
restrictA pointer is the only way to access a data block (optimization hint, C99)
#include <stdio.h>

int main() {
    const float PI = 3.14159;
    // PI = 3.14;     // ❌ Error: cannot modify a const variable

    int radius = 5;
    float area = PI * radius * radius;
    printf("Area: %.2f\n", area);

    return 0;
}

Other Keywords

These keywords don’t fit neatly into the above categories but are essential.

KeywordPurpose
sizeofGet the size (in bytes) of a type or variable
signedExplicitly declare a signed integer
unsignedDeclare an unsigned integer
shortDeclare a short integer
longDeclare a long integer
#include <stdio.h>

int main() {
    printf("int size: %zu bytes\n", sizeof(int));
    printf("char size: %zu bytes\n", sizeof(char));
    printf("float size: %zu bytes\n", sizeof(float));
    printf("double size: %zu bytes\n", sizeof(double));

    int x = 42;
    printf("Variable x size: %zu bytes\n", sizeof(x));

    return 0;
}

Output:

int size: 4 bytes
char size: 1 byte
float size: 4 bytes
double size: 8 bytes
Variable x size: 4 bytes
Courses