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

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...

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 ...

Place holder and control character in c language

 In the C programming language, placeholders and control characters are used to format and control the output of text in console-based programs. They are special characters or sequences of characters that have specific meanings. Here are the placeholders and control characters commonly used in C: 1. Placeholders:    - %d: Used to display signed integers.      Example: printf("The value is %d", 10);    - %u: Used to display unsigned integers.      Example: printf("The value is %u", 10);    - %f: Used to display floating-point numbers.      Example: printf("The value is %f", 3.14);    - %c: Used to display characters.      Example: printf("The character is %c", 'A');    - %s: Used to display strings (sequence of characters).      Example: printf("The string is %s", "Hello");    - %p: Used to display memory addresses (pointers)...