C Cheatsheet 
gcc <file-name>.c -o <exe-filename>.exe
./<exe-filename>.exe#include <stdio.h>
int main() {
printf("Hello, world\n");
return 0;
}#include <library.h>// declare variable
<type> <name> = <value>;
// declare array
<type> <name>[] = {<value>, <value>};
// string
char <name>[] = "<content>";
char <name>[<str-length>] = "<content>";
/*
Types:
unsigned int, short, long = %u = positive number
signed int, short, long = %d = negative numbers
bool = %d = true/false
char = %c = one character (in single quiote)
int = %d = numbers
short = %hd = numbers (-32_768 to 32_767)
long = %ld = numbers (-9223372036854775808 to 9223372036854775807)
float = %f = floating numbers (6 decimals)
double = %lf = floating numbers (15 decimals)
long double = %Lf = floating numbers (19 decimals)
void = N/A = only for function return as empty
*/// create struct
struct <struct-name> {
<type> <key-name>;
<type> <key-name>;
}
// initialize empty struct variable
struct <struct-name> <variable-name>;
// initialize filled struct variable
struct <struct-name> <variable-name> = {<value>, <value>};<type> name() {
//...
}
// return
<type> name() { return x }
// parameters
<type> name(<type> param1) { }if (condition) {
//...
} else if (condition2) {
//...
} else {
//...
}switch (statement) {
case x:
//...
break;
case y:
//...
break;
default:
//...
}for (int i=0; i < 10; i++) {
//...
}while (condition) {
//...
}// str -> int
#include <stdlib.h>
int number = atoi(<string>);
// int -> str
#include <stdlib.h>
char str[20];
sprintf(str, "%d", <number>);
// int -> float
float decimal = (float) <number>;
// flaot -> int
int number = (int) <float>;// print formatted output
// without variable
printf("Hello, world");
// with variable
printf("Hello, <format-type>", <variable>);// get user input
scanf(<format-type>, <variable-addres>);// todo// create new pointer variable with name pointer
<type> *pointer;
// change pointer address
pointer = <address>;
// change data from address/pointer
*pointer = <value>
// read data from address/pointer
*pointer
// get address from variable
&variable// get start time
clock_t startTime = clock();
// create delay
void delay(int ms) {
clock_t startTime = clock();
while (clock() < startTime + msDelay);;
}- Calculator
- Sorting algorithm
- Snake