What is a Variable?
A variable is an identifier which is used to store some value. Constants can never change at the time of execution. Variables can change during the execution of a program and update the value stored inside it.
A single variable can be used at multiple locations in a program. A variable name must be meaningful. It should represent the purpose of the variable.
Example: Height, age, are the meaningful variables that represent the purpose it is being used for. Height variable can be used to store a height value. Age variable can be used to store the age of a person
A variable must be declared first before it is used somewhere inside the program. A variable name is formed using characters, digits and an underscore.
Following are the rules that must be followed while creating a variable:
- variable name should consist of only characters, digits and an underscore.
- In the variable name should not begin with a number.
- The variable name should not consist of whitespace.
- A variable name should not consist of a keyword.
- ‘C’ is a case sensitive language that means a variable named ‘age’ and ‘AGE’ are different.
Following are the examples of valid variable names in a ‘C’ program:
height or HEIGHT _height _height1 My_name
Following are the examples of invalid variable names in a ‘C’ program:
1height Hei$ght My name
For example, we declare an integer variable my_variable and assign it the value 48:
int my_variable; my_variable = 48;
By the way, we can both declare and initialize (assign an initial value) a variable in a single statement:
int my_variable = 48;
Constants are the fixed values that never change during the execution of a program. Following are the various types of constants:
An integer constant is nothing but a value consisting of digits or numbers. These values never change during the execution of a program. Integer constants can be octal, decimal and hexadecimal.
- Decimal constant contains digits from 0-9 .
- Octal constant contains digits from 0-7, and these types of constants are always preceded by 0.
- Hexadecimal constant contains a digit from 0-9 as well as characters from A-F. Hexadecimal constants are always preceded by 0X.
E.g of Constants are :
Example, 111, 1234 Example, 012, 065 Example, 0X2, 0Xbcd
A character constant contains only a single character enclosed within a single quote (”). We can also represent character constant by providing ASCII value of it.
Example, 'A', '9'
A string constant contains a sequence of characters enclosed within double quotes (” “).
Example, "Hello", "Programming"
Like integer constants that always contains an integer value. ‘C’ also provides real constants that contain a decimal point or a fraction value. The real constants are also called as floating point constants. The real constant contains a decimal point and a fractional value.
Example, 202.15, 300.00