3. Integer Types
• Values of an integer type are whole numbers.
• The int type is usually 32 bits, but may be 16 bits on older CPUs.
• Long integers may have more bits than ordinary integers; short integers may have fewer bits.
• The specifiers long and short, as well as signed and unsigned, can be combined with int to form
integer types.
• Only six combinations produce different types:
short int int long int
unsigned int unsigned short int unsigned long int
• The order of the specifiers doesn’t matter. Also, the word int can be dropped (long int can be
abbreviated to just long).
4. Signed & Unsigned Integer
• The integer types, in turn, are divided into two categories: signed and unsigned.
• The leftmost bit of a signed integer (known as the sign bit) is 0 if the number is
positive or zero, 1 if it’s negative.
• An integer with no sign bit (the leftmost bit is considered part of the number’s
magnitude) is said to be unsigned.
• By default, integer variables are signed in C—the leftmost bit is reserved for the sign.
• To tell the compiler that a variable has no sign bit, declare it to be unsigned.
• Unsigned numbers are primarily useful for systems programming and low-level,
machine-dependent applications.
5. Integer Types Defined By ANSI C
Standard
Type
Int
Unsigned int
Signed int
Short int
Unsigned short int
Signed short int
Long int
Signed long int
Unsigned long int
Long long int
Unsigned long long -
int
Typical Size in Bits
16 or 32
16 or 32
16 or 32
16
16
16
32
32
32
64
64
Minimal Range
-32,767 to 32,767
0 to 65,535
Same as int
Same as int
0 to 65,535
Same as short int
-2,147,483,647 to 2,147,483,647
Same as long int
0 to 4,294,967,295
-263 to 263 – 1
0 to 264 – 1
6. Floating Types
• C provides three floating types, corresponding to different floating-point
formats:
• float Single-precision floating-point
• double Double-precision floating-point
• long double Extended-precision floating-point
• float is suitable when the amount of precision isn’t critical.
• double provides enough precision for most programs.
• long double is rarely used.
• The C standard doesn’t state how much precision the float, double,
and long double types provide, since that depends on how numbers
are stored.
7. Floating Types Defined By ANSI C
Standard
Type
Float
Double
Long double
Typical size in Bits
32
64
80
Minimal Range
Six digits of precision
Ten digits of precision
Ten digits of precision
8. Character Types
• The only remaining basic type is char, the character type.
• Today’s most popular character set is ASCII (American
StandardCode for Information Interchange), a 7-bit code
capable of representing 128 characters.
• ASCII is often extended to a 256-character code known as
Latin-1 that provides the characters necessary forWestern
European and many African languages.
9. Operations on Characters
• When a character appears in a computation, C uses its
integer value.
• Consider the following examples, which assume the ASCII
character set:
char ch;
int i;
i = 'a'; /* i is now 97 */
ch = 65; /* ch is now 'A' */
ch = ch + 1; /* ch is now 'B' */
ch++; /* ch is now 'C' */
10. • Characters can be compared, just as numbers can.
• An if statement that converts a lower-case letter to upper case:
if (ch >= ‘a’ && ch <= 'z')
ch = ch - 'a' + 'A';
• Comparisons such as ch>=‘a’ are done using the integer values of
the characters involved.The fact that characters have the same
properties as numbers has advantages.
• For example, it is easy to write a for statement whose control variable
steps through all the upper-case letters:
for (ch = 'A'; ch <= 'Z'; ch++)…
11. Character Types Defined By ANSI C
Standard
Type
Char
Unsigned char
Signed char
Typical Size in Bits
8
8
8
Minimal Range
-127 to 127
0 to 255
-127 to 127
12. Format Specifiers for Different Data Types
DataType
Unsigned
Short
Int
Long int
Long long int
Float
Double
Long double
Char
Format Specifiers
%u
%h
%d
%ld
%lld
%f
%lf
%Lf
%c
13. Declaring Variables
Variables
Local Global
LocalVariables: Local variables will be declared inside a function . Different functions
can have variables with the same name as local variables are not known
outside their own function.
GlobalVariables: Global variables will be declared outside of all functions. It is known
by all functions.
A variables is a named memory location that can hold various values.
14. Example:
#include<stdio.h>
void f1(void);
int max; /*This is GlobalVariable*/
int main()
{ max = 10; f1(); return 0;}
void f1(void)
{ int i ; /*This is LocalVariable*/
for ( i=0; i<max; i++) printf(“%d”,i ) ; }
15. Constants
Constants refer to fixed values that may not be altered by the
program.
Integer type constant : Integer type constants are specified as
numbers without fractional component . Ex: int i = 10 ;
Floating type constant : Floating type constants are specified as
numbers with fractional component . Ex: int i= 10.07 ;
Character type constant : Character type constants are enclosed
between single quotes . Ex: char ch = ‘A’ ;
String type constant : A string is a set of characters enclosed by
double quotes. Ex: char ch = “Bangladesh” ;
16. INITIALIZE VARIABLES
A variable may be given an initial value when it is
declared.This is called variable initialization.
Examples:
1. int i = 10 ;
2. int I = 10 .07;
3. char ch = ‘A’ ;
4. char ch = “Bangladesh” ;
17. Type Conversations In
Expressions
• C allows the mixing of types within an expressions
because it has a strict set of conversion rules that dictate
how type differences are resolved.
Integral promotion : In C, whenever a char or short
int is used in an expression , its value
is automatically elevated to int
during the evaluation of that
expression .
18. Type Promotion : After automatic integral promotions have been applied,
the C compiler will convert all operands “up” to the type of the largest
operand. It is done on an operation-by-operation basis, as described
below:
if an operand is a long double
then the second is converted to long double
else if an operand is a double
then the second is converted to double
else if an operand is a float
then the second is converted to float
else if an operand is a unsigned long
then the second is converted to unsigned long
else if an operand is a long
then the second is converted to long
else if an operand is a unsigned
then the second is converted to unsigned
19. Type Conversations In
Assignments
In an assignment statement, in C, when the type of the left side is
larger than the type of the right side, the process cause no
problem. In opposite case, data loss may occur.
For Example :
Going from long long to long : 32 bits data will be lost
Going from long to int : 16 bits data will be lost
Going from int to char : 8 bits data will be lost
Going from double to int : fractional part will be lost
Going from long double to double : precision will be lost
Going from double to float : precision will be lost
20. Type Casting
Sometimes we need to transform the type of the variable
temporarily. Type cast causes a temporary type change.
General form of type cast:
(type) value
For example:
float f ;
f = 100.2 ;
printf(“%d”,(int) f ) ; /*print f as an integer*/
Here, the type cast causes the value of f (float) to be
converted to an integer