3/13/2012
ESc101: Variable Types
The content of these slides are taken from the lecture
slides of Prof. Arnab Bhattacharya, Prof. R.K. Ghosh,
Instructor: Krithika Venkataramani Prof. Dheeraj Sanghi and Prof. Manindra Agrawal
Semester 2, 2011-2012
1 2
Krithika Venkataramani ([email protected]) Krithika Venkataramani ([email protected])
Variables Types of Variables
Variables signify data that may be modified Each variable ‘type’ represents the domain of values
Name of a variable can contain letters, digits and underscore _ Integer: int or char
Example: i, y2k, big_name, bigger_name_2 Character: char
Boolean: int or char
Case-sensitive: camel, CAMEL and CaMeL are different
real: double or float
Name cannot start with a digit
However they can store only a subset of the domain
However,
Example: 1d is not valid
int can store numbers from -231 to 231 -1
Name can start with an underscore, but do not do so
Initial values of variables are specified as constants of the
Example: avoid valid names such as _bad
same type
Certain keywords are special
int i = 0;
They are reserved and cannot be used
double d = 1.4;
Example: main, if
char c = 'A'
3 4
Krithika Venkataramani ([email protected]) Krithika Venkataramani ([email protected])
1
3/13/2012
Example Program to Add two numbers Different Data Types
/* Program to add two numbers */ Different types are needed because one type is not suitable
#include <stdio.h> // Include headers for representing data of another type.
void main() // Main function Mixing types may result in precision loss, overflow,
{ underflow
int a=2, b=3, c; // Declare variables Application performance suffers while performing
scanf("%d",
f("%d" &&a);) // Read
R d 'a'
' ' ffrom kkeyboard
b d numerically intensive computation if inappropriate data types
are used.
scanf("%d", &b); // Read 'b' from keyboard Exceptions must be handled explicitly or they lead to errors.
c = a + b; Use of appropriate type is important both for efficiency and
correctness
printf("%d\n", c); // Write 'c' to screen
}
5 6
Krithika Venkataramani ([email protected]) Krithika Venkataramani ([email protected])
Integer Types Integer Representation
Two different int types: signed and unsigned We represent integers as binary numbers
Maximum signed int in 16 bit: 011111111111111, i,e., 215 - 1 For example: 56 will be stored as
00111000
Maximum unsigned int in 16 bit: 111111111111111, i.e., 216 - 1
Possible types to suit our needs are: How do we store negative numbers?
1st bit of memory is usually the sign bit
short int, unsigned short int, unsigned int, long int, unsigned long int.
In case of 8 bit memory
memory, only 7 bits are for magnitude
magnitude.
Similarly 32-bit memory would have 31 bits for magnitude
Hence the largest positive integer that can be stored in an integer
variable on our PCs is: 231 – 1.
Smallest number:
There are variations on storing magnitude
Overflow: Trying to store numbers outside the range
7 8
Krithika Venkataramani ([email protected]) Krithika Venkataramani ([email protected])
2
3/13/2012
Different Real Number Types Representation of Real Numbers
Real numbers of arbitrary precision cannot be represented Use the scientific notation: f * bk
Different types: float, double, long double With this notation, we need to store f and k.
double is more accurate than float We also need to decide the value of ‘b’.
1/3 is printed as 0.33333334326.. as a float, but 0.33333333333.. as a The most commonly used representation is:
double
Use 1 bit for sign
double
d bl iis usedd ffor precision
i i critical
iti l calculations
l l ti Value of b is taken as 2
By default floating point constants are stored as a double. Use 8 bits to store k (called exponent)
To force float constant should be suffixed with f, i.e., 7.5f or 7.5F. Use 23 bits to store f (called mantissa), in normalized form with
integer part of the fraction to be exactly 1 (e.g. 1.0011)
Format specifier "%lf", "%Lf" are used for using double and
long double using scanf/printf Exponent can be from -127 to +126
So the range is from 2-127 to 2126, or 10-38 to 10+38 approx.
9 10
Krithika Venkataramani ([email protected]) Krithika Venkataramani ([email protected])
Errors in representing real numbers Range of different data types
There are three types of errors: Variables are stored in a predefined space
Underflow: Trying to store exponent less than -127 A unit of storage is a Byte
A Byte has space to store a sequence of 8 binary digits
Overflow: Trying to store exponent more than 126
Different variable types have different storage space assigned
Rounding off: Storing the nearest floating point number Assignment of space is machine dependent
Floating point arithmetic Type Space assigned in Bytes Range
The hardware has to do a lot more for floating point arithmetic
char 1 - 27 to (27-1)
compared to integer arithmetic
unsigned char 1 0 to (28-1)
Do not store numbers as floating point, unless you really
short int 2 - 215 to (215-1)
need fractions
unsigned short int 2 0 to (216-1)
int 4 - 231 to (231-1)
unsigned int 4 0 to (232-1)
float 4 (approx) [10-38, 1038]
double 8 (approx) [10-308, 10308]
11 12
Krithika Venkataramani ([email protected]) Krithika Venkataramani ([email protected])
3
3/13/2012
Input and output of variables Character type
Correct type specification must be used Variable type ‘char’ used for representing characters
Type Format Specifier
Characters are special integers of much shorter size
char %c
Only 256 characters can be represented
int %d
Digits 0-9 are not represented by 00000000 - 00001001
unsigned int %u
float %f,, %g,
g, %e 0-9 represented by a continuous sequence
double %lf Similarly A-Z (a-z) also represented by a continuous
long double %Lf sequence
scanf is for input ASCII character set is most widely used
Format: scanf(“<specification>”, &<name>); specifies a standard that maps characters to numbers 0-127
E.g. c is a char: scanf(“%c”, &c); Extended ASCII assigns symbols to numbers 128-255
printf is for output ASCII and Extended ASCII use 1 Byte for storage
Format: printf(“<specification>”, <name>); Unicode includes characters from all languages of the world
E.g. c is a char: printf(``%c'', c); Unicode uses 2 Bytes
13 14
Krithika Venkataramani (
[email protected]) Krithika Venkataramani (
[email protected])
The ASCII Table Printing the Code of a Character
/*Program to print the code of a character*/
#include <stdio.h>
void main()
{
int code; //Declare variable to store the code
code = (int) getchar(); //Asking user to input the character
printf("%d", code); //printing the code of the character
}
15 16
Krithika Venkataramani ([email protected]) Krithika Venkataramani ([email protected])
4
3/13/2012
Additional data types Additional formats for octal and hexadecimal
Data type Format specifier Size (machine Range
dependent)
int %d(decimal), %i 4 bytes -231 -1 to 231 -1
Data type Format Display/ Read
unsigned int %u 4 bytes 0 to 232 -1
specifier
short int (unsigned) %hd (%hu) 2 bytes -215 -1 to 215 -1
unsigned int %o unsigned octal integer
long int (unsigned) %ld (%lu) 8 bytes -263 -1 to 263 -1
char %c %d
%c, 1 byte -128 to 127 unsigned
g int %x, %X unsigned
g hexadecimal integer
g
unsigned char %u, %d 1 byte 0 to 255 unsigned long int %lo unsigned octal integer
string %s array of -- unsigned long int %lx, %lX unsigned hexadecimal integer
characters
unsigned short int %ho unsigned octal integer
float %f, %g, %e 4 bytes 3.410-38 to 3.41038
unsigned short int %hx, %hX unsigned hexadecimal integer
double %lf, %lg, %le 8 bytes 1.710-308 to 1.710308
long double %Lf, %Lg, %Le 16 bytes ?
1 bit: 1 or 0 1 Byte: 8 bits
Note: for 32-bit machines long int and int are same 17 18
Krithika Venkataramani (
[email protected]) Krithika Venkataramani (
[email protected])