C++ Syntax
C++ syntax rules.
C++ Syntax
A C++ program is a collection of commands or statements.
Let's break down the code of a simple "Hello World" program to understand its structure:
The #include <iostream> Header
#include <iostream>: This is a header file library that lets us work with input and output objects, such as cout. Header files add functionality to C++ programs.
The std Namespace
using namespace std: This means that we can use names for objects and variables from the standard library without prefixing them with std::.
The main() Function
int main(): This is called a function. Any code inside its curly brackets {} will be executed. Every C++ program must have a main() function.
Output with cout
cout: (pronounced "see-out") is an object used together with the insertion operator (<<) to output/print text.
Return 0
return 0: ends the main function. A value of 0 typically indicates successful execution.
Note: Every C++ statement ends with a semicolon (;). Omitting it is a common syntax error!
Note: The body of int main() could also be written as: int main () { cout << "Hello World! "; return 0; }. However, writing one statement per line makes the code more readable.