#include <iostream>
int main()
{
int variable = 8;
std::cout << variable << std::endl;
variable = 20;
std::cout << variable << std::endl;
std::cin.get();
}
data type: char(1), short(2), int(4), long(4), long long(8). They can also be combined with unsigned.
char will treat number as a character.
for decimal value: float(4), (long) double(8).
float var1 = 5.5f;
double var2 = 5.2;
bool: boolean can be true(1) or false(0).
sizeof(): to show how big a data type is.
sizeof(bool);
Functions: prevent code duplication.
Head files: store function definition and declaration.
#pragma once: make sure the content is only copied once, prevent redefinition(duplicate error).
#include: <> for compiler to search the file, "" for files in current folder.
C standard library: usually has .h suffix. C++ does not.
set break points
Conditions or branches will affect performance. Using mathmatical calculation instead of comparison.
The following code will print hello!.
#include <iostream>
int main()
{
const char* ptr = "hello!";
// const char* ptr = nullptr;
if (ptr)
{
std::cout << ptr << std::endl;
}
else
{
std::cout << "ptr is null!" <<std::endl;
}
}
for loops and while loops
#include <iostream>
int main()
{
for (int i = 0; i < 5; i++)
{
std::cout << "Hello world!" << std::endl;
}
int i = 0;
std::cout << "======================" << std::endl;
while (i < 5)
{
std::cout << "Hello world!" << std::endl;
i++;
}
std::cin.get();
}
do while: code in loop will be run at least once.
Used with loops
- continue: go to the next iteration of the loop.
- break: end the loop. Also used in switch.
- return: exit the function.
example
#include <iostream>
#define LOG(x) std::cout << x << std::endl
int main()
{
char* buffer = new char[8];
// allocate an 8-byte memory, buffer is a pointer that points to the beginning of the memory
memset(buffer, 0, 8); // 3 parameter: a pointer, a value, a size.
char** ptr = &buffer; // pointer to pointer
delete[] buffer; //
std::cin.get();
}
The result of the following example is 2.
#include <iostream>
#define LOG(x) std::cout << x << std::endl
int main()
{
int a = 5;
int& ref = a;
ref = 2;
LOG(ref);
}
Pass the reference of variables that will influence the variable.
- use pointer: result 6
#include <iostream>
#define LOG(x) std::cout << x << std::endl
void Increment(int* value)
{
(*value)++;
}
int main()
{
int a = 5;
Increment(&a);
LOG(a);
}
- use reference(syntax sugar): result 6
#include <iostream>
#define LOG(x) std::cout << x << std::endl
void Increment(int& value)
{
value++;
}
int main()
{
int a = 5;
Increment(a);
LOG(a);
}