Home | | Computer Science 11th std | Introduction to fundamental Data types

# Introduction to fundamental Data types

Fundamental (atomic) data types are predefined data types available with C++. There are five fundamental data types in C++: char, int, float, double and void.

Introduction to fundamental Data types:

Fundamental (atomic) data types are predefined data types available with C++. There are five fundamental data types in C++: char, int, float, double and void. Actually, these are the keywords for defining the data types.

## (1) int data type:

Integers are whole numbers without any fraction. Integers can be positive or negative. Integer data type accepts and returns only integer numbers. If a variable is declared as an int, C++ compiler allows storing only integer values into it. If you try to store a fractional value in an int type variable it will accept only the integer portion of the fractional value..

## Illustration 9.1: C++ Program to get 2 integer numbers and display their sum:

//Program to receive two integer numbers and display their sum

#include <iostream>

using namespace std; int main()

{

int num1, num2, sum;

//variables num1, num2, and sum are declared as integers

cout << "\n Enter Number 1: ";

cin >> num1;

cout << "\n Enter Number 2: "; cin >> num2;

sum = num1 + num2;

cout << "\n The sum of " << num1 << " and " << num2 << " is " << sum;

}

In the above program, there are three variables declared viz. num1, num2 and sum. All these three variables are declared as integer types. So, three different integer values can be stored in these variables.

The variables num1 and num2 are used to store the values that are obtained from the user during the execution of the program, whereas the variable sum is used to store the processed (resultant) value.

During the execution of the above program, line numbers 7 and 9 prompt the user to Enter number 1 and number 2.

Input stream cin in line 8 and 10 will receive the values given by the user and store the same in variable num1 and num2.

Line 11; both the values of num1 and num2 will be added and the result will be assigned to the variable sum.

## (2) char data type:

Character data type accepts and returns all valid ASCII characters. Character data type is often said to be an integer type, since all the characters are represented in memory by their associated ASCII Codes. If a variable is declared as char, C++ allows storing either a character or an integer value.

## Illustration 9.2: C++ Program to accept any character and display its next character

#include <iostream>

using namespace std;

int main()

{

char ch;

cout << "\n Enter a character: ";

cin >> ch;

ch = ch + 1;

cout << "\n The Next character: " << ch;

}

### The output produced by the program will be

Enter a character: A

The Next character: B

In the above program, ch is declared as a char type variable to hold a character. When the user enters a character, it will be stored in the ch variable as an integer value (ie. Equivalent ASCII code).

ch = ch + 1;

In this statement, the value of ch is incremented by 1 and the new value is stored back in the same variable ch. (Remember that, arithmetic operations are carried out only on the numbers not with alphabets).

Another program illustrates how int and char data types are working together.

## Illustration 9.3: C++ program to get an ASCII value and display the corresponding character

#include <iostream>

using namespace std;

int main ()

{

int n;

char ch;

cout << "\n Enter an ASCII code (0 to 255): ";

cin >> n;

ch = n;

cout << "\n Equivalent Character: " << ch;

}

### The output

Enter an ASCII code (0 to 255): 100

Equivalent Character: d

In the above program, variable n is declared as an int type and another variable ch as a char type. During execution, the program prompts the user to enter an ASCII value. If the user enters an ASCII value as an integer, it will be stored in the variable n. In the statement ch = n; the value of n is assigned into ch. Remember that, ch is a char type variable.

For example, if a user enters 100 as input; initially, 100 is stored in the variable n. In the next statement, the value of n i.e., 100 is assigned to ch. Since, ch is a char type; it shows the corresponding ASCII character as output. (Equivalent ASCII Character for 100 is d).

## (3) float data type:

If a variable is declared as float, all values will be stored as floating point values.

### There are two advantages of using float data types.

(1) They can represent values between the integers.

(2) They can represent a much greater range of values.

At the same time, floating point operations takes more time to execute compared to the integer type ie., floating point operations are slower than integer operations. This is a disadvantage of floating point operation.

## Illustration 9.4: C++Program to find the area of circle

#include <iostream>

using namespace std;

int main()

{

float r, area;

cout << "\n Enter Radius: ";

cin >> r;

area = 3.14 * r * r;

cout << "\n The Area of the circle is " << area;

}

### Output:

Enter Radius: 6.5

The Area of the circle is 132.665

In the above example, two variables r and area are declared as float type in a single statement. Variable r is used to hold radius which is a data provided by the user and area for holding the area of the circle which is obtained through the formula. For example, if the user inputs the radius value as 6.5, then the statement area = 3.14 * r * r; multiplies the value of r twice with 3.14 (value of pi) and the resultant value 132.665 will be kept in the variable area.

In this case, r and area both are same type of variables. So, these two variables are declared in a single statement. Declaring the same variables in a single statement reduces the processing time rather than multiple declarations.

## (4) double data type:

This is for double precision floating point numbers. (precision means significant numbers after decimal point). The double is also used for handling floating point numbers. But, this type occupies double the space than float type. This means, more fractions can be accommodated in double than in float type. The double is larger and slower than type float. double is used in a similar way as that of float data type.

## (5) void data type:

The literal meaning for void is ‘empty space’. Here, in C++, the void data type specifies an empty set of values. It is used as a return type for functions that do not return any value.

Tags : C++ , 11th Computer Science : Chapter 9 : Introduction to C++
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 9 : Introduction to C++ : Introduction to fundamental Data types | C++

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.