C
Updated: September 8, 2025Categories: Languages
Printed from:
C Programming Cheatsheet
Language Overview
- Low-level, compiled, procedural programming language
- Developed by Dennis Ritchie at Bell Labs in 1972
- Foundation for many modern programming languages
- Provides direct access to system hardware and memory
- Used for systems programming, embedded systems, and performance-critical applications
Basic Syntax
C
123456789#include <stdio.h> // Standard input/output header
// Basic program structure
int main() {
// Program logic goes here
printf("Hello, World!\n");
return 0; // Mandatory return for main function
}
Data Types
Primitive Types
C
12345678910111213141516171819// Integer Types
short // 16-bit integer (-32,768 to 32,767)
int // Standard integer (-2,147,483,648 to 2,147,483,647)
long // Long integer
long long // Extended long integer
// Floating Point Types
float // Single-precision floating point
double // Double-precision floating point
long double // Extended precision floating point
// Character Types
char // 8-bit character
signed char // Signed character
unsigned char // Unsigned character
// Boolean (in C99 and later)
_Bool // Boolean type (0 or 1)
Derived Types
C
12345678910111213141516171819202122// Pointers
int *ptr; // Pointer to integer
char *str; // Pointer to character (string)
// Arrays
int arr[10]; // Fixed-size integer array
char name[50]; // Character array (string)
// Structures
struct Person {
char name[50];
int age;
float height;
};
// Unions
union Data {
int i;
float f;
char str[20];
};
Variables and Constants
C
123456789// Variable Declaration
int age = 30; // Initialized variable
const float PI = 3.14; // Constant value
// Type Qualifiers
volatile int sensor; // Can change unexpectedly
static int count = 0; // Retains value between function calls
extern int global; // Declares variable defined elsewhere
Operators
Arithmetic Operators
C
1234567int a = 10, b = 5;
int sum = a + b; // Addition
int diff = a - b; // Subtraction
int prod = a * b; // Multiplication
int div = a / b; // Division
int mod = a % b; // Modulus
Comparison Operators
C
1234567a == b; // Equal to
a != b; // Not equal to
a > b; // Greater than
a < b; // Less than
a >= b; // Greater than or equal to
a <= b; // Less than or equal to
Logical Operators
C
12345int x = 1, y = 0;
x && y; // Logical AND
x || y; // Logical OR
!x; // Logical NOT
Bitwise Operators
C
1234567a & b; // Bitwise AND
a | b; // Bitwise OR
a ^ b; // Bitwise XOR
~a; // Bitwise NOT
a << 2; // Left shift
a >> 2; // Right shift
Control Structures
Conditional Statements
C
123456789101112// If-Else
if (condition) {
// Code if true
} else if (another_condition) {
// Alternative condition
} else {
// Default case
}
// Ternary Operator
int result = (x > y) ? x : y; // Conditional assignment
Loops
C
12345678910111213141516171819// For Loop
for (int i = 0; i < 10; i++) {
// Repeated code
}
// While Loop
while (condition) {
// Repeated code
}
// Do-While Loop
do {
// Always executes at least once
} while (condition);
// Break and Continue
break; // Exit loop
continue; // Skip current iteration
Functions
C
1234567891011121314151617181920212223// Function Declaration
int add(int a, int b) {
return a + b;
}
// Function Pointer
int (*operation)(int, int);
// Variadic Functions
#include <stdarg.h>
int sum_numbers(int count, ...) {
va_list args;
va_start(args, count);
int sum = 0;
for (int i = 0; i < count; i++) {
sum += va_arg(args, int);
}
va_end(args);
return sum;
}
Memory Management
C
123456789101112131415// Dynamic Memory Allocation
int *arr = malloc(10 * sizeof(int)); // Allocate memory
if (arr == NULL) {
// Handle allocation failure
}
// Resizing Memory
arr = realloc(arr, 20 * sizeof(int));
// Freeing Memory
free(arr);
// Calloc for Zero-Initialized Memory
int *zeroed_arr = calloc(10, sizeof(int));
Preprocessor Directives
C
12345678910111213#include <stdio.h> // Include standard library
#define MAX_SIZE 100 // Constant definition
#define SQUARE(x) ((x) * (x)) // Macro function
#ifdef DEBUG
printf("Debug mode\n");
#endif
#ifndef HEADER_H
#define HEADER_H
// Header file contents
#endif
File I/O
C
1234567891011FILE *file = fopen("example.txt", "r"); // Open file
if (file == NULL) {
// Handle file open error
}
char buffer[100];
fgets(buffer, sizeof(buffer), file); // Read line
fprintf(file, "Writing to file\n"); // Write to file
fclose(file); // Close file
Error Handling
C
1234567#include <errno.h>
#include <string.h>
if (some_function() == -1) {
printf("Error: %s\n", strerror(errno));
}
Structures and Unions
C
12345678910111213141516171819// Structure
struct Student {
char name[50];
int age;
float gpa;
};
// Typedef for easier type definition
typedef struct {
int x, y;
} Point;
// Union
union Data {
int i;
float f;
char str[20];
};
Enumerations
C
1234567enum Days {
MONDAY, // 0
TUESDAY, // 1
WEDNESDAY, // 2
// ...
};
Best Practices
- Always initialize variables
- Use const for values that won't change
- Check memory allocation return values
- Close files and free dynamically allocated memory
- Use meaningful variable and function names
- Minimize global variables
- Comment complex code sections
Build and Compile
Bash
123456789# Compile with gcc
gcc -o program program.c
# Compile with warnings
gcc -Wall -Wextra -o program program.c
# Debug build
gcc -g -o program program.c
Testing Approaches
- Manual testing
- Unit testing with frameworks like Check or Unity
- Valgrind for memory leak detection
- Static code analysis tools
Performance Considerations
- Minimize dynamic memory allocation
- Use stack memory when possible
- Optimize loops and avoid redundant computations
- Use appropriate data types
- Consider compiler optimization flags (-O2, -O3)
Resources for Further Learning
- "The C Programming Language" by Kernighan and Ritchie
- "C Programming: A Modern Approach" by K. N. King
- Online resources: GeeksforGeeks, CProgramming.com
- Websites: Stack Overflow, C FAQ
Continue Learning
Discover more cheatsheets to boost your productivity