NAVIGATE

RESOURCES

CHEATSHEETS

INFO

C Cheatsheet

A ready-to-use page to support your daily code development in C!

C

img
C logo

C is a powerful and widely used programming language known for its efficiency, flexibility, and versatility. Developed in the early 1970s by Dennis Ritchie at Bell Labs, C has influenced many other programming languages and operating systems, making it one of the most influential languages in the history of computing. C is often referred to as a "middle-level" language because it combines low-level features for direct hardware manipulation with high-level constructs for structured programming. It provides a concise syntax and a rich set of operators, making it suitable for systems programming, embedded development, operating systems, and application software. C's portability and performance make it a popular choice for developing software that requires close interaction with hardware or demands high computational efficiency. Despite its complexity and the potential for manual memory management errors, C remains a cornerstone language in computer science education and industry programming due to its power, efficiency, and ability to directly access system resources.

Preprocessor Directives

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Files Inclusion
#include <stdio.h>
#include <stdlib.h>
#include "myheader.h"

// Constants Definition
#define PI 3.14159
#define "String definition"

// Macro Definition
#define MACRO_NAME value

// Header Guards
#ifndef MYHEADER_FILE_H
#define MYHEADER_FILE_H
    // code block
#endif

Variables and Types

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Variables and Data Types
int integer_variable = 42;
float float_variable = 3.14;
double double_variable = 19.435e-17;
char character_variable = 'A';

// Signedness
unsigned  int uint_variableA = 435;
unsigned  int uint_variableB = -435;
unsigned  char uchar_variableA = 102;
unsigned  char uchar_variableB = -201;
signed  int sint_variableA = 456;
signed  int sint_variableB = -3546;
signed  char schar_variableA = 45;
signed  char schar_variableB = -23;

// Integer Number Base
int hex_format = 0x4E2A;
int dec_format = 3243;
int bin_format = 0b0100101010;
int oct_format = 043;

Operations

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Arithmetic
int a=546, b=4563;
int c;
c = a + b;  
c = a - b;  
c = a * b;  
c = b / a;  // Result in c is rounded
float fc;
fc = b / a; // Returns a float but result is rounded
fc = (float)b / a; // Returns a float with result not rounded
c = b % a;  //Modulus operator; it returns the remainder of the b/a division

// Bitwise Operators
int a = 5, b = 3;
int resultA1 = a & b;    // bitwise AND
int resultA2 = ~(a & b); // bitwise NAND
int resultB1 = a | b;    // bitwise OR
int resultB2 = ~(a | b); // bitwise NOR
int resultC1 = a ^ b;    // bitwise XOR
int resultC2 = ~(a ^ b); // bitwise XNOR
int resultD = ~a;        // bitwise NOT
int resultE = a >> b;    // bitwise right shift
int resultF = a << b;    // bitwise left shift

// Shorthand Operators
int a=33, b=44, c=55, d=66;
a += 45;    // Put in a the result of a+45
b -= 43;    // Put in b the result of b+43
c *= 2;     // Put in c the result of c*2
c /= 33;    // Put in d the result of d/33
a &= 0xf0f0;// Put in a the result of a&0xf0f0
b |= 0x0f0f;// Put in b the result of b|0x0f0f
c ^= 0x5555;// Put in c the result of c^0x5555
d ~= d;     // Put in d the result of ~d
a <<= 4;    // Shift 4 bits left
b >>= 3;    // Shift 3 bits right

Control Flow

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// if-else Control Flow
if (condition) {
    // code block
} else if (another_condition) {
    // code block
} else {
    // code block
}

// switch-case Control Flow
switch (expression) {
    case constant_value1:
        // code block
        break;
    case constant_value2:
        // code block
        break;
    default:
        // code block
}

Loops

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// for Loop
for (int i = 0; i < 5; i++) {
    // code block
}
for (int j = 45; i >= 7; i--) {
    // code block
}

// while-do Loop
while (condition) {
    // code block
}

// do-while Loop
do {
    // code block
} while (condition);

Functions

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Main Function
int main() {
    printf("Hello, World!\n");
    return 0;
}

// Function Prototypes
int add(int a, int b);
void nothing(float a, double b);
void stringify(char* my_string);

// Function Definitions
int add(int a, int b) {
    return a + b;
}
void nothing(float a, double b){
    char internal_string[] = "This function does nothing!";
    return;
}
void stringify(char* my_string){
    char* starting_string = "The argument is ";
    char tmp_string[32];
    strcpy(tmp_string, my_string);
    my_string = "\0";
    strcpy(my_string, starting_string);
    strcat(my_string, tmp_string);           // 'my_string' is updated; when the function returns, its space is freed
    return;                                 // This function returns nothing; result is returned through my_string
}

Pointers and Arrays

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Pointers to Variable
int number = 10;                                // 'number' is the value; '&number' is 'number' address
int* pnt_to_number = &number;                   // 'pnt_to_number' is 'number' address; '&pnt_to_number' is 'pnt_to_number' address; '*pnt_to_number' is 'number' value
int** pnt_to_pnt_to_number = &pnt_to_number;    // 'pnt_to_pnt_to_number' is 'pnt_to_number' address; '&pnt_to_pnt_to_number' is  'pnt_to_pnt_to_number' address; 
                                                // '*pnt_to_pnt_to_number' is 'pnt_to_number' value that is 'number' address; '**pnt_to_pnt_to_number' is 'number' value
number = 45;
*pnt_to_number = 23;
**pnt_to_pnt_to_number = 67;

void null_pointer = NULL;

// Pointers to Function
int (*pnt_to_function)(int, int);    // Create a pointer to a function returning an integer and accepting two integer as parameters
pnt_to_function = &function_name;    // Assign the function address to the pointer
int a = (*pnt_to_function)(22, 33);  // Call the function through its pointer

// Arrays
float float_array[];                    // Empty array
int integer_array[5] = {1, 2, 3, 4, 5}; // Populated array with length explicited
char char_array[] = "Hello";            // Populated array with implicit length; '\0' is automatically added

// Strings
char my_string_vector[] = "C Programming";
char* my_string_pointer = "C Programming";
// NOTE1: The two statements are equivalent 
// NOTE2: You don't have to NULL terminate the string when it is declared in this way
// Compiler does it for you; so the following is bad (not wrong but bad)
char* my_string_pointer_nulterm = "C Programming\0";

Other Data Types

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Structures
struct Person {
    char name[50];
    int age;
    char* comment;
};

// Enums
enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

// Typedefs
typedef  signed  int sint;        // 'sint' type is defined
typedef  unsigned  int uint;      // 'uint' type is defined
typedef  struct PersonStruct {   // 'Person' type is defined from PersonStruct structure
    char name[50];
    int age;
    char* comment;
}Person;
typedef  struct {                // 'Person' type is defined from anonymous structure
    char name[50];
    int age;
    char* comment;
}Person;
typedef  char* pchar             // 'pchar' type is defined

Memory allocation

0
1
2
3
4
5
// Dynamic Memory Allocation
int* dynamic_array = (int*)malloc(5 * sizeof(int));     // Allocate 5 memory cells
int* dynamic_array = (int*)realloc(14 * sizeof(int));   // Free previously allocated memory and realloc 14 memory cells
free(dynamic_array);                                    // Free previously allocated memory
int* dynamic_array = (int*)calloc(11, sizeof(int));     // Allocate 11 memory cells and initialize them to 0

Operators

0
1
2
3
4
5
6
7
8
// Conditional Operator (Ternary Operator)
int x = (condition) ? true_value : false_value;

// Sizeof Operator
int size = sizeof(int);

// Typecasting
float float_number = (float)integer_variable;

Comments

0
1
2
3
4
5
6
7
8
9
// Comments
// This is a single-line comment
// You have to repeat it at the beginning of each line
// to create a multi-line comment

/*
   This is a multi-line comment.
   You just have to use the start and stop symbol to delimit it.
*/

File Handling

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// File Handling
void* fopen(char* filename, char* mode);  // 'filename' is a string; 
                                          //'mode' can be: 'r'->read-only 'w'->write-mode 'a'->append-mode
                                          //               'rb'->binary read-only 'wb'->binary write-mode 'ab'->binary append-mode
                                          //               'r+', 'w+', 'a+'->read and write mode
int fclose(void* fp);

int fgetc(void* fp);
int getc(void* fp);
char* fgets(char *s, int size, FILE *stream);   
char* gets(char *s);                            // Specific for stdin, read till \n

int fputc(int c, void* fp);
int putc(int c, void* fp);
int fputs(const char *s, FILE *stream);
int puts(const char *s);                        // Specific for stdout, adds a \n

int fseek(void* fp, long offset, int whence);
long ftell(void* fp);
void rewind(void* fp);
int fsetpos(void* fp, fpos_t* pos)
int fgetpos(void* fp, fpos_t* pos)



Share this page

Whatsapp Facebook LinkedIn Reddit Twitter Mail

Comments

Be polite and respectful in the comments section. In case of doubts, read this before posting.

Posted comments ⮧

Michael2024-03-04 17:03:01

Good morning, can you add more information about C standard library? At least the most used ones like math, stdlib, stdio, string, ctype... It would be great, thanks

WebMaster - Vanadium2024-03-05 20:06:56

OK, I'll try in the next days. Thanks for the suggestion!

If you liked

🍵
♥