C++ Basic Syntax

Let us look at the important terms and syntax before jumping into the internals of C++ programming.

Tokens

Tokens are the smallest meaningful symbols in the language. In other words, tokens are the minimals chunk of program that have meaning to the compiler. The following table lists all tokens used in C++ programming.

Token Type Description/ Purpose Example
keywords Words withspecial meaning to the compiler
 
int, double, for, auto
identifiers Names of things that are not built into the language
 

cout, std, x, myFunction

Literals Basic constant values whose value is specified directly in the source code
 
"Hello, world!", 24.3, 0, ’c’
Operators Mathematical or logical operations
 
+, -, &&, %, <<
Punctuation/Separators

Punctuation defining the structure of a program

{ } ( ) , ;
Whitespace Spaces of various sorts; ignored by the compiler
 
Spaces, tabs, newlines, comments

Here is complete list of keyword in C++.

 

Escape sequences

An escape sequence is a symbol used to represent a special character in a text literal. Here are all the C++ escape sequences which you can include in strings that you want to manipulate:

Escape Sequence Represented Character
\a System bell (beep sound)
\b Backspace
\f Formfeed (page break)
\n Newline (line break)
\r “Carriage return” (returns cursor to start of line)
\\ Backslash
\t Tab
\' Single quote character
\" Double quote character
\some integer x The character represented by x

 

Comments

Comments help the person writing a program, and anyone else who must read the source file, understand what’s going on. The compiler ignores comments, so they do not add to the file size or execution time of the executable program.

Syntax
  • Single line comments start with a double slash symbol (//) and terminate at the end of the line. A comment can start at the beginning of the line or on the same line following a program statement. 
// this is single line comment
  • Multiline comments begins with the /* character pair and ends with */ (not with the end of the line).
/* this
is a
very
very long
multiline
comment
*/

/* this can also be used in single line comment */

 

Directives

Directives are not part of the basic C++ language, but they’re necessary anyway. For example, in "Helloworld" program presented in earlier post, the two lines after comment are directives. The first is a preprocessor directive, and the second is a using directive.

A preprocessor directive is an instruction to the compiler. For example, in preprocessor directive 

#include <iostream>

#include tells the preprocessor to include the contents of another file, here the iostream file, which defines the procedures for input/output.

Before going with using directive, lets look at namespace stuff. A namespace is a part of the program in which certain names are recognized; outside of the namespace they’re unknown. A C++ program can be divided into different namespaces.

If we want to access an identifier defined in a namespace, we tell the compiler to look for it in that namespace using the scope resolution operator (::) as

std::cout << "Hello, world!\n";

or, we can use using directive as

using namespace std;

which says that all the program statements that follow are within the std namespace.

 

C++ Introduction
C++ Operators