Different Data Types in C'

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Different Data Types in

‘C’
Submitted by :
Manish Mann
ECE/09/128
Introduction
 Data Type: set of values together with a set of
operations is called a data type
 Data types are used to store various types of
data that is processed by program.
 Data type attaches with variable to determine
the number of bytes to be allocate to variable
and valid operations which can be performed
on that variable.
Data Types

 C data can be classified into four categories:


◦ Fundamental data types
◦ Data type modifiers

◦ Derived data types

◦ User Defined data types


Fundamental Data Types
 Fundamental data types are those that are not
composed of any other data types.
Five types of fundamental data types are:-

1. int data type (for integers):


Examples: 0
-6728
91
No commas are used within an integer.
Size: 2 bytes
Range: -32768 to 32767
Fundamental Data Type
2. char data type (for characters):
The smallest integral data type.
Used for characters: letters, digits, and special symbols.
Each character is enclosed in single quotes.
Examples: 'A‘,'a', '0', '*', '+', '$', '&‘.
A blank space is a character and is written ' ', with a space.
Size : 1 byte
Range : -128 to 127

3. float data type (for floating – point numbers):


It represents any real number.
Size : 4 bytes
Range: 3.4 * 10-38 to 3.4 * 1038 -1
Fundamental Data Types
4. double data type (for double precision floating–point
numbers):
it is used to define BIG floating point numbers.
It reserves twice the storage for the number.
Size : 8 bytes
Range : 3.4 * 10-4932 to 3.4 * 104932 -1

5. void data type :


It specifies the empty set of values.
It is used for the functions that doesn't return value.
Modifiers Data Types
 The data types explained above have the following
modifiers.
1. short
2. long
3. signed
4. unsigned
 The modifiers define the amount of storage allocated to
the variable. The amount of storage allocated is not cast in
stone. ANSI has the following rules:
o short int <= int <= long int
o float <= double <= long double
:- What this means is that a 'short int' should assign less than or
the same amount of storage as an 'int' and the 'int' should be less
or the same bytes than a 'long int'.
Modifiers Data Types
Types Size (in bytes) Range
……………………………………………………………
short int 2 -32,768 -> +32,767
unsigned short int 2 0 -> +65,535
unsigned int 4 0 -> +4,294,967,295
long int 4 -2,147,483,648 -> +2,147,483,647
signed int 2 same as int
signed char 1 -128 -> +127
unsigned char 1 0 -> +255
float 4
double 8
long double 12
Derived Data Types
 The Data types that are derived from the
fundamental data types.
 The types of derived data types are:
1. Arrays
2. Functions
3. Pointers
4. References
5. Constants
Derived Data Types : Arrays
 Arrays are used in C to represent structures of
consecutive elements of the same type. The
definition of a (fixed-size) array has the following
syntax:
int array[100];
which defines an array named array to hold
100 values of the primitive type int.
 If declared within a function, the array dimension

may also be a non-constant expression, in which


case memory for the specified number of
elements will be allocated.
Derived Data Types : Functions
 A function is a named part of a program that can be invoked from
other parts of the programs as often needed.
 Syntax of function:
type fuction-name(parameter list)
{ …….
body of function
}
 A function must have a return statement.
 A function can be invoked by two manners:
1. Call by value: It copies the values of actual parameters and
creates its own copy of arguments and uses them.
2. Call by reference : It doesn’t create a copy and directly refers
to the original parameters only different name i.e references.
Derived Data Types : Pointers
 In declarations , the asterisk modifier (*) specifies
a pointer type.
 For example, where the specifier int would refer to
the integer type, the specifier int * refers to the
type "pointer to integer".
 The following line of code declares a pointer-to-
integer variable called ptr:
int *ptr;
 Pointer values associate two pieces of information:
1. Memory address
2. Data type
Derived Data Types : References
 A reference is an alternative name for a object.
 A reference variable provides an alias for a

previously defined variable.


 Its declaration consist of a base type, an &

(ampersand) and a reference variable name.


 In the following example, ptr is set so that it

points to the data associated with the variable a:


int *ptr;
int a;
ptr = &a;
Derived Data Types : Constants
 The keyword const can be added to the
declaration of an object to make that object a
constant rather than a variable.
 The value of the named constant cannot be

altered during program run.


 Syntax:

const type name = value;


 So it must be initialized at the time of
declaration.
User Defined Data Types
 The User Defined data types are that derived
data types which are defined by the user…
 The types of user defined derived data types

are :

1. structure
2. union
3. enumeration
User Defined Data Types : Structure
 A structure is a variables referenced under one name,
providing a convinient means of keeping related
informantion together.
 The variables that make up structure are called structure
elements.
 Keyword struct is used to construct a structure.
 Syntax :
struct name{
structure elements;
};
 A structure is diff. from an array as, array is the collection of
similar data types where as structure is collection of diff.
data types.
User Defined Data Types : Union
 Unions in C are related to structures and are
defined as objects that may hold (at different
times) objects of different types and sizes.
 They are analogous to variant records in other.
 Unlike structures, the components of a union all
refer to the same location in memory
programming languages.
 The size of a union is equal to the size of its
largest component type.
 Keyword union is used to declare and create a
union.
User Defined Data Types :
Enumeration
 Used to create a group of values which are named constants
in C, C++, C#, Java, etc.
 Examples in C/C++/C#:
enum days {Mon, Tue, Wed, Thu, Fri, Sat, Sun}; // Mon = 0, Tues =
1, . . ., etc.
days today, yesterday = Wed;
today = yesterday + 1;
 The first constant in an enumeration is assigned the value
zero, and each subsequent value is incremented by one over
the previous constant.
 Specific values may also be assigned to constants in the
declaration, and any subsequent constants without specific
values will be given incremented values from that point
onward.
Why so many data types?????
 The reason for providing so many data types
is to allow programmer to take advantage of
hardware characteristics.

 Helps in avoiding wastage in memory.

 Helps in avoiding wastege of time.

You might also like