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
Pointer Arithmetic in C
Pointer arithmetic allows you to perform mathematical operations on pointers. This is particularly useful when working with arrays and dynamic memory allocation.
Basic Pointer Arithmetic Operations
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr;
printf("Original pointer address: %p\n", ptr);
printf("Value at pointer: %d\n", *ptr);
// Increment pointer
ptr++;
printf("After increment: %p\n", ptr);
printf("Value at pointer: %d\n", *ptr);
// Increment by 2
ptr += 2;
printf("After adding 2: %p\n", ptr);
printf("Value at pointer: %d\n", *ptr);
// Decrement pointer
ptr--;
printf("After decrement: %p\n", ptr);
printf("Value at pointer: %d\n", *ptr);
return 0;
}Pointer Subtraction
#include <stdio.h>
int main() {
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *start = &arr[2]; // Points to arr[2]
int *end = &arr[7]; // Points to arr[7]
printf("Start pointer points to: %d\n", *start);
printf("End pointer points to: %d\n", *end);
// Calculate distance
int distance = end - start;
printf("Distance between pointers: %d elements\n", distance);
// Verify by accessing elements
printf("Elements between start and end: ");
for (int *p = start; p <= end; p++) {
printf("%d ", *p);
}
printf("\n");
return 0;
}Pointer Comparison
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr1 = &arr[1];
int *ptr2 = &arr[3];
int *ptr3 = &arr[1];
printf("ptr1 points to: %d\n", *ptr1);
printf("ptr2 points to: %d\n", *ptr2);
printf("ptr3 points to: %d\n", *ptr3);
// Pointer comparisons
if (ptr1 == ptr3) {
printf("ptr1 and ptr3 point to the same location\n");
}
if (ptr1 < ptr2) {
printf("ptr1 comes before ptr2 in memory\n");
}
if (ptr2 > ptr1) {
printf("ptr2 comes after ptr1 in memory\n");
}
return 0;
}Practical Example: Array Traversal
#include <stdio.h>
int findElement(int *arr, int size, int target) {
int *end = arr + size; // Pointer to end of array
for (int *ptr = arr; ptr < end; ptr++) {
if (*ptr == target) {
return ptr - arr; // Return index
}
}
return -1; // Not found
}
void reverseArray(int *arr, int size) {
int *start = arr;
int *end = arr + size - 1;
while (start < end) {
// Swap elements
int temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main() {
int numbers[] = {10, 25, 30, 45, 50};
int size = 5;
printf("Original array: ");
for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
// Find element
int index = findElement(numbers, size, 30);
if (index != -1) {
printf("Element 30 found at index: %d\n", index);
}
// Reverse array
reverseArray(numbers, size);
printf("Reversed array: ");
for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}Key Points:
- Pointer arithmetic is scaled by the size of the data type
- Adding 1 to an int pointer moves it by sizeof(int) bytes
- Pointer subtraction gives the number of elements between pointers
- Only pointers to the same array should be compared or subtracted