Lecture 4 - Lecture 4 Monday Sept 9, 2002 Variables and...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 45 1652 a b c are variables. 1 2548 4527 are memory addresses. 45 12 57 are values stored or data. b Variables 12 2548 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 void short sizeof break switch private float continue for if delete etc...... Types of Data Data Types Integer Float double char Short int Long int Unsigned short int Unsigned long int Long double 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 1 2 4 8 Size ( bytes) Range 128 to 127 32768 : 32767 3.4e38 : 3.4e38 1.7e308: 1.7e308 Integer data types Size Range (bytes) Short Int or signed short 2 32768:32767 int 2 0 : 65,535 Unsigned short int 2 32768 : 32767 Int or signed int 2 0 : 65535 unsigned int 4 2147483648: Long int or signed long 2147483647 int 4 0 :4294967295 Data type Floating point data types Data type float double Long double Size ( bytes ) 4 8 10 Range 3.4e38 : 3.4e38 1.7e308 : 1.7e308 1.7e4932 : 1.7e4932 Character data types Data type Char or signed char Unsigned char Size ( bytes ) 1 Range 128 : 127 1 0 : 255 sizeof operator Different machines give different storage size for the data types. The previously quoted sizes and range are for the MSDOS 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; } ...
View Full Document

Ask a homework question - tutors are online