The syntax of C++ is the set of rules defining how a C++ program is written and compiled. C++ syntax is largely inherited from the syntax of its ancestor language C, and has influenced the syntax of several later languages including but not limited to Java, C#, and Rust.
Basics
Much of C++'s syntax aligns with C syntax, as C++ provides backwards compatibility with C. The C++ "Hello, World!" program program is as follows:
Prior to C++23, the "Hello, world!" program used iostreams.
Identifier An identifier is the name of an element in the code. There are certain standard naming conventions to follow when selecting names for elements. Identifiers in C++ are case-sensitive. An identifier can contain:
Any Unicode character that is a letter (including numeric letters like Roman numerals) or digit. Currency sign (such as ¥). Connecting punctuation character (such as _). An identifier cannot:
Start with a digit. Be equal to a reserved keyword, null literal or Boolean literal. The identifier nullptr is not a reserved word, but is a global constant that refers to a null pointer literal. Similarly, the words true and false refer to the Boolean values true and false respectively. Since C++26, the identifier of just an underscore (_) acts as an unnamed placeholder; it can be reused in multiple declarations in the same scope (like auto [_, username, _] = getUserData();), and suppresses compiler warnings of no use (equivalent to the [[maybe_unused]] attribute); trying to read and write to _ later is ill-formed.
Keywords
Keywords The following words may not be used as identifier names or redefined, of which there are 80.
The keyword restrict, introduced in C99, is a type qualifier used to provide information about pointer aliasing to the compiler. It is not part of the C++ standard, although some C++ compilers provide it as a language extension. The keyword fortran is a conditionally supported keyword in C. It is a type specifier used to indicate Fortran language linkage for function declarations and other external declarations, allowing implementations to provide calling conventions and name mangling suitable for interoperability with Fortran code. C++ does not define fortran as a standard keyword (unlike asm); however, C++ implementations may conditionally support language linkage specifications for languages other than C and C++, including Fortran.
Reserved words for literal values The following words refer to literal values used by the language, of which there are 3.
Alternative operator keywords The following words are reserved keywords, but are used as alternative spellings for operators and tokens that use non-ISO646 characters, of which there are 11.
Identifiers with special meaning The following words may be used as identifier names, but bear special meanings in certain contexts, of which there are 7.
Preprocessor directives The following tokens are recognised by the preprocessor in the context of preprocessor directives, of which there are 20.
The following macros are defined in the C/C++ standard library:
NULL (expands to (void*)0, used prior to the introduction of nullptr to represent a null pointer) NDEBUG (a macro meaning "no-debug", used primarily to disable assert() if defined) assert() (a macro with arguments, which terminates the program if the argument evaluates to false, 0, or nullptr, and can be disabled by NDEBUG) __COUNTER__ (a macro time such that when used, the preprocessor expands it into a unique sequential integer literal, starting from 0 and increments by 1 every time it is expanded)
Reserved namespaces The C++ standard reserves the namespaces std (for C++ standard library symbols) and posix (unused, but reserved for ISO/IEC 9945 and POSIX-related symbols). It is undefined behaviour to add declarations or definitions to these namespaces, with the exception of adding template specialisations to symbols in namespace std. The C++ standard further reserves the module names matching std and std.*.
Code blocks The separators { and } signify a code block and a new scope. Class members and the body of a method are examples of what can live inside these braces in various contexts. Inside of method bodies, braces may be used to create new scopes, as follows:
Comments C++ has two kinds of comments: traditional comments and end-of-line comments. Traditional comments, also known as block comments, start with /* and end with */, they may span across multiple lines.
End-of-line comments start with // and extend to the end of the current line.
Documentation comments in the source files are processed by the external Doxygen tool to generate documentation. This type of comment is identical to traditional comments, except it starts with /** and follows conventions defined by the Doxygen tool. Technically, these comments are a special kind of traditional comment and they are not specifically defined in the language specification.
Command-line arguments Much like in C, the parameters given on a command line are passed to a C++ program with two predefined variables - the count of the command-line arguments in argc and the individual arguments as character strings in the pointer array argv. So the command:
myFilt p1 p2 p3
results in something like:
While individual strings are arrays of contiguous characters, there is no guarantee that the strings are stored as a contiguous group. The name of the program, argv[0], may be useful when printing diagnostic messages or for making one binary serve multiple purposes. The individual values of the parameters may be accessed with argv[1], argv[2], and argv[3], as shown in the following program:
Objects
C++ introduces object-oriented programming (OOP) features to C. It offers classes, which provide the four features commonly present in OOP (and some non-OOP) languages: abstraction, encapsulation, inheritance, and polymorphism. One distinguishing feature of C++ classes compared to classes in other programming languages is support for deterministic destructors, which in turn provide support for the Resource Acquisition is Initialization (RAII) concept.
Object storage As in C, C++ supports four types of memory management: static storage duration objects, thread storage duration objects, automatic storage duration objects, and dynamic storage duration objects.
… excerpt ends here. Continue reading the full article.


