Variables and Data Types in Programming
E N D
Presentation Transcript
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 c a b 1652 2548 4527 • a b c are variables. • 2548 4527 are memory addresses. • 45 12 57 are values stored or data.
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 void short sizeof break switch private float continue for if delete etc……
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… • 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
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
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; }