Data Types
Data Types
Data Types
com
ed
uc
at
io
n.
co
m
(visibility) and life time of variables and/or functions within a C Program. This
specifies precede the type that they modify.
Types
Basic types
ks
hi
Description
Enumerated
types
The type void The type specifier void indicates that no value is available.
.s
a
They are again arithmetic types and they are used to define variables
that can only be assigned certain discrete integer values throughout the
program.
w
w
Derived
types
They include (a) Pointer types, (b) Array types, (c) Structure types, (d)
Union types and (e) Function types.
www.sakshieducation.com
www.sakshieducation.com
Storage
Value range
size
co
m
Type
1 byte
unsigned char
1 byte
0 to 255
signed char
1 byte
int
2 or 4
bytes
unsigned int
2 or 4
-128 to 127
0 to 65,535 or 0 to 4,294,967,295
short
2 bytes
unsigned short
2 bytes
long
4 bytes
-2,147,483,648 to 2,147,483,647
ks
bytes
ed
uc
at
io
n.
char
-32,768 to 32,767
0 to 4,294,967,295
hi
0 to 65,535
4 bytes
.s
a
unsigned long
To get the exact size of a type or a variable on a particular platform, you can use
the sizeof operator. The expressions sizeof (type) yields the storage size of the object
w
w
or type in bytes. Following is an example to get the size of int type on any machine:
www.sakshieducation.com
www.sakshieducation.com
Example program
#include<stdio.h>
co
m
#include<limits.h>
int main()
printf("%d",sizeof(int));
return 0;
}
ed
uc
at
io
n.
When you compile the above program you get the output as, Storage size for int: 4
hi
(1) Explanation
ks
We had seen earlier that the range of an Integer constant depends upon the
compiler. For a 16-bit compiler like Turbo C or Turbo C++ the range is 32768 to
.s
a
w
w
like Intel 8086/8088. As against this, a 32-bit compiler like VC++ generates machine
language code that is targeted towards a 32-bit microprocessor like Intel Pentium.
Note that this does not mean that a program compiled using Turbo C would
not work on 32-bit processor. It would run successfully but at that time the 32-bit
processor would work as if it were a 16-bit processor. This happens because a 32-bit
3
www.sakshieducation.com
www.sakshieducation.com
processor provides support for programs compiled using 16-bit compilers. If this
backward compatibility support is not provided the 16-bit program would not run on it.
This is precisely what happens on the new Intel Itanium processors, which have withdrawn support
for 16-bit code.
th
nd
co
m
Remember that out of the two/four bytes used to store an integer, the highest bit
(16 /32 bit) is used to store the sign of the integer. This bit is 1 if the number is
negative and 0 if the number is positive.
ed
uc
at
io
n.
(2) Signed
With such a declaration, the range of permissible integer values (for a 16-bit OS) will
hi
shift from the range -32768 to +32767 to the range 0 to 65535. Thus, declaring an
ks
integer as unsigned almost doubles the size of the largest possible value that it can
otherwise take. This so happens because on declaring the integer as unsigned, the
.s
a
left-most bit is now free and is not used to store the sign of the number. Note that an
unsigned integer still occupies two bytes. This is how an unsigned integer can be
w
w
declared:
unsigned int i ;
unsigned i ;
Like an unsigned int, there also exists a short unsigned int and a long unsigned int.
By default a short int is a signed short int and a long int is a signed long int.
www.sakshieducation.com
www.sakshieducation.com
(3)Long
int:-
C offers a variation of the integer data type that provides what are called short
and long integer values. The intention of providing these variations is to provide
integers with different ranges wherever possible. Though not a rule, short and long
co
m
integers would usually occupy two and four bytes respectively. Each compiler can
decide appropriate sizes depending on the operating system and hardware for which
it is being written, subject to the following rules:
ed
uc
at
io
n.
long variables which hold long integers are declared using the keyword long, as in,
long int i ;
hi
ks
long integers cause the program to run a bit slower, but the range of values that we
can use is expanded tremendously. The value of a long integer typically can vary
.s
a
from -2147483648 to +2147483647. More than this you should not need unless you
If there are such things as longs, symmetry requires shorts as wellintegers that need
w
w
less space in memory and thus help speed up program execution. short integer
variables are declared as,
short int j ;
short int height ;
5
www.sakshieducation.com
www.sakshieducation.com
C allows the abbreviation of short int to short and of long int to long. So the
declarations made above can be written as,
long i ;
long abc ;
short height ;
Naturally, most C programmers prefer this short-cut.
co
m
short j ;
ed
uc
at
io
n.
hi
A float occupies four bytes in memory and can range from -3.4e38 to +3.4e38. If this
ks
is insufficient then C offers a double data type that occupies 8 bytes in memory and
has a range from -1.7e308 to +1.7e308. A variable of type double can be declared as,
.s
a
double a, population;
If the situation demands usage of real numbers that lie even beyond the range
offered by double data type, then there exists a long double that can range from -
w
w
www.sakshieducation.com
www.sakshieducation.com
TOPICS 3
(1)
CHAR TYPES
Signed char and unsigned char
Signed and unsigned chars, both occupying one byte each, but having different
Consider the statement
Char ch = A;
co
m
ranges. To begin with it might appear strange as to how a char can have a sign.
ed
uc
at
io
n.
Here what gets stored in ch is the binary equivalent of the ASCII value of A (i.e.
binary of 65). And if 65s binary can be stored, then -54s binary can also be stored
(in a signed char).
A signed char is same as an ordinary char and has a range from -128 to +127.
Unsigned char has a range from 0 to 255.
hi
main ( )
ks
char ch = 291;
.s
a
w
w
What output do you expect from this program? Possibly, 291 and the character
corresponding to it. Well, not really. Surprised? The reason is that ch has been
defined as a char, and a char cannot take a value bigger than +127. Hence when
value of ch exceeds +127, an appropriate value from the other side of the range is
picked up and stored in ch. This value in our case happens to be 35, hence 35 and
www.sakshieducation.com