Complete reference for the C Standard Library and POSIX headers — designed for students and developers. Click any header to see all its functions, signatures, and a ready-to-compile example.
Part of The Direct Path to Linux Ubuntu — free for all students.
Ready-to-compile C programs. Save to a .c file, then compile with the command shown in the comment.
The classic starting point — output to stdout.
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
/* gcc -Wall -o hello hello.c && ./hello */Read a name and age with scanf.
#include <stdio.h>
int main(void) {
char name[64]; int age;
printf("Name: "); scanf("%63s", name);
printf("Age: "); scanf("%d", &age);
printf("Hi %s, you are %d years old.\n", name, age);
return 0;
}
/* gcc -Wall -o input input.c && ./input */Add, subtract, multiply, divide two numbers.
#include <stdio.h>
int main(void) {
double a, b;
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
printf("%.2f + %.2f = %.2f\n", a, b, a + b);
printf("%.2f - %.2f = %.2f\n", a, b, a - b);
printf("%.2f * %.2f = %.2f\n", a, b, a * b);
if (b != 0) printf("%.2f / %.2f = %.2f\n", a, b, a / b);
else printf("Division by zero!\n");
return 0;
}
/* gcc -Wall -o calc calc.c && ./calc */Allocate an int array at runtime and sort it.
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) { return *(int*)a - *(int*)b; }
int main(void) {
int n; printf("How many integers? "); scanf("%d", &n);
int *arr = malloc(n * sizeof(int));
if (!arr) { perror("malloc"); return 1; }
for (int i = 0; i < n; i++) arr[i] = rand() % 100;
qsort(arr, n, sizeof(int), cmp);
for (int i = 0; i < n; i++) printf("arr[%d] = %d\n", i, arr[i]);
free(arr);
return 0;
}
/* gcc -Wall -o dyn dyn.c && ./dyn */Build and traverse a singly linked list.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node { int val; struct Node *next; } Node;
Node *push(Node *head, int val) {
Node *n = malloc(sizeof(Node));
n->val = val; n->next = head; return n;
}
void print_list(Node *n) {
for (; n; n = n->next) printf("%d -> ", n->val);
printf("NULL\n");
}
void free_list(Node *n) {
while (n) { Node *t = n->next; free(n); n = t; }
}
int main(void) {
Node *list = NULL;
for (int i = 1; i <= 5; i++) list = push(list, i * 10);
print_list(list);
free_list(list);
return 0;
}
/* gcc -Wall -o list list.c && ./list */