Variables and Constants in C Programming

Welcome Folks, in this module we are going to discuss the Variables and Constants in C Programming, Cheers to all of you for making it up here, we are yet to climb more stairs, till now we have seen the basic structure of C Programming and also about the input-output function, in this we will see the one step higher.

So, are you all ready, let’s dive into the depth of Variables and Constants in C Programming lecture.

Variables and Constants in C Programming

Variables and Constants in C Programming

In C Programming, Variables in C Programming are like a storage area or we can say it is like a container that is used to store data and that data can be changed also depending upon the requirement. Here the container means in which you store some materials in daily life. For example, you drink water and that water is stored in a water bottle, so here water is the data, and the water bottles are the container.

Similarly, in programming data is stored in a variable, the constant is nothing but the type of variable only but you can’t change the value of that variable declared as constant throughout the whole program.

Variables in C Programming

Till now, we have written a code that only gives the simple sentence written on the screen. However, Programming is not limited to here only, we can also write code that performs some useful tasks Variables in C Programming.

Variables in C Programming is nothing but a name given to a storage area or container that our programs use to store the values and also can manipulate whenever required.

Each Variable in C Programming has a specific data type, which determines the value the particular variable will hold, the size and layout of the variable’s memory, and the set of operations that can be applied to that particular variable.

There are some rules for declaring the Variables in C Programming i.e., the name of a variable can contain the letters, digits, and the underscore character. It must begin with either an underscore or with some letter. Upper and lowercase letters are considered different letters because C is case-sensitive.

A Variable in C Programming is a name, in which some value is assigned and that can be changed. For example, int num= 2; here in this example the variable name is num, which is associated with the value 2, int is a data type which represents that this variable can hold the integer values. We will cover the data types in the next module.

The syntax for declaring Variables in C Programming is: Data_type variable_name = value;

In the above syntax you all can notice the Data_type, so don’t worry about that we will cover this topic in the upcoming module, it simply talks about what type or kind of value you are going to store in the variable like an integer type, decimal type, etc.

Let’s see an example of Variables in C Programming to get a clearer idea.

#include <stdio.h>
int main() {
   int a;
   int b;
   char ch;
   float f;
   a = 20;
   b = 30;
   ch = ‘A’;
   f = 20.97;
return 0;
}

Here in the example, the int, char, and float are the data types that tell us the nature of the value which the variable is going to hold and a, b, ch, and f are the variable names that are storing a particular value, like variable a is storing a value 20 in it and a variable b is storing a value 30 in it, variable ch is storing a character A in it, and variable f is storing the value 20.97 in it. So, these are the simple ways to declare variables and assign the different values to I as per requirements.

Constants in C programming

Suppose you are writing a program and want some value to be fixed throughout the program i.e., the value should not be altered anywhere in the middle of the program for that we have Constants in C programming, with the help of this you can make the value fixed throughout the program.

Constants in C programming are something that is used for representing a fixed value. It is also called Literals. It is used to depict a particular value throughout the program.

The Constants in C programming can be defined in 2 ways:

By using the const keyword in C programming

Syntax: const data_type constant_name = value;

Example: const float PI = 3.14;

Here, PI is the variable name that is assigned with the value 3.14, and also the const keyword has been used, so the value of the variable PI has become constant throughout the program.

By using the #define pre-processor in C programming

Syntax: #define constant_name value

Example: #define PI 3.14

Here, we have used a preprocessor directive to define the constant. It is declared outside of the main function with the header files.

Difference between the Variables and Constants in C Programming

Variables in C Programming Constants in C Programming
The value can be changed whenever required. The value can’t be changed once assigned.
It can only be expressed with the data types. It can be defined in two ways. one using the const keyword and the other with #define preprocessor.
Example: int a = 20; float b = 3.14;

Example: const float PI = 3.14;
#define PI 3.14

So, in this module we have covered a lot about the variables and constants in C Programming, hope you all liked it and learned a lot. Must be excited about the next module.

Until then, keep practising, and analyzing, and Happy Learning!