Variables and Constants in C Programming

Variables

A variable is a named location in memory that is used to hold a value that may be modified by the program.

A variable is an identifier that is used to represent some specified type of information within the designated portion of a program. The information (data items) must be assigned to the variable at some point in the program. The data item can then be accessed later in the program simply by referencing to the variable name.

Thus, the information represented by the variable can change during the execution of the program. So, quantity that may vary during program execution is called variable. Each variable has a specific storage location in memory where its numerical value is stored. These locations can contain integer, real, or character constants.

Declaration of Variables

In C programming, all variables that are used in the program must be declared at first in variable declaration section.

Syntax for Declaring Variables


data_type var;

or

data_type var1, var2, var3, ...., varN;

or

data_type variable_name;

or

data_type comma_separated_list_of variables;

Examples of Variable Declaration


1.	int a;

2.	int var1, var2, count;

3.	float ram, size;

4.	char ch, op, var3;

5. double area, num1, factorial;

6. long int x, y, z;

Rules for Creating Valid Variables

Following rules must be followed while creating variables in C Programming :

  1. In ANSI standard 31 characters long variables are allowed. (But some implementation of C only recognizes 8 characters). It would be better to follow the rule of 8 characters.
  2. While naming variables first character should always be letter.
  3. Both uppercase and lowercase letters may be used and are they are recognized as differently. For example: COUNT and count are two different variables.
  4. Only underscore ( _ ) is allowed among special symbols and is treated as letter.
  5. Space and other symbols are not allowed and they are invalid.
  6. Meaninful name should be given based on context.

Examples of Valid Variables

area

test1 

my_count

fx

for1

Examples of Invalid Variables

a  rea 		(space can not be used)

1test 		(must start from letter)

my-count 	(hyphen is not allowed)

f(x) 		(parantheses are not allowed)

for 		(keyword can not be used as an identifier)

Constants

In C program, there are some entities that do not change during the execution of program. Any entity that don not change during program execution are known as constants.

Constants are fixed value that remain unchanged during the execution of a program. There are different constants in C programming, they are:

  1. Integer Constants : Examples: 1334, -9746 etc.
  2. Floating Point or Real Constants : Examples: +325.34, 426.0, 32E-89, 35e+56 etc.
  3. Character Constants : Examples: 'A', '5', '+', '?' etc.
  4. String Constants : Examples: “This is C and C++ Programming language.”, “green”, “My name &&&!!! ???”, “$2525” etc.

Declaration of Constants

In C programming, we can also make named memory location to hold constant value that does not change during program execution. To use these type of constants we need to declare and initialize them first.

Syntax for Declaring Constant Using const


const data_type constant_name = constant_value;

Examples of Declaring Constants


1.	const int a = 10;

2.	const float pi = 3.141592;

3.	const char op = ‘+’;