⚡ ULTIMATE C PROGRAMMING REFERENCE ⚡

Your complete guide to C programming mastery

🔥 BASIC I/O FUNCTIONS

Function Description Example
printf()print formatted output to screenprintf("hello %s\n", name);
scanf()read formatted input from userscanf("%d", &number);
getchar()read single characterchar c = getchar();
putchar()print single characterputchar('A');
puts()print string with newlineputs("hello world");
fgets()safe string inputfgets(buffer, 100, stdin);

💾 MEMORY MANAGEMENT

Function Description Example
malloc()allocate memory on heapint *ptr = malloc(10 * sizeof(int));
calloc()allocate zeroed memoryint *ptr = calloc(10, sizeof(int));
realloc()resize allocated memoryptr = realloc(ptr, 20 * sizeof(int));
free()deallocate memoryfree(ptr);
sizeof()get size of data typeint size = sizeof(int);

📝 STRING FUNCTIONS

Requires: #include <string.h>

Function Description Example
strlen()get string lengthint len = strlen("hello");
strcpy()copy stringstrcpy(dest, source);
strncpy()copy n charactersstrncpy(dest, source, 5);
strcat()concatenate stringsstrcat(dest, source);
strcmp()compare stringsif (strcmp(str1, str2) == 0)
strchr()find character in stringchar *pos = strchr(str, 'a');
strstr()find substringchar *pos = strstr(str, "hello");

🧮 MATH FUNCTIONS

Requires: #include <math.h>

Function Description Example
pow()power functiondouble result = pow(2.0, 3.0);
sqrt()square rootdouble result = sqrt(16.0);
abs()absolute value (int)int result = abs(-5);
fabs()absolute value (double)double result = fabs(-5.5);
ceil()round updouble result = ceil(4.2);
floor()round downdouble result = floor(4.8);
rand()random numberint num = rand() % 100;
srand()seed random generatorsrand(time(NULL));

📁 FILE OPERATIONS

Function Description Example
fopen()open fileFILE *fp = fopen("file.txt", "r");
fclose()close filefclose(fp);
fread()read from filefread(buffer, 1, 100, fp);
fwrite()write to filefwrite(data, 1, 100, fp);
fprintf()print to filefprintf(fp, "number: %d\n", num);
fscanf()read formatted from filefscanf(fp, "%d", &num);
feof()check end of filewhile (!feof(fp))

📚 PROGRAMMING GLOSSARY

Data Structures & Memory

array
Collection of elements of same type stored in contiguous memory
int numbers[5] = {1, 2, 3, 4, 5}; // array of 5 integers
struct
Custom data type that groups related variables together
struct student {
    char name[50];
    int age;
    float gpa;
};
heap
Dynamic memory area where malloc/calloc allocate memory. You control when to free it
int *ptr = malloc(100 * sizeof(int)); // allocated on heap
stack
Automatic memory area where local variables live. Automatically cleaned up when function ends
void func() {
    int x = 10; // x lives on the stack
}

Pointers & Memory Operations

pointer
Variable that stores the memory address of another variable
int x = 42;
int *ptr = &x; // ptr is a pointer, stores address of x
dereference pointer
Using * to access the value that a pointer points to
int value = *ptr; // dereference ptr to get the value
& operator
"Address of" operator, gets the memory address of a variable
int x = 10;
int *ptr = &x; // get address of x
-> operator
Shorthand for accessing struct members through a pointer
struct point *p = malloc(sizeof(struct point));
p->x = 10; // same as (*p).x = 10;

Data Types & Declarations

union
Like struct but all members share the same memory location
union data {
    int i;
    float f;
    char c;
}; // only one member can hold a value at a time
enumeration (enum)
Defines a set of named integer constants
enum day {MONDAY, TUESDAY, WEDNESDAY}; // MONDAY=0, TUESDAY=1, etc.
unsigned
Data type modifier that only allows positive numbers (no negative)
unsigned int count = 42; // can't be negative, larger positive range
type alias (typedef)
Creates a new name for an existing data type
typedef struct point Point; // now can use Point instead of struct point
typedef int* IntPtr; // IntPtr is now alias for int*

Compilation & Preprocessing

compiler
Program that translates your C code into machine code that the CPU can execute
// gcc -o program program.c // gcc is the compiler
macro
Preprocessor directive that defines text replacement
#define SQUARE(x) ((x) * (x))
int result = SQUARE(5); // becomes ((5) * (5))
recursion
When a function calls itself to solve smaller versions of the same problem
int factorial(int n) {
    if (n <= 1) return 1; // base case
    return n * factorial(n - 1); // recursive case
}

Memory Issues & Debugging

memory leak
When you malloc memory but forget to free it
void bad_function() {
    int *ptr = malloc(100 * sizeof(int));
    // forgot free(ptr); - this is a memory leak
}
segmentation fault (segfault)
Trying to access memory you're not allowed to
int *ptr = NULL;
*ptr = 10; // segfault - dereferencing null pointer
buffer overflow
Writing past the end of an array
char buffer[5];
strcpy(buffer, "this string is too long"); // buffer overflow