Unit 1 – Computational Thinking

Introduction to computers, algorithms, data types, and basic C programming.

1. What is Computational Thinking?

Computational Thinking (CT) is a problem-solving method used to break down complex tasks into smaller, manageable parts. It involves:

  • Decomposition – Breaking problems into smaller units
  • Pattern Recognition – Finding similarities in problems
  • Abstraction – Removing unnecessary details
  • Algorithm Design – Creating step-by-step instructions

2. Basic Structure of a C Program

Every C program follows a simple but well-defined structure. Understanding these components helps you read, write, and organize code clearly.

        // Basic C Program Structure
        
        // 1. Preprocessor Directives
        #include 
        
        // 2. Global Declarations (optional)
        int globalVar = 10;
        
        // 3. main() Function
        int main() {
        
            // 4. Variable Declarations
            int num = 5;
        
            // 5. Statements
            printf("Number = %d", num);
        
            // 6. Return Statement
            return 0;
        }
            
1. Preprocessor Directives

These lines begin with # and are processed before compilation. The most common directive is #include, which loads header files.

Examples:

  • #include <stdio.h> – standard input/output functions
  • #include <math.h> – mathematical functions
  • #define PI 3.14 – macro definition
2. Global Declarations (Optional)

Variables or functions declared outside main() are global. They can be accessed anywhere within the file.

3. The main() Function

Execution of every C program starts from main(). It can return an integer, usually 0 to indicate success.

4. Variable Declarations

Variables must be declared before use. Declarations specify the data type and name.

        int age;
        float marks;
        char grade;
            
5. Statements & Expressions

Statements perform actions such as assignments, calculations, or printing output. Each statement ends with ;.

6. The Return Statement

return 0; marks successful program completion and returns control to the operating system.

4. Data Types in C

Data types define the type of data a variable can store and how much memory it occupies. C provides several built-in (primitive) types that form the foundation of all operations.

1. Primary (Basic) Data Types

The basic data types in C are:

Type Description Typical Size Format Specifier
int Stores integer values (positive or negative) 4 bytes %d
float Stores single-precision decimal numbers 4 bytes %f
double Double-precision decimal values 8 bytes %lf
char Stores a single character 1 byte %c
2. Derived Data Types

These are built from basic types:

  • Arrays – Collection of values of the same type
  • Pointers – Store memory addresses
  • Functions – Block of code returning specific data types
3. User-Defined Data Types

Created by the programmer:

  • struct – Group of different data types
  • union – Shared memory for multiple variables
  • enum – Named integer constants
  • typedef – Creates a new name for an existing type
4. Example
        #include 
        
        int main() {
            int age = 20;
            float marks = 85.5;
            char grade = 'A';
            double pi = 3.141592;
        
            printf("Age: %d\n", age);
            printf("Marks: %.2f\n", marks);
            printf("Grade: %c\n", grade);
            printf("PI: %lf\n", pi);
        
            return 0;
        }
            

5. Taking Input from User

// Program: Taking user input
#include 

int main() {
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    printf("You entered: %d", num);
    return 0;
}
            

6. Example Programs

Program 1: Add Two Numbers
#include 

int main() {
    int a, b, sum;

    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    sum = a + b;

    printf("Sum = %d", sum);
    return 0;
}
            
Program 2: Check Even or Odd
#include 

int main() {
    int n;

    printf("Enter a number: ");
    scanf("%d", &n);

    if(n % 2 == 0)
        printf("Even");
    else
        printf("Odd");

    return 0;
}