Variables Vs Constants

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 straightforward to define variables in a C program.

Example

// 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

This is the basic process of declaring a variable in a C program:

Example

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".
  • Note: "A" and 'A' are different; the first one is a string constant consisting of character A and \0. While the second 'A' represents is a character constant whose integer value is 65.

Example 1: We will now develop a program that demonstrates the utilization of the #define pre-processor for defining constants in the C programming language.

Example

/* create a program to display the use of #define processor in C.
#include<stdio.h>
#include<conio.h>
#define PI 3.14
void main()
{
int radius, area;
printf(" Please enter the radius of circle\n");
scanf("%d", &radius);
area = PI * radius * radius;
printf(" Area of circle is : %d", area);
getch();
}

Output:

Output

[Program Output]

Create a program demonstrating the utilization of the const keyword in the C programming language.

Example

/* create a program to display the use of const keyword in C. */
#include<stdio.h>
#include<conio.h>
void main()
{
const int Len = 5;
const int Width = 5;
int area;
area = Len * Width;
printf(" Ares of Rectangle is : %d", area);
 getch();
}

Output:

Output

[Program Output]

Difference between Variables and Constant in C Program

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

Input Required

This code uses input(). Please provide values below: