C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Monday, October 8, 2012

Constants in C Plus Plus Programs

Constant may be defined as a quantity used in a program that cannot be changed during program execution. There are two types of constants in C++ programming language.
  1. Literal Constants
  2. Symbolic Constants
types of constants in C++

1. Literal Constants

A literal constant is a value that is directly typed in a program. For example 100 or 10.50 are examples of literal constants. Literal constants can be further divided into the following categories according to the type of value:
  1. Integer Constant: positive or negative whole numbers like 100, 999, 2, 5056, 10, -233, -50 are called integer constants.
  2. Real Constants: numeric values with fractional parts like 2.45, 999.99, -5.25, -6.023 etc are called Real constants or floating point constants.
  3. Character Constants: a single character in single quotes like 'a', 'p' or '=' are called Character constants.
  4. String Constants: a set of characters in double quotations like "Enter a number" or "Asad" is called a string constant.

2. Symbolic Constants

A symbolic constant may be defined as a name given to a constant quantity in a program. A symbolic constant can be useful to make a program more readable and easier to understand. A symbolic constant is defined for a constant literal value that is frequently used in a program. For example, in a mathematical program we can use PI as symbolic constant instead of the value 3.1415.

How To Declare a Symbolic Constant?

A symbolic constant can be defined in a C++ program, in two ways:
1. const Qualifier
const qualifier is used to define a constant in C++ programs. The syntax is as under:
 const datatype identifier = value
For example, to define PI, we will use the following C++ statement
const float PI=3.1415;

Example C Plus Plus Program Const Qualifier

/*
1. Symbolic Constants in C++
2. Use of const qualifier
Program to input radius and calculate
circumference of circle by formula=2xpixr
By: Mahmood Alam, Instructor(IT)
Govt. Post Graduate College of Commerce,
Rahim Yar Khan
*/
#include<iostream.h>
#include <stdio.h>
#include <conio.h>


int main()
{
float radius, circum;
const float PI=3.1415;
cout<<"Enter radius=";
cin>>radius;
circum = 2 * PI * radius;
cout<<"circumference="<<circum;
getch();
return 0;
}
2. define DIRECTIVE
define preprocessor directive is also to define a constant in a C++ program. The syntax to define a symbolic constant PI with define directive is as follows:
 #define identifier  value
For example to define a symbolic constant PI with define directive, we will type:
#define PI 3.1415

Example C Plus Plus Program #define Directive

/*
1. Symbolic Constants in C++
2. Use of #define directive
Program to input radius and calculate
circumference of circle by formula=2xpixr
By: Mahmood Alam, Instructor(IT)
Govt. Post Graduate College of Commerce,
Rahim Yar Khan
*/
#include<iostream.h>
#include <stdio.h>
#include <conio.h>
#define PI 3.1415

int main()
{
float radius, circum;
cout<<"Enter radius=";
cin>>radius;
circum = 2 * PI * radius;
cout<<"circumference="<<circum;
return 0;
}

The Difference between defining a symbolic constant by const qualifier or #define directive is:
Sr.No. const qualifier #define directive
1 Here we need to define data type of constant here we do not need to define data type of constant
2 = symbols is used = symbol is not needed
3 This statement is terminated by semi colon. No semi colon at end
Share:

Basic C Plus Plus Data Types

Data Type specifies what type of data will be stored in a variable and what type of operations can be performed on it. So, the data type of a variable my be defined as the set of values and operations on these values. For example, if we want to use whole numbers data in our C++ program, we will use int data type variables. int stands for integer data type. int data type has
  1. a set of values   -32768 to +32767
  2. and predefined operations on these values like +(addition), - (subtraction), *(mutplication) and / (division) etc.
Therefore, it is clear from the above, that we can store a whole number from -32768 to 32767 in an integer variable. Moreover, we can use addition, subtraction, multiplication and division operations on integer variables.
Basic data Types in C++
There are the following data types in C++.
  1. integer data types ( int, short int, long int, unsigned int, unsigned long int)
  2. real data types (float, double, long double)
  3. char

1. Integer Data Types

Integer data types deal with whole numbers. We will define integer variables to store and operate whole numbers. Integer data type is used to define integer variables. integer variables can hold whole numbers that is numbers without fractions or decimal point. For example 100, 8, 991, -2, 786 and 32400 are whole numbers. These whole numbers will range from -32768 to 32767.
Now we can divide integer data types in to some categories according to the memory space they take and the range of whole numbers they can deal with.
  •     int data type
int data type deal with whole numbers. in MS-DOS, a variable of int data type takes two bytes in memory. It can store values between the range -32768 to +32767.
  •     short int data type
short int data type can store whole numbers from -32768 to +32767. A variable of short int data type will take two bytes in memory.
  •     long int data type
long int data type is used to store large whole numbers. It takes four bytes in memory. Its range is from -2,147,483,648 to 2,147,483,647.
  •  unsigned int data type  
It is used to store only positive whole numbers from 0 to 65,535. Its variable takes two bytes in memory.
  •  unsigned long int data type  
It is used to store only positive whole numbers from 0 to 4,294,967,295. Its variable takes four bytes in memory.

2. Real Data Types

Real data types deal with real numbers, that is, numbers with factional parts or decimal points like 4.50, -90.20, 2.49 and 999.99 etc.
  • float data type
float data type deals with real numbers. A variable of float data type will hold four bytes in memory. It can store real values from 3.4x10-38 to 3.4x10+38. A float variable can store real numbers with accuracy of 6 decimal places.
  • double data type
double data type deals with large real numbers. A variable of double data type will hold eight bytes in memory. It can store real values from 1.7x10-308 to 1.7x10+308. Moreover, a double variable can store real numbers with accuracy up to 15 decimal places.
  • long double data type
double data type deals with very large real numbers. A variable of long double data type will hold ten bytes in memory. It can store real values from 1.7x10-4932 to 1.7x10+4932. Moreover, a long double variable can store real numbers with accuracy up to 19 decimal places.

3. Character Data Type

char data type is used to store a single character value. It can store a letter A-Z or a-z, a number 0-9 and some special characters or symbols. A character variable (variable of char data type) takes one byte in memory. It can store a single character. Character value is enclosed in single quotes like 'a' or 't' or '#' etc.
Share:

EasyCPPprogramming.blogspotcom

Labels