me
Superdanby

C/C++ Programming Overview

C C++

Basic types

  • data register
  • int, float, char, long long, double, short
  • C++ exclusive: bool
int i = 0;
int a = b;

Array

1
2
3
4
5
6
char carray[23];
// char carray2[4] = a;
long long po [8];
po[0] = 1;
po[7] = po[0];
// po[8];

Pointers and Reference

  • address register
  • *, &
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int a = 10, b = 1;
int* aptr = NULL;
// double * dptr = &a;
aptr = &a; // aptr -> a, a = 10, b = 1
*aptr = b; // aptr -> a, a = 1, b = 1
a = 2; // aptr -> a, a = 2, b = 1
b = 7; // aptr -> a, a = 2, b = 7
&a = b; // aptr -> old a, old a = 2, b(a) = 7;
printf("%d %d", a, *aptr); // 7 2

int * potato = NULL;
if(potato == NULL)
    printf("hi");

potato = (int *)malloc(sizeof(int) * 127);

if(potato == NULL)
    printf("Memory allocation failed.\n");

potato = NULL;
if(potato != NULL)
    free(potato);

potato[4] = 4; // *(potato + 4) = 4;

Operators

  1. =, assignment, double d = 3.1415926;
  2. + - * / %, basic arithmetic, e = d / 12;
  3. < > ==, comparisons, a > b
  4. && ||, logic operations, a && b || c
  5. & | ^ >> <<, bit operations, a & 1 === a % 2 == 1
  6. (), priority, a || b && c vs (a || b) && c
  7. ++, --, increment & decrement by 1, ++a vs a++
  8. Precedence

Conditional control

  • 0 means False, non-0 means True
  1. if, else
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
if(1)
{
    printf("Potato");
}
else
{
    printf("P04A40");
}

int a = 0;
if(a++)
{
    printf("Smashed potato.");
}
  1. switch case
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
int a = 32, b = 0;

switch (a) {
    case 32:
        printf("32 potatos");
    case 0:
        printf("Void");
        break;
    default:
        printf("Frog");
}

Loops

  1. for
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
int i = 1;
for(i -= 1; i <= INT_MAX; i++)
{
    printf("%d", i);
}

float a[10];
float * b = (float *)malloc(sizeof(float) * 10);
float* a_end = a + 10;

for(float* a_ptr = a; a < a_end; a++)
    printf("%f ", *a_ptr);

// deferences between array and pointer
// C++ only
for(auto x : a)
    printf("%f ", x);
/*
for(auto x : b)
    printf("%f ", x);
*/
  1. while

1
2
while(True)
{}
1
2
3
int x = 10;
while(x--)
{}

Functions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <stdlib.h>

int potato(int frog)
{
    int potato2;
    frog = 11;
    return 0;
}

int main()
{
    int frog = 1;
    int potato2;
    potato(frog);
    printf("%d\n", frog);
    return 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
#include <stdio.h>
#include <stdlib.h>

int potato()
{
    // int frog = 11;
    // return frog;
    return 11;
}

void potato2(int * frog1, int * frog2)
{
    *frog1 = 11;
    *frog2 = 12;
    return;
}

int main()
{
    int frog = 1;
    frog = potato();
    printf("%d\n", frog);

    frog = 1;
    int frogg = 2;
    potato2(&frog, &frogg);
    printf("%d %d\n", frog, frogg);
    return 0;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <stdlib.h>

int potato(int ** frog)
{
    // int a[1];
    // int * aptr = (int *)malloc(sizeof(int));
    *frog = (int *)malloc(sizeof(int));
    (*frog)[0] = -1; // *(frog + 0) = -1;
    return 0;
}

int main()
{
    int * frog = NULL;
    potato(&frog);
    printf("%d\n", frog[0]);
    return 0;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int potato(int &frog) // int &frog(potato function) = frog(main function)
{
    frog = 11;
    return 0;
}

int main()
{
    int frog = 1;
    potato(frog);
    cout << frog << "\n";
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;


void swap(int &potato, int &frog)
{
    int s = potato;
    potato = frog;
    frog = s;
    return;
}

int main()
{
    int potato, frog;
    cin >> potato >> frog;
    swap(potato, frog);
    cout << potato << frog << "\n";
}
  1. Recursion

Scope

  • global
  • local
  • {}

User-defined Types

  • typedef
  • struct
1
2
3
4
5
6
typedef long long ll;

typedef struct potato{
int frog;
potato* next;
} potato;
 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
struct potato {
int frog;
potato* next;
};

potato potato1, potato2;
potato1.frog = 1;
potato1.next = &potato2;
potato2.frog = 2;
potato2.next = &potato1;

potato * potato_ptr;
potato_ptr -> frog = potato1.frog;
potato_ptr -> next = potato1.next;

for(int i = 0; i < 10; i++)
{
    cout << potato_ptr -> frog << " ";
    potato_ptr -> frog = (potato1 -> next) -> frog;
    potato_ptr -> next = (potato1 -> next) -> next;
}

potato * potato3, * potato4;
potato3 -> frog = 3;
potato3 -> next = potato4;
potato4 -> frog = 4;
potato4 -> next = potato3;

potato_ptr = potato3;

for(int i = 0; i < 10; i++)
{
    cout << potato_ptr -> frog << " ";
    potato_ptr = potato_ptr -> next;
}

Preprocessors

 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
#include <stdlib.h>
#include "user-defined.h"

#define potato frog

int x = potato; // int x = frog;

#define multiply(potato2, frog2) (potato2 * frog2)

int y, z;

x = multiply(y, z); // x = (y * z);

#undef potato

x = potato; // x = potato

#if 1 == 2
    printf("OMG!\n");
#elif 2 == 3
    printf("LOL\n");
#else
    printf("LGTM!\n");
#endif

#ifndef potato
    #define potato
    #include "potato.h"
#endif

Qualifiers

  • const: forbids assignement

    1
    2
    3
    4
    5
    6
    7
    
    int a = 10;
    const int b = a;
    int * ptr1 = a;
    const int * ptr2 = a;
    int const * ptr3 = a;
    int * const ptr4 = a;
    const int * const ptr5 = a;
    

  • static

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    foo()
    {
        static int i = 0;
        ++i;
    }
    bar()
    {
        int i = 0;
        ++i;
    }
    

  • volatile