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.
1 2 |
// It prints Hello, World! cout<<"Hello World!"; |
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-
1 2 3 |
/* It prints Hello World! */ cout<<"Hello World!"; |
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.
Example:-
1 2 3 4 5 |
{//start of block //block of statement(s) }//end of block |
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.