C Programming Cheat Sheet
A comprehensive guide to C language fundamentals
Basic Syntax Data Types
Program Structure Primary Types
int - integers
// Hello World Program float - single precision
#include <stdio.h> double - double precision
char - characters
int main() {
void - no value
printf("Hello, World!");
return 0; Type Modifiers
} short
long
Comments signed
unsigned
// Single line comment
/* Multi-line Example
comment */
int count = 10;
float price = 15.50;
char letter = 'A';
double pi = 3.14159265359;
Variables & Constants Operators
Variable Declaration Arithmetic
int age; 5 + 3 // Addition
float salary = 1000.0; 5 - 3 // Subtraction
char name[50] = "John"; 5 * 3 // Multiplication
5 / 3 // Division
Constants 5 % 3 // Modulus
Comparison
#define PI 3.14159
const int MAX = 100; a == b // Equal to
a != b // Not equal to
Scope a < b // Less than
Local variables a > b // Greater than
Global variables a <= b // Less than or equal
Static variables a >= b // Greater than or equal
Logical
a && b // Logical AND
a || b // Logical OR
!a // Logical NOT
Control Flow Loops
If-Else For Loop
if (x > 10) { for (int i = 0; i < 10; i++) {
printf("x is greater than printf("%d ", i);
10"); }
} else if (x == 10) {
printf("x is 10"); While Loop
} else {
printf("x is less than 10"); while (count > 0) {
} printf("%d ", count);
count--;
Switch }
switch(grade) { Do-While Loop
case 'A':
printf("Excellent"); do {
break; printf("%d ", count);
case 'B': count--;
printf("Good"); } while (count > 0);
break;
default: Break & Continue
printf("Invalid");
} break; // Exit loop
continue; // Skip iteration
Functions Arrays
Declaration & Definition Declaration
// Function prototype int numbers[5];
int add(int a, int b); int values[5] = {1, 2, 3, 4, 5};
char name[] = "John";
// Function definition
int add(int a, int b) { Accessing Elements
return a + b;
} int first = numbers[0];
numbers[2] = 10;
Function Call
Multi-dimensional Arrays
int result = add(5, 3);
int matrix[3][3] = {
Recursion {1, 2, 3},
{4, 5, 6},
int factorial(int n) { {7, 8, 9}
if (n <= 1) };
return 1;
else
return n * factorial(n-1);
}
Pointers
Declaration & Initialization
int x = 10;
int *ptr = &x; // ptr holds
address of x
Dereferencing
int y = *ptr; // y gets value at
ptr
Pointer Arithmetic
ptr++; // Move to next int
ptr += 2; // Move 2 ints forward
ptr--; // Move to previous int
Pointers & Arrays
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr; // p points to
arr[0]
int third = *(p+2); // third = 3
Structures & Unions Memory Management
Structure Definition Dynamic Allocation
struct Person { // Allocate memory for one int
char name[50]; int *ptr = (int *)
int age; malloc(sizeof(int));
float salary;
}; // Allocate memory for array of
10 ints
struct Person person1 = {"John", int *arr = (int *) malloc(10 *
25, 50000.0}; sizeof(int));
Accessing Members // Allocate and initialize
memory
person1.age = 26; int *nums = (int *) calloc(10,
printf("%s", person1.name); sizeof(int));
Unions Resizing Memory
union Data { // Resize previously allocated
int i; memory
float f; arr = (int *) realloc(arr, 20 *
char str[20]; sizeof(int));
};
Freeing Memory
union Data data;
data.i = 10; free(ptr);
free(arr);
free(nums);
File I/O
Opening & Closing Files Reading & Writing
FILE *file; // Read character
char c = fgetc(file);
// Open file for reading
file = fopen("data.txt", "r"); // Read string
char str[100];
// Open file for writing fgets(str, 100, file);
file = fopen("output.txt", "w");
// Write character
// Close file fputc('A', file);
fclose(file);
// Write string
fputs("Hello", file);
// Formatted I/O
fprintf(file, "Value: %d", x);
fscanf(file, "%d", &x);
Preprocessor Directives
Include & Define Other Directives
#include <stdio.h> // Standard #undef PI // Undefine macro
library
#include "myheader.h" // Custom #pragma once // Include file only
header once
#define PI 3.14159 #error "Compilation stopped" //
#define SQUARE(x) ((x)*(x)) Force error
Conditional Compilation #line 100 "newfile.c" // Change
line number
Predefined Macros
#ifdef DEBUG
printf("Debug mode"); printf("%s", __FILE__); // File
#endif name
printf("%d", __LINE__); // Line
#ifndef BUFFER_SIZE number
#define BUFFER_SIZE 1024 printf("%s", __DATE__); //
#endif Compilation date
printf("%s", __TIME__); //
Compilation time
C Programming Language Cheat Sheet
A comprehensive reference for C programmers