C Programs Tutorials | IT Developer
IT Developer

C Programming - C Keywords



Share with a Friend

C Programming - C Keywords

C Keywords

C keywords are reserved words predefined by the language and have a special meaning to the compiler. They cannot be used as identifiers (e.g., variable names, function names). Keywords help define the structure and functionality of a program.

List of Keywords in C

Below is the list of the 32 standard keywords in C:

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

Categories of Keywords

  1. Data Types
    • Define variable types.
    • Examples: int, float, double, char, void
  2. Control Flow
    • Used for decision-making and loops.
    • Examples: if, else, switch, case, for, while, do, break, continue
  3. Storage Classes
    • Define the scope and lifetime of variables/functions.
    • Examples: auto, static, extern, register
  4. Modifiers
    • Modify the behavior of variables.
    • Examples: short, long, signed, unsigned
  5. Special Purpose
    • Serve specific tasks.
    • Examples: sizeof, typedef, volatile, return, goto
  6. Structure and Union
    • Define compound data types.
    • Examples: struct, union
  7. Enumeration
    • Used for enumerated types.
    • Example: enum

Key Points About C Keywords

  1. Case Sensitivity:
    • All keywords in C are lowercase.
    • For example, Int or INT is not the same as int.
  2. Reserved Words:
    • Keywords cannot be redefined or used as identifiers.

Example (Invalid Code):

int while = 5;  // Error: 'while' is a keyword

  1. Extensions in C:
    • The C standard has 32 keywords, but some compilers add extra keywords (e.g., asm, inline).
  2. Proper Usage:
    • Ensure keywords are used in their correct context.

Examples of Keywords in Code

Control Flow Keywords

#include <stdio.h>

int main() {

    int num = 10;

    if (num > 5) {  // 'if' and 'else' are control flow keywords

        printf("Number is greater than 5.\n");

    } else {

        printf("Number is 5 or less.\n");

    }

   

    return 0;  // 'return' is a keyword

}

Data Type and Modifier Keywords

#include <stdio.h>

int main() {

    unsigned int age = 25;  // 'unsigned' and 'int' are keywords

    printf("Age: %u\n", age);

   

    return 0;

}

Storage Class Keywords

#include <stdio.h>

void demoFunction() {

    static int counter = 0;  // 'static' is a storage class keyword

    counter++;

    printf("Counter: %d\n", counter);

}

 

int main() {

    demoFunction();

    demoFunction();

    return 0;

}

Summary

  • Definition: Keywords are reserved words with special meaning to the compiler.
  • Usage: Keywords must be used appropriately to avoid errors.
  • Restrictions: Cannot be used as variable names, function names, or other identifiers.
  • Purpose: Keywords form the core syntax and structure of C programming.