C Cheatsheet - C Language Syntax & Pointer Reference
This reference is for developers writing C for embedded, OS, or performance-critical work, where unlike managed languages you own memory and pointer lifetimes. It covers data types and constants, control flow, pointers and arrays (where the footguns live), functions, manual allocation and freeing, file I/O, and the preprocessor. Entries are grouped by the build block you are working in, with notes flagging where behavior is easy to get wrong. After reading you should be able to decide when to use a pointer vs an array, manage malloc/free without leaks, and encode compile-time logic with #ifdef.
Data Types & Variables 8
int x = 10;float pi = 3.14f;double e = 2.718;char c = 'A';char str[] = "Hello";const int MAX = 100;enum {RED, GREEN, BLUE};typedef int Integer;Control Flow 8
if (x > 0) { } else if (x < 0) { } else { }switch (x) { case 1: break; default: break; }for (int i = 0; i < n; i++) { }while (condition) { }do { } while (condition);break;continue;goto label;Pointers & Arrays 8
int *p;int x = 10; int *p = &x;*p = 20;int arr[5] = {1, 2, 3, 4, 5};int *p = arr;*(p + i)int **pp;void *vp;Functions 6
int add(int a, int b) { return a + b; }void swap(int *a, int *b);int (*fp)(int, int) = add;static int count = 0;extern int globalVar;inline int square(int x) { return x * x; }Memory Management 7
int *p = (int *)malloc(sizeof(int) * 10);int *p = (int *)calloc(10, sizeof(int));p = (int *)realloc(p, sizeof(int) * 20);free(p);memset(p, 0, size);memcpy(dest, src, size);memmove(dest, src, size);Structs & Unions 6
struct Point { int x; int y; };struct Point p = {1, 2};p.x = 10;struct Point *pp = &p; pp->x = 10;union Data { int i; float f; };typedef struct { int x; int y; } Point;File I/O 8
FILE *fp = fopen("file.txt", "r");fclose(fp);fscanf(fp, "%d", &x);fprintf(fp, "%d\n", x);fread(buf, size, count, fp);fwrite(buf, size, count, fp);fgets(buf, size, fp);fseek(fp, offset, SEEK_SET);Preprocessor 7
#include <stdio.h>#include "myheader.h"#define MAX 100#define SQUARE(x) ((x) * (x))#ifdef DEBUG ... #endif#ifndef HEADER_H ... #define HEADER_H ... #endif#pragma onceTips
- Always check that malloc's return value is not NULL.
- Set the pointer to NULL after free to avoid dangling pointers.
- An array name decays to a pointer in most expressions, but sizeof(arr) is still the whole array size.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us