C Programming

Introduction to C Programming; C Program Structure; Data Types and Variables

C-CAT

Introduction to C Programming

History of C

YearEvent
1960sBCPL (Basic Combined Programming Language) — by Martin Richards
1970B Language — by Ken Thompson at Bell Labs
1972C Language — by Dennis Ritchie at Bell Labs (AT&T)
1978"The C Programming Language" book — Kernighan & Ritchie (K&R C)
1989ANSI C (C89) — first standard by ANSI
1990ISO C (C90) — same as C89 but ISO approved
1999C99 — new features (inline, variable-length arrays, bool, etc.)
2011C11 — thread support, atomic operations, anonymous structs/unions
2017C17/C18 — bug fixes
2023C23 — latest standard

Why Learn C?

  1. Foundation language — understanding C helps you learn any language
  2. System programming — OS kernels, device drivers, embedded systems written in C
  3. Performance — C is one of the fastest languages (close to hardware)
  4. Portability — C programs can run on any platform with minor changes
  5. Control — direct memory management through pointers
  6. Widely used — Linux kernel, PostgreSQL, Python interpreter, Git are written in C

Where is C Used?

DomainApplications
Operating SystemsLinux kernel, Unix, Windows kernel code
Embedded SystemsMicrocontroller programs, Arduino, IoT devices
Database EnginesSQLite, parts of MySQL, PostgreSQL
Compilers/InterpretersCPython (Python interpreter), GCC, LLVM
Game EnginesLow-level graphics, real-time systems
NetworkingNetwork protocol implementations
Scientific ComputingHigh-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

HeaderContents
<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

TypeSizeFormatRange
char1 byte%c / %d-128 to 127
unsigned char1 byte%u0 to 255
short2 bytes%hd-32768 to 32767
unsigned short2 bytes%hu0 to 65535
int4 bytes%d-2,147,483,648 to 2,147,483,647
unsigned int4 bytes%u0 to 4,294,967,295
long4 or 8 bytes%ldsame as int or larger
long long8 bytes%lld-9.2×10^18 to 9.2×10^18
float4 bytes%f3.4e-38 to 3.4e+38 (7 digits precision)
double8 bytes%lf / %f1.7e-308 to 1.7e+308 (15 digits precision)
long double12-16 bytes%Lfeven 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

Put this topic into timed practice

Open mock tests when you want full-exam pacing, or keep drilling in practice mode.