2025年11月7日金曜日

How do we use the reserved words in c ?

 You use the reserved words, or keywords, in C by incorporating them directly into your code exactly as they are defined to perform their specific, built-in functions. You cannot use them as names for your own variables, functions, or types.

Here's a breakdown of how you typically use them:


1. Defining Program Structure and Entry Point 🏠

Keywords define the essential scaffolding of your program.

  • int and void in main: You use these to declare the return type of the entry point function.

    C
    int main() { 
        // ... 
        return 0; 
    }
    // 'int' tells the OS the function returns an integer status code.
    // 'void' can also be used if you explicitly return nothing.
    

2. Declaring Data Types and Variables 📊

Keywords are essential for telling the compiler how to interpret and reserve memory for your data.

  • int, float, char: Used when declaring variables.

    C
    int age;          // Declares a variable for an integer
    float salary = 50000.50; // Declares and initializes a float
    char initial = 'J'; // Declares a single character
    
  • const: Used to declare a variable whose value cannot be changed after initialization.

    C
    const int MAX_SIZE = 100;
    

3. Controlling Execution Flow 🚦

Keywords form the backbone of conditional logic and repetition.

  • Decision Making:

    C
    if (score >= 90) {
        printf("Grade A\n");
    } else if (score >= 80) {
        printf("Grade B\n");
    } else {
        printf("Grade C\n");
    }
    
  • Loops:

    C
    for (int i = 0; i < 5; i++) { // 'for' keyword starts the loop structure
        printf("%d\n", i);
    }
    
  • Breaking Out:

    C
    while (isRunning) {
        // ... code ...
        if (error) {
            break; // 'break' keyword immediately exits the loop
        }
    }
    

4. Defining Functions and Scopes 🧩

Keywords help define what a function does and what scope its variables have.

  • return: Used inside a function to send a value back to the caller (or end execution).

    C
    int add(int a, int b) {
        return a + b; // Sends the sum back
    }
    
  • Storage Class Keywords: Control the scope and lifetime of variables.

    C
    static int counter = 0; // 'static' ensures 'counter' retains its value between function calls
    

🛑 The Golden Rule: Cannot Be Reused

The most important thing to remember is that you cannot use them for anything else. For example, you cannot do this:

C
// ILLEGAL USAGE: Keywords cannot be identifiers
int float = 5.5;      // ERROR: 'float' is a keyword
void if() { ... }     // ERROR: 'if' is a keyword

Since keywords define the structure of the language, a simple diagram illustrating the control flow keywords might help solidify how they guide the program's execution path.

Would you like to see the full list of the 32 standard C keywords?

0 件のコメント:

コメントを投稿