C Variables and Constants

Contents

What are Variables?

In this Tutorial, You will learn about variables.  A Variable is a named location in a memory which holds values which may change during program execution. A variable name is a name given to the memory location where different constants are stored. The variables can only be used after its declaration.  So you must declare the variable before you use it.

Loading…

Here I have listed several rules for constructing a variable:

  1. The variable name can contain digits, letters and the underscore(_).  eg: radius, first_name etc.
  2. The first character of the variable name must be a letter. The underscore is also legal first character but it is not advised.
  3. No comma and blank spaces are allowed within a variable name.
  4. No special symbols except underscore are allowed.
  5. Case matters (that is upper case and lower case).  e.g: Area, AREA, area, AR ea are different variables.
  6. Keywords cannot be used as variables.

What are Constants?

Those quantities that never change during the execution of the program are called constants. These quantities are always stored in the same memory location.

The different kinds of constants that can be used on C are categorized on the basis of their nature.  They are explained in briefly on the basis of the category they belong to.

Non-modifiable variables can also be used in C programming.

For example,

Const double exp = 2.71828;

Here we have added a keyword const in the above program whose value cannot be changed throughout the program.

  1. Integer constant: 
    An integer constant must have at least one digit which must not have a decimal point. The constant could be either positive or negative, by default it is positive. Integer constants must not include commas and blank spaces. It occupies 2 bytes in memory.                                                                                                                                                                                                               
  2. Real constant:     
    Real constants are in two forms. They are fraction form and an exponential form. Fraction form must have at least one digit which must be a decimal point. It can either be positive or negative, by default it is positive. Real constants never include any commas and blank spaces in Fraction form and in exponential form, there will be mantissa part and the exponential part which is separated by letter ‘e’.
    For example,  3.0, 0.456E-5, 0.003456                                                                                                            
  3. Character constant:   
    A character constant is a single alphabet or a single digit or a single special symbol enclosed in a single quotation mark. The maximum length of a character constant is 1 character. It occupies 1 byte in memory. For example, ‘a’, ‘e’, ‘3, ‘{‘ etc.
  4. String constant:
    A string is a sequence of character enclosed within a double quotation mark. For example, “Welcome”.
Loading…