C Programming Theory

The Character Set

A Character denote any alphabet, digit or special symbol used to represent information as mentioned below:-

Alphabet: A,B,C.......Y,Z;    a,b,c,d...,y,z;
digit: 0,1,2,...,9;
Special Symbol: ~!@#$%^&*()_-=+|\/{[()]};:"',<>.?

Constant, Variable and Keyword

Constant

Constant are generally two types

  1. Primary Constant(Integer, Real, Character)
  2. Secondary Constant(Array, Pointer, Structure, Union, Enum, Etc)

Rules for Constructing Integer Constant

  • An Integer constant must have at least one digit.
  • It must not have decimal point
  • It can be either Positive or Negative.
  • If no sign precedes an integer constant, it is assumed to be positive.
  • No commas or blanks are allowed within an integer constant.
  • The allowable range for integer constants is -2147483648 to +2147483647.

Rules for Constructing Real Constant

  • A real Constant must have at least one digit.
  • It must have a decimal point.
  • It could be either positive or negative.
  • Default sign is positive.
  • No commas or blanks are allowed within real constant.

Rules for Constructing Character Constant

  • A character constant is a single alphabet, a single digit or single symbol enclosed within single inverted commas.
  • Both the inverted commas should point to the left. For example 'A','I','5','=' are valid character constant.

Rules for Constructing Variable Name

  • A variable name is any combination of 1 to 31 alphabet, digit or underscores. Some compiler allow variable names whose length could be up to 247 characters. Still, it would be safer to stick to the rule of 31 characters. Do not create unnecessarily long variable name as it adds to your typing efforts.
  • The first character in the variable name must be an alphabet or underscore.
  • No commas or blanks are allowed within a variable name.
  • No special symbol other than an underscore (as in gross_sal) can be used in a variable name.
  • Eg: int name, symbol, address ;
     float number, c_number, computer_code;

Data Type

In the C programming language, data types are used to define the type of data that a variable can hold. Here are the primary categories of data types in C:

1) Basic Data Types

a) Integer Type

int: A basic integer type. Size typically 4 bytes.

short: A short integer type. Size typically 2 bytes.

long: A long integer type. Size typically 4 or 8 bytes.

long long: A longer integer type. Size typically 8 bytes.


b) Floating- Point Type

float: Single precision floating point. Size typically 4 bytes.

double: Double precision floating point. Size typically 8 bytes.

long double: Extended precision floating point. Size typically 8, 12, or 16 bytes, depending on the system.

c) Character Type

char: A character type. Size typically 1 byte. Can hold a single character or small integer.

unsigned char: An unsigned character type. Size typically 1 byte.

signed char: A signed character type. Size typically 1 byte.


Derived Data Type

a) Array

Collections of elements of the same type stored in contiguous memory locations.
Example: int arr[10];

b) Pointer

Variables that store memory addresses.

Example: int *ptr;

c) Struct

Collections of variables of different types grouped together.

struct {
    int age;
    float salary;
} person;

d) Union

Similar to structures but with all members sharing the same memory location.

union {
    int i;
    float f;
} data;

d) Enumeration

User-defined types consisting of named integer constants.
enum { RED, GREEN, BLUE };

e) Void

Indicates that no value is available.

Commonly used in function return types to indicate that the function does not return a value, and in pointers to specify a generic pointer type.

Example: 

void functionName(void);

Escape Sequence

Escape sequences in C are used to represent special characters within string literals and character constants. They begin with a backslash (\) followed by one or more characters. Here's a list of common escape sequences in C:

Common Escape Sequences

  1. New Line "\n" 
    Example: printf("Hello\nWorld"); prints:
  2. Horizontal Tab "\n" 
    Example: printf("Hello\tWorld"); prints:
  3. Backspace "\b"
     
    Example: printf("Hello\bWorld"); prints:
  4. Carriage return "\r" 
    Example: printf("Hello\rWorld"); prints:
  5. Form feed "\f"
    Example: printf("Hello\fWorld"); might behave differently based on the system, often advancing to a new page.
  6. Alert(Bell) "\a"
    Example: printf("\a"); may produce a beep sound on some systems.
  7. Backslash "\\"
    Example: printf("C:\\Program Files"); prints:
  8. Single Quote " \' " 
    Example: printf("It\'s OK."); prints:
  9. Double Quote" \" "
    Example: printf("She said, \"Hello.\""); prints:
  10. Question Mark " \? "
    Example: printf("What\?"); prints:

Numeric Escape Sequences

  1. Octal Number :"\ooo"
    Example: printf("\101");
  2. Hexadecimal Number:"\xhh"
    Example: printf(\x41);

Unicode Escape Sequences(C11 and later)

    C11, also known as ISO/IEC 9899:2011, is a standard for the C programming language published by the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) in December 2011. It is the successor to the C99 standard and introduces several new features, enhancements, and corrections to the language and its standard library.
    Represents Unicode characters.
    • \u is followed by 4 hexadecimal digits.
    • \U is followed by 8 hexadecimal digits.
    • Example: printf("\u03A9"); prints the Greek letter Omega (Ω).
    • Example: printf("\U0001F600"); prints the Emoji (😀).

    C keywords

    auto

    double

    int

    Struct

    break

    else

    long

    switch

    case

    enum

    register

    typedef

    char

    extern

    return

    union

    continue

    for

    signed

    void

    default

    goto

     sizeof

    volatile

    do

    if

    static

    while

     Example of usages of keyword,

    auto

    #include <stdio.h>

    void exampleFunction() {
        // Declaring an automatic variable using 'auto'
        auto int x = 10;

        // The same declaration without 'auto' (since it's implicit)
        int y = 20;

        printf("Value of x: %d\n", x);
        printf("Value of y: %d\n", y);
    }

    int main() {
        exampleFunction();
        return 0;
    }

    double, float, int, char

    #include<stdio.h>
    int main(){
    int age;
    float height;
    double mobile_number;
    char gender;
    char name[10];
    return 0;
    }


    enum

    #include <stdio.h>

    // Defining an enum for days of the week
    enum Day {
        SUNDAY,
        MONDAY,
        TUESDAY,
        WEDNESDAY,
        THURSDAY,
        FRIDAY,
        SATURDAY
    };

    int main() {
        // Declaring a variable of type enum Day
        enum Day today;

        // Assigning a value to the enum variable
        today = WEDNESDAY;

        // Printing the value
        printf("The value of today is: %d\n", today);  // Output: 3

        return 0;
    }

    struct


    #include <stdio.h>
    #include <string.h>

    // Define a struct for employee information
    typedef struct {
        char name[50];
        char address[100];
        char mobileNumber[15];
        char email[50];
    } Employee;

    int main() {
        // Declare an array to store 5 employees
        Employee employees[5];

        // Manually input employee data for simplicity
        strcpy(employees[0].name, "Alice Johnson");
        strcpy(employees[0].address, "123 Maple Street, Springfield");
        strcpy(employees[0].mobileNumber, "555-1234");
        strcpy(employees[0].email, "alice@example.com");

        strcpy(employees[1].name, "Bob Smith");
        strcpy(employees[1].address, "456 Oak Avenue, Lincoln");
        strcpy(employees[1].mobileNumber, "555-5678");
        strcpy(employees[1].email, "bob@example.com");

        strcpy(employees[2].name, "Charlie Brown");
        strcpy(employees[2].address, "789 Pine Road, Centerville");
        strcpy(employees[2].mobileNumber, "555-8765");
        strcpy(employees[2].email, "charlie@example.com");

        strcpy(employees[3].name, "Diana Prince");
        strcpy(employees[3].address, "321 Birch Boulevard, Metropolis");
        strcpy(employees[3].mobileNumber, "555-4321");
        strcpy(employees[3].email, "diana@example.com");

        strcpy(employees[4].name, "Eve Davis");
        strcpy(employees[4].address, "654 Cedar Lane, Gotham");
        strcpy(employees[4].mobileNumber, "555-6789");
        strcpy(employees[4].email, "eve@example.com");

        // Print employee data
        for (int i = 0; i < 5; i++) {
            printf("Employee %d:\n", i + 1);
            printf("  Name: %s\n", employees[i].name);
            printf("  Address: %s\n", employees[i].address);
            printf("  Mobile Number: %s\n", employees[i].mobileNumber);
            printf("  Email: %s\n\n", employees[i].email);
        }

        return 0;
    }


    double

    #include <stdio.h>

    int main() {
        double pi = 3.141592653589793;

        printf("Value of pi: %lf\n", pi);

        return 0;
    }

    long

    #include <stdio.h>

    int main() {
        long population = 7800000000;

        printf("World population: %ld\n", population);

        return 0;
    }

    typedef

    #include <stdio.h>

    typedef unsigned long ulong;

    int main() {
        ulong distance = 1234567890;

        printf("Distance: %lu\n", distance);

        return 0;
    }

    signed

    #include <stdio.h>

    int main() {
        signed int a = -10;
        signed int b = 20;

        printf("a: %d, b: %d\n", a, b);

        return 0;
    }

    sizeof

    #include <stdio.h>

    int main() {
        int a;
        double b;
        char c;

        printf("Size of int: %zu bytes\n", sizeof(a));
        printf("Size of double: %zu bytes\n", sizeof(b));
        printf("Size of char: %zu bytes\n", sizeof(c));

        return 0;
    }

    volatile

    #include <stdio.h>

    volatile int flag = 0;

    void setFlag() {
        flag = 1;
    }

    int main() {
        printf("Initial flag value: %d\n", flag);
        setFlag();
        printf("Flag value after setFlag(): %d\n", flag);

        return 0;
    }

    static

    #include <stdio.h>

    void counter() {
        static int count = 0;  // static variable retains its value between function calls
        count++;
        printf("Counter: %d\n", count);
    }

    int main() {
        counter();
        counter();
        counter();

        return 0;
    }

    for, break, switch, goto, default, while, do, if, and return

    #include <stdio.h>

    void displayMenu() {
        printf("Menu:\n");
        printf("1. Print Numbers 1 to 5 (for loop)\n");
        printf("2. Demonstrate break in a loop\n");
        printf("3. Demonstrate switch-case\n");
        printf("4. Demonstrate goto\n");
        printf("5. Exit\n");
    }

    int main() {
        int choice;
        int i;

        while (1) {
            displayMenu();
            printf("Enter your choice: ");
            scanf("%d", &choice);

            switch (choice) {
                case 1:
                    // Example of for loop
                    printf("Numbers 1 to 5 using for loop:\n");
                    for (i = 1; i <= 5; i++) {
                        printf("%d ", i);
                    }
                    printf("\n");
                    break;

                case 2:
                    // Example of break in a loop
                    printf("Demonstrating break in a loop:\n");
                    for (i = 1; i <= 10; i++) {
                        if (i == 6) {
                            break;  // Exit the loop when i equals 6
                        }
                        printf("%d ", i);
                    }
                    printf("\n");
                    break;

                case 3:
                    // Example of switch-case with default
                    printf("Enter a number (1-3): ");
                    int num;
                    scanf("%d", &num);
                    switch (num) {
                        case 1:
                            printf("You entered one.\n");
                            break;
                        case 2:
                            printf("You entered two.\n");
                            break;
                        case 3:
                            printf("You entered three.\n");
                            break;
                        default:
                            printf("Invalid number! Default case.\n");
                    }
                    break;

                case 4:
                    // Example of goto
                    printf("Demonstrating goto statement:\n");
                    goto myLabel;  // Jump to myLabel
                    printf("This will be skipped.\n");
                    myLabel:
                    printf("Jumped to myLabel using goto.\n");
                    break;

                case 5:
                    // Exit the program
                    printf("Exiting the program.\n");
                    return 0;  // Example of return

                default:
                    printf("Invalid choice! Please try again.\n");
            }
           
            // Example of do-while loop
            int confirmExit = 0;
            do {
                printf("Do you want to exit? (1 for Yes, 0 for No): ");
                scanf("%d", &confirmExit);
                if (confirmExit == 1) {
                    return 0;  // Exit the program
                }
            } while (confirmExit != 1 && confirmExit != 0);
        }

        return 0;
    }











          0 Comments had been done yet:

          Post a Comment