Show Menu
Cheatography

C Syntax Cheat Sheet (DRAFT) by

C syntax for beginners.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Declaring Variables

int i;
Signed integer. 1 bit to determine positive or negative and 31 bits for value.
unsigned int j;
Unsigned integer. All 32-bits for values.
const int k = 10;
Declare and asign a constant int.
char c;
A single byte.

Loop examples

for(int i = 0; i < 5; i++) {
  printf("%i ", i);
} // Output 0 1 2 3 4

int j = 5;
while (j--) {
  printf("%i ", j);
} // Output 4 3 2 1 0
 

Array Declar­ation

char arr[] = "Hello";
Char array with string literal.
char arr[5];
An array for 5 charac­ters.
char arr = {'A', 'B', '\0'};
With an array literal.
char *arr;
Declar­ation of a pointer.

Manipu­lating an Array

char s[] = "Hello";

// Read out one element
char FirstChar = s[0];

// Modify one element
s[1] = 'a';

// Read out each element
int len = strlen(s);
for(int i = 0; i < len; i++) {
  printf("s[%i] = %c\n", i, s[i]);
}