Knowlet

Unit 1: Fundamentals of C Programming and Control Structures

1. Fundamentals of C Programming

This section covers the basic building blocks of the C language.

Data Types

Data types define the type and size of data a variable can store.

Data Type Keyword Typical Size (bytes) Description
Integer int 2 or 4 Stores whole numbers (e.g., 5, -10).
Character char 1 Stores a single character (e.g., 'a', '$').
Floating Point float 4 Stores single-precision numbers with decimals (e.g., 3.14).
Double double 8 Stores double-precision numbers with decimals.
No Type void N/A Used for functions that return nothing or for generic pointers.

These can be modified with qualifiers like short, long, signed, and unsigned.

Expressions and Operations

An expression is a combination of variables, constants, and operators that evaluates to a single value.

  • Arithmetic Operations: + (add), - (subtract), * (multiply), / (divide), % (modulus/remainder).
  • Relational Operations: == (equal to), != (not equal to), >, <, >=, <=. These evaluate to 1 (true) or 0 (false).
  • Logical Operations: && (logical AND), || (logical OR), ! (logical NOT).
  • Assignment Operation: = (assigns the value on the right to the variable on the left).

Input and Output

C uses standard library functions for I/O. You must #include .

  • Output: printf() is used to print formatted output to the console.
    printf("Hello, World!\n");
    printf("The number is: %d\n", myVariable);
  • Input: scanf() is used to read formatted input from the console.
    int age;
    printf("Enter your age: ");
    scanf("%d", &age); // & is the "address-of" operator
Common Mistake: Forgetting the ampersand (&) in scanf() is the most common bug for beginners. scanf needs the *address* of the variable to store the value.

2. Writing Simple C Programs

A simple C program has a standard structure:

// 1. Preprocessor Directives
#include 

// 2. Main function (execution starts here)
int main() {
    // 3. Variable Declaration
    int a, b, sum;

    // 4. Program Logic / Statements
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    sum = a + b;

    printf("The sum is: %d\n", sum);

    // 5. Return Statement
    return 0; // 0 indicates successful execution
}

3. Control Structures

Control structures allow you to control the flow of your program's execution.

Decision Making (if, switch)

  • if Statement: Executes code if a condition is true.
    if (age >= 18) {
        printf("You can vote.");
    }
  • if-else Statement: Executes one block if true, another if false.
    if (num % 2 == 0) {
        printf("Even");
    } else {
        printf("Odd");
    }
  • else if Ladder: Used for multiple exclusive conditions.
    if (marks >= 90) {
        printf("Grade A");
    } else if (marks >= 80) {
        printf("Grade B");
    } else {
        printf("Grade C");
    }
  • switch Statement: Efficiently checks a variable against a list of constant values.
    switch (day) {
        case 1:
            printf("Monday");
            break; // A break is crucial!
        case 2:
            printf("Tuesday");
            break;
        default:
            printf("Invalid day");
    }
switch Mistake: Forgetting break causes "fall-through." If day is 1, the program will print "Monday", then "Tuesday", then "Invalid day".

Loops (while, do-while, for)

  • while Loop: An entry-controlled loop. The condition is checked *before* the loop body executes.
    int i = 1;
    while (i <= 5) {
        printf("%d ", i);
        i++;
    }
    // Output: 1 2 3 4 5
  • do-while Loop: An exit-controlled loop. The loop body *always* executes at least once.
    int i = 1;
    do {
        printf("%d ", i);
        i++;
    } while (i <= 5);
    // Output: 1 2 3 4 5
  • for Loop: The most common loop. It combines initialization, condition, and increment in one line.
    for (int i = 1; i <= 5; i++) {
        printf("%d ", i);
    }
    // Output: 1 2 3 4 5

Jump Statements (break, continue, goto)

  • break: Immediately terminates the innermost loop (for, while, do-while) or switch statement.
  • continue: Skips the rest of the current loop iteration and moves to the next iteration.
  • goto: Unconditionally jumps to a labeled statement.
    if (error) {
        goto cleanup;
    }
    // ...
    cleanup:
        printf("Cleaning up resources.");
    Exam Tip: Use goto sparingly. It can make code very difficult to read and debug. It's generally considered bad practice but is mentioned in the syllabus.

Nested Loops

A loop inside another loop. This is commonly used for working with 2D arrays or printing patterns.

// Prints a 3x3 square of stars
for (int i = 0; i < 3; i++) {      // Outer loop (rows)
    for (int j = 0; j < 3; j++) {  // Inner loop (columns)
        printf("* ");
    }
    printf("\n"); // Newline after each row
}

4. Problem Solving Applications

This unit requires applying the above control structures to solve elementary problems, such as:

  • Mathematics:
    • Finding the factorial of a number (using a for loop).
    • Checking if a number is prime (using a for loop and if).
    • Generating the Fibonacci series (using a while loop).
    • Finding the sum of digits of a number (using a while loop and %).
  • Statistics:
    • Calculating the average of n numbers.
    • Finding the maximum and minimum values in a list of numbers.

xxx

Did this help you understand better?

Your feedback improves the quality of this resource for everyone.