***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Monday, April 26, 2021

Data types in C++

 C++ Datatypes

The basic built in data types are char, int, float, double and void.

Type

Keyword

 

 

Character

char

 

 

Integer

int

 

 

Floating point

float

 

 

Double floating point

double

 

 

Valueless

void

Integer data type: these are the numbers without having decimal point that may (or) may not

  prefixed with a ‘-‘ (or) ‘+’ sign. Ex: 234, -765.

 Float data type: these are the numbers with decimal points value. In C++ floats are stored with 6 digits of decimal precession. Ex: 9.67 , 897.567,….

 Character data type: A single character is enclosed with single quotes can be defined as a character data type. Ex: ‘n’ , ‘d’, …..

 Void data type: it is the latest addition to C++, it is value less (or) null data type.


Several of the basic types can be modified using one or more of these type modifiers:

  • signed
  • unsigned
  • short
  • long

The following table shows the variable type, how much memory it takes to store the value in memory, and what is maximum and minimum value which can be stored in such type of variables.

 

Type

 

 

Length

 

 

Range

 

 

 

char

 

1byte

 

-128 to 127 or 0 to 255

 

 

unsigned char

 

1byte

 

0 to 255

 

 

signed char

 

1byte

 

-128 to 127

 

 

int

 

4bytes

 

-2147483648 to 2147483647

 

 

unsigned int

 

4bytes

 

0 to 4294967295

 

 

signed int

 

4bytes

 

-2147483648 to 2147483647

 

 

short int

 

2bytes

 

-32768 to 32767

 

 

unsigned short

 

2bytes

 

0 to 65,535

 

 

int

 

 

 

 

 

 

 

 

signed short int

 

2bytes

 

-32768 to 32767

 

 

long int

 

4bytes

 

-2,147,483,648 to 2,147,483,647

 

 

signed long int

 

4bytes

 

-2,147,483,648 to 2,147,483,647

 

 

unsigned long

 

4bytes

 

0 to 4,294,967,295

 

 

int

 

 

 

 

 

 

 

 

float

 

4bytes

 

+/- 3.4e +/- 38 (~7 digits)

 

 

double

 

8bytes

 

+/- 1.7e +/- 308 (~15 digits)

 

 

long double

 

8bytes

 

+/- 1.7e +/- 308 (~15 digits)

 



Derived Data types

The data-types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. These can be of four types namely:

  • Function
  • Array
  • Pointers
  • Reference

1. Function :

A function is a block of code or program-segment that is defined to perform a specific well-defined task. A function is generally defined to save the user from writing the same lines of code again and again for the same input. All the lines of code are put together inside a single function and this can be called anywhere required. main() is a default function that is defined in every program of C++.

FunctionType      FunctionName(parameters)

{

// function body;

}

2. Array :

An array is a collection of items stored at continuous memory locations. The idea of array is to represent many instances in one variable.

Syntax:

DataType      ArrayName[size_of_array];

Ex:   int   a[5];

3. Pointer :

Pointers are symbolic representation of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. It’s general declaration in C is:

datatype    *var_name;

Ex:  int   *ptr;

4. Reference

A reference variable is an alias, that is, another name for an already existing variable. Once a reference is initialized with a variable, either the variable name or the reference name may be used to refer to the variable.

Example:

int  i=10;

int &R = i;

References are often confused with pointers but three major differences between references and pointers are −

  • You cannot have NULL references. You must always be able to assume that a reference is connected to a legitimate piece of storage.
  • Once a reference is initialized to an object, it cannot be changed to refer to another object. Pointers can be pointed to another object at any time.
  • A reference must be initialized when it is created. Pointers can be initialized at any time.

User defined datatypes:

C++ allows programmers to define their identifier that would represent an existing data type. There are three such types:

      Structure

      Union

      enum

      class

1. Structure:

It is a package of variables of different types under a single name. This is done to handle data efficiently. "struct" keyword is used to define a structure.

struct [structure tag] {

   member definition;

   member definition;

   ...

   member definition;

} [one or more structure variables]; 

Ex:

struct Books {

   char  title[50];

   char  author[50];

   char  subject[100];

   int   book_id;

} book; 

2. Union:

These allow storing various data types in the same memory location. Programmers can define a union with different members, but only a single member can contain a value at a given time.

union [union tag] {

   member definition;

   member definition;

   ...

   member definition;

} [one or more union variables];

Ex:

union Data {

   int i;

   float f;

   char str[20];

} data;

3. enum:

Enumeration is a special data type that consists of integral constants, and each of them is assigned with a specific name. "enum" keyword is used to define the enumerated data type.

Variables of type enum can also be defined. They can be defined in two ways:

// In both of the below cases, "day" is

// defined as the variable of type week.

enum week{Mon, Tue, Wed};

enum week day;

// Or

enum week{Mon, Tue, Wed}day;

4. class:

A class is a user-defined data type. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.




No comments:

Post a Comment