"Access a comprehensive cheatsheet for C programming. This downloadable PDF offers concise explanations, examples, and quick references for functions, syntax, and key concepts. Perfect for both beginners and seasoned programmers."
By CodeWithHarry
Updated: April 5, 2025
This cheatsheet is designed to help you quickly revise C syntax before exams. Covers basics, control flow, arrays, strings, pointers, functions, and file I/O—the topics most commonly asked in practicals and viva.
# Compile and rungcc program.c -o program./program# With warnings (recommended)gcc -Wall program.c -o program
2. Data Types & Variables
Primitive Types
char c = 'A'; // 1 byteint i = 42; // Typically 4 bytesfloat f = 3.14f; // 4 bytesdouble d = 3.14159; // 8 bytes
Size and Ranges
printf("Size of int: %zu\n", sizeof(int));
3. Input / Output
Output with printf
printf("Integer: %d\n", 10);printf("Float: %.2f\n", 3.14);printf("Char: %c\n", 'A');printf("String: %s\n", "Hello");// This is a single line comment/* This is a multi-line comment */
Input with scanf
int num;scanf("%d", &num);char name[50];scanf("%49s", name); // Avoid buffer overflow
gets and puts
char str[100];gets(str);puts(str);// NOTE: gets() is unsafe. Use fgets(str, sizeof(str), stdin) instead.
4. Control Flow
if-else
if (a > b) { printf("a is greater");} else { printf("b is greater");}
switch
switch (ch) { case 1: printf("One"); break; case 2: printf("Two"); break; default: printf("Other");}
Loops
// for loopfor (int i = 0; i < 5; i++) printf("%d ", i);// while loopint i = 0;while (i < 5) i++;// do-while loopint j = 0;do { j++; } while (j < 5);
5. Arrays
int arr[5] = {1, 2, 3, 4, 5};// Array sizesize_t size = sizeof(arr) / sizeof(arr[0]);// Traversalfor (size_t i = 0; i < size; i++) printf("%d ", arr[i]);