0% found this document useful (0 votes)
7 views4 pages

C++ Basic Data Types Explained

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

C++ Basic Data Types Explained

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lecture-7

BASIC DATA TYPES IN C++

C ++ Data Types

User defined type Built in types Derived type


Structure Array
Union Function
Class pointer
enumeration

Integral type void Floating point

int char float double

Both C and C++ compilers support all the built in types. With the exception of void the basic
datatypes may have several modifiers preceding them to serve the needs of various situations. The
modifiers signed, unsigned, long and short may applied to character and integer basic data types.
However the modifier long may also be applied to double.

Data types in C++ can be classified under various categories.

TYPE BYTES RANGE

char 1 -128 to 127

usigned 1 0 to 265

sgned char 1 -128 to 127

int 2 -32768 to 32768

unsigned int 2 0 to 65535

singed int 2 -32768 to 32768

short int 2 -32768 to 32768

19 P.T.O
long int 4 -2147483648 to 2147483648

signed long int 4 -2147483648 to 2147483648

unsigned long int 4 0 to 4294967295

float 4 3.4E-38 to 3.4E+38

double 8 1.7E -308 to 1.7E +308

long double 10 3.4E-4932 to 1.1E+ 4932

The type void normally used for:

1) To specify the return type of function when it is not returning any value.
2) To indicate an empty argument list to a function.

Example:

Void function(void);

Another interesting use of void is in the declaration of genetic pointer

Example:

Void *gp;

Assigning any pointer type to a void pointer without using a cast is allowed in both C and ANSI C.
In ANSI C we can also assign a void pointer to a non-void pointer without using a cast to non void
pointer type. This is not allowed in C ++.

Example:

void *ptr1;

void *ptr2;

Are valid statement in ANSI C but not in C++. We need to use a cast operator.

ptr2=(char * ) ptr1;

USER DEFINED DATA TYPES:

STRUCTERS AND CLASSES

We have used user defined data types such as struct,and union in C. While these more features have
been added to make them suitable for object oriented programming. C++ also permits us to define
20 P.T.O
another user defined data type known as class which can be used just like any other basic data type to
declare a variable. The class variables are known as objects, which are the central focus of oops.

ENUMERATED DATA TYPE:

An enumerated data type is another user defined type which provides a way for
attaching names to number, these by increasing comprehensibility of the code. The enum keyword
automatically enumerates a list of words by assigning them values 0,1,2 and soon. This facility
provides an alternative means for creating symbolic.

Example:

enum shape { circle,square,triangle}

enum colour{red,blue,green,yellow}

enum position {off,on}

The enumerated data types differ slightly in C++ when compared with ANSI C. In C++, the
tag names shape, colour, and position become new type names. That means we can declare new
variables using the tag names.

Example:

Shape ellipse;//ellipse is of type shape

colour background ; // back ground is of type colour

ANSI C defines the types of enums to be ints. In C++,each enumerated data type retains its
own separate type. This means that C++ does not allow an int value to be automatically converted to
an enum.

Example:

colour background =blue; //vaid

colour background =7; //error in c++

colour background =(colour) 7;//ok

How ever an enumerated value can be used in place of an int value.

Example:

int c=red ;//valid, colour type promoted to int

By default, the enumerators are assigned integer values starting with 0 for the first
enumerator, 1 for the second and so on. We can also write

enum color {red, blue=4,green=8};

enum color {red=5,blue,green};

21 P.T.O
C++ also permits the creation of anonymous enums ( i.e, enums without tag names)

Example:

enum{off,on};

Here off is 0 and on is [Link] constants may be referenced in the same manner as regular constants.

Example:

int switch-1=off;

int switch-2=on;

ANSI C permits an enum defined with in a structure or a class, but the enum is
globally visible. In C++ an enum defined with in a class is local to that class.

22 P.T.O

You might also like