Programming in C
UNIT-I: Introduction to ‘C’ Language
History, Structures of ‘C’ Programming, Function as building blocks. Language Fundamentals: Character set, C Tokens, Keywords, Identifiers, Variables, Constant, Data Types, and Comments.
UNIT-II: Operators
Types of operators, Precedence and Associativity, Expression, Statement and types of statements. Built-in Operators and function: Console-based I/O and related built-in I/O function: printf(), scanf(), getch(), getchar(), putchar(); Concept of header files, Preprocessor directives: #include, #define.
UNIT-III: Control Structures
Decision making structures: If, If-else, Nested If-else, Switch; Loop Control structures: While, Do-while, for, Nested for loop; Other statements: break, continue, goto, exit.
UNIT-IV: Simple Arithmetic Problems
Addition / Multiplication of integers, Determining if a number is positive/negative/even/odd, Maximum of 2 or 3 numbers, Sum of first n numbers, Integer division, Digit reversing, Table generation, Factorial, sine series, cosine series, Pascal Triangle, Prime number, Factors of a number, Other problems such as Perfect number, GCD numbers, Swapping.
UNIT-V: Functions
Basic types of function, Declaration and definition, Function call, Types of function, Parameter passing, Call by value, Call by reference, Scope of variable, Storage classes, Recursion.

UNIT-I: Introduction to ‘C’ Language

1. History of 'C' Language

The C programming language was developed by Dennis Ritchie in 1972 at Bell Labs. It was initially designed for system programming, particularly for writing operating systems. C has since evolved and influenced many other languages, such as C++, Java, and Python.

Key Features of C:

Example: The C language is used in developing operating systems like Unix and embedded systems.

2. Structure of a C Program

A C program consists of several key components:

Example C Program Structure:

    #include <stdio.h>
    
    int main() {
        printf("Hello, World!\\n");
        return 0;
    }
            

Explanation: This program simply prints "Hello, World!" to the screen. The printf function is used for output, and return 0 indicates the program finished successfully.

3. Functions as Building Blocks

In C, functions are used to group code that performs a specific task. Functions help organize the program and make it more modular. Functions can either be built-in (like printf) or user-defined.

Structure of a Function in C:

Example of a Simple Function:

    #include <stdio.h>
    
    int add(int a, int b) {
        return a + b;
    }
    
    int main() {
        int result = add(5, 3);  // Calling the add function
        printf("Sum: %d\\n", result);
        return 0;
    }
            

Explanation: The function add takes two integers as input and returns their sum. In the main function, the add function is called with the values 5 and 3, and the result is printed using printf.

4. Language Fundamentals

In C, there are several language fundamentals that every programmer should understand. These include:

Character Set:

The character set of C includes all alphabets (both uppercase and lowercase), digits, special characters, and whitespace. Characters are represented by single quotes, like 'A', 'b', etc.

C Tokens:

C tokens are the basic building blocks of a C program. They include:

Example Code:

    // This is a single-line comment
    #include <stdio.h>
    
    int main() {
        int num = 10;       // Declaring a variable
        printf("Number: %d\\n", num);  // Printing the variable
        return 0;
    }
            

Explanation: The program declares an integer variable num, assigns it the value 10, and prints it using the printf function.

5. Variables, Constants, and Data Types

In C, a variable is a container for storing data values, while constants are fixed values that cannot be changed once assigned.

Data Types:

C provides several data types to define the type of data a variable can store. Some of the common data types are:

Constants in C:

Constants are values that cannot be changed during program execution. They can be declared using the const keyword.

Example Code with Variables and Constants:

    #include <stdio.h>
    
    int main() {
        int x = 5;       // Variable
        const int y = 10; // Constant
        printf("x = %d, y = %d\\n", x, y);
        return 0;
    }
            

Explanation: The program declares a variable x with a value of 5 and a constant y with a value of 10. The values are then printed using printf.

UNIT-II: Operators, Expressions, Console I/O, and Preprocessor Directives

1. Types of Operators in C

C supports various operators to perform computations and logical operations.

Examples:

    #include <stdio.h>
    
    int main() {
        int a = 5, b = 2;
    
        // Arithmetic
        printf("Add: %d\n", a + b);
        printf("Modulus: %d\n", a % b);
    
        // Relational
        printf("a == b: %d\n", a == b);
    
        // Logical
        printf("a > 0 && b > 0: %d\n", a > 0 && b > 0);
    
        // Assignment
        a += 3;
        printf("a after += 3: %d\n", a);
    
        // Bitwise
        printf("a & b: %d\n", a & b);
    
        // Conditional
        int max = (a > b) ? a : b;
        printf("Max: %d\n", max);
    
        return 0;
    }
            

2. Precedence and Associativity

Operator precedence determines the order of evaluation. Associativity determines direction.

Example:

    #include <stdio.h>
    
    int main() {
        int result = 10 + 2 * 3;
        printf("Result: %d\n", result);  // Output: 16
        return 0;
    }
            

Explanation: * has higher precedence than +.

3. Expressions, Statements & Types of Statements

Expression: Any combination of variables and operators (e.g., a + b).

Statement: A complete instruction (e.g., int x = 5;).

Types:

Example:

    #include <stdio.h>
    
    int main() {
        int x = 5;
        if (x > 0) {
            printf("Positive\n");
        }
        return 0;
    }
            

4. Built-in Console I/O Functions

Example:

    #include <stdio.h>
    
    int main() {
        char ch;
        printf("Enter a char: ");
        ch = getchar();     // Input
        printf("You entered: ");
        putchar(ch);        // Output
        return 0;
    }
            

5. Header Files and Preprocessor Directives

Header Files: Contain code for built-in functions.

Preprocessor Directives:

Example of #define:

    #include <stdio.h>
    #define PI 3.14159
    
    int main() {
        float r = 3;
        float area = PI * r * r;
        printf("Area: %.2f\n", area);
        return 0;
    }
            

UNIT-III: Control Structures in C

1. Decision Making Structures

1.1 if Statement

Executes a block of code if the condition is true.

    #include <stdio.h>
    
    int main() {
        int num = 10;
        if (num > 0) {
            printf("Positive number");
        }
        return 0;
    }
            
    Output:
    Positive number
            

1.2 if-else Statement

Executes one block if true, another if false.

    #include <stdio.h>
    
    int main() {
        int num = -5;
        if (num >= 0) {
            printf("Positive");
        } else {
            printf("Negative");
        }
        return 0;
    }
            
    Output:
    Negative
            

1.3 Nested if-else

Multiple conditions checked in hierarchy.

    #include <stdio.h>
    
    int main() {
        int num = 0;
        if (num > 0) {
            printf("Positive");
        } else if (num < 0) {
            printf("Negative");
        } else {
            printf("Zero");
        }
        return 0;
    }
            
    Output:
    Zero
            

1.4 switch Statement

Used for multiple selections based on value.

    #include <stdio.h>
    
    int main() {
        int choice = 2;
        switch (choice) {
            case 1: printf("One"); break;
            case 2: printf("Two"); break;
            case 3: printf("Three"); break;
            default: printf("Invalid");
        }
        return 0;
    }
            
    Output:
    Two
            

2. Loop Control Structures

2.1 while Loop

Repeats a block while the condition is true.

    #include <stdio.h>
    
    int main() {
        int i = 1;
        while (i <= 5) {
            printf("%d ", i);
            i++;
        }
        return 0;
    }
            
    Output:
    1 2 3 4 5
            

2.2 do-while Loop

Executes first, then checks the condition.

    #include <stdio.h>
    
    int main() {
        int i = 1;
        do {
            printf("%d ", i);
            i++;
        } while (i <= 5);
        return 0;
    }
            
    Output:
    1 2 3 4 5
            

2.3 for Loop

Compact loop structure for fixed iterations.

    #include <stdio.h>
    
    int main() {
        for (int i = 1; i <= 5; i++) {
            printf("%d ", i);
        }
        return 0;
    }
            
    Output:
    1 2 3 4 5
            

2.4 Nested for Loop

Loops inside another loop (useful for patterns).

    #include <stdio.h>
    
    int main() {
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                printf("(%d,%d) ", i, j);
            }
            printf("\n");
        }
        return 0;
    }
            
    Output:
    (1,1) (1,2) (1,3) 
    (2,1) (2,2) (2,3) 
    (3,1) (3,2) (3,3)
            

3. Other Control Statements

3.1 break Statement

Exits a loop or switch early.

    #include <stdio.h>
    
    int main() {
        for (int i = 1; i <= 10; i++) {
            if (i == 5) break;
            printf("%d ", i);
        }
        return 0;
    }
            
    Output:
    1 2 3 4
            

3.2 continue Statement

Skips to the next iteration.

    #include <stdio.h>
    
    int main() {
        for (int i = 1; i <= 5; i++) {
            if (i == 3) continue;
            printf("%d ", i);
        }
        return 0;
    }
            
    Output:
    1 2 4 5
            

3.3 goto Statement

Jumps to a labeled statement (use with caution).

    #include <stdio.h>
    
    int main() {
        int num = 1;
        if (num == 1) {
            goto label;
        }
        printf("This will be skipped\n");
    
        label:
        printf("Jumped to label!\n");
        return 0;
    }
            
    Output:
    Jumped to label!
            

3.4 exit() Function

Terminates the program immediately.

    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
        printf("Before exit\n");
        exit(0);
        printf("This won't be printed\n");
        return 0;
    }
            
    Output:
    Before exit
            

UNIT-IV: Simple Arithmetic Problems

1. Addition / Multiplication of Integers

    #include <stdio.h>
    
    int main() {
        int a = 5, b = 3;
        printf("Addition: %d\n", a + b);
        printf("Multiplication: %d\n", a * b);
        return 0;
    }
            
    Output:
    Addition: 8
    Multiplication: 15
            

2. Positive / Negative / Even / Odd

    #include <stdio.h>
    
    int main() {
        int num = -7;
        if(num > 0)
            printf("Positive\n");
        else if(num < 0)
            printf("Negative\n");
        else
            printf("Zero\n");
    
        if(num % 2 == 0)
            printf("Even\n");
        else
            printf("Odd\n");
        return 0;
    }
            
    Output:
    Negative
    Odd
            

3. Maximum of 2 and 3 Numbers

    // Maximum of 2 numbers
    #include <stdio.h>
    
    int main() {
        int a = 10, b = 20;
        if(a > b)
            printf("Max: %d\n", a);
        else
            printf("Max: %d\n", b);
        return 0;
    }
            
    Output:
    Max: 20
            
    // Maximum of 3 numbers
    #include <stdio.h>
    
    int main() {
        int a = 10, b = 20, c = 15;
        if(a > b && a > c)
            printf("Max: %d\n", a);
        else if(b > c)
            printf("Max: %d\n", b);
        else
            printf("Max: %d\n", c);
        return 0;
    }
            
    Output:
    Max: 20
            

4. Sum of First N Numbers

    #include <stdio.h>
    
    int main() {
        int n = 5, sum = 0;
        for(int i = 1; i <= n; i++) {
            sum += i;
        }
        printf("Sum = %d\n", sum);
        return 0;
    }
            
    Output:
    Sum = 15
            

5. Integer Division

    #include <stdio.h>
    
    int main() {
        int a = 10, b = 3;
        printf("Quotient = %d\n", a / b);
        printf("Remainder = %d\n", a % b);
        return 0;
    }
            
    Output:
    Quotient = 3
    Remainder = 1
            

6. Digit Reversing

    #include <stdio.h>
    
    int main() {
        int num = 1234, rev = 0;
        while(num != 0) {
            rev = rev * 10 + num % 10;
            num /= 10;
        }
        printf("Reversed = %d\n", rev);
        return 0;
    }
            
    Output:
    Reversed = 4321
            

7. Multiplication Table of a Number

    #include <stdio.h>
    
    int main() {
        int n = 5;
        for(int i = 1; i <= 10; i++) {
            printf("%d x %d = %d\n", n, i, n * i);
        }
        return 0;
    }
            
    Output:
    5 x 1 = 5
    5 x 2 = 10
    ...
    5 x 10 = 50
            

8. Factorial of a Number

    #include <stdio.h>
    
    int main() {
        int n = 5, fact = 1;
        for(int i = 1; i <= n; i++) {
            fact *= i;
        }
        printf("Factorial = %d\n", fact);
        return 0;
    }
            
    Output:
    Factorial = 120
            

9. Prime Number

    #include <stdio.h>
    
    int main() {
        int num = 7, isPrime = 1;
        for(int i = 2; i <= num / 2; i++) {
            if(num % i == 0) {
                isPrime = 0;
                break;
            }
        }
        if(isPrime)
            printf("Prime Number\n");
        else
            printf("Not Prime\n");
        return 0;
    }
            
    Output:
    Prime Number
            

10. Pascal’s Triangle (n = 5)

    #include <stdio.h>
    
    int fact(int n) {
        int f = 1;
        for(int i = 1; i <= n; i++) f *= i;
        return f;
    }
    
    int main() {
        int n = 5;
        for(int i = 0; i < n; i++) {
            for(int space = 0; space < n - i; space++) printf(" ");
            for(int j = 0; j <= i; j++) {
                printf("%d ", fact(i)/(fact(j)*fact(i-j)));
            }
            printf("\n");
        }
        return 0;
    }
            
    Output:
         1
        1 1
       1 2 1
      1 3 3 1
     1 4 6 4 1
            

11. GCD of Two Numbers

    #include <stdio.h>
    
    int main() {
        int a = 36, b = 60, gcd;
        for(int i = 1; i <= a && i <= b; ++i) {
            if(a % i == 0 && b % i == 0)
                gcd = i;
        }
        printf("GCD = %d\n", gcd);
        return 0;
    }
            
    Output:
    GCD = 12
            

12. Swapping Two Numbers

    #include <stdio.h>
    
    int main() {
        int a = 5, b = 10, temp;
        temp = a;
        a = b;
        b = temp;
        printf("a = %d, b = %d\n", a, b);
        return 0;
    }
            
    Output:
    a = 10, b = 5
            

UNIT-V: Functions in C

1. Basic Types of Functions

Functions in C are blocks of code that perform a specific task. There are mainly two types:

2. Declaration and Definition

Function Declaration: Tells the compiler about the function name, return type, and parameters.

Function Definition: Contains the actual body or implementation of the function.

Example:

    // Declaration
    int add(int, int);
    
    // Definition
    int add(int a, int b) {
        return a + b;
    }
            

3. Function Call

To use a function, we call it from another function (typically main()).

Example:

    #include <stdio.h>
    
    int add(int a, int b) {
        return a + b;
    }
    
    int main() {
        int sum = add(5, 3);
        printf("Sum = %d\\n", sum);
        return 0;
    }
            

Output:

    Sum = 8
            

4. Types of Functions

Example (Function with arguments and return value):

    #include <stdio.h>
    
    int multiply(int a, int b) {
        return a * b;
    }
    
    int main() {
        int result = multiply(4, 5);
        printf("Product = %d\\n", result);
        return 0;
    }
            

Output:

    Product = 20
            

5. Parameter Passing

There are two types of parameter passing:

Example (Call by Value):

    #include <stdio.h>
    
    void change(int x) {
        x = 100;
    }
    
    int main() {
        int a = 5;
        change(a);
        printf("Value of a = %d\\n", a);
        return 0;
    }
            

Output:

    Value of a = 5
            

Example (Call by Reference):

    #include <stdio.h>
    
    void change(int *x) {
        *x = 100;
    }
    
    int main() {
        int a = 5;
        change(&a);
        printf("Value of a = %d\\n", a);
        return 0;
    }
            

Output:

    Value of a = 100
            

6. Scope of Variables

Example:

    #include <stdio.h>
    
    int global = 50; // Global
    
    int main() {
        int local = 10; // Local
        printf("Global = %d, Local = %d\\n", global, local);
        return 0;
    }
            

Output:

    Global = 50, Local = 10
            

7. Storage Classes

Storage classes define scope, lifetime, and linkage of variables.

Example (Static variable):

    #include <stdio.h>
    
    void counter() {
        static int count = 0;
        count++;
        printf("Count = %d\\n", count);
    }
    
    int main() {
        counter();
        counter();
        counter();
        return 0;
    }
            

Output:

    Count = 1
    Count = 2
    Count = 3
            

8. Recursion

Recursion is when a function calls itself to solve a smaller part of the problem.

Example: Factorial using Recursion

    #include <stdio.h>
    
    int factorial(int n) {
        if(n == 0) return 1;
        else return n * factorial(n - 1);
    }
    
    int main() {
        int num = 5;
        printf("Factorial of %d = %d\\n", num, factorial(num));
        return 0;
    }
            

Output:

    Factorial of 5 = 120