Skip to main content

Constants

Defination:
             A quantity that cannot change its value during execution of the program is called constant.

explanation:
            constants are the expressions which have fixed value.constants  are generally categorized into: Literal,Boolean and symbolic constants.They are used to express particular values within the source code of a program.There are four types of constants in C++.These are:
  1. integer constants
  2. floating point constants
  3. character constants
  4. string constants
Integer Constants:  
                          A numerical value without a decimal part is called integer constants.The plus(+) and minus(-) sings can also be used with an integer constants.Integer constants are used in expressions for calculations.To print an integer constants,it is given in the output statement without quotation marks.For example to print integer constants 220 and 130,the output statements is written as:
                              cout<<220;
                              cout<<130;

Floating-point Constants:
                            Numeric values that have an integer as well as a decimal part are called floating-point values.These values can also be written in exponential notation.
                           In exponential notation, a floating-point constants is written as 123.6E9. The symbol E represents the exponent.A floating-point constants may be a positive or a negative value.The exponent may also be a +ve or a -ve value.

Character Constants:
                            A single character enclosed in single  quotation marks is called Character constants.For example 'a','+','/' represent character constants.

String Constants:
                           A sequence of character consisting of alphabets,digits and/or special characters enclosed in double quotation  marks is called string constants
                          For example,"Pakistan" and "Peshawar-3456" are examples of string constants.
Const Qualifier:
                     The data item that follows the keyword "const" cannot change its value during  execution of the program.
     
Example:
In the following program p has been declared as floating point constants and a value 3.14 has been assigned to it.

#include<iostream>
using namespace std;
main()
{
    int s;
     const float p=3.14;
     s=4;
     peri=4*p*s;
     cout<<"RESULT IS ="<<peri;
}


Comments

Popular posts from this blog

Programs

PROGRAM 1: #include<iostream> using namespace std; int main() {    cout<<"HELLO WORLD";    return 0; } OUTPUT OF THE PROGRAM: HELLO WORLD                 ............................................................................................................................. PROGRAM 2: #include<iostream> using namespace std; int main() {    cout<<"c++ is best language";    return 0; } OUTPUT OF THE PROGRAM: c++ is best language      .............................................................................................................................                    

Initialization Of Variables

Initialization Of Variables:   When a variable is declared,a memory location is assigned to it.The value in that memory location is  also assigned to the variable.This preassigned value of a variable,if not used by mistake,may result in incorrect result.To avoid this situation, a known value is  assigned to the variable.This value is assigned to the variable at the time of its declaration.Assigning a known value to a variable at the time of its declaration is called initializing of the variable.        For example, to declare variables a,b and c of integer type and assigning values a=220 and b=70,the statement is written as:            int a=220,b=70,c;