1 / 18

Variables and Data Types in Programming

Learn about variables, data types, and rules for variable declaration in programming. Explore integer, float, char, short int, long int, double, and long double data types with examples. Understand the usage of the sizeof operator to find the storage size of data types.

deborahkim
Télécharger la présentation

Variables and Data Types in Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 4 Monday Sept 9, 2002

  2. 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.

  3. 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.

  4. 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.

  5. 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……

  6. Types of Data

  7. 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

  8. 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’

  9. 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.

  10. 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.

  11. 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.

  12. Tabular understanding • Primary Data types

  13. Integer data types

  14. Floating point data types

  15. Character data types

  16. 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.

  17. sizeof operator contd • Usage

  18. 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; }

More Related