Presentation is loading. Please wait.

Presentation is loading. Please wait.

Integer VariablestMyn1 Integer Variables It must be possible to store data items in a program, and this facility is provided by variables. A variable is.

Similar presentations


Presentation on theme: "Integer VariablestMyn1 Integer Variables It must be possible to store data items in a program, and this facility is provided by variables. A variable is."— Presentation transcript:

1 Integer VariablestMyn1 Integer Variables It must be possible to store data items in a program, and this facility is provided by variables. A variable is an area in memory that is identified by a name that the programmer supplies, and which can be used to store an item of data of a particular type. The name can consist of any combination of upper or lower case letters, underscores, and the digits 0 to 9, but it must begin with a letter (or an underscore). Generally variables’ names should be indicative of the kind of data that they hold.

2 Integer VariablestMyn2 Suppose we want to use a variable to record how many apples we have: int apples; The statement above is called a declaration because it declares the name apples. The same statement is also called a definition, because it causes memory to be allocated for the variable apples. At the time of definition it is also possible to give an initial value: int apples=10; Always initialize your variables when you define them!

3 Integer VariablestMyn3 The type of an integer variable will determine how much memory is allocated for it, and consequently the maximum and minimum value that can be stored in it. There are four basic types of integer variables: Type NameTypical Memory per Variable char1 byte short2 bytes int4 bytes long4 or 8 bytes

4 Integer VariablestMyn4 Variables of type short, type int and type long can store negative as well as positive values, so you can also write them as signed short, signed int and signed long. An unsigned integer variable can only store positive values, and those types are unsigned short, unsigned int and unsigned long.

5 Integer VariablestMyn5 10000000000000000111111111111111 00000000000000001111111111111111 16 bit signed integers: Minimum=-32768 Maximum=32767 16 bit unsigned integers: Minimum=0 Maximum=65535 sign bit

6 Integer VariablestMyn6 #include "stdafx.h" #include using namespace System; using namespace std; int main(array ^args) { cout<<"Minimum value for type short: " <<SHRT_MIN<<endl; cout<<"Maximum value for type short: " <<SHRT_MAX<<endl; cout<<"Maximum value for type unsigned short: " <<USHRT_MAX<<endl; cout<<"Minimum value for type int: " <<INT_MIN<<endl; cout<<"Maximum value for type int: " <<INT_MAX<<endl; return 0; }

7 Integer VariablestMyn7


Download ppt "Integer VariablestMyn1 Integer Variables It must be possible to store data items in a program, and this facility is provided by variables. A variable is."

Similar presentations


Ads by Google