C Language

Variables and Constants in C

In C Language by Baqir AliLeave a Comment

What is a Variable?

A variable is a memory location where we store our data/constants. During coding calculations as a computer needs to store data/constants somehow while processing in registers. The same is the case with applications it needs to store, retrieve data/constants while the application is running. A variable should have a name and a memory location or address. A very clear example is entering data/constants into the calculator and keeps calculating and storing the previously entered numbers.

We can consider a variable name as a name that we give to the location in memory. Within that memory our program stores the constants therefore the contents of the variable can change or the variable itself.

From the above paragraph, it is clear that we need two major concepts. Firstly the value (Constant) and secondly the place the value should be stored (Variable).

What is Constant?

A constant is a quantity that does not change therefore we can call it values that we store inside the variable or computer memory.

There are many ways to define constants in c language.

#define pi 3.1416
or
const float pi=3.1416

in the above example in both cases, you can not change the value of pi which is defined as constant.

Variable Example

Here is an example of variable 3x+y=20, 3 and 20 cannot change, we call them constants, X & Y can vary or changes.

Consequently, we use variables to reserve some space in a computer’s memory for easy reference to the data.

#include<stdio.h>
void main(void)
{
   int a;
   int b,c;
   a=10;b=20;
   c=a+b;
   printf("The sum of a and b is %d",c);
}

A visual example of the above coding in C.

Variable in C

Example 1:

Output is:

The sum of a and b is 30

Here a,b,c are variable names or identifiers, and 10, 20 are constants or literals. similarly, 001 and 011 are the addresses we can not change.

Get The Coding Examples From C Language Code Example Page and if you want to know more about C language visit our Complete C Course or C documentation.

Variables and Constants Live Code

Support us by sharing this post

About the Author

Baqir Ali

Facebook Twitter

Hi, I am Baqir Ali ---- Blogger, Programmer, and a Teacher. I am currently working as a Programmer for Softnat Technologies. I have tons of ideas and data to share with you all via blogging.

Leave a Comment