C Programming Keywords

Keywords in C programming are reserved words that have special meanings and purposes defined by the language. They form the backbone of C’s syntax and cannot be used as identifiers, such as variable or function names. In this article, we’ll explore what keywords are, list the standard C keywords, and demonstrate their usage with practical examples.


Example: Using Keywords in C

#include <stdio.h>

// Using keywords to define a loop and conditional statement
int main() {
    int i; // Keyword 'int' declares an integer variable
    for (i = 1; i <= 5; i++) { // 'for' keyword defines a loop
        if (i % 2 == 0) { // 'if' keyword for conditional check
            printf("%d is even\n", i);
        } else { // 'else' keyword for alternative condition
            printf("%d is odd\n", i);
        }
    }
    return 0; // 'return' keyword to exit the function
}

Output:

1 is odd
2 is even
3 is odd
4 is even
5 is odd

What Are Keywords in C?

Keywords in C are predefined, reserved words that have specific meanings to the compiler. They are used to define the structure and logic of a program, such as declaring variables, controlling program flow, or defining functions. Because they are reserved, keywords cannot be used as identifiers (e.g., variable names, function names) in your program.

For example, int is a keyword used to declare integer variables, and if is a keyword used to create conditional statements. Attempting to use a keyword like if as a variable name will result in a compilation error.


List of C Keywords

The C programming language (C89/C90 standard) defines 32 keywords. These are:

Keyword Purpose
auto Defines automatic storage duration (rarely used)
break Exits from a loop or switch statement
case Defines a branch in a switch statement
char Declares a character variable
const Declares a constant (unchangeable) value
continue Skips to the next iteration of a loop
default Specifies the default branch in a switch statement
do Starts a do-while loop
double Declares a double-precision floating-point variable
else Specifies an alternative branch in an if statement
enum Defines an enumerated type
extern Declares a variable or function defined elsewhere
float Declares a single-precision floating-point variable
for Defines a for loop
goto Jumps to a labeled statement (use sparingly)
if Defines a conditional statement
int Declares an integer variable
long Modifies the size of a data type (e.g., long int)
register Suggests storing a variable in a CPU register (rarely used)
return Exits a function and returns a value
short Modifies the size of a data type (e.g., short int)
signed Specifies a signed data type (default for most types)
sizeof Returns the size of a variable or data type
static Preserves variable value between function calls
struct Defines a structure to group related variables
switch Defines a multi-branch selection statement
typedef Creates an alias for a data type
union Defines a union to share memory among variables
unsigned Specifies an unsigned data type (non-negative)
void Indicates no value or type
volatile Indicates a variable may change unexpectedly
while Defines a while loop

Note: Modern C standards (e.g., C99, C11) introduced additional keywords like inline, restrict, and _Bool. Always check your compiler’s documentation for the complete list.


Why Are Keywords Important?

Keywords are essential for the following reasons:

  1. Define Program Structure: Keywords like if, for, and return control the flow and logic of a program.
  2. Ensure Consistency: As reserved words, keywords provide a standardized way to write C code that is understood by all compilers.
  3. Prevent Naming Conflicts: Since keywords cannot be used as identifiers, they avoid confusion with user-defined names (see C Identifiers).
  4. Enable Type Safety: Keywords like int, float, and char define variable types, ensuring proper data handling (see C Variables).

Tips for Working with Keywords

  • Avoid Keyword Names: Do not use keywords as variable or function names, as this will cause compilation errors (e.g., int int; is invalid).
  • Understand Context: Learn the purpose of each keyword to use it correctly. For example, use const for variables that shouldn’t change, like const float PI = 3.14159;.
  • Use Keywords Sparingly: Avoid overusing keywords like goto or register, as they can make code harder to read or less portable.
  • Comment Keyword Usage: For complex code, use comments to explain how keywords like switch or struct are used (see C Comments).
  • Stay Updated: Be aware of new keywords introduced in modern C standards (e.g., C99 or C11) if you’re using a newer compiler.

Example: Using Keywords in a Program

#include <stdio.h>

// Structure to store student information
struct Student {
    char name[50]; // Keyword 'struct' defines a custom type
    int roll_no;
    float marks;
};

int main() {
    // Declare a constant for maximum students
    const int MAX_STUDENTS = 2; // Keyword 'const' for unchangeable value

    // Declare an array of Student structures
    struct Student students[MAX_STUDENTS];

    // Loop to input student details
    for (int i = 0; i < MAX_STUDENTS; i++) { // Keywords 'for' and 'int'
        printf("Enter name for student %d: ", i + 1);
        scanf("%s", students[i].name);
        printf("Enter roll number: ");
        scanf("%d", &students[i].roll_no);
        printf("Enter marks: ");
        scanf("%f", &students[i].marks);
    }

    // Display student details
    printf("\nStudent Details:\n");
    for (int i = 0; i < MAX_STUDENTS; i++) {
        printf("Name: %s, Roll No: %d, Marks: %.2f\n",
               students[i].name, students[i].roll_no, students[i].marks);
    }

    return 0; // Keyword 'return' to exit the program
}

Sample Input:

Enter name for student 1: Alice
Enter roll number: 101
Enter marks: 85.5
Enter name for student 2: Bob
Enter roll number: 102
Enter marks: 90.0

Output:

Student Details:
Name: Alice, Roll No: 101, Marks: 85.50
Name: Bob, Roll No: 102, Marks: 90.00

This example demonstrates the use of keywords like struct, const, for, int, and return to create a program that manages student data, showcasing their role in defining program structure and logic.


Did You Know?

  • The 32 keywords in C89 were standardized to ensure portability across different compilers, as defined in The C Programming Language by Kernighan and Ritchie.
  • Some keywords, like auto and register, are rarely used in modern C programming due to compiler optimizations and changes in coding practices.
  • Keywords are case-sensitive in C, so Int or IF are not keywords and can be used as identifiers (though this is not recommended).

Leave a comment