C++ Basics

Before writing C++ programs, you should understand the basic building blocks of the language. These concepts are known as C++ Basics. They help you understand how a C++ program is written and how the compiler reads your code.

In this tutorial, we will learn about C++ Tokens, C++ Keywords, C++ Identifiers, constants, literals, namespaces, the scope resolution operator, and preprocessor directives.


C++ Tokens

Whenever you write a C++ program, you use different elements such as variable names, numbers, operators, and keywords. These small individual parts are called tokens.

You can think of tokens as the words that make up a sentence. Just as a sentence is made of words, a C++ program is made of tokens.

Example

int age = 20;

The above statement contains the following tokens:

  • int → Keyword
  • age → Identifier
  • = → Operator
  • 20 → Constant
  • ; → Special Symbol

Without tokens, the compiler cannot understand your program.


C++ Keywords

C++ Keywords are reserved words that already have a special meaning in C++. The compiler recognizes these words and uses them for specific purposes.

Since keywords already have predefined meanings, you cannot use them as variable or function names.

Some common keywords are:

int
float
char
if
else
for
while
return
class

Example

int marks = 95;

Here, int is a keyword. It tells the compiler that the variable marks will store an integer value.

❌ This is not allowed:

int int = 10;

Here, int is used as a variable name, which will produce an error because it is a keyword.


C++ Identifiers

Identifiers are the names you give to variables, functions, classes, and other user-defined items in a program.

For example:

int age;
float salary;

In this example, age and salary are identifiers.

Rules for Naming Identifiers

  • It can contain letters, digits, and underscores (_).
  • It must not start with a number.
  • It cannot contain spaces.
  • It cannot be a C++ keyword.
  • C++ is case-sensitive, so Age and age are different identifiers.

Valid Identifiers

student
rollNumber
marks_1
totalMarks

Invalid Identifiers

1student
student name
int
total%

A meaningful identifier makes your program easier to read and understand.


Constants

A constant is a value that cannot be changed after it is created.

For example, the value of π (Pi) is always the same, so it is better to store it as a constant.

const float PI = 3.14159;

If you try to change the value of PI later in the program, the compiler will show an error.

Constants are useful when a value should remain fixed throughout the program.


Literals

Literals are fixed values written directly in the program.

For example:

10
3.14
'A'
"CoderMantra"
true

Each of these is a literal because the value is written directly instead of being stored in a variable.

Example

int age = 20;

Here:

  • age is an identifier.
  • 20 is an integer literal.

Similarly,

cout << "Welcome";

"Welcome" is a string literal.


Scope Resolution Operator (::)

The scope resolution operator is represented by ::.

It is mainly used to tell the compiler where a variable or function belongs.

One of the most common places where you will see it is with cout.

std::cout << "Hello";

Here:

  • std is the namespace.
  • :: is the scope resolution operator.
  • cout is used to display output.

The operator simply tells the compiler to use cout from the std namespace.


C++ Namespace

A C++ Namespace is used to group similar code together and avoid name conflicts.

The C++ Standard Library is stored inside a namespace called std.

That is why beginners usually write:

std::cout << "Hello";

Instead of writing std:: every time, you can write:

using namespace std;

Now you can simply write:

cout << "Hello";

Using namespaces keeps large programs organized and prevents confusion when different libraries have functions with the same name.


Preprocessor Directives

Preprocessor directives are instructions that are processed before the program is compiled. Every preprocessor directive starts with the # symbol.

The most commonly used directive is:

#include <iostream>

The #include directive tells the compiler to include the iostream header file before compiling the program. This header file is required if you want to use cout and cin.

Another common directive is:

#define PI 3.14159

It creates a symbolic constant that can be used in different parts of the program.

Preprocessor directives help prepare the program before the actual compilation begins.


Now that you have learned these C++ Basics, you are ready to move on to variables, data types, operators, and other important concepts. These fundamentals will make it much easier to understand and write C++ programs.


Frequently Asked Questions (FAQs)

Q1. What are tokens in C++?

C++ Tokens are the smallest individual elements of a C++ program. They include keywords, identifiers, literals, operators, and special symbols that the compiler recognizes while processing the source code.

Q2. What is the difference between C++ keywords and identifiers?

C++ Keywords are reserved words with predefined meanings, such as int, if, and return, whereas C++ Identifiers are user-defined names used for variables, functions, classes, and other program elements. Identifiers cannot use reserved keywords as names.

Q3. What is a namespace in C++, and why is it used?

A C++ Namespace is used to organize code and prevent naming conflicts between variables, functions, and classes. The std namespace, for example, contains many standard library components like cout and cin.

Q4. What is the scope resolution operator (::) in C++?

The scope resolution operator (::) is used to access members of a namespace or class and to define class member functions outside the class definition. It also helps distinguish between local and global variables when they share the same name.

Q5. What is the purpose of preprocessor directives in C++?

Preprocessor directives are instructions that are processed before compilation begins. They are commonly used to include header files (#include), define macros (#define), and control conditional compilation.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top