Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constants, Data Types and Variables

Similar presentations


Presentation on theme: "Constants, Data Types and Variables"— Presentation transcript:

1 Constants, Data Types and Variables
Programming in C++ Constants, Data Types and Variables

2 Constants A constant is the quantity that does not change. This quantity can be stored at a locations in the memory of the computer. A variable can be consider as a name given to the location in memory where this constant is stored. Three Basic Types of Constant Integer Constants Real Constants String Constants

3 Integer Constant Rules for Constructing Integer Constant
An integer constant must have at least one digit. It must not have a decimal point. It could be either negative or positive. If no sign precedes an integer constant it is assumed to be positive. No commas or blanks are allowed in integer constant. Example 426 -777 +20000

4 Floating Point Constant / Real Constants
Rules for Constructing Real Constant A real constant must have at least one digit. It must have a decimal point. It could be either positive or negative Default sign is always positive. No commas or blanks are allowed in real constant. Example

5 Character Constant Rules for Constructing Character Constants
A character constant is either a single alphabet, a single digit or a single special symbol enclosed within Single inverted commas. The maximum length of a character constant can be 1 character. Example ’a’ ’5’ ’=’ ’G’

6 Data Types Built-in Data Type
A computer Program operates on data and produce an output. In C++, each data must be of specific data type. The data type determines how the data is represented in the computer and kind of processing the computer can perform on it. There are two kind of data types. Built-in data type User define data type Built-in Data Type The are already defined by C++. int ( For working with integer numbers) float (For working with real numbers having decimal points) char ( for working with character data)

7 Variables Variable is a location in memory, referenced by an identifier, that contain a data value that can be changed. Identifier: name given to a variable is known as identifier. Rules for writing Identifier The first character must be letter or underscore ( _ ). You can use upper and lowercase letters and digits from 1 to 9. Identifier can be as long as you like but only the first 250 character are recognizable in C++ Compiler.

8 Integer Variables Integer variables represent integer numbers like 1, 30,000 and -45. Integer variables do not store real numbers. Defining integer variables The amount of memory occupied by integer types is system dependent. On 32-bit system like windows 98/XP an integer occupies 4 bytes of memory. This allows an integer variable to hold numbers in the range from -2,147,483,648 to 2,147,483,647.

9 Integer Variables Example
#include<iostream.h> void main ( ) { int var1; //define var1 int var2, var3; //define var2, var3 var1 = 20; //assign value to var1 var2 = var1 + 10; //assign value to var2 cout<<“Result =”; cout<<var2<< endl; //displaying the sum of var }

10 Character Variables Type char stores integer that range in value from -128 to 127. Variables of this type occupy only 1 byte of memory. Character variables are sometime use to store number but they are much more commonly used to store ASCII characters. Character Constants Character constants use single quotation marks around a character, like ‘a’ and ‘b’. When the C++ compiler encounters such a character constant, it translates it into the corresponding ASCII code.

11 Character Variable Example
#include<iostream.h> void main ( ) { char charvar1 = ‘A’; //define char variable char charvar2 = ‘\t’; cout << charvar1; // display character cout << charvar2; charvar1 = ‘B’; //reassigning charvar1 to B cout << charvar1; }

12 Floating point Types Floating point variables represent number with a decimal place e.g or 10.2 There are three kind of floating point variables in C++ float double long double

13 float Type float stores numbers in the range of about to .
It occupy 4 bytes of memory. Double and Long Double: Double and long double, are similar to float except that they require more memory space and provide a wider range of values and more precision.

14 float #include<iostream.h> void main ( ) { float rad, area; float PI =3.14; cout << “Enter radius of circle”; cin >> rad; area = PI * rad * rad; cout <<“Area is” << area << endl; }

15 Variable Type Summary Bytes of Memory Digits of precision High range
Low Range Keyword 1 n/a 127 -128 Char 2 32,767 -32,768 short 4 2,147,483,647 -2,147,483,648 int long 7 float 8 15 double


Download ppt "Constants, Data Types and Variables"

Similar presentations


Ads by Google