0% found this document useful (0 votes)
40 views

Basic Derived User Defined: Data Types

This document discusses various data types in C++. It describes basic data types like integers, characters, floats, and Booleans. It also covers derived data types such as arrays, structures, classes, unions, and enums. Finally, it discusses user-defined data types that can be created using typedef, enumerated types, structures, and unions. Data types define the size and type of values a variable can store and the operations that can be performed on that variable.

Uploaded by

muthumanin
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)
40 views

Basic Derived User Defined: Data Types

This document discusses various data types in C++. It describes basic data types like integers, characters, floats, and Booleans. It also covers derived data types such as arrays, structures, classes, unions, and enums. Finally, it discusses user-defined data types that can be created using typedef, enumerated types, structures, and unions. Data types define the size and type of values a variable can store and the operations that can be performed on that variable.

Uploaded by

muthumanin
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
You are on page 1/ 7

DATA TYPES

 Data type defines size and type of values that a variable can store along with the
set of operations that can be performed on that variable.
 Every variable used in program hold data, and every data must have their own
type.
 C++ provides built-in data types that correspond to integers, characters, floating-
point values, and Boolean values.

DATA TYPES

Basic Derived User


Defined

Integral Void Float


bool

Array,
Structure, Class,
Function,
Union, Enum
Pointer,
int char float double reference

Basic Data Types

 The basic data types are integral, float and void. C++ language supports both
signed and unsigned literals.

DR.N.MUTHUMANI DEPT. OF COMPUTER APPLICATIONS SRCAS


 The memory size of basic data types may change according to 32 or 64 bit
operating system.
 The basic data types size is given according to 32 bit OS.

Typical Bit
Type Typical Range Sample Applications
Width

Very small numbers and ASCII


Char 1byte -127 to 127
characters

Small numbers and full PC


unsigned char 1byte 0 to 255
character set

-2147483648 to Counting, small numbers, loop


Int 4bytes
2147483647 control

unsigned int 4bytes 0 to 4294967295 Large numbers and loops

-2147483648 to
signed int 4bytes Large numbers, populations
2147483647

Counting, small numbers, loop


short int 2bytes -32768 to 32767
control

unsigned short Counting, small numbers, loop


Range 0 to 65,535
int control

signed short Counting, small numbers, loop


Range -32768 to 32767
int control

-2,147,483,648 to
long int 4bytes Large numbers, populations
2,147,483,647

signed long int 4bytes same as long int Large numbers, populations

unsigned long
4bytes 0 to 4,294,967,295 Astronomical distances
int

Float 4bytes +/- 3.4e +/- 38 (~7 digits) Scientific (7-digit) precision)

Double 8bytes
+/- 1.7e +/- 308 (~15 Scientific (15-digit precision)

DR.N.MUTHUMANI DEPT. OF COMPUTER APPLICATIONS SRCAS


digits)

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


long double 8bytes
digits) Financial (18-digit precision)

Bool 1 byte True or False

wchar_t 2 or 4 bytes 1 wide character

Example Program

#include <iostream.h>
using namespace std;
int main()
{
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
return 0;
}

User Defined Data Types


1. Definition of own types (typedef).
 C++ allows us to define our own types based on other existing data types. In
order to do that the keyword typedef is used.
 Syntax:

typedef existing_type new_type_name ;

where existing_type is a C++ fundamental or any other defined type


and new_type_name is the name that the new type we are going to define will
receive.

 For example:

DR.N.MUTHUMANI DEPT. OF COMPUTER APPLICATIONS SRCAS


typedef char C;
typedef unsigned int NUM;
typedef char * word;
typedef char name [50];

 In this case we have defined four new data types : C, NUM, word, and name
as char, unsigned int, char* and char[50] respectively.

C char_A;
Num mark;
word table;
name bca;

 typedef can be useful to define a type that is repeatedly used within a


program.

2. Enumerated Types
 Enumerations serve to create data types to contain something different
that is not limited neither to either numerical or character constants nor to
constants true and false.
 Syntax:

enum model_name {
value1,
value2,
value3,
.
.
} object_name;

 Example:

enum months { january=1, february, march, april, may, june, july,


august,

september, october, november, december} y2k;

3. Structures.
 A structure is a set of different types of data that may have different
lengths grouped together under a unique name.

DR.N.MUTHUMANI DEPT. OF COMPUTER APPLICATIONS SRCAS


 Syntax:
struct structure_name {
date type1 element1;
data type2 element2;
data type3 element3;
.
.
} var_name;

where structure_name is a name for the model of the structure type and
the optional parameter var_name is a valid identifier (or identifiers) for
structure variables.

 If the structure definition includes the parameter stucturel_name (optional),


that parameter becomes a valid data type name equivalent to the
structure. For example:

struct product {
char name [30];
float price;
};

product guava;
product apple,orange, melon;

 Once declared, product has become a new valid data type name like the
fundamental ones int, char or short and variables can be declared of that
type.
 It is possible to declare the structure variables apple, orange and melon this
way:

struct products {
char name [30];
float price;
} guava,apple, orange, melon;

 The members of the structure are accesses with the help of dot (.)
operator.

DR.N.MUTHUMANI DEPT. OF COMPUTER APPLICATIONS SRCAS


apple.name
apple.price
orange.name
orange.price
melon.name
melon.price

4. Unions
 Unions allow a portion of memory to be accessed as different data types,
since all of them are in fact the same location in memory.
 Its declaration and use is similar to the one of structures but its
functionality is totally different
 Syntax:

union model_name {
type1 element1;
type2 element2;
type3 element3;
.
.
} object_name;

 All the elements of the union declaration occupy the same space of
memory. Its size is the one of the greatest element of the declaration. For
example:

union u1 {
char c;
int i;
float f;
} mytypes;

defines three elements:

mytypes.c
mytypes.i
mytypes.f

each one of a different data type.

DR.N.MUTHUMANI DEPT. OF COMPUTER APPLICATIONS SRCAS


 Since all of them are referring to a same location in memory, the
modification of one of the elements will affect the value of all of them.

Derived Data Types:


Data types that are derived from the built-in data types are known as derived data
types. The various derived data types provided by C++ are arrays, junctions,
references and pointers.
 Array An array is a set of elements of the same data type that are referred to by
the same name. All the elements in an array are stored at contiguous (one after
another) memory locations and each element is accessed by a unique index or
subscript value. The subscript value indicates the position of an element in an
array.
 Function A function is a self-contained program segment that carries out a
specific well-defined task. In C++, every program contains one or more functions
which can be invoked from other parts of a program, if required.
 Reference A reference is an alternative name for a variable. That is, a reference
is an alias for a variable in a program. A variable and its reference can be used
interchangeably in a program as both refer to the same memory location. Hence,
changes made to any of them (say, a variable) are reflected in the other (on a
reference).
 Pointer A pointer is a variable that can store the memory address of another
variable. Pointers allow to use the memory dynamically. That is, with the help of
pointers, memory can be allocated or de-allocated to the variables at run-time,
thus, making a program more efficient.

DR.N.MUTHUMANI DEPT. OF COMPUTER APPLICATIONS SRCAS

You might also like