Basic Data Types in C++.

Basic Data Types

  • C++ supports a large number of data types. The built in or basic data types supported by C++ are integer, floating point and character. 
  • C++ also provides the data type bool for variables that can hold only the values True and false.
  • Some commonly used data types are summarized in table along with description.
www.mskuthar.blogspot.com
Data Types in C++
TypeDescription
intSmall integer number
long intLarge integer number
floatSmall real number
doubleDouble precision real number
long doubleLong double precision real number
charA Single Character

  • The exact sizes and ranges of values for the fundamental types are implementation dependent. 
  • The header files <climits> (for the integral types) and <cfloat> (for the floating-point types) specify the ranges of values supported on your system.

C++ string Class

  • Because a char variable can only store one character in its memory place, a variable capable of holding a whole string requires another data type. Although C++ has no builtin data type capable of doing this, Standard C++ offers a string class capable of allowing the programmer to construct a string type variable. 
  • To declare and use such objects (variables) we must include an additional header file in our source code: < string >
// This program demonstrates the string class.
#include <iostream>
#include <string> // Required for the string class.
using namespace std;
int main ()
{
string mystring = "This is a string";
 cout << mystring;
 return 0;
}

Variable Initialization
  • Variable is a place that can store data in the computer memory and is given a symbolic name for easy reference. 
  • The variables can be used during program execution to hold different values at different times.
Declaration of a variable
Before a variable is used in a program, we must declare it. This activity enables the compiler to make available the appropriate type of location in the memory.

float total;
You can declare more than one variable of same type in a single single statement
int x, y;
Initialization of variable
When we declare a variable it's default value is undetermined. We can declare a variable with some initial value.

int a = 20;
The other way to initialize variables, known as constructor initialization, is done by enclosing the initial value between parentheses () :
For example:
int a (0);
Both ways of initializing variables are equivalent in C++.

Constants

  • A variable that doesn't change its value during program execution is known as a constant variable. 
  • Any attempt to change a constant's value will cause an error message. One of the basic data types may be a constant in C++; const qualifier can be used to declare constant as shown below:
const float PI = 3.1415;
  • The above declaration means that PI is a constant of float types having a value 3.1415.
  • Examples of valid constant declarations are:
const int RATE = 50;
const float PI = 3.1415;
const char CH = 'A';

Type Conversion

  • The process in which one pre-defined type of expression is converted into another type is called conversion. There are two types of conversion in C++.


1. Implicit conversion
2. Explicit conversion

Implicit conversion
  • Data type can be mixed in the expression. For example


double a;
int b = 5;
float c = 8.5;
a = b * c;
  • When two operands of different type are encountered in the same expression, the lower type variable is converted to the higher type variable. The following table shows the order of data types.
Order of data types
Data type
long double
double
float
long
int
char
order

(highest)
To

(lowest)
  • The int value of b is converted to type float and stored in a temporary variable before being multiplied by the float variable c. 
  • The result is then converted to double so that it can be assigned to the double variable a.
Explicit conversion
  • It is also called type casting. It temporarily changes a variable data type from its declared data type to a new one. 
  • It may be noted here that type casting can only be done on the right hand side the assignment statement.
totalPay = static_cast<double>(salary) + bonus;

Initially variable salary is defined as float but for the above calculation it is first converted to double data type and then added to the variable bonus.

1 Comments

Previous Post Next Post