In this section, we will learn the use of variables and constants in C programming language.
What are Variables in C language?
- Variables are data names that are used to store data during the execution of a program.
- A variable can store data value that can be modified at different stages during the execution of a program.
- A variable name helps users to depict the true nature or function of a data name in the program.
- A user can choose a name that is meaning to the nature of its function in the program.
How to use variables?
- Variables must begin with a letter or underscore.
- The length of a variable name should not be more than eight characters.
- Uppercase and lowercase variable names are treated differently by the compiler. E.g. Score is not the same as the score or SCORE.
- Keywords cannot be used as variable names.
- Whitespaces are not allowed while naming a variable name.
How to declare variables?
Step 1: Declare the data type
Step 2: Name the variable
Step 3: Declaration must end with a semicolon
Syntax: data-type var1,var2,var3,…;
int sum; int total,number;
Here, int and float are the data types and sum, total, number are variable names. Each of the statements is ending with a semicolon ‘;‘.
How can we name a variable in a program?
Some examples of the variable names are given below:
What are Constants in C language?
Constants in C are fixed values that cannot be changed during the execution of a program.
There are several types of constants that are supported in C:
- Integer constants
- Real constants
- Single character constants
- Strings constants
- Backslash character constants
What are Integer Constants?
It consists of a sequence of digits.
There are three types of integers:
- Decimal integer – It consists of a set of digits, 0 to 9, optionally either having – or + sign. E.g. – 1234, -456, 45621, +23.
- Octal integer – It consists of a combination of digits from 0 to 7, starting from digit 0. E.g. – 012, 0163, 0.
- Hexadecimal integer – It consists of a sequence of digits preceded by 0x or 0X. It may also consist of an alphabet from A to F or to f. E.g. – 0x6f, 0Xcsd, 0Xd.
What are Real Constants?
It consists of fractional parts like 21.342 to be used for data having decimal in it. It is used to represent quantities that vary such as distances, heights, prices, etc.
Mantissa E Exponent
- It is either an integer or a real number expressed in decimal notation.
- It may include plus or minus signs.
- The word e between mantissa and exponent can be written in lowercase or uppercase.
- It represents a real number in floating point form. E.g. – 0.25e3, 11e+6, 3.5E-4, -2.8E-5
- Exponential notations are used to represent numbers either very large or very small in magnitude.
Below are the examples of numeric constants:
What are Single Character Constants?
- It consists of a single character enclosed within a pair of single quotation marks. E.g. – ‘7’,‘K’,‘;’
- It can be used to perform arithmetic operations on character constants, since each character constant represents an integer value.
What are String Constants?
- It consists of a sequence of characters enclosed in double quotes.
- It can include letters, numbers, special characters and blank space. E.g. – “Hey”,“2020”,“GOOD LUCK”,“7+9”,“K”
- It is not the same as character constants. E.g. – ‘X’ and “X’ are not the same.
- It does not have an equivalent integer value like character constant.
- It is mainly used to create meaningful programs.
What are Backslash Character Constants?
C programming language supports backslash character constants that are used in performing output functions.
- It is used to perform output functions like new line, backspace, single quote, null, question mark, etc.
- It represents only one character, even if it has two characters.
- The character combinations are called escape sequences.
A list of backslash character constants are listed in the table below:
How can we use Variables and Constants in a program?
Below is an example to show the use of variables and constants in a program.
Difference between Variables and Constants