C++ Basic Syntax

In this tutorial you will learn about the C++ Basic Syntax and its application with practical example.

C++ Basic Syntax

Comments in C++ Program

C++ Comments are a set of statements that are not executed by the C++ compiler. The use of comments makes it easy for humans to understand the source code. Usually comments gives you inside or explanation about the variable, method, class or any statement that exists in source code. The comment statements are ignored during the execution of the program.

C++ Single-line Comments:-

A ‘//’ (double forward slash) is used to specify a single line comment, which extends up to the newline character.

C++ Multi-line Comments:-

If you want to comment multiple lines then you can do it using /* and */, everything in between from /* to */ is ignored by the compiler-

C++ Tokens

In C++, a program is consists of various tokens. In C++, tokens are smallest individual units can either be a keyword, an identifier, a constant, a string literal, or a symbol. In C++, we have five types of tokens as following –

  • Keywords
  • Identifiers
  • Constants
  • Punctuators
  • Operators

C++ Punctuators

In C++, a punctuator is a token used to separate two individual tokens used in a c++ program. It is also called as separators. In C++, we have the brace { and }, parenthesis ( and ), and brackets [ and ], comma (,), semicolon (;), asterisk (*), colon (:), number sign #(hash) as punctuators.

C++ Semicolon

The semicolon is a statement terminator, that is each of the individual statement must be ended with a semicolon. In C++, it is mandatory to put a semicolon (;) at the end of each of the executable statement. Otherwise, the compiler will raise a syntax error.

Curly Braces In C++

In C++, an opening and closing curly braces is used to group all of the statements in a block.

Block in C++

Block is a set of statement grouped together inside opening and closing braces.


C++ Special Characters

Character Name Description
// Double slash Marks the beginning of a comment.
# Pound sign Marks the beginning of a preprocessor directive.
< > Opening and closing brackets Encloses a filename when used with the #include directive.
( ) Opening and closing parentheses Used in naming a function, as in int main().
{ } Opening and closing braces Encloses a group of statements, such as the contents of a function.
” “ Opening and closing quotation marks Encloses a string of characters, such as a message that is to be printed on the screen.
; Semicolon Marks the end of a complete programming statement.

Whitespace in C++

Whitespace is a term used to refer all blanks, tabs, newline characters. Whitespace is used to separate one part of statement from another, whitespace is usually ignored by compiler.

Escape sequences in C++

Escape sequence is a special string comprise a backslash (\) followed by a letter or by a combination of digits used to control output on monitor. Escape sequences are typically used to represent actions such as newline,carriage returns,tab movements and non printing characters over the monitor. The following table lists the common ANSI escape sequences and their meaning.

Escape Sequence Represents
\a Bell (alert)
\b Backspace
\f Formfeed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\’ Single quotation mark
\ " Double quotation mark
\\ Backslash
\? Literal question mark
\ ooo ASCII character in octal notation
\x hh ASCII character in hexadecimal notation
\x hhhh Unicode character in hexadecimal notation if this escape sequence is used in a wide-character constant or a Unicode string literal.

In this tutorial we have learn about the C++ Basic Syntax and its application with practical example. I hope you will like this tutorial.