1. Balochistan University of IT & MS
Lecture 3
Data Types in C
short, int, long, float, double, char
CS106
Introduction to Programming
Methodologies & Abstraction
Fall 2005
Balochistan University
of I.T & M.S
Faculty of System Sciences
Sadique Ahmed Bugti
2. Balochistan University of IT & MS
The Data Type int
• int is short for integer
• Integers are whole numbers, which have
no fractional part, or decimal point.
– They can be both positive and negative.
– Unsigned integers (positive values only) are
supported.
– In addition, there are short and long
integers.
• Examples of integers include:
0 57 4567 128933
3. Balochistan University of IT & MS
The Data Type int
• The keyword used to define integers is:
int
• Integer variables are declared using:
int variable_name;
• For example:
int a;
• declares that you want to create an int
variable called a
4. Balochistan University of IT & MS
The Integral Types
• int is considered the “natural” type for
working with integers.
– Other integral types such as char, short and
long are intended for specialized use.
5. Balochistan University of IT & MS
long int
• Sometimes extra digits are needed to
store numbers larger than those allowed
in a normal int.
long int variable_name;
• The long qualifier informs C that extra
storage is required for the integer.
• Long integers are normally in the range:
-231… 231-1 or -2147483648...2147483647
6. Balochistan University of IT & MS
short int
• Sometimes less digits are needed to
store a number:
short int variable_name;
• The short qualifier informs C that less
storage is required for the integer.
• Short integers are normally in the range:
-215… 215-1 or -32768..32767
7. Balochistan University of IT & MS
unsigned integers
• Unsigned integers contain positive values only
– An integer with n bits uses n bits for the number
– An integer with n bits → 2n integers in the range
[0,2n-1]
e.g. if n=16 → 65536 integers, 0..65535
• Unsigned integers are declared:
unsigned int variable_name;
unsigned short variable_name;
unsigned long variable_name;
8. Balochistan University of IT & MS
signed integers
• Signed integers allow both positive and
negative values:
– An integer with n bits uses n-1 bits for the
number and 1 bit for the sign
• (+ve = 0, -ve = 1)
– 2n integers in the range [-(2n-1),(2n-1-1)]
e.g. n=16 → 65536 integers, -322768..32767
9. Balochistan University of IT & MS
signed integers
• All int declarations default to signed.
• Signed integers are declared:
signed int variable_name;
which is equivalent to:
int variable_name;
10. Balochistan University of IT & MS
Integers in C
On a 16-bit System
Type Approximate Range
short -32768..32767
unsigned short 0..65535
int -32768..32767
unsigned int 0..65535
long -2147483648..2147483647
unsigned long 0..4294967295
11. Balochistan University of IT & MS
A note on ints
• Suffixes can be appended to an integer
constant to specify its type.
– Can be used with int, long, or unsigned long
Example Type Suffix
u or U unsigned 37U
l or L long 37L
ul or UL unsigned long 37UL
12. Balochistan University of IT & MS
Integer Overflow
int b = 2000000000, c =2000000000;
a = b + c; /* out of range */
• At run-time the variable a may be assigned an
incorrect value. The logical value of the
• expression b+c is 4 billion, which is greater
• than the maximum value which can be stored
in a variable of type int.
• This is known as integer overflow.
13. Balochistan University of IT & MS
Integer Overflow
• How do we fix this problem?
• What if we declare:
long a, b = 2000000000, c = 3000000000;
– Does this work?
• What about trying:
unsigned long a, b = 2000000000, c = 2000000000;
14. Balochistan University of IT & MS
Floating Point Numbers
• A real or floating-point number has an integral
part and a fractional part which are separated
by a decimal point.
e.g. 0.01 70.456 3e+5 .5067e-16 3.14159
• Floating-point numbers can be either positive
or negative
– Floating-point numbers in C are always signed
• ANSI C provides three floating types:
1. float
2. double
3. long double.
15. Balochistan University of IT & MS
Floating Point Numbers
• Exponential (scientific) notation can be used to
represent real numbers:
e ± exponent
123000.0 = 1.23x105 = 1.23e5 = 1.23E5
• e or E is read as “times 10 to the power”
e.g. 0.34e-4 = 0.000034
2500 = 25e2
e.g. 333.7777e-22
Integer Fraction Exponent
333 7777 e-22
16. Balochistan University of IT & MS
Floating Point Numbers
Examples
Number Scientific Notation Exponential Notation
1000000000 1.0x109 1.0e9
123000 1.23x105 1.23e5
322.56 3.2256x102 3.2256e2
0.000056 5.6x10-5 5.6e-5
17. Balochistan University of IT & MS
A note on floats
• A suffix can be appended to a floating
constant to specify its type:
Suffix Type Example
F f or F float 3.7
l or L long double 3.7L
• The working floating type in C is double,
not float
18. Balochistan University of IT & MS
float
• The type float denotes normal precision
real numbers.
• Single precision floating-point numbers
are declared using:
float variable_name;
19. Balochistan University of IT & MS
double
• The type double denotes double
precision real-numbers.
• Double precision floating-point numbers
are declared using:
double variable_name;
• The qualifier long double denotes
extended precision.
long double variable_name;
20. Balochistan University of IT & MS
Examples of Floating-Point
Numbers
• The following are examples of valid
floating point numbers in C:
3.14159 /*of type double*/
314.159e-2F /*of type float*/
0e0 /*equivalent to 0.0*/
1. *equivalent to 1.0*/
21. Balochistan University of IT & MS
Examples of Floating-Point
Numbers
• The following are examples of invalid
floating point numbers in C:
3.14,159 /*comma not allowed*/
314159 /*decimal point or
exponent needed*/
.e0 /*integral or
fractional part needed*/
22. Balochistan University of IT & MS
Floating Point Types in C
• The possible values that a floating type can be
assigned are described in terms of attributes
called precision and range.
– Precision describes the number of significant
decimal places that a floating value carries.
– The range describes the limits of the largest and
smallest positive floating point values that can
be represented in a variable of that type.
– e.g. a float has precision of six significant
figures
23. Balochistan University of IT & MS
Floating Point Types in C
On a 16-bit System
• For example:
x=123.456789 /*9 significant digits*/
results in x being assigned the value:
0.123456x103
Type Approximate
Range
Significant
Digits
float 10-37..1038 6
double 10-307..10-308 15
long double 10-4931..10-4932 17
24. Balochistan University of IT & MS
Floating Point
• How small is 10-37?
• The mass of one electron is
approximately 10-27 grams, 10-37 is one
ten-billionth of 10-27
• How large is 1037?
• If you multiply the diameter of the Milky
Way galaxy in kilometers by a trillion
(1,000,000,000,000), the result is just
one ten-thousandth of 1037
25. Balochistan University of IT & MS
char
• The type char represents single
characters.
– A very short integer
• Characters are enclosed in single quotes
( ‘ ).
– ‘A’, ‘a’, and ‘!’ are character constants
• Characters are declared using the
statement:
char variable_name;
26. Balochistan University of IT & MS
char
• An example of a character value is the
letter A. An example of declaring a
character variable called letter is,
char letter;
letter = 'A';
• Note the assignment of the character A
to the variable letter is done by enclosing
the value in single quotes.
27. Balochistan University of IT & MS
char
• The backslash character ( ) is called
the escape character.
– It is used to signal that a special character
follows.
– For example ’ can be used to assign an
apostrophe to a character variable.
• Note:
– While characters are enclosed in single
quotes ( ‘ ), a different type, the string, is
enclosed in double quotes ( “ ).
28. Balochistan University of IT & MS
Character Types in C
On a 16-bit System
Type Approximate Range
char (signed) -128..127
unsigned char 0..255
29. Balochistan University of IT & MS
char – special characters
Character Name Meaning
b Backspace Move the cursor to the left
by one character
n New line Go to next line
r Return Go to beginning of current
Line
t Tab Advance to next tab stop
’ Apostrophe Character ‘
” Double quote Character “
Backslash Character
30. Balochistan University of IT & MS
Couldn’t a double be used for all
numbers?
• Is having more than one numeric type
necessary?
– int versus double
• Operations involving integers are faster
• Less storage space is needed
• Operations with integers are always precise
• There is some loss of accuracy with floating-
point numbers
31. Balochistan University of IT & MS
Couldn’t a double be used for all
numbers?
• With doubles, a larger range of numbers can
be represented as compared to type int.
• For example:
– An 16-bit grayscale image of size 1024 x 2048
contains 2,097,152 pixels
• int @ 2 bytes per pixel = 4,096kb = 4mb
• double @ 8 bytes per pixel = 16mb
– If the image were color (Red, Green, Blue),
storage would be increased threefold.
• int → 12mb
• double → 48mb
32. Balochistan University of IT & MS
Type Qualifiers
• The keyword const is called a type
qualifier because it is used in
declarations to tell the compiler how
identifiers can be used
33. Balochistan University of IT & MS
const
• Sometimes you want to use a value that
does not change, such as π.
const float PI=3.1415927;
• The general form of a constant
declaration is:
const type variable = value;
• Constants must be initialized at
declaration time and can never be
changed.
34. Balochistan University of IT & MS
const
• Note:
– It is usual to initialize a const with a value as
it cannot get a value any other way.
• Examples of constants:
const float PI = 3.1415927;
const int TRUE = 1;
const char letter = 'a‘;
35. Balochistan University of IT & MS
Note
• The standard header file limits.h defines
constants for the various integer
numerical limits.
eg: CHAR_MIN, CHAR_MAX,INT_MAX etc.
• The standard header file float.h defines
constants for the various floating-point
numerical limits.
eg: FLT_MIN, FLT_MAX, DBL_MIN etc.
36. Balochistan University of IT & MS
Overflow Error
• The following will work on a Unix system, but
will fail on a PC:
int zip;
…
zip = 92126;
• Why does this fail?
– On Unix systems, integers are 32 bits, providing
a range of –231.. 231-1.
– On the PC, integers are 16 bits , so the range is
215.. 215-1