TechSchool
Home / Cheat sheets / C

C cheat sheet

96 one-line examples for C. Open it while you code, or print it and keep it near the keyboard. Free, with no signup and no ads.

Print and Read

CodeWhat it does
printf("Hello\n");Print text to the screen.
printf("%d\n", x);Print an int value.
printf("%f\n", x);Print a float value.
printf("%c\n", ch);Print one char value.
printf("%s\n", s);Print a string.
printf("%5.2f\n", x);Print a float with two decimals.
printf("%%\n");Print a percent sign.
scanf("%d", &x);Read an int into x.
scanf("%f", &x);Read a float into x.
scanf("%c", &ch);Read one char into ch.
scanf("%s", buf);Read a word into a buffer.
puts("text");Print text and add a newline.

Variables and Types

CodeWhat it does
int x = 5;Declare an int and set it.
float f = 1.5f;Declare a float value.
double d = 2.5;Declare a double value.
char c = 'A';Declare a char value.
unsigned int u = 10;Declare a value that is never negative.
long n = 100000L;Declare a larger integer type.
const int MAX = 100;Declare a value that cannot change.
int a = 5, b = 6;Declare two ints on one line.
sizeof(int)Get the size of a type in bytes.
sizeof xGet the size of a variable.
(float) xConvert x to a float.
x = (int) d;Convert a double to an int.

Operators

CodeWhat it does
a + bAdd two values.
a - bSubtract b from a.
a * bMultiply two values.
a / bDivide a by b.
a % bGet the remainder of a divided by b.
a++Add one to a.
a--Subtract one from a.
a += 5Add five to a.
a == bTest if two values are equal.
a != bTest if two values differ.
a < b && b < cTest if both conditions are true.
a > b || a < 0Test if at least one condition is true.

Conditions

CodeWhat it does
if (x > 0) { }Run a block when x is positive.
if (x > 0) { } else { }Run one of two blocks.
else if (x == 0) { }Test another condition.
if (!done) { }Run a block when done is false.
switch (n) { }Choose a block by an int value.
case 1: break;Run this case and stop.
default: break;Run this case when no case matches.
x > 0 ? a : bChoose a or b by a condition.
if (x >= 1 && x <= 9)Test if x is between one and nine.
if (s == NULL) { }Test if a pointer is empty.
if (c == 'y') { }Test a char value.
if (x) { }Run a block when x is not zero.

Loops

CodeWhat it does
for (int i = 0; i < n; i++) { }Repeat a block n times.
for (int i = n; i > 0; i--) { }Count down from n to one.
while (x > 0) { }Repeat while a condition is true.
do { } while (x > 0);Run a block at least one time.
break;Leave the loop now.
continue;Skip to the next loop step.
for (;;) { break; }Make a loop with no condition.
for (int i = 0; i < n; i += 2)Step by two on each pass.
while (scanf("%d", &x) == 1)Read numbers until input ends.
for (int i = 0; i < 3; i++)Loop three times.
while (1) { break; }Loop until a break runs.
for (int i = 0; s[i]; i++)Walk a string until the null byte.

Arrays and Strings

CodeWhat it does
int a[5];Declare an array of five ints.
int a[3] = {1, 2, 3};Declare and fill an array.
int a[5] = {0};Set all array items to zero.
a[0] = 10;Set the first array item.
a[i]Read the item at index i.
char s[10];Declare a string buffer of ten bytes.
char s[] = "hi";Declare a string from a literal.
s[0]Read the first char of a string.
strlen(s)Get the string length.
strcpy(d, s);Copy a string into d.
strcmp(a, b) == 0Test if two strings are equal.
s[i] == '\0'Test for the end of a string.

Functions

CodeWhat it does
int add(int a, int b) { }Define a function that returns an int.
return a + b;Send a value back to the caller.
void show(int x) { }Define a function with no return value.
int add(int a, int b);Declare a function before you use it.
add(2, 3);Call a function with two values.
return;Leave a void function early.
int main(void) { }Define the program entry point.
return 0;End main and report success.
static int count = 0;Keep a file-local variable.
void f(int *p) { }Pass a pointer to a function.
f(&x);Pass the address of x.
int (*fp)(int) = f;Store a function address in a pointer.

Pointers and Memory

CodeWhat it does
int *p = &x;Point p at the address of x.
*p = 5;Set the value that p points to.
int y = *p;Read the value that p points to.
int *p = NULL;Make a pointer that points nowhere.
int *p = malloc(n * sizeof(int));Allocate memory on the heap.
free(p);Release heap memory.
p = NULL;Clear a pointer after free.
int a[3]; int *p = a;Point at the first array item.
*(p + 1)Read the next item by pointer math.
if (p != NULL) { }Test a pointer before you use it.
struct P { int x; };Define a struct type.
p->x = 1;Set a struct field through a pointer.
More on TechSchool
See the C interview questions Python JavaScript TypeScript HTML CSS React Vue 3 Node.js Java PHP C++ C# Go SQL MongoDB Redis Linux Git Docker Data Structures