What is Constants and Variables in C++ - C Variables and Constants Full Explanation

What is Constants and Variables in C/C++ - Definition & full Explanation of Variables & Constants in C/C++ Language...




Constants and Variables in C++





Variables and Constants are important components of every programming language.


Constants and Variables in C++


Constants;

Constants are the expressions which have a fixed value. Constants are generally categorized into Literal, Boolean and Symbolic constants. They are used to ad value permanently to a variable in the program.

Consider the following line of code:

VarA = 777; 

In this line of code, 777 is a literal constant. Further categorized literal Constants into the following types.
  1.   String constant
  2.   Numeric constants
  3.   Character constants
1. String Constants: A string constant is a sequence of characters enclosed in the double quotation ("This is double quotation") makes is called string constant. Its maximum length is 256 characters. Following are some examples of valid string Constants.

“Welcome to the first C++ program”
“The result=”


Constants and Variables in C++



2.  Numeric constants: These Constants consist of positive and negative both of signed numerals. There are four types of numeric Constants. These are:
  1. Integer constant
  2. Octal constant
  3. Hexadecimal constant
  4. Floating constant

    Integer constants: integer Constants are those positive or negative signed numbers that do not contain a decimal point e.g. 2010, -230, etc.
    Floating point constants: The numeric Constants having a decimal point are called floating point Constants e.g. 33.55 and -0.22 etc. This constant can also be either positive signed or negative-signed. These Constants can also be represented in its exponential form by the use of alphabet ‘e’ or ‘E’ to denote the example of the numbers. For example.
    1. 9010E10
    2. 7810.11e-11
    3. -10.990e8
    4. -1.001e-1
    Hexadecimal constants: To represent a hexadecimal constant, start the constant with 0x or 0X, followed by a sequence of digits in the range 0 through 9 and a through f (or A through F). The digits
    a to f represent value in the range 10 through 15. For example:
       
    Int I = 0x3fff; // Hexadecimal constant
    Int j = 0X3FFF; // Hexadecimal constant

    Octal constants: Constants representation in the octal number system is called octal Constants. Octal constant begins the specification with 0, which followed by a sequence of digits in the range 0-7. Consider the following example:


    Int I = 0377; // Octal constant
    Int j = 0397; // Error: 9 is not an octal number

    Octal numbers are integer numbers in the range of digits 0 to 7.

    3. Character constants: A character enclosed within single quotes is called character constant. Consider the following examples:

    ‘A’, ‘a’, ‘.’, ‘?’ 

    Constants and Variables in C++




    Variables;


    A variable in C++ is a name for a price of memory that can be used to store information. A variable can be thought of like a mailbox where information can be put and retrieved from. All computers have memory, called Random Access Memory (RAM) that is available for programs to use. When a variable is declared, some space in that memory is reserved.
    Consider the following segment of code:

    Int a = 5;
    Int b = 2;
    Int result = a-b;

    In this segment, a, b and the result are the integer Variables that reside in RAM and store integer values.





    Previous
    Next Post »