Skip to main content

Keywords in C Language with example

Here is a comprehensive list of keywords in the C programming language:

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

Note that these keywords have predefined meanings in the C language and cannot be used as identifiers (variable names, function names, etc.) in your programs.

Certainly! Here are some of the keywords in the C programming language along with examples:

1. auto: Declares a local variable with automatic storage duration.
   Example: auto int x = 5;

2. break: Terminates the current loop or switch statement.
   Example:
   ```c
   for (int i = 0; i < 10; i++) {
       if (i == 5) {
           break;  // terminates the loop when i reaches 5
       }
   }
   ```

3. case: Defines a constant value within a switch statement.
   Example:
   ```c
   switch (x) {
       case 1:
           printf("Value is 1");
           break;
       case 2:
           printf("Value is 2");
           break;
       default:
           printf("Value is neither 1 nor 2");
   }
   ```

4. const: Declares a constant variable that cannot be modified.
   Example: const int MAX_VALUE = 100;

5. continue: Jumps to the next iteration of a loop.
   Example:
   ```c
   for (int i = 0; i < 10; i++) {
       if (i == 5) {
           continue;  // skips the rest of the loop body and moves to the next iteration
       }
   }
   ```

6. do: Starts a do-while loop.
   Example:
   ```c
   int i = 0;
   do {
       printf("%d ", i);
       i++;
   } while (i < 5);
   ```

7. else: Represents an alternative branch in an if statement.
   Example:
   ```c
   if (x > 0) {
       printf("Positive");
   } else {
       printf("Non-positive");
   }
   ```

8. enum: Defines an enumerated data type.
   Example:
   ```c
   enum Color {
       RED,
       GREEN,
       BLUE
   };
   enum Color selectedColor = GREEN;
   ```

9. extern: Declares a variable or function that is defined in another source file or external to the current scope.
   Example: extern int globalVariable;

10. float: Represents a floating-point data type.
    Example: float pi = 3.14159;

11. for: Starts a for loop with initialization, condition, and increment/decrement expressions.
    Example:
    ```c
    for (int i = 0; i < 10; i++) {
        printf("%d ", i);
    }
    ```

12. if: Represents a conditional statement.
    Example:
    ```c
    if (x > 0) {
        printf("Positive");
    }
    ```

13. int: Represents an integer data type.
    Example: int age = 25;

14. return: Terminates the current function and returns a value.
    Example:
    ```c
    int sum(int a, int b) {
        return a + b;
    }
    ```

15. sizeof: Returns the size in bytes of a data type or variable.
    Example: sizeof(int);

16. static: Declares a variable or function with static storage duration.
    Example:
    ```c
    static int counter = 0;
    static void incrementCounter() {
        counter++;
    }
    ```

17. switch: Starts a switch statement with multiple possible execution paths based on a variable's value.
    Example:
    ```c
    switch (choice) {
        case 1:
            printf("First option selected");
            break;
        case 2:
            printf("Second option selected");
            break;
        default:
            printf("Invalid option");
    }
    ```

18. while: Starts a while loop with a condition.
    Example:
    ```c
    int i = 0;
    while (i < 5) {
        printf("%d ", i);
        i++;
    }
    ```

These are some of the keywords in the C programming language. Each keyword serves a specific purpose and has its own rules and usage within the language. Understanding and using these keywords correctly is essential for writing valid and effective C programs.

Comments

Popular posts from this blog

Tokens, Identifiers, Data Types, Sequence Control, Subprogram Control, Arrays, Structures, Union, String, Pointers, Functions, File Handling, Command Line Argumaents, Preprocessors in C with example

 Let's discuss each concept briefly and provide examples for better understanding: 1. Tokens: Tokens are the smallest building blocks in C programming. They include keywords, identifiers, constants, strings, operators, and punctuators. Example: ```c #include <stdio.h> int main() {     int num = 42;  // 'int', 'main', 'return', '42', '=', ';' are tokens     printf("Hello, World!");  // 'printf', '(', ')', 'Hello, World!', ';', are tokens     return 0;  // 'return', '0', ';' are tokens } ``` 2. Identifiers: Identifiers are names used to identify variables, functions, or other user-defined entities. Example: ```c int age = 30;  // 'age' is an identifier (variable name) void displayMessage() {  // 'displayMessage' is an identifier (function name)     // function body } ``` 3. Data Types: Data types define the type of data that can be stored in ...

Web Programming: HTML, DHTML, XML, Scripting, Java, Servlets, Applets

 Web programming encompasses various technologies and concepts used to develop web applications. Let's explore each of them in detail: 1. HTML (Hypertext Markup Language): HTML is the standard markup language used to create the structure and content of web pages. It uses tags to define elements like headings, paragraphs, images, links, forms, etc. Example: ```html <!DOCTYPE html> <html> <head>     <title>My Web Page</title> </head> <body>     <h1>Hello, World!</h1>     <p>This is a paragraph.</p>     <img src="image.jpg" alt="Image">     <a href="https://www.example.com">Visit Example</a> </body> </html> ``` 2. DHTML (Dynamic HTML): DHTML is a combination of HTML, CSS, and JavaScript that allows web pages to become more dynamic and interactive. Example (DHTML with JavaScript): ```html <!DOCTYPE html> <htm...

Built in function in Python with example

 Python provides a rich set of built-in functions that are readily available for use without requiring any import statements. These functions are part of the Python Standard Library and cover various aspects of programming. Let's look at some common built-in functions with examples: 1. `len()` - Returns the number of items in an object: ```python fruits = ["apple", "banana", "orange", "grape"] print(len(fruits))  # Output: 4 text = "Hello, World!" print(len(text))  # Output: 13 ``` 2. `print()` - Prints the specified message to the console: ```python print("Hello, World!")  # Output: Hello, World! ``` 3. `range()` - Generates a sequence of numbers: ```python # Generate numbers from 0 to 9 numbers = list(range(10)) print(numbers)  # Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # Generate numbers from 5 to 14 (exclusive) numbers = list(range(5, 15)) print(numbers)  # Output: [5, 6, 7, 8, 9, 10, 11, 12, 13, 14] # Generate even numb...