C Tutorial — Learn C Programming
Key takeaway: This free C tutorial teaches you systems programming from scratch — variables, pointers, arrays, functions, structs, and memory management — with practical code examples you can compile and run.
C is a general-purpose, procedural programming language created in 1972. It is used for operating systems, embedded systems, game engines, and performance-critical applications. This tutorial is for beginners, CS students, and developers who want to understand how computers work at a low level.
Last updated: September 2026
Pointers in C
Pointers are variables that store memory addresses of other variables. They are one of the most powerful features of C programming, allowing direct memory manipulation and efficient programming.
Pointer Declaration and Initialization
#include <stdio.h>
int main() {
int num = 42;
int *ptr; // Pointer declaration
ptr = # // Assign address of num to ptr
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", &num);
printf("Value of ptr: %p\n", ptr);
printf("Value pointed by ptr: %d\n", *ptr);
// Modify value through pointer
*ptr = 100;
printf("New value of num: %d\n", num);
return 0;
}Pointers with Different Data Types
#include <stdio.h>
int main() {
int intVar = 10;
float floatVar = 3.14;
char charVar = 'A';
int *intPtr = &intVar;
float *floatPtr = &floatVar;
char *charPtr = &charVar;
printf("Integer: %d, Address: %p\n", *intPtr, intPtr);
printf("Float: %.2f, Address: %p\n", *floatPtr, floatPtr);
printf("Character: %c, Address: %p\n", *charPtr, charPtr);
return 0;
}Pointers and Arrays
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr; // Points to first element
printf("Array elements using pointer:\n");
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d, *(ptr + %d) = %d\n",
i, arr[i], i, *(ptr + i));
}
// Pointer arithmetic
printf("\nUsing pointer arithmetic:\n");
for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, *ptr);
ptr++; // Move to next element
}
return 0;
}Pointers and Functions
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void modifyArray(int *arr, int size) {
for (int i = 0; i < size; i++) {
arr[i] *= 2; // Double each element
}
}
int main() {
int x = 10, y = 20;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
int numbers[] = {1, 2, 3, 4, 5};
printf("\nOriginal array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
modifyArray(numbers, 5);
printf("\nModified array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}Important Notes:
- & (address-of operator) gets the address of a variable
- * (dereference operator) accesses the value at an address
- Uninitialized pointers can cause segmentation faults
- Always initialize pointers before use