Giter VIP home page Giter VIP logo

clang's Introduction

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

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

clang's People

Contributors

tomiis4 avatar lordware avatar

Stargazers

Andy Shevchenko avatar  avatar

Watchers

 avatar  avatar

Forkers

lordware yolao

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.