C++ Variables and Constants


Variables

  • A variable is a place to store a piece of information.
  • Variables are those quantities whose value vary during the execution of the program.
  • Variable name is a name given to memory cells location of a computer where data is stored.

Declaring a Variable :


You have to tell the computer that you are going to stora a number in a variable before you
can actually do it. This is called variable declaring.
To declare a variable, you need to know what kind of information it will store like will it store
an integer number, floating number or a text-string, or something else.

This will be done by using 'DATA TYPES'.

Syntax :

Data type variable-name

Example :

int x, float y


Rules of naming variables :

  • Variable name must begin with a letter.
  • Keywords are not allowed to use as a variable name.
  • White space is not allowed.
  • Only underscore, special symbol is allowed between two characters.
  • Variable names are case sensitive, hence x and X are different.
  • These can't have special characters.
  • The length of indentifier may be upto 31 characters but only only the first 8 characters are significant by compiler.

Constants :

Constants are those quantities whose value does not vary during the execution of the program
that is, value is fixed.

C++ has two types of constants :

1. Numeric constants
2. Non numeric constant

Numeric constants :

Thses have numeric value having combination of sequence of digits i.e. from 0-9 as
alone digit or combination of 0-9 with or without decimal point having positive or
negative sign.

These are further sub-divided into two categories :

(a) Integer Numeric constant
(b) Real or float Numeric constant

Integer Numeric constant :

  • These have integer data combination of 0-9 without any decimal point.
  • It can be positive or negative.
  • Use of blank space and comma is not allowed between integer constants.
  • Example:

    420,-34 etc.

Real or Float Numeric constant :

  1. It must have at least one digit.
  2. It must have a decimal point which may be positive or negative.
  3. Use of blank space and comma is not allowed between real constants.
  4. Example:

    20.4,52.54 etc.
Character constants :

These have either a single character or a group of characters or a character with backslash
used for special pupose.

These are of following types :

1. Single character constant
2. String character constant
3. Backslash character constant

Single character constant :

  1. It is a single alphabet or a digit or a special symbol enclosed in a single quote.
  2. Maximum length of a character constant is 1.
  3. Example:

  4. 'A','R','$' etc.
String character constant :

  1. It is collection of characters enclosed in double quotes.
  2. It may contain letters, digits, special characters and blank space.
  3. Example:

  4. "bestprogrammingtutorials"
Backslash character constant or Escape sequence :

These are discussed in the topic.



     Prev                                                   NEXT

0 comments:

Post a Comment