The variables and constants are the building blocks of a C program. They are used in expressions, read as user input values, help to compare values in conditional statements etc. We can build complex C programs using variables and constants.
You will learn about constants and variables, types of constants available in C, rules to construct constants and variables, declaration and definition of variables in c and an example program to practice.The Example program is written using Dev-C++ compiler version 18.104.22.168 installed on a Windows 7 64-bit system.
Constants are those program elements that do not change during the execution of a program. That’s why they are called constants.
A constant can be any one of the following.
This can be A - Z or a - z.
Any digit or digits between 0 - 9.
All kinds of special character symbols on keyboard - @, #, $, %, ^, &, *, (, ), !, etc.
Types of Constants
There are three types of constants used in C programming.
- Integer constants
- Real constants
- Character constants
Numeric constants like integers and real constants can be positive or negative. When no symbol attached to it, then it is a positive number.
The real constant contains an integer part and a decimal part.
For example, 4.1, -3.5, 334.44, -443.4434
The character constants are enclosed in single quotes. Even digits that are enclosed in single quotes are considered as a character.
for example, ‘b’, ‘H’, ‘5’, ’33’ etc.
Variables are those program elements that change during the execution of a C program. They are the variable elements, not fixed elements like constants. All values in a program are stored in memory locations. Each memory location is like a small container that holds some value. A variable is a name for this container, whose value changes frequently during the execution of a C program.
The following figure shows memory locations and a value of 10 stored at location 0x5000. This memory location has a name – Number.
The Number, in this case, s called a variable. You can always refer to the variable by its name, not by its value because the value changes continuously.
Rules to Construct Constants and Variables
There are rules to construct constants and variables.
Rules for Constants
You can use a constant directly into an expression in C programs. C programming language will allow only specific operation of a type constant.
For example, you can perform arithmetic operations on numbers, but not on characters.
Rules for Variables
There are important rules to remember before you create a variable. These rules are listed below.
Rule 2: No other special character allowed in variable names except an underscore.
Rule 3: No white space or a comma allowed in a variable name.
Rule 4: Variable name length depends on the compiler.
This is not a rule, but a best practice – always use meaningful variable names in your program.
Syntax for Constants and Variables
There is no specific syntax for a constant, you can use them directly in your programs.
Every variable must be declared and initialized before you use it in a C program. The syntax to declare a variable is
<data_type> is the type of data for the variable.
<variable_name> is a user-defined name for the variable.
Syntax to initialize a variable is
<p><variable_name> is the user-defined variable name.</p>
<value> is the data value stored in memory for that.
grade = 'A';
You can initialize a variable at the time of declaration itself.
char grade = 'B';
Note:- Every statement in C programming language must end with a semi-colon;
An Example Program
The output of the above example program is given below.
Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.
Brian W. Kernighan, Dennis M. Ritchie. 1988. C Programming Language, 2nd Edition. Prentice Hall.
Kanetkar, Yashavant. 20 November 2002. Let us C. Bpb Publications.