Introduction to computers, algorithms, data types, and basic C programming.
Computational Thinking (CT) is a problem-solving method used to break down complex tasks into smaller, manageable parts. It involves:
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;
}
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
Variables or functions declared outside main() are global.
They can be accessed anywhere within the file.
main() Function
Execution of every C program starts from main().
It can return an integer, usually 0 to indicate success.
Variables must be declared before use. Declarations specify the data type and name.
int age;
float marks;
char grade;
Statements perform actions such as assignments, calculations, or printing output.
Each statement ends with ;.
return 0; marks successful program completion and returns control to the operating system.
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.
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 |
These are built from basic types:
Created by the programmer:
#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;
}
// Program: Taking user input #includeint main() { int num; printf("Enter a number: "); scanf("%d", &num); printf("You entered: %d", num); return 0; }
#includeint main() { int a, b, sum; printf("Enter two numbers: "); scanf("%d %d", &a, &b); sum = a + b; printf("Sum = %d", sum); return 0; }
#includeint main() { int n; printf("Enter a number: "); scanf("%d", &n); if(n % 2 == 0) printf("Even"); else printf("Odd"); return 0; }