DEV Community

Cover image for Variables, Datatypes and Storage of C++
Zaynul Abedin Miah
Zaynul Abedin Miah

Posted on

Variables, Datatypes and Storage of C++

Variable name: A label for memory location.
Value: The something that would be stored in a variable.
Storage: A place where data can be stored.
Declaration: Announcing a variable at the (usually) beginning of a program.
Naming Convention: A set of rules about the names of the variables.
Assignment: assigning value to a variable.

Naming Variables

  • All variable names must begin with a letter of the alphabet or an underscore(_).

  • After the first initial letter, variable names can also contain letters and numbers.

  • C++ is a strongly typed language. Variable names are case sensitive.

//Valid Names
double simple_interest;

int student_age;
float Student_percentile;
int Zayn123;

//Invalid Names
int 123_age;


Enter fullscreen mode Exit fullscreen mode

Intialisation

  • Variables when just declared have garbage value until they are assigned a value for the first time.

  • We can assign a specific value from the moment variable is declared, called as initialization of variable.

//Initialisation of variable
float a = 10;

//Declaration
int b;

//Assignment 
b = 20;
Enter fullscreen mode Exit fullscreen mode

Data Types

Boolean - boolean
Character - char
Integer - int
Floating point - float
Double Floating Point - double

#include<iostream>
#include<iomanip>
using namespace std
bool x = True;
bool isWeatherRainy = False;

cout<< x <<endl;
cout<< isWeatherRainy <<endl;

int y = 5128;

float pi = 3.14159265;
double pi = 3.14159265;

cout<< y<<endl;
cout<< setprecision(10)<<pi<<endl;
cout<< setprecsion(10)<<pi_d<<endl;

//Char
char letter = 'A';
char dollar = '$';
cout << letter <<endl;
cout << dollar <<endl;
Enter fullscreen mode Exit fullscreen mode

return 0;

Output:
1
0
5128
3.141591785
3.14159265
A
$

sizeof(int): You can use in built function sizeof() to determine the size of data they are storing.

Binary Number System

One of the four kinds of number systems is the binary number system, which is used to say what a number is in a binary system. A binary number system uses only 0 (zero) and 1 (one) to represent a number (one). "bi" means "two" in the word "binary."

Binary to Decimal Conversion
The process of converting a binary number to decimal involves multiplying each binary digit by the appropriate power of 1 or 0 in order to reach the corresponding power of 2. Consider that n is the number of digits in a binary number, The Conversion From Binary to Decimal B = an-1…a3a2a1a0. Now, the corresponding decimal number is given as D= (an-1 × 2n-1) +…+(a3 × 23) + (a2 × 22) + (a1 × 21) + (a0 × 20).

Let us go through an example to understand the concept better.

Example: Convert (10011)2 to a decimal number.

Solution:

`The given binary number is (10011)2.

(10011)2 = (1 × 24) + (0 × 23) + (0 × 22) + (1 × 21) + (1 × 20)

= 16 + 0 + 0 + 2 + 1 = (19)10

Hence, the binary number (10011)2 is expressed as (19)10.`

Decimal to Binary Conversion
A comparable binary representation exists for every decimal number. These binary digits have programming and coding uses in computers. The reason for this is that human beings have no way of decoding the binary digits of 0 and 1.

Image description
You can use several ways, such as a formula, the division method, and so on, to convert decimal integers to binary. Substitute the remaining formula here. Decimal to binary conversion involves the following steps:

Step 1. Divide the integer by 2, while noting the quotient and remainder.

Step 2. Then, divide the quotient again by 2, and record the 3rd quotient and remainder.

Step 3. Like this, keep dividing each successive quotient by 2 until you get a quotient of zero.

Step 4. After this, just write all the remainders in reverse order to get the binary representation of the integer.

It's a tradeoff, but signed integers have a better negative range than unsigned integers. However, if an integer has a minimum and maximum range, it doesn't matter if it's signed or unsigned.
Data Type Memory (bytes)


  1. short int: 2 bytes 16 bits
  2. unsigned short int: 2 bytes 16 bits
  3. unsigned int: 4 bytes 32 bits
  4. int: 4 bytes
  5. long int: 4 bytes 32 bits
  6. unsigned long int: 4 bytes 64 bits
  7. long long int: 8 bytes 16 bits
  8. unsigned long long int: 8 bytes 64 bits
  9. unsigned char: 1 bytes 8 bits
  10. float: 4 bytes 32 bits
  11. double: 8 bytes 64 bits
  12. long double: 12 bytes

Storage of Integers
The hardware for doing addition is known as adder. It is built inside the processor.

Image description
The number that is on the carry is later being discarded.

if the first bit is 1
The negetive numbers are stored as 2s compliment form.
2’s Complement Method: Positive numbers are represented in the same way as they are represented in sign magnitude method. If the number is negative then it is represented using 2’s complement. First represent the number with positive sign and then take 2’s complement of that number.

Example: Let we are using 5 bits registers. The representation of -5 and +5 will be as follows:

+5 is represented as it is represented in sign magnitude method. -5 is represented using the following steps:

(i) +5 = 0 0101

(ii) Take 2’s complement of 0 0101 and that is 1 1011. MSB is 1 which indicates that number is negative.

MSB is always 1 in case of negative numbers.
To negate a number simply add 1 to it's 2s compliment.

If the first bit of sign number is 0 then to get what the number is you simply interpret it as
Number=00001001
1001
2^0+2^1+2^2+2^3 then add 1 you will get 9.
And with unsigned number you simply combine the powers of 2 to get what the number is.

Range of signed vs unsigned
Range of signed int: ((-2^32) - 0 - (2^31-1))
Range of unsigned int: (0 - (2^32-1))

Range of signed long: ((-2^63) - 0 - (2^63-1))
Range of unsigned long: (0 - (2^64-1))

*Storage of float and double
*

Float has comparatively lesser storage when compared to double. Float has 32 bits of storage, whereas double has 64 bits of storage. Float accepts decimals of up to 6 points.
Example: 23.75
2^4,2^3,2^2,2^1,2^0
16,8,4,2,1
As, 16+4+2+1=23
so, 10111

     .75
     2^-1,2^-2,2^-3,2^-4,2^-5 and so on
     0.5,0.25,0.125 and so on
     0.5+0.25=.75
     11000
Enter fullscreen mode Exit fullscreen mode

Image description

Storage of char
Char is a C++ data type designed for the storage of letters. Char is an abbreviation for an alphanumeric character (ASCII). It is an integral data type, meaning the value is stored as an integer. A char takes a memory size of 1 byte. It also stores a single character.

char bucket = 'A';
  cout << bucket << endl;

  int mybucket = bucket;
  cout << mybucket << endl;
Enter fullscreen mode Exit fullscreen mode

Output:
A
65

Macros VS Constant

A macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by #define directive.

The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.

Type Casting

Type casting refers to the conversion of one data type to another in a program. Typecasting can be done in two ways: automatically by the compiler and manually by the programmer or user. Type Casting is also known as Type Conversion.

For example, to find the sum of two variables, one of int type & other of float type. So, you need to type cast int variable to float to make them both float type for finding the sum. In this blog we will learn how to perform type conversion in C++.

In C++, there are two types of type conversion i.e. implicit type conversion & explicit type conversion.

Implicit Casting:
The word “implicit” means ‘understood’ or ‘embedded’. In implicit C++ type casting, the data type in which the value is to be converted is not specified in the program. It is automatically done by the C++ compiler.

Explicit Casting:
In explicit C++ type casting, the data type in which the value is to be converted is clearly specified in the program. It is done by cast operator. The cast operator is a unary operator. It converts the value of an expression into a value of the type specified.

Example:

#include<iostream>
using namespace std;

int main(){


      cout<<(float)5/3<<endl;
      char letter = 'A';
      cout << letter <<endl;
      cout << (char)(letter+1) <<endl;
      cout << (bool)5 + 1 <<endl;
return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:
1.66667
A
B
2

5 is converted to boolean which returns true so, 1+1=2

Top comments (0)