C++ data types with examples.

Q1. Explain C++ data types with appropriate examples.

Ans. C++ like any other language provides ways and facilities to handle different types of data by providing data types.

Data types are means to identify the type of data and associated operations of handling it.

C++ data types are of two types:

  • Fundamental types
  • Derived types

 

  1. Fundamental Data Types

Fundamental (atomic) data types are those that are not composed of other data types.

Five fundamental data types are:

  • int Data Type (for integers)

Integers are whole numbers such as 5, 39, -1917, 0 etc. they have no fractional parts. Integers in C++ by int data type. An identifier declared as int cannot have fractional part. Integers can be positive or negative.

  • char Data Type (for characters)

Characters can store any member of the C++ implementation’s basic set. If a character from this set is stored in a character variable, its value is equivalent to the integer code of that character. An identifier declared as char becomes a character variable.

  • float Data Type (for floating-point numbers)

A number having fractional part is a floating-point number. For example, 3.14159 is a floating-point number. The decimal point signals that it is a floating-point number not an integer. Floating-point numbers can also be written in exponent notation. For example, 147.9101 would be written as 1.479101E02. an identifier declared as float becomes a floating-point variable and can hold floating-pint numbers. Floating-point variables represent real numbers, which are used for measurable quantities like distance, area, temperature etc. and typically have a fractional part.

  • double Data Type (for double precision floating-point numbers)

The data type double is also used for handling floating-point numbers. But it is treated as a distinct data type because, it (double data type) occupies twice as much memory as type float, and stores floating-point numbers with much larger range and precision (significant numbers after decimal point). It stands for double precision floating-point. It is used when type float is too small or insufficiently precise.

  • void Data Type (for empty set of values and non-returning functions)

The void type specifies an empty set of values. It is used as the return type for functions that do not return a value. No object of type void may be declared.

 

 

  1. Derived Data Type

From the fundamental types other types can be derived by using the declaration operators.

  • Array

Arrays refer to a named list of a finite number n of similar data elements. Each of the data elements can be referenced respectively by a set of consecutive numbers, usually 0, 1, 2, 3,….,n. If the name of an array of 10 elements is ARY, then its elements will be referenced as shown below:

ARY[0], ARY[1], ARY[2], ARY[3],…….,ARY[9]

Array can be one dimensional, two dimensional or multi dimensional.

For a type A, T[size] is the array of size elements of type A. the elements are indexed from 0 to size-1.

For example,

float a[3];                  /*declares array of three floats : a[0], a[1], a[2] */

 

int b[2][4];                 /*declares a two dimensional array of integrs:

b[0][0], b[0][1], b[0][2], b[0][3]

b[1][0], b[1][1], b[1][2], b[1][3]*/

 

Here, there are two integer arrays b[0] and b[1] each having 4 integer elements.

 

Char n[10];   /* declares a character array of size 10 */

  • Functions

A function is a named part of a program that can be invoked from other parts of the program as often needed.

  • Pointers

A pointer is a variable that holds a memory address. This address is usually the location of another variable in memory. If one variable contains the address of another variable, the first variable is said to pint to the second.

 

 

If a variable is going to hold a pointer, it must b declared as such. A pointer declaration consists of a base type, an * (asterisk), and the variable name. The general form of declaring a pointer variable is

type *ptr;

where type is any valid C++ type and ptr is the name of the pointer variables.

For example:

char *a;

declares a pointer to character.

The base type of the pointer defines what type of variables the pointer can point to. Technically, any type of pointer can point anywhere in memory. However, all pointer arithmetic is done relative to its base type so it is important to declare the pointer correctly.

  • References

A reference is an alternative name for an object. A reference variable provides an alias for a previously defined variable. A reference declaration consists of a base type, an & (ampersand), a reference variable name equated to a variable name. The general form of declaring a reference variable is

type & ref-var = var-name;

where type is any valid C++ data type, ref-var is the name of reference variable that will point to variable denoted by var-name.

For example, if sum is declared as a reference variable for a variable total, then both sum and total represent the same variable and can be used interchangeably. Following program segment illustrates it:

int total;

int &sum = total;

total = 100;

cout,,”Sum = “<<sum<<”\n”;

cout<<”Total = “<<total<<”\n”;

The output of above program segment will be as follows:

Sum = 100

Total = 100

Both the variables refer to the same data object in the memory, thus, print the same value.

  • Constants

The keyword const can be added to the declaration of an object to make that object a constant rather than a variable. Thus, the value of the named constant cannot be altered during the program run. The general form of constant declaration is as follows:

const type name = value;

where const is the keyword that must be used for declaring a constant, type is any valid C++ data type, name is the name of the constant and value is the constant value of the data type type. For instance,

const int upperage = 50;

declares a constant named a upperage of type integer that holds value 50;

a constant must be initialized at the time of declaration. If you give only const in place of const int, it means the same.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: