Skip to content

lordware/CLang

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 

Repository files navigation

C Cheatsheet

File

Run file

gcc <file-name>.c -o <exe-filename>.exe

./<exe-filename>.exe

Hello world

#include <stdio.h>

int main() {
    printf("Hello, world\n");

    return 0;
}

Importing libraries

#include <library.h>

Variables

// 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
*/

structs

// 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>};

Functions

<type> name() {
	//...
}

// return
<type> name() { return x }

// parameters 
<type> name(<type> param1) {  }

Logic statements

If/else

if (condition) {
    //...
} else if (condition2) {
    //...
} else {
    //...
}

Switch/case

switch (statement) {
    case x:
        //...
        break;
    case y:
        //...
        break;
    default:
        //...
}

Loops

For-I

for (int i=0; i < 10; i++) {
    //...
}

While

while (condition) {
    //...
}

Converting

// 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>;

Build-In Functions

printf

// print formatted output

// without variable
printf("Hello, world");

// with variable
printf("Hello, <format-type>", <variable>);

scanf

// get user input

scanf(<format-type>, <variable-addres>);

memory

// todo

Pointers

// 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

Libraries

time.h

// get start time
clock_t startTime = clock();


// create delay
void delay(int ms) {
    clock_t startTime = clock();

    while (clock() < startTime + msDelay);;
}

Projects

  • Calculator
  • Sorting algorithm
  • Snake

About

My CLang projects with cheatsheet

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 100.0%