Lecture 4 Monday Sept 9, 2002
Variables and Data Types ►A►A►A►A variable is simply a name given by the programmer that is used to refer to computer storage location (memory). ►T►T►T►The term variable is used because the value stored in the variable can change. ► T► T► T► The value stored is the data. ►D►D►D►Data can be of different types.
Pictorial understanding Variables ab c a b c are variables are memory addresses are values stored or data.
Rules for Variable names & declaration ► T► T► T► The variable name must begin with a letter or underscore(_), and may contain only letters, underscores, or digits. It cannot contain any blanks, commas, or special symbols, such as () &, $ # etc… ► A► A► A► A varible name cannot be a keyword ► T► T► T► The variable name cannot consist of more than 31 characters.
Keywords ► A► A► A► A keyword is a word that is set aside by the language for a special purpose and can only be used in a specific manner. Eg : auto voidshort sizeof break switch privatefloat continueforifdelete etc……
Types of Data Data Types Integer Short intLong int Unsigned short int Unsigned long int Floatdouble Long double char Signed chatUnsigned char
Definitions ► I► I► I► Integer – A zero, or any positive or negative value without a decimal point. eg : 0, 5, -19, ► F► F► F► Float – Also called real number, any positive or negative number having a decimal point eg : , -6.3, 0.34, -6.67
Definitions contd… ►C►C►C►Char – Includes : 1. l etters of the alphabet (upper and lower case). 2. T en digits 0 thro S pecial symbols – $, !, b, A etc…. 4. A single character is any one letter, digit or special symbol enclosed within single quotes. Eg ‘ A ‘ ‘ $ ‘ ‘ b’
Char contd….. ►C►C►C►Characters are stored as ASCII or EBCDIC code. ►A►A►A►ASCII – American standard code for Information Interchange. ►E►E►E►EBCDIC – Extended Binary Coded Decimal Interchange Code. ►E►E►E►Each code corresponds to a number between -128 to 128 if signed else 0 to 255 for unsigned.
Definitions contd… ► s► s► s► short int – represents fairly small integer values and requires half the amount of storage with respect to int. ► l► l► l► long int represents fairly large integer values and requires double the amount of storage with respect to int. ►U►U►U►Unsigned is used to increase the range in the positive side.
► d► d► d► double refers to same data type as float but with greater size and range of numbers. ► l► l► l► long double - To further increase size and range of numbers. ►N►N►N►Note: In some computers int and short int have the same storage size.
Tabular understanding ► Primary Data types Data Type Data TypeSize ( bytes) Range CharIntFloatdouble to : e38 : 3.4e e308: 1.7e308
Integer data types Data type Size (bytes) Range - Short Int or signed short int int - Unsigned short int - Int or signed int - unsigned int - Long int or signed long int int - Unsigned long int : : 65, : : : :
Floating point data types Data type Size ( bytes ) Range floatdouble Long double e38 : 3.4e e308 : 1.7e e308 : 1.7e e4932 : 1.7e4932
Character data types Data type Size ( bytes ) Range Char or signed char Unsigned char : : 255
sizeof operator ► D► D► D► Different machines give different storage size for the data types. ►T►T►T►The previously quoted sizes and range are for the MS-DOS environment. ►T►T►T►The sizeof operator is used to find the storage size of data types of the computer under use. ►T►T►T►The info got from this operator can be very useful when transferring a program to a different computer.
sizeof operator contd ► Usage ExpressionResult sizeof(char)sizeof(int)sizeof(float)sizeof(double)1248
Problem # 3 ► T► T► T► To find storage size of a data type ? #include<iostream.h> // preprocessor directive void main() // function main { int a =3, ss; // Variable declaration part ss=sizeof(a); // find size of int a cout<<“The size of int a is ”<<ss<<endl; cout<<“the value of a is ”<<a<<endl; }