C++ Programming, Computer Science

C++ Statements&Declaration of Variables.

BEFORE: C++ Principles of Object-Oriented Programming

NEXT: C++ Types, Variables and Arithmetic


A C++ program is comprised of various components such as functions, methods, classes, etc. The instructions that form part of a C++ program typically reside inside of functions or methods. These functions are comprised of C++ statements. You will find yourself using various types of statements in your C++ code as listed here:

  • declarations – these are used to declare variables and constants that will be used in your application
  • assignments – these are used to assign values to variables in your application code
  • preprocessor directives – covered in the topic on Code Formatting
  • comments – used to document your code
  • function declarations – covered in the topic on Code Formatting
  • executable statements – these are used to perform operations and execute instructions. Examples would be cout << “Hello World!”; which outputs Hello World! to the console.

In C, all variables must be declare before they are used in executable statements. Their actual use appears elsewhere in the scope, sometimes far away. So, we should go back at the beginning of the program if we want to see its type or its initialization.

C++ allows the declaration of a variable anywhere in the scope. This means that a variable can be declared right at the place of its first use. This makes the program much easier to write or read, so that reducing the errors is more efficient.


A declaration is a statement that introduces a name into the program. It specifies a type for the named entity:

  • A type defines a set of possible values and a set of operations (for an object).
  • An object is some memory that holds a value of some type.
  • A value is a set of bits interpreted according to a type.
  • A variable is a named object.

We first look at the definition of scope and dynamic initialization of the variables.

Scope Resolution Operator.

C++ is a block-structured language. The same variable name can be used to have different meanings in different blocks. The scope of the variable extends from the point of its declaration till the end of the block containing the declaration. A variable declared inside a block is said to be local to that block. Consider the following segment of a program:



     int x = 20;







     int x = 10;




The two declaration of x to two different memory locations containing different values. Statements in the second block cannot to the variable x declared in the first block. Another style is as follows:

Block two is contained in the block one. The declaration inner inner block hides a declaration of the same variable in an outer block, therefore, each declaration of x causes it to refer a different data object. Within the inner block, the variable x will refer to the data object declared therein.

In C, the global version of a variable cannot be accessed from within the inner block. C++ resolves this problem by introducing a new operator :: called the scope resolution operator. This can be to uncover a hidden variable. It takes the following form:

This operator allows access to the global version of a variable. To illustrate the concepts presented so far, let’s see an example:

#include “stadfx.h”

#include <iostream>

using namespace std;

int m = 10; //global m

int main()


int m=20; //local m


int k = m;

int m = 30;//local m to inner block

cout << “We are in a inner block \n”;

cout << “k = ” << k << “\n”;

cout << “m = ” << m << “\n”;

cout << “::m = ” << :: m << “\n”;


cout << “\n We are in a outer block \n”;

cout << “m = ” << m << “\n”;

cout << “::m = ” << ::m << “\n”

return 0;



A major application of the scope resolution operator is in the classes to identify the class to which a member function belongs. This will be dealt in detail later where the classes are introduced.

In the previous example the operator int determines the type of the data as integer. This lead us to the definition of data types in C++.

Basic Data Types

Data types in C++ can be classified under various categories as shown in Figure 3.1:

Both C and C++ compilers support all the built-in data type (also called basic data types), which seize and range is shown as follows:

The type void has two normal uses: 1) to specify the return type of a function when it is not returning any value, and 2) to indicate an empty argument list to a function. Example:

void functl(void);

We will explain more about every type of data as we come into more complex examples.

BEFORE: C++ Principles of Object-Oriented Programming

NEXT: C++ Types, Variables and Arithmetic

Note: I recommend Visual Studio IDE (Interface Development Environment) , and after Introducción a C++ en Visual Studio.


  1. BalaguruswamyObjectOrientedProgrammingWithC++Fourth
  2. 2-Tour-Basics
  3. Curso  Module 1 Introducing C++  C++ Fundamentals


Review by: Larry Francis Obando – Technical Specialist – Educational Content Writer.

Escuela de Ingeniería Eléctrica de la Universidad Central de Venezuela, Caracas.

Escuela de Ingeniería Electrónica de la Universidad Simón Bolívar, Valle de Sartenejas.

Escuela de Turismo de la Universidad Simón Bolívar, Núcleo Litoral.

Contact: Caracas, Quito, Guayaquil, Cuenca – Telf. 00593998524011

WhatsApp: +593998524011

email: dademuchconnection@gmail.com

2 comentarios en “C++ Statements&Declaration of Variables.”


Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Salir /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Salir /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Salir /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Salir /  Cambiar )

Conectando a %s