UTIES Jamshedpur

C Language
Complete Notes

The mother of modern languages โ€” master pointers, memory management, and systems programming.

๐ŸŽฌ Learn alongside! These notes complement the C playlist on @utiesjsr YouTube channel. Code along and build strong fundamentals.
Introduction

What is C?

๐Ÿ”ง The Language That Changed Everything

C is a general-purpose, procedural, low-level programming language developed by Dennis Ritchie at Bell Labs between 1969-1973. It was created to write the UNIX operating system. C gives you direct memory access via pointers, making it incredibly powerful for system programming, embedded devices, and performance-critical applications.

โšก Blazing Fast

No runtime overhead, manual memory control โ†’ operating systems, databases, game engines.

๐ŸŒ Foundation of Modern Languages

C++ , Java, C#, Python, Rust โ€” all influenced by C syntax and concepts.

โš™๏ธ Compilation Process

๐Ÿ“„ .c (Source)
โ†’
๐Ÿ”จ Preprocessor
โ†’
โš™๏ธ Compiler
โ†’
๐Ÿ”— Linker
โ†’
๐Ÿ–ฅ๏ธ Executable

Preprocessor handles #include, #define. Compiler produces assembly โ†’ object files. Linker combines into binary.

Setup

Setup & Compilation

๐Ÿ–ฅ๏ธ Compilers & IDEs

  • GCC (GNU Compiler Collection) โ€” gcc main.c -o output
  • Clang โ€” LLVM compiler, modern error messages
  • MSVC โ€” Visual Studio compiler on Windows
  • IDEs: VS Code (with C/C++ extension), Code::Blocks, CLion, Dev-C++
// Compile and run gcc hello.c -o hello ./hello // With warnings and standard gcc -Wall -std=c99 program.c -o program
Chapter 02

Syntax & Variables

Hello World + Basics

#include <stdio.h> int main() { // printf is output function printf("Hello, C!\n"); // Variables int age = 20; float price = 99.99f; double pi = 3.1415926535; char grade = 'A'; return 0; }
๐Ÿ“–
Rules: C is case-sensitive. Variables must be declared before use. Statements end with semicolon ;.
Chapter 03

Data Types

Primitive Data Types

TypeSize (bytes)Format SpecifierRange
int4%d-2e9 to 2e9
float4%f6-7 decimal digits
double8%lf15 decimal digits
char1%c-128 to 127
voidNo value
short2%hd-32768 to 32767
long4/8%ldDepends on system
int a = 42; unsigned int b = 100; // only positive long long c = 1234567890123LL; sizeof(a); // returns size in bytes
Chapter 04

Input & Output

#include <stdio.h> // Output printf("Number: %d\n", 10); printf("Float: %.2f\n", 3.14159); printf("Char: %c\n", 'X'); // Input int age; printf("Enter age: "); scanf("%d", &age); // & address-of operator // String input (no spaces) char name[50]; scanf("%s", name); // array name = address // String with spaces fgets(name, 50, stdin);
โš ๏ธ
Always use & with scanf for non-array variables. fgets is safer than gets() (deprecated).
Chapter 05

Operators

// Arithmetic int sum = 10 + 3; int remainder = 10 % 3; // 1 (modulo) // Increment/Decrement int i = 5; int pre = ++i; // i=6, pre=6 int post = i++; // post=6, i=7 // Relational & Logical int result = (a > b) && (c < d); int max = (x > y) ? x : y; // ternary // Bitwise operators int andOp = a & b; // AND int orOp = a | b; // OR int xorOp = a ^ b; // XOR int shift = a << 2; // left shift
Chapter 06

Control Flow

// if-else if (score >= 90) { printf("A\n"); } else if (score >= 75) { printf("B\n"); } else { printf("C\n"); } // switch-case switch (day) { case 1: printf("Monday"); break; case 2: printf("Tuesday"); break; default: printf("Invalid"); }
Chapter 07

Loops

// for loop for (int i = 0; i < 5; i++) { printf("%d ", i); } // while loop int count = 0; while (count < 3) { count++; } // do-while (executes at least once) int num; do { printf("Enter positive: "); scanf("%d", &num); } while (num <= 0);

๐Ÿ“ Loop Practice

Q1
Print Fibonacci series up to N terms.
N=70 1 1 2 3 5 8
Q2
Check if a number is palindrome.
121Palindrome โœ…
Q3
Print star pattern (pyramid).
Chapter 08

Arrays

// 1D array int nums[5] = {1, 2, 3, 4, 5}; nums[0] = 10; // modify // 2D array (matrix) int matrix[2][3] = {{1,2,3}, {4,5,6}}; // Array traversal for (int i = 0; i < 5; i++) { printf("%d ", nums[i]); }
๐Ÿ“Œ
Array index starts at 0. No bounds checking in C โ€” out-of-bounds leads to undefined behavior.
Chapter 09

Strings

// C strings = char arrays terminated by '\0' char str1[] = "Hello"; // size 6 (includes '\0') char str2[20] = {'H','i','\0'}; // String functions (#include <string.h>) #include <string.h> strlen(str1); // length (5) strcpy(dest, src); // copy strcat(str1, " World"); // concatenate int cmp = strcmp(str1, str2); // compare (0 if equal)
Chapter 10

Functions

// Function declaration (prototype) int add(int a, int b); // Function definition int add(int a, int b) { return a + b; } // Pass by value (copy) void changeValue(int x) { x = 100; // doesn't affect original } // Recursion int factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); }
Chapter 11

Pointers

int x = 10; int *ptr = &x; // pointer stores address of x *ptr = 20; // dereference โ†’ x becomes 20 // Pointer arithmetic int arr[] = {10, 20, 30}; int *p = arr; // points to arr[0] *(p + 1) = 25; // arr[1] becomes 25 // Null pointer int *nullPtr = NULL; // Pointer to pointer int **ptr2 = &ptr;
โš ๏ธ
Always initialize pointers. Dereferencing NULL causes segmentation fault. Use NULL for null pointers.
Chapter 12

Dynamic Memory Allocation

#include <stdlib.h> // malloc - allocates uninitialized memory int *arr = (int*)malloc(5 * sizeof(int)); // calloc - allocates zero-initialized memory int *arr2 = (int*)calloc(5, sizeof(int)); // realloc - resize memory arr = (int*)realloc(arr, 10 * sizeof(int)); // free - prevent memory leak free(arr); free(arr2);
๐Ÿ’ฃ
Every malloc/calloc/realloc must have a matching free! Forgetting causes memory leaks.
Chapter 13

Structures & Unions

// Structure struct Student { char name[50]; int roll; float marks; }; struct Student s1 = {"Akarsh", 101, 95.5}; printf("%s", s1.name); // Pointer to structure struct Student *ptr = &s1; printf("%d", ptr->roll); // arrow operator // Union (shares memory) union Data { int i; float f; char str[20]; };
Chapter 14

File I/O

#include <stdio.h> // Writing to file FILE *fptr = fopen("data.txt", "w"); if (fptr == NULL) { printf("Error opening file!"); return 1; } fprintf(fptr, "Number: %d\n", 42); fclose(fptr); // Reading from file fptr = fopen("data.txt", "r"); char buffer[100]; while (fgets(buffer, 100, fptr) != NULL) { printf("%s", buffer); } fclose(fptr);
ModeMeaning
"r"Read only (file must exist)
"w"Write (overwrites/creates)
"a"Append
"r+"Read and write
Chapter 15

Preprocessor Directives

// Macros #define PI 3.14159 #define SQUARE(x) ((x)*(x)) #define MAX(a,b) ((a)>(b)?(a):(b)) // Conditional compilation #ifdef DEBUG printf("Debug mode\n"); #endif // Header guards // #ifndef MYHEADER_H // #define MYHEADER_H // ... content ... // #endif // Predefined macros printf("File: %s, Line: %d\n", __FILE__, __LINE__);
Chapter 16

Advanced Topics

Function Pointers

int add(int a, int b) { return a + b; } int (*operation)(int, int) = &add; int result = operation(5, 3); // 8

Command Line Arguments

int main(int argc, char *argv[]) { for (int i = 0; i < argc; i++) { printf("arg %d: %s\n", i, argv[i]); } }

Variable Arguments (stdarg.h)

#include <stdarg.h> int sum(int count, ...) { va_list args; va_start(args, count); int total = 0; for (int i = 0; i < count; i++) total += va_arg(args, int); va_end(args); return total; }

๐ŸŽฏ Advanced Challenges

Challenge 1
Implement your own malloc-like memory allocator using a static array.
Challenge 2
Write a generic swap macro that works with any data type.
Challenge 3
Implement a linked list library (create, insert, delete, traverse).

C โ€” The Foundation

"C is not just a language โ€” it's the foundation of modern computing. Master pointers, memory, and algorithms. We'll explore all these concepts in the UTIES C series. Keep coding, keep building. Much love! ๐Ÿ’™"

UTIES Coding School