C Cheatsheet - C Language Syntax & Pointer Reference

All essential C commands organized by use case, with 58+ entries you can copy and run directly. Find the right command fast when you need it.

Languages·58 commands·Last updated 2026-07-21
Back to Languages

Data Types & Variables 8

int x = 10;
Integer variable
float pi = 3.14f;
Single-precision float
double e = 2.718;
Double-precision float
char c = 'A';
Character variable
char str[] = "Hello";
String (char array)
const int MAX = 100;
Constant
enum {RED, GREEN, BLUE};
Enum type
typedef int Integer;
Type alias

Control Flow 8

if (x > 0) { } else if (x < 0) { } else { }
Conditional branch
switch (x) { case 1: break; default: break; }
Multi-way branch
for (int i = 0; i < n; i++) { }
for loop
while (condition) { }
while loop
do { } while (condition);
do-while loop
break;
Break out of loop/switch
continue;
Skip current iteration
goto label;
Jump (use with care)

Pointers & Arrays 8

int *p;
Pointer declaration
int x = 10; int *p = &x;
Take an address
*p = 20;
Dereference
int arr[5] = {1, 2, 3, 4, 5};
Array initialization
int *p = arr;
Array name is the base address
*(p + i)
Pointer arithmetic (equals arr[i])
int **pp;
Double pointer
void *vp;
Generic pointer

Functions 6

int add(int a, int b) { return a + b; }
Function definition
void swap(int *a, int *b);
Function declaration (prototype)
int (*fp)(int, int) = add;
Function pointer
static int count = 0;
Static local variable
extern int globalVar;
Extern variable declaration
inline int square(int x) { return x * x; }
Inline function

Memory Management 7

int *p = (int *)malloc(sizeof(int) * 10);
Allocate memory dynamically
int *p = (int *)calloc(10, sizeof(int));
Allocate and zero-initialize
p = (int *)realloc(p, sizeof(int) * 20);
Reallocate memory
free(p);
Free memory
memset(p, 0, size);
Initialize memory
memcpy(dest, src, size);
Copy memory
memmove(dest, src, size);
Safe memory copy (overlap-safe)

Structs & Unions 6

struct Point { int x; int y; };
Struct definition
struct Point p = {1, 2};
Struct initialization
p.x = 10;
Access a member
struct Point *pp = &p; pp->x = 10;
Pointer access to a member
union Data { int i; float f; };
Union (shared memory)
typedef struct { int x; int y; } Point;
Struct alias

File I/O 8

FILE *fp = fopen("file.txt", "r");
Open a file (r/w/a/rb/wb)
fclose(fp);
Close a file
fscanf(fp, "%d", &x);
Formatted read
fprintf(fp, "%d\n", x);
Formatted write
fread(buf, size, count, fp);
Binary read
fwrite(buf, size, count, fp);
Binary write
fgets(buf, size, fp);
Read a line
fseek(fp, offset, SEEK_SET);
Seek within a file

Preprocessor 7

#include <stdio.h>
Include a system header
#include "myheader.h"
Include a custom header
#define MAX 100
Macro definition
#define SQUARE(x) ((x) * (x))
Macro function
#ifdef DEBUG ... #endif
Conditional compilation
#ifndef HEADER_H ... #define HEADER_H ... #endif
Header guard
#pragma once
Modern header guard

Tips

  • 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

Commands are compiled from the official docs below. Click to verify the latest usage.

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