In C++, void means “no type”. It’s commonly used to declare functions that don’t return a value, functions that take no parameters
Here, greet() performs an action but doesn’t return anything.
In C++, the statement return 0; is most commonly seen in the main() function.
Return value of main():
By convention, returning 0 from main() indicates that the program executed successfully.
Any non-zero value usually signals an error or abnormal termination.
If you omit return 0; in main(), modern C++ standards (C++11 and later) automatically assume return 0; at the end of main().
What \n Does
- It tells the program to move the cursor to the next line when printing text.
- Commonly used in
std::coutstatements to format output.
x += 3; // equivalent to: x = x + 3;
In modern C++ (since C++11), the keyword auto tells the compiler to automatically deduce the type of a variable or function return value from its initializer or expression.
int i {}; // i is initialized to 0
Explanation
- This is uniform initialization (introduced in C++11).
- The curly braces
{}tell the compiler to value-initialize the variable. - For fundamental types like
int, value-initialization sets it to zero. - while (std::cin >> score && score != -1)
- && (Logical AND)
- Both conditions must be true for the loop to continue;
- Input must succeed.
- The value must not be
-1.
沒有留言:
發佈留言