Variables and Constants in C Programming

Variables

  • A meaningful name given to a data storage location in the computer memory is known as a Variable.
  • When a variable is used it actually refers to a address of the memory where the data is stored.
  • A variable name can comprise of letters, digits and underscore characters.
  • The first character has to be an alphabet.
  • There should be no commas or blank spaces between the variable names.

Variable declaration

All the variables that we wish to to use in the program need to be declared.

Syntax:
data-type  variable-name;

In the above syntax a data-type is declared along with a variable name.

Meaningful names should be given to the variables.

Example: A simple variable declaration.
int emp_id;
float salary;


Variables can be declared anywhere in the program according to their needs.

Multiple variables can also be declared in one statement but the data-type has to be the same.

Example: Multiple variable declaration
float salary, bonus;

Generally variables are declared in three places as follows:
1. Local variable is declared inside the function.
2. Formal parameters are declared in the definition of the function parameters.
3. Global variables are declared outside all the functions.

Constants


The identifiers whose value does not/ cannot change are known as Constants.

Variables can change their values but constants will never change their value.

Types of constants

There are three types of constants:

1. Integer constants
  • An integer quantity which contains a sequence of digits is known as an Integer constant. There are no decimal points.
  • They can be either positive or negative.
  • Blanks and commas are not allowed.
  • Its value must lie within the range of its values.
Example: A decimal integer which consists of any combinations of the digits from 0 to 9.
0  5250   9990

Example: An octal integer constant which is a combination of digits from 0 through 7. Its first digit should always be a 0.
0    03   0567

Example: Hexadecimal integer begin with 0x or 0X followed by the combination of the digits from 0 through 9 and a through f.
0x   0x4   0xcdf

Certain suffixes are given to the constants.

The suffixes are given for the following types.
Long – I or L
Unsigned long – ul or UL.

Example:
6000U            unsigned (decimal)
0x3000U        unsigned (hexadecimal)
3421ul            unsigned long(decimal)
0325U            unsigned (octal)

2. Floating point constants
These type of constants contain a decimal point or an exponent. They can be either negative or positive. Commas or blanks are not allowed within a real constant.

Example: Floating point constants
+3.2f-4     -0.3e-3    325.0     -33.75

Unlike the integer constants they too have a suffix for the following type:

Float – f or F
Long double – I or L

3. Character constant
They consist of a single character which are enclosed in single quotes.

Example: Character constant
'b'  '@'  '5'

The characters are stored by using the machines character set using the ASCII code.

Constants declaration

A constant can be declared by using the const keyword and assigning it a value.

Example: Simple constant
const float pi = 3.14;

The const keyword tells us that the value of pi can never be changed.

Other way of declaring the constant is by using the pre-processor command define. #define can be placed anywhere in the program

Example: declaring a constant by using a pre-processor
#define pi 3.14159