C++ syntax refers to the rules that define how a C++ program is written and interpreted by the compiler. Understanding basic syntax is essential for writing correct and readable code.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
#include <iostream>
: Includes the standard input-output stream library.using namespace std;
: Lets you use standard C++ classes/functions without the std::
prefix.int main()
: The entry point of every C++ program.cout
: Used for printing output to the console.return 0;
: Ends the program and returns 0 to the system.Each statement in C++ ends with a semicolon (;
). Code blocks are enclosed in curly braces {}
.
// This is a single-line comment
/*
This is a
multi-line comment
*/
C++ is case-sensitive. For example, main
and Main
are treated as different identifiers.
Whitespace (spaces, tabs, newlines) is ignored by the compiler but should be used to improve code readability.