basics
all c++ programs must have a main() function
Comments
// Single line comment
/* This is a multi-line comment.
This line will be ignored.
So will this one. */Initialization
double d = 1.7789856453427678;
int a{d}; //Compile time error - value of d will not fit in a
int a(d); //ok - a is 1
int a = d; //ok - a is 1Why so many ways of initialization?
It is an historical question. The first one is int i=0;. This one is from the early C language from the 1970's.
Next first C++ versions introduced a function like initialization syntax which writes here int i(0);.
But because of the most vexing parse ambiguities, the curly braces initialization was invented.
And for compatibility reasons, all those syntaxes are still valid...
https://stackoverflow.com/questions/62756177/why-have-different-ways-of-variable-initialization-in-c
Printing
std::cout
std::cout<< is a insertion operator
std::endl vs
std::endl vs std::endl ❌
Prefer '\n' over std::endl when outputting text to the console because std::end moves cursor to next line + flushes buffer
✅
Buffering & flushing
std::cout is buffered , meaning a buffer stores all cout until it is full then it will print the output all together into the terminal. increase efficiency as less operations to terminal.
Use std::flush to manually flush out the buffer to ensure immediate display
Last updated