This page is in development, NOT TO BE USED FOR TRAINING!!

FIXME: Check spelling

FIXME: check definition of words used

FIXME: More info C programming is statically typed programming language

See ISO-9899

See Charter

See Wiki

See Books

Example

#include <stdio.h>

// Comment
/* Comment */

// Return highest number in an array
int highestNumber(int numbers[], int size)
{
    int largest = numbers[0];
    for(int i = 1; i < size; i++)
    {
        if(numbers[i] > largest)
        {
            largest = numbers[i];
        }
    }
    return largest;
}

int main() {
    int const size = 7;

    int numbers[size] = {8, 24, 8, 12, 4, 87, 13};

    printf("Highest Number is %d\n", highestNumber(numbers,size));

    return 0;
}

Data types

Defined maximums depends per platform these are examples:

CHAR_BIT    :   8
CHAR_MAX    :   127
CHAR_MIN    :   -128
INT_MAX     :   2147483647
INT_MIN     :   -2147483648
DBL_MIN     :   0.000000
DBL_MAX     :   179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000
FLT_MIN     :   0.000000
FLT_MAX     :   340282346638528859811704183484516925440.000000
LONG_MAX    :   9223372036854775807
LONG_MIN    :   -9223372036854775808
SCHAR_MAX   :   127
SCHAR_MIN   :   -128
SHRT_MAX    :   32767
SHRT_MIN    :   -32768
UCHAR_MAX   :   255
UINT_MAX    :   4294967295
ULONG_MAX   :   18446744073709551615
USHRT_MAX   :   65535

See implementation-defined constants

See Reference for datatypes

FIXME: Implement https://www.youtube.com/watch?v=k12BJGSc2Nc&list=PLHTh1InhhwT75gykhs7pqcR_uSiG601oh&index=56

identifiers

identifier is the developer-defined name of a program element.

// Store value '12' in a variable 'something' which is an identifier
int something = 12;
//  ^^^^^^^^^ - identifier

identifiers can be following abcefghijklmnopqrstuvwxyzABCDEFGHCIJKLMNOPQRSTUVWXYZ_0123456789, but they can NOT start with a number.

int 5something = 12;
//  ^ - ILLEGAL! (fixme: why?)

Standard method to write identifiers is to use camel case -> somethingIsNothing (notice that first letter is lowercase)

int someVariable = 12;
//  ^ - Notice lower-case used

Integer

Stores an (integer)[https://en.wikipedia.org/wiki/Integer] which is a number that can be written without a fractional component those are for example 21, 4, 0, and -2048 while 9.75 and √2 are not. ref

// Stores the number '10' in variable 'x'
int x = 10;

integer is represented by int in C18

   int x = 10;
// ^^^ - Interage datatype

Limits for integer value are for example from -2147483648 to 2147483647 on Linux.

int x = 10;
//      ^^ - Value

Float (fixme: summary?)

ref; https://youtu.be/PZRI1IfStY0

FIXME:

"Float is 32-bit number used for storing decimal numbers" nope and nope
there's no guarantee it's 32 bits, and it doesn't store decimals
e.g. 0.1 cannot be represented exactly by a float

Float is … used for storing … from FLT_MIN to FLT_MAX.

These limits are for example 0.000000 to 340282346638528859811704183484516925440.000000

// Stores number 10.5 in variable 'x'
float x = 10.5;
//        ^^^^ decimal

FLT_MIN and FLT_MAX are defined in float.h on unix where using these in limits.h is marked as legacy. (?)

Double (fixme: summary?)

Real floating-point type, usually referred to as a double-precision floating-point type.

Double takes twice as much space compared to float.

FIXME:

a double can store values below 0 e.g. -DBL_MAX or -2
it can also store values outside that range (positive and negative infinity)
and other values that are not a number
and it's not guaranteed to be 64 bits
and I don't know what "not compatible with 32-bit systems" means, but I'm pretty sure it's wrong

These limits are for example 0.000000 to 1797693134862315708145274237317043567980705675258449965989174768031572
6078002853876058955863276687817154045895351438246423432132688946418276
8467546703537516986049910576551282076245490090389328944075868508455133
9423045832369032229481658085593321233482747978262041447231687381771809
19299881250404026184124858368.000000

(FIXME: Rephrase)
Double can represent gigantic numbers as floats, but you lose a ton of precision!

// Stores number 10.5 in variable 'x'
double x = 10.5;
//         ^^^^ decimal

Character

Characters are in C represented under the shortcut of ‘‘char’‘ref which stores an integer based on characters that were used to store the value. (fact-check!)

// Store a value '65' in a variable 'foo' on system with ASCII table standard
char x = 'A'; 

These conversion characters are standardized in an (ASCII table)[https://en.wikipedia.org/wiki/ASCII] depending on the system used for example (IBM mainframe)[https://en.wikipedia.org/wiki/IBM_mainframe] systems using Extended Binary Coded Decimal Interchange Code (EBCDIC)[https://en.wikipedia.org/wiki/EBCDIC] doesn’t recognize the (ASCII tables)[https://en.wikipedia.org/wiki/ASCII] standard.

Note that conversion characters are usually referenced as decimal ASCII characters in a sence of ‘base-10 numbering system’ not to be confused with (Floating-point arithmetic)[https://en.wikipedia.org/wiki/Floating-point_arithmetic]

See Conversion characters

Character array

FIXME: More info requried

char x[] = "char "

Boolean

Booleans were introduced in the header C99 which are used to store a value ‘000000’ for false and ‘000001’ for true assuming bit size limited on 8 bits.

In Standard revision C18 this definition is standardized in stdbool.h library which is expected to be included if boolean data types are to be used in the code.

#include <stdbool.h>

// Store value '000000' in variable 'x'
bool x = false;

long long

FIXME: Add info

Unsigned

FIXME: Fact check!

Unsigned datatypes are used to determine a value to never be negative

unsigned int x = 10;
// FIXME: Add example for long long
unsigned long long x =

Unsigned long long

FIXME: Add info

Printf is used in C lang to format string and return it as output.

// Output string
printf("%s", "Hello World"); // Returns 'Hello World'

// Output value of variable
char something = "hello";
printf("%d", something); // Returns 'hello'

// Output interage stored in char
char something = 'A';
printf("%i, something); # Returns '65' on a system that is using ASCII standard

Operators

Operators are used to create an expression

// This is binary operator becuase plus has two operands
int x = 5 + 5;
//      ^^^^^ - Expression
//      ^   ^ - Operands
//        ^   - Operator

There are unary, binary and tonary operators

Aritmetical operators (math)

+ = Addition

// 10 is added by 7 -> value '17' is stored in variable 'x'
int x = 10 + 7;

- = Substraction

// 10 is subtracked by 3 -> value '7' is stored in variable 'x'
int x = 10 - 3;

/ = Division

// 10 is devided by 2 -> value '5' is stored in variable 'x'
int x = 10 / 2;

* = Multiplication

// 10 is multiplied by 2 -> value '20' is stored in variable 'x'
int x = 10 * 2;

% = Modules (reminder of interage devision)

// 10 devided by 3 and whatever is left is set as value of variable 'x' (Stores '1' in variable 'x')
int x = 10 % 3;

Comparison Operators

== = Equal

// If variable 'c' stores a value equal to value '5' -> Return true -> Outputs "Hello World"
if(c == 5)
{
    printf("%s\n", "Hello World");
}

!= = Not Equal

// If variable c stores a value not equal to value '5' -> Return true -> Outputs "Hello World"
if(c != 5)
{
    printf("%s\n", "Hello World");
}

< = Left is smaller then right

// If variable c stores value smaller then value '5' -> Return true -> Outputs "Hello World"
if(c < 5)
{
    printf("%s\n", "Hello World");
}

> = Left is bigger then right

// If variable c stores value bigger then value '5' -> Return true -> Outputs "Hello World"
if(c > 5)
{
    printf("%s\n", "Hello World");
}

<= = Left is smaller or equal to right

// If variable c stores value smaller or equal to value '5' -> Return true -> Outputs "Hello World"
if(c <= 5)
{
    printf("%s\n", "Hello World");
}

>= = Left is bigger or equal to right

// If variable c stores value bigger or equal to value '5' -> Return true -> Outputs "Hello World"
if(c >= 5)
{
    printf("%s\n", "Hello World");
}

Unary minus

FIXME: More info needed

int x = 5;
int y = -x;

// x is 5
// y is -5

Increments

FIXME: Info needed

// `a++;` is same as `a = a + 1;`
a = 5;
b = 5;
b = a++;
//   ^^ - this is evaulated after variable 'a'

// a is 5
// b is 6

Pre-Increments

FIXME: Info needed

b = 5;
a = 5;
b = ++a;
//  ^^ this is evaulated prior to variable 'a'

// b is 6
// a is 6

See Operator precedence

Logic

Expressions

Expressions are any legal combination of code that returns a legal value.

if (true && false || true) ...
//  ^^^^^^^^^^^^^^^^^^^^^ Expression

In C lang expressions are evaulated from left to right

   if(true && false || true) ...
//    ^^^^^^^^^^^^^ - First
//    ^^^^^^^^^^^^^^^^^^^^^ - Second (true AND false is **false** -> **false** OR true is true)

If statements

If statements are used to return an action depending on the logic provided in the expression.

int x = 10;

// If variable 'x' does NOT store a value 10
if(10 != x)
{
    action

// Else If if statement above is false and if variable 'x' does store a value 10
} else if(10 == x)
{
    // In this example this would trigger
    action
// In case both if statements above return false
} else {
    action
};

If statements can also be written in a one line

if (expression) action;

For loops

Run the loop untill expression is false

For loops are usually initiated with expression based on initialization -> comparison -> update

Example of expression for for loop int i = 0; i <= 10; i++ ^^^^^^^^^ initialization ^^^^^^^ Comparison ^^^ Update

Loop with this expression is going to run untill comparison returns false. (for 10 cycles in this example)

for(expression)
{
    action
};

While loop

While expression is true, run the loop

while(i < 20)
{
    // code here
    i++;
};

Functions

FIXME: Needs fact check!

Functions are a collection of code used to perform specific task

#include <stdio.h>

// Output 'Hello Jon aged 67' and return 0

void sayHi(char name[], int age)
//         ^^^^^^^^^^^^^^^^^^^^ - Specifying input
//   ^^^^^ - identifier
//^^ - Prefix?
{
    printf("Hello %s aged %d\n", name, age);
}

int main()
{
    sayHi("Jon", 67);
    return 0;
}

You can use void prefix for a function to set it to not return any information (meaning not storing info in variables?)

void funcname()
{
    // code
}

FIXME: info

Libraries

FIXME: info

0 Comments for this cheatsheet. Write yours!