Lecture 4 Monday Sept 9 2002 Variables and
Lecture 4 Monday Sept 9, 2002
Variables and Data Types ►A variable is simply a name given by the programmer that is used to refer to computer storage location (memory). ► The term variable is used because the value stored in the variable can change. ► The value stored is the data. ► Data can be of different types.
Pictorial understanding a b 45 1652 Variables 12 2548 a b c are variables. 1652 2548 4527 are memory addresses. 165345 12 57 are values stored or data. c 57 4527
Rules for Variable names & declaration 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 varible name cannot be a keyword ► The variable name cannot consist of more than 31 characters. ►
Keywords ► 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 break continue etc…… void switch for short private if sizeof float delete
Types of Data Types Integer Short int Long int Float Unsigned short int double Unsigned long int Long double char Signed chat Unsigned char
Definitions Integer – A zero, or any positive or negative value without a decimal point. eg : 0 , 5 , -19 , -26351 ► Float – Also called real number, any positive or negative number having a decimal point eg : 10. 652 , -6. 3 , 0. 34 , -6. 67 ►
Definitions contd… ► 1. 2. 3. 4. Char – Includes : letters of the alphabet (upper and lower case). Ten digits 0 thro 9. Special symbols – $ , !, b, A etc…. A single character is any one letter, digit or special symbol enclosed within single quotes. Eg ‘ A ‘ ‘$‘ ‘ b’
Char contd…. . ► ► Characters are stored as ASCII or EBCDIC code. ASCII – American standard code for Information Interchange. EBCDIC – Extended Binary Coded Decimal Interchange Code. Each code corresponds to a number between -128 to 128 if signed else 0 to 255 for unsigned.
Definitions contd… short int – represents fairly small integer values and requires half the amount of storage with respect to int. ► long int represents fairly large integer values and requires double the amount of storage with respect to int. ► Unsigned is used to increase the range in the positive side. ►
Definitions contd… double - refers to same data type as float but with greater size and range of numbers. ► long double - To further increase size and range of numbers. ► Note: In some computers int and short int have the same storage size. ►
Tabular understanding ► Primary Data types Data Type Char Int Float double Size ( bytes) 1 2 4 8 Range -128 to 127 -32768 : 32767 -3. 4 e 38 : 3. 4 e 38 -1. 7 e 308: 1. 7 e 308
Integer data types Data type Size Range (bytes) - Short Int or signed short 2 -32768: 32767 int - Unsigned short int 2 0 : 65, 535 - Int or signed int 2 -32768 : 32767 - unsigned int 2 0 : 65535 - Long int or signed long 4 -2147483648: int 2147483647 - Unsigned long int 4 0 : 4294967295
Floating point data types Data type Size ( bytes ) Range float double Long double 4 8 10 -3. 4 e 38 : 3. 4 e 38 -1. 7 e 308 : 1. 7 e 308 -1. 7 e 4932 : 1. 7 e 4932
Character data types Data type Size ( bytes ) Range Char or signed char 1 -128 : 127 Unsigned char 1 0 : 255
sizeof operator Different machines give different storage size for the data types. ► The previously quoted sizes and range are for the MS-DOS environment. ► The sizeof operator is used to find the storage size of data types of the computer under use. ► The info got from this operator can be very useful when transferring a program to a different computer. ►
sizeof operator contd ► Usage Expression sizeof(char) sizeof(int) sizeof(float) sizeof(double) Result 1 2 4 8
Problem # 3 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; } ►
- Slides: 18