Programming in C Constants Data Types and Variables
Programming in C++ Constants, Data Types and Variables Computer Science Department
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 Computer Science Department
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 Computer Science Department
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 +300. 25 221. 005 -19845. 0 Computer Science Department
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’ Computer Science Department
Data Types • 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) Computer Science Department
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. Computer Science Department
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. Computer Science Department
Integer Variables Example #include<iostream. h> void main ( ) { int var 1; int var 2, var 3; var 1 = 20; var 2 = var 1 + 10; cout<<“Result =”; cout<<var 2<< endl; } Computer Science Department //define var 1 //define var 2, var 3 //assign value to var 1 //assign value to var 2 //displaying the sum of var 1 + 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. Computer Science Department
Character Variable Example #include<iostream. h> void main ( ) { charvar 1 = ‘A’; charvar 2 = ‘t’; cout << charvar 1; cout << charvar 2; charvar 1 = ‘B’; cout << charvar 1; } Computer Science Department //define char variable // display character //reassigning charvar 1 to B
Floating point Types • Floating point variables represent number with a decimal place e. g. 3. 14787 or 10. 2 • There are three kind of floating point variables in C++ – float – double – long double Computer Science Department
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. Computer Science Department
float #include<iostream. h> void main ( ) { float rad, area; float PI =3. 14; cout << “Enter radius of circle”; cin >> rad; area = PI * rad; cout <<“Area is” << area << endl; } Computer Science Department
Variable Type Summary Keyword Low Range High range Digits of precision Bytes of Memory Char -128 127 n/a 1 short -32, 768 32, 767 n/a 2 int -2, 147, 483, 648 2, 147, 483, 647 n/a 4 long -2, 147, 483, 648 n/a 4 float 7 4 double 15 8 Computer Science Department
- Slides: 15