cft

Variables and Data Types in C++ : Part 2

Part 2 in C++ series


user

Lubna Fathima

a year ago | 4 min read

Introduction to Variables

A variable is a storage place that has some memory allocated to it. It is used to store some form of data. Different types of variables require different amounts of memory.

Variable Declaration

In C++, we can declare variables as follows:

● data_type: Type of the data that can be stored in this variable. It can be int, float, double, etc.

● variable_name: Name given to the variable.

data_type variable_name;

Example: int x;

In this way, we can only create a variable in the memory location. Currently, it doesn’t have any value. We can assign the value in this variable by using two ways:

● By using variable initialization.

● By taking input

Here, we can discuss only the first way, i.e., variable initialization. We will discuss the second way later.

data_type variable_name=value;

Example: int x = 20;

Rules for defining variables in C++

● You can’t begin with a number. Ex- 9a can't be a variable, but a9 can be a variable. ● Spaces and special characters except for underscore(_) are not allowed.

● C++ keywords (reserved words) must not be used as a variable name.

● C++ is case-sensitive, meaning a variable with the name ‘A’ is different from a variable with the name ‘a’. (Difference in the upper-case and lower-case holds true).

C++ Keywords

All variables use data-type during declaration to restrict the type of data to be stored. Therefore, we can say that data types tell the variables the type of data they can store. Pre-defined data types available in C++ are:

● int: Integer value

● unsigned int: Can store only positive integers

● float, double: Decimal number

● char: Character values (including special characters)

● unsigned char: Character values

● bool: Boolean values (true or false)

● long: Contains integer values but with the larger size

● unsigned long: Contains large positive integers or 0

● short: Contains integer values but with smaller size

Table for datatype and its size in C++:

(This can vary from compiler to compiler and system to system depending on the version you are using)Examples:

int price = 5000;// Integer (whole number)

float interestRate = 5.99f; // Floating point number

char myLetter = 'D'; // Character

bool isPossible = true; // Boolean

string myText = "Coding Ninjas"; // String

auto keyword in c++

The auto keyword specifies that the type of the declared variable will automatically be deduced from its initializer.

It would set the variable type to initialize that variable’s value type or set the function return type as the value to be returned. Example:

auto a = 11 // will set the variable a as

int typeauto b = 7.65 //will set the variable b as float

auto c = "abcdefg" // will set the variable c as string

Scope of a variable

Scope of a variable refers to the region of visibility or availability of a variable i.e the parts of your program in which the variable can be used or accessed.

There are mainly two types of variable scopes: Local Scope and Global Scope Example:

void person () {

string gender = "Male"; //This variable gender is Local to the function person ()

//and cannot be used outside this function.

}

Example:

#include <iostream>

using namespace std;

// Global variable declaration: and can be used anywhere in code int g;

int main() {

g=10;

// Using global variable cout << g return 0;

}

There are three types of variables based on the scope of the variable in C++ -

  • Local Variables
  • Instance Variables
  • Static Variables

Overflow and Underflow

Overflow occurs when we assign a value to more than its range, and Underflow is the opposite of the overflow.

In the case of overflow and underflow, the C++ compiler doesn't throw any errors. It simply changes the value.

For example, in case of an int variable, the maximum value of int data type is 2,147,483,647 (NT MAX) and after incrementing 1 on this value, it will return -2,147,483,648 (INTMIN). This is known as overflow.

The minimum value of int data type is -2,147,483,648 (INT MIN) and after decrementing 1 on this value, it will return 2,147,483,647 (INT MAX). This is known as underflow. Example:

#include <iostream>

using names pace std;

int main() {

int x = INT_MAX; //2147483647

int y = INT_MIN; //-2147483648

x = X + 1;

y =y- 1;

cout < x << endl;

cout < y;

Output: -2147483648 2147483647

Typecasting

Converting an expression of a given data type into another data type is known as type-casting or type-conversion.

There are two types of type conversions:

- Implicit Type Conversion

- Explicit Type Conversion

Implicit Type Conversion

  • It is automatically performed by the compiler itself to ensure that the calculations between the same data types take place and avoid any loss of data.
  • Such types of conversions take place when more than one data type is present in an expression.
  • The rule associated with implicit type conversions involves upgrading the data type of all the variables to the data type of the variable with the "largest data type".
  • The order of automatic type conversion or the sequence for smallest to largest data type(left to right) for this type conversion is given as: bool-> char -> short int -> int -> unsigned int -> long -> unsigned-> long long -> float-> double -> long double
  • Example:

#include <iostream>

using namespace std;

int main() {

int number = 200;

char letter = 'c';

float dec = 0.7;

int res1 = number + letter; // here Letter is implicitly converted to int and its va // ASCII value of c i.e. 99

cout << res1 << " ";

float res2 = res1 + dec; // here resi is implicitly converted to float.

cout < res2;

}

Output: 299 299.7

Explicit Type Conversion:

  • This process is also called typecasting, and it is user-defined.
  • Here the user can typecast the result to make it of a particular data type which may lead to data loss and is also known as forceful casting.
  • Syntax:

(type) expression

Example:

int main() {

double dbl = 5.6;

int res = (int)dbl + 10; // Here dbl is explicity con verted to int i.e value of dbl becomes 5.

cout <<"Result = " << res;

}

Upvote


user
Created by

Lubna Fathima


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles