Unit 1: Fundamentals of C Programming and Control Structures

Table of Contents

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.

Input and Output

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

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)

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)

Jump Statements (break, continue, goto)

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: