1
Posted November 20, 2009 by Spyros in C/C++ Programming
 
 

2. C++ Variables and Constants

C++
C++

In the C++ programming language, we use input data which, under the proper editing, return the awaited results as output data. These data must be stored in one or more memory places to be able to use them in the process of programming. Those different parts of memory where data is stored, represented by a name, are split into two different categories, variables and constants. Of course, there more than that, like pointers or structs, but these two are the main containers used and discussed in this post. The difference between them is that the variables can alter their defined values while constants get only one immutable (not changing) value while the program is executed and till it reaches the end. For that reason, for each variable, there should be a definition on how many memory positions they occupy and also the range of the values that they can contain. Hence, the size of a variable depends on the type that we define for it. The three most popular c++ variable types are :

1.Probably the most used variable type ever is the integer variable(int). As its name implies, it can store integer values. These variables are perfect in situations where we need to store, say, a counter for a while or for repetitive command (we will talk about them in a later post). Notice the example below :


#include <iostream.h>

void main()
{

|----variable type

|    |-------------the name of the variable, representing a certain memory place

int m=6; <---------initialization of the variable (getting a value)

cout<<m;

}

2. One more important variable type is the one used for storing float numbers. This variable type occupies about 4-10 bytes of memory. In these variables we can store whichever number we want to. For instance :


#include <iostream.h>

void main()
{

float m=356.5454;

cout<<m;

}

3. The character variables(char). In them, we can store characters, like a name. Actually, a char variable stores just one character but a char array is able to store one or more characters forming a string. The char variables occupy just one byte of memory. We can initialize these variables with a number that corresponds to a certain ASCII character or the actual character itself in quotes like :


#include <iostream.h>

void main()
{

char m=’A’;   // or "char m=65;"

cout<<m;

}

The Constants

The constants, as stated above, have a certain and non changing value throughout the whole program execution. Most times, we tend to declare them right above and outside of the main function. The value that the constant value gets when initialized can never be changed after that. To declare one, there are actually some different ways, some of which are :

1. Using const like :


#include <iostream.h>

|------constant declaration

|       |------------- type of constant, like we do in variables

const int N=50;

void main()
{

cout<<Ν;

}

2. Using  #define, as a macro :


#include <iostream.h>

#define N 10

void main()
{

cout<<Ν;

}

Since the speed of the program execution and the occupied memory plays an important role, there are some constraints on the types of variables, so that the memory positions can be precisely calculated for each variable. This post will end with an array indicating the utmost number of bytes for each different variable type.

Type Bytes Other names Range of Values
int * Signed,signed int Depends on the computer
Unsigned int * Unsigned Depends on the computer
_int8 1 Char,signed char -128 to 127
_int16 2 Short,short int,signed short int -32,768 to 32,767
_int32 4 Signed,signed int -2,147,483,648 to 2,147,483,647
_int64 8 None -9,223,372,036,854,775,808 to
Char 1 Signed char -128 to 127
Unsigned char 1 None 0 to 255
Short 2 Short int,signed short int -32,768 to 32,767
Unsigned short 2 Unsigned short int 0 to 65,535
Long 4 Long int,signed long int -2,147,483,648 to 2,147,483,647
Unsigned long 4 Unsigned long int 0 to 4,294,967,295
Enum * None Like integers
Float 4 None 3.4Ε +/- 38 (7 digits)
Double 8 None 1.7E +/- 308 (15 digits)
Long double 10 none 1.2E +/- 4932 (19 digits)

Make sure that you also read the first part, 1. Your First C++ Program

This is a guest post by a good personal friend of mine under the name Black Shadow.


Spyros