C Programming
Introduction to C Programming; C Program Structure; Data Types and Variables
C-CAT
Introduction to C Programming
History of C
| Year | Event |
|---|---|
| 1960s | BCPL (Basic Combined Programming Language) — by Martin Richards |
| 1970 | B Language — by Ken Thompson at Bell Labs |
| 1972 | C Language — by Dennis Ritchie at Bell Labs (AT&T) |
| 1978 | "The C Programming Language" book — Kernighan & Ritchie (K&R C) |
| 1989 | ANSI C (C89) — first standard by ANSI |
| 1990 | ISO C (C90) — same as C89 but ISO approved |
| 1999 | C99 — new features (inline, variable-length arrays, bool, etc.) |
| 2011 | C11 — thread support, atomic operations, anonymous structs/unions |
| 2017 | C17/C18 — bug fixes |
| 2023 | C23 — latest standard |
Why Learn C?
- Foundation language — understanding C helps you learn any language
- System programming — OS kernels, device drivers, embedded systems written in C
- Performance — C is one of the fastest languages (close to hardware)
- Portability — C programs can run on any platform with minor changes
- Control — direct memory management through pointers
- Widely used — Linux kernel, PostgreSQL, Python interpreter, Git are written in C
Where is C Used?
| Domain | Applications |
|---|---|
| Operating Systems | Linux kernel, Unix, Windows kernel code |
| Embedded Systems | Microcontroller programs, Arduino, IoT devices |
| Database Engines | SQLite, parts of MySQL, PostgreSQL |
| Compilers/Interpreters | CPython (Python interpreter), GCC, LLVM |
| Game Engines | Low-level graphics, real-time systems |
| Networking | Network protocol implementations |
| Scientific Computing | High-performance numerical code |
C Program Structure
Basic Structure
/* Preprocessor directives */
#include <stdio.h> // Standard Input/Output header
#include <stdlib.h> // Standard Library header
/* Global declarations (optional) */
int global_var = 10;
/* Main function - entry point of every C program */
int main(int argc, char *argv[]) {
/* Local variable declarations */
int x = 5;
float pi = 3.14;
/* Statements */
printf("Hello, World!\n");
printf("x = %d, pi = %.2f\n", x, pi);
/* Return 0 to indicate successful execution */
return 0;
}
Compilation Process
Source Code Pre-processing Compilation Assembly Linking
(.c file) (.i file) (.s file) (.o file) (executable)
hello.c → [Preprocessor] → [Compiler] → [Assembler] → [Linker] → a.out / hello
(expand macros) (type check) (machine code) (link libs)
(replace headers)
Compiling a C program:
gcc hello.c -o hello # Compile and link
gcc -S hello.c # Compile to assembly only
gcc -c hello.c # Compile to object file only
gcc hello.c -Wall -Wextra # Enable all warnings
./hello # Run the program
Essential Headers
| Header | Contents |
|---|---|
<stdio.h> | printf, scanf, fopen, fclose, fgets, fprintf |
<stdlib.h> | malloc, calloc, free, exit, atoi, atof, qsort |
<string.h> | strlen, strcpy, strcat, strcmp, strstr, memset, memcpy |
<math.h> | sqrt, pow, sin, cos, abs, ceil, floor |
<ctype.h> | isalpha, isdigit, toupper, tolower |
<time.h> | time, clock, difftime, strftime |
<limits.h> | INT_MAX, INT_MIN, CHAR_MAX, etc. |
<stdbool.h> | bool, true, false (C99+) |
Data Types and Variables
Fundamental Data Types
| Type | Size | Format | Range |
|---|---|---|---|
char | 1 byte | %c / %d | -128 to 127 |
unsigned char | 1 byte | %u | 0 to 255 |
short | 2 bytes | %hd | -32768 to 32767 |
unsigned short | 2 bytes | %hu | 0 to 65535 |
int | 4 bytes | %d | -2,147,483,648 to 2,147,483,647 |
unsigned int | 4 bytes | %u | 0 to 4,294,967,295 |
long | 4 or 8 bytes | %ld | same as int or larger |
long long | 8 bytes | %lld | -9.2×10^18 to 9.2×10^18 |
float | 4 bytes | %f | 3.4e-38 to 3.4e+38 (7 digits precision) |
double | 8 bytes | %lf / %f | 1.7e-308 to 1.7e+308 (15 digits precision) |
long double | 12-16 bytes | %Lf | even higher precision |
#include <stdio.h>
#include <limits.h>
int main() {
char ch = 'A';
int i = 42;
float f = 3.14f;
double d = 3.141592653589793;
printf("char: %c (%d bytes, value=%d)\n", ch, (int)sizeof(char), ch);
printf("int: %d (%d bytes)\n", i, (int)sizeof(int));
printf("float: %.2f (%d bytes)\n", f, (int)sizeof(float));
printf("double: %.15lf (%d bytes)\n", d, (int)sizeof(double));
printf("INT_MAX = %d\n", INT_MAX); // 2147483647
printf("INT_MIN = %d\n", INT_MIN); // -2147483648
return 0;
}
Variable Declaration
// Declaration: type variable_name;
int x;
float salary;
char grade;
// Declaration with initialization
int a = 10;
float pi = 3.14159;
char letter = 'C';
// Multiple declarations
int p, q, r;
int m = 1, n = 2, k = 3;
// Constants (cannot be modified)
const int MAX_SIZE = 100;
const float PI = 3.14159;
Type Casting
int a = 5, b = 2;
int result = a / b; // 2 (integer division)
float fresult = (float)a / b; // 2.5 (explicit cast)
double dresult = (double)a / b; // 2.5
// Implicit (automatic) conversion
int x = 3.7; // float → int: x = 3 (truncated, not rounded)
float y = 5; // int → float: y = 5.0 (widening conversion)
sizeof Operator
printf("int: %zu bytes\n", sizeof(int)); // 4
printf("double: %zu bytes\n", sizeof(double)); // 8
printf("char: %zu bytes\n", sizeof(char)); // 1
printf("int array[5]: %zu bytes\n", sizeof(int[5])); // 20
Continue learning
Related notes
Definition of AI; Need of AI
Artificial Intelligence
Introduction to Data Engineering; Big Data — The 5 V's; Types of Data
Big Data and Data Engineering
What Is a Computer?; Machine Cycle: Fetch–Decode–Execute; CPU Organization
Computer Architecture
What is a Computer?; Computer System Components; Booting Process
Computer Fundamentals
Put this topic into timed practice
Open mock tests when you want full-exam pacing, or keep drilling in practice mode.