Difference Between Variables and Constants:
What is a variable?
A variable is like a container containing the values or data of a particular data type that helps to execute the program. The value of a variable can change depending on the conditions or information given at the program’s run time. A variable can be defined in both ways, like the uppercase and lowercase letters, digits and the underscores. A variable can store the value of the different data types like integer, float, character, enum.
Rules for defining the variable name in C programming language:
- The variables are case sensitive.
- The variable name must start with an alphabet letter or underscore.
- It includes the letter, digits and underscore.
- There should not be a white space in a variable name.
- The name of a variable should not be any reserved keywords like int, float, str, char, etc.
Declaration of Variables:
It is simple to declare the variables in the C program.
// declare the variable in C.
Datatype variable_name;
Datatype variable_name1, variable_name2, vriable_name3;
int a, b, d;
int _c;
char letter;
float z ;
har str[] = “welcome”;
Initialization of Variables:
It is the simple initialization of variable in C program:
Datatype variable_name; // declaration of variable
Variable_name = 10; // define value;
Declaration and initialization of variable:
Datatype variable_name = value;
int a = 10;
float x = 5.5;
char letter = ‘A’;
int z = 1, a = 5, c = 2;
Constants:
A constant is a fixed value whose value cannot be changed during program’s execution or once the value is defined. It is also known as literal. For example, 5, 20, ‘a’, ‘Radius’, 5.2, “Welcome back”, etc. A constant can be defined in two ways, like #define pre-processor and by const keyword. The constants can be different data types, such as integer constants, floating constants, character constants, string constants and enumeration constants. Let’s understand briefly about them:
Integer constant:
An integer constant is a whole number and can be large without including any decimal points. For example, 0, 1, 2, 123, 5767, 05, 0X23, 0xFFF, etc.
Float constant:
The float constants are the part of an integer constant that containing a decimal point, fractional form and exponential form.
Here are some example of floating point constants:
0.5, 35.05, 2.3e6, 3.52f or 3.52F, PI = 3.14, etc.
Character Constants:
It is a single character constant enclosed within a single quotation mark (like ‘a’, ‘A’), called a character constants. There are some valid constants as: ‘g’, ‘D’, ‘ ‘, ‘#’.
String Constant:
It is the character set of string constants that are enclosed in a double quote. The character may be letters, numbers, special symbols and some blank space. Furthermore, a string constant contains zero, one or more continuous character in double quotation marks. For example, “Hello Friends”, “Computer”, “5987”, ” ” , “A”.
Difference between Variables and Constant in C Program:
Please find below Difference Between Variables and Constants in C language –
Variables | Constants |
---|---|
It is a variable that stores data type value in a program. | It is similar to a variable and cannot be changed during program execution. |
It is a variable that can be changed after defining the variable in a program | It is a fixed variable that cannot be changed after defining the variable in a program. |
The value of a variable can change depending on the conditions. | In constants, the value cannot be changed. |
Typically, it uses int, float, char, string, double, etc. data types in a program. | It can be express in two ways: #define pre-processor and the const keyword. |
Example: int a = 5; float radius = 5.2; char ‘A’; | Example: const int Len = 5; #define PI 3.14 |
Related Post:
Features of C Programming Language
Dynamic memory allocation in C
Difference Between Type Casting and Type Conversion in C
C Program to generate Fibonacci Triangle
C Program to print “hello” without semicolon
C Program to swap two numbers without third variable
Count the number of digits in C