The language that built Unix, Linux, and every operating system. Fast, powerful, and close to the hardware.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
// Compile: gcc hello.c -o hello
// Run: ./hello
#include <stdio.h>
int main() {
int age = 15;
float height = 5.9;
char grade = 'A';
char name[] = "Mayank";
double pi = 3.14159265;
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);
printf("Pi: %.8f\n", pi);
return 0;
}
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("%d + %d = %d\n", a, b, a + b);
printf("%d / %d = %d\n", a, b, a / b);
printf("%d %% %d = %d\n", a, b, a % b);
printf("%d == %d => %d\n", a, b, a == b);
printf("5 & 3 = %d\n", 5 & 3);
printf("5 | 3 = %d\n", 5 | 3);
printf("5 ^ 3 = %d\n", 5 ^ 3);
printf("1 << 3 = %d\n", 1 << 3);
return 0;
}
#include <stdio.h>
int main() {
int score = 85;
if (score >= 90) printf("Grade: A\n");
else if (score >= 80) printf("Grade: B\n");
else if (score >= 70) printf("Grade: C\n");
else printf("Grade: F\n");
int day = 3;
switch (day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
default: printf("Other day\n"); break;
}
return 0;
}
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) printf("for: %d\n", i);
int n = 0;
while (n < 5) { printf("while: %d\n", n); n++; }
int m = 0;
do { printf("do-while: %d\n", m); m++; } while (m < 5);
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) continue;
printf("odd: %d\n", i);
}
return 0;
}
#include <stdio.h>
int add(int a, int b) { return a + b; }
float area(float r) { return 3.14159 * r * r; }
int fact(int n) { return n <= 1 ? 1 : n * fact(n-1); }
void greet(char name[]) { printf("Hello, %s!\n", name); }
int main() {
printf("5+3=%d\n", add(5,3));
printf("area(5)=%.2f\n", area(5));
printf("5!=%d\n", fact(5));
greet("Mayank");
return 0;
}
#include <stdio.h>
int main() {
int nums[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) printf("%d ", nums[i]);
printf("\n");
int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) printf("%3d", matrix[i][j]);
printf("\n");
}
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char s[50] = "Hello";
printf("len=%zu\n", strlen(s));
strcat(s, " World");
printf("%s\n", s);
printf("cmp=%d\n", strcmp("abc","abd"));
char *p = strchr(s, 'W');
if (p) printf("found at %ld\n", p-s);
return 0;
}
Group related data together. Struct is a custom box. Enum is a named list of choices.
#include <stdio.h>
#include <string.h>
struct Person {
char name[50];
int age;
float gpa;
};
enum Color { RED, GREEN, BLUE };
void print_person(struct Person p) {
printf("%s (age %d) gpa=%.1f\n", p.name, p.age, p.gpa);
}
int main() {
struct Person me;
strcpy(me.name, "Mayank");
me.age = 15;
me.gpa = 9.5;
print_person(me);
enum Color c = GREEN;
printf("Color: %d\n", c);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr = malloc(5 * sizeof(int));
if (!arr) return 1;
for (int i = 0; i < 5; i++) arr[i] = (i+1) * 10;
for (int i = 0; i < 5; i++) printf("%d ", arr[i]);
printf("\n");
free(arr);
int *big = realloc(arr, 10 * sizeof(int));
if (big) { arr = big; }
return 0;
}
#include <stdio.h>
int main() {
int x = 42;
int *p = &x;
printf("x=%d addr=%p *p=%d\n", x, (void*)p, *p);
*p = 100;
printf("x now=%d\n", x);
int arr[] = {10,20,30};
int *ap = arr;
for (int i = 0; i < 3; i++) printf("%d ", *(ap+i));
printf("\n");
return 0;
}
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *p = arr;
printf("*p = %d\n", *p);
printf("*(p+1) = %d\n", *(p+1));
printf("*(p+2) = %d\n", *(p+2));
p += 3;
printf("after p+=3: %d\n", *p);
int *start = &arr[0];
int *end = &arr[4];
printf("distance: %ld ints\n", end - start);
return 0;
}
#include <stdio.h>
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }
void calc(int (*op)(int,int), int a, int b) {
printf("result: %d\n", op(a, b));
}
int main() {
calc(add, 10, 5);
calc(sub, 10, 5);
calc(mul, 10, 5);
int (*ops[3])(int,int) = {add, sub, mul};
for (int i = 0; i < 3; i++) printf("%d\n", ops[i](10,5));
return 0;
}
#include <stdio.h>
int main() {
FILE *f = fopen("test.txt", "w");
fprintf(f, "Hello File!\n");
fprintf(f, "Line 2: %d\n", 42);
fclose(f);
f = fopen("test.txt", "r");
char buf[256];
while (fgets(buf, sizeof(buf), f)) printf("%s", buf);
fclose(f);
return 0;
}
#include <stdio.h>
#define PI 3.14159
#define SQUARE(x) ((x)*(x))
#define MAX(a,b) ((a)>(b)?(a):(b))
#ifdef DEBUG
#define LOG(msg) printf("DEBUG: %s\n", msg)
#else
#define LOG(msg)
#endif
int main() {
printf("PI=%f\n", PI);
printf("5^2=%d\n", SQUARE(5));
printf("max(3,7)=%d\n", MAX(3,7));
LOG("testing");
return 0;
}
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
struct Flags {
unsigned int bold : 1;
unsigned int italic : 1;
unsigned int underline : 1;
unsigned int size : 5;
};
int main() {
union Data d;
d.i = 42;
printf("int: %d\n", d.i);
d.f = 3.14;
printf("float: %f (int was: %d)\n", d.f, d.i);
struct Flags f = {1, 0, 1, 14};
printf("bold=%d italic=%d underline=%d size=%d\n",
f.bold, f.italic, f.underline, f.size);
return 0;
}
Each node points to the next. Like a treasure hunt where each clue leads to the next.
#include <stdio.h>
#include <stdlib.h>
struct Node { int data; struct Node *next; };
void push(struct Node **head, int val) {
struct Node *n = malloc(sizeof(struct Node));
n->data = val;
n->next = *head;
*head = n;
}
void print(struct Node *head) {
while (head) { printf("%d -> ", head->data); head = head->next; }
printf("NULL\n");
}
void free_list(struct Node *head) {
while (head) { struct Node *t = head; head = head->next; free(t); }
}
int main() {
struct Node *list = NULL;
push(&list, 30); push(&list, 20); push(&list, 10);
print(list);
free_list(list);
return 0;
}
Makefiles, header files, multi-file projects. How real C projects are built.
# project structure:
# src/main.c src/utils.c include/utils.h
# Makefile
# Makefile example:
CC = gcc
CFLAGS = -Wall -Iinclude
SRCS = src/main.c src/utils.c
OBJS = $(SRCS:.c=.o)
TARGET = app
all: $(TARGET)
$(TARGET): $(OBJS)
$(CC) $(OBJS) -o $(TARGET)
src/%.o: src/%.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJS) $(TARGET)
# utils.h:
#ifndef UTILS_H
#define UTILS_H
int add(int a, int b);
void print_array(int *arr, int len);
#endif