C++ Programming & Tutorials for Beginners

C++ Variables and Constants

Home  »  C++ Programming  »  C++ Variables and Constants


     Variables and symbolic constants allow programmers to refer to memory locations by name. Variables and constants have to be declared before they can be used. The declaration of a variable includes the data type of the variable and an optional initial value. Several variables of the same type may be declared in the same declaration.


sometype name1,name2,........;
sometype name1 = expr1, name2 =expr2,.......;

Contents

C++ Variables

    A variable is used for storing a value either a number or a character and a variable also vary its value means it may change his value Variables are used for given names to locations in the Memory of Computer where the different constants are stored. These locations contain Integer ,Real or Character Constants. For example :

int a;

here a is variable name.



C++ Constants

     Literals are the most obvious kind of constants. Constants in C++ are fixed values those are not changed during the Execution of program for example

int a = 10;

Literal constants can be divided in Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.

Integer Numerals
    Integer Constants refers to a Sequence of digits which Includes only negative or positive Values and many other things those are as follows :

  1. An Integer Constant must have at Least one Digit
  2. it must not have a Decimal value
  3. it could be either positive or Negative
  4. if no sign is Specified then it should be treated as Positive
  5. No Spaces and Commas are allowed in Name


Real Constants :-
  1. A Real Constant must have at Least one Digit
  2. it must have a Decimal value
  3. it could be either positive or Negative
  4. if no sign is Specified then it should be treated as Positive
  5. No Spaces and Commas are allowed in Name
  6. Like 251, 234.890 etc are Real Constants

    In The Exponential Form of Representation the Real Constant is Represented in the two Parts The part before appearing e is called mantissa whereas the part following is called Exponent.
  1. In Real Constant The Mantissa and Exponent Part should be Separated by letter
  2. The Mantissa Part have may have either positive or Negative Sign
  3. Default Sign is Positive


Single Character Constants:-
    A Character is Single Alphabet a single digit or a Single Symbol that is enclosed within Single inverted commas. Like 'S' ,'1' etc are Single Character Constants

String Constants:-
    String is a Sequence of Characters Enclosed between double Quotes These Characters may be digits ,Alphabets Like "Hello" , "1234" etc.

Backslash Character Constants:-
    C++ Also Supports Backslash Constants those are used in output methods For Example \n is used for new line Character These are also Called as escape Sequence or backslash character Constants For Ex:

\t For Tab ( Five Spaces in one Time )
\b Back Space etc.



Declaration of variables

     In order to use a variable in C++, we must first declare it specifying which data type we want it to be. The syntax to declare a new variable is to write the specifier of the desired data type (like int, bool, float...) followed by a valid variable identifier. For example:

int a;
float a;

   These are two valid declarations of variables. The first one declares a variable of type int with the identifier a. The second one declares a variable of type float with the identifier mynumber. Once declared, the variables a and mynumber can be used within the rest of their scope in the program. If you are going to declare more than one variable of the same type, you can declare all of them in a single statement by separating their identifiers with commas. For example:

int a,b,c;



Scope of Variables

     All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous code at the beginning of the body of the function main when we declared that a, b, and result were of type int. A variable can be either of global or local scope. A global variable is a variable declared in the main body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block.

Global variables can be referred from anywhere in the code, even inside functions, whenever it is after its declaration. The scope of local variables is limited to the block enclosed in braces ({ }) where they are declared.



Initialization of variables

     When declaring a regular local variable, its value is by default undetermined. But you may want a variable to store a concrete value at the same moment that it is declared. In order to do that, you can initialize the variable. There are two ways to do this in C++:
The first one, known as c-like initialization, is done by appending an equal sign followed by the value to which the variable will be initialized:
type identifier = initial_value ;
For example, if we want to declare an int variable called a initialized with a value of 0 at the moment in which it is declared, we could write:

int a = 10;

// initialization of variables



Output:
10


Previous
Next Post »