Variable and Constant in C programming


In software programming language, variables are used to store data or values. A variable can store result of calculation, results of a database query etc so instead of repeating these values in multiple place in your code we can use the variable wherever it is needed in your code.


Variable and Constant  can be different across languages. Here are examples of how variables and constants are handled with C programming languages.

 What is Variable ?
1.It is a name of storage space which is used to store data.
2.It's value may be changed.
3.It always contains last value stored to it.
4.It is always declared with data type.

Variable Declaration

int rollno;
float marks;
char grade;

Here rollno is a variable of type int ,marks is a variable of type float and grade is a variable of type char
 
Variable Initialization  

When you assign a variable, you use the = symbol. The name of the variable goes on the left and the value you want to store in the variable goes on the right.

int rollno=201;
float marks=85.6;
char grade='A';

Here 201 is the value of rollno,85.6 is the value of marks and A is the value of grade. Character value is always written in single quotes.
 
Note: Make sure you don't confuse the assignment operator (=) with the equality operator (==). The individual = symbol assigns value while the == symbol checks if two things are equal.


Second way of Initialization  

int rollno;
float marks;
char grade;
rollno=201;
marks=85.6;
grade='A';

Rules to declare a variable  
1.The first letter of a variable should be alphabet or underscore(_).
2.The first letter of variable should not be digit.
3.After first character it may be combination of alphabets and digits.
4.Blank spaces are not allowed in variable name.
5.Variable name should not be a keyword.


What is Constant?
1.An element of program whose value can not be changed at the time of execution of program is called constant.
2.It is also called literals.
3.It may be int, float and character data type.

Rules for constructing integer constant

1.It must have at least one digit.
2.It must not have a decimal point.
3.It may be positive or negative.
4.The range of integer constant is between -32768 to +32767.
5.No comma or blank space are allowed in integer constant.

Rules for constructing floating point constant

1.It must have at least one digit.
2.It must have a decimal point.
3.It may be positive or negative.
4.No comma or blank space are allowed in floating point constant.

Rules for constructing character constant

1.It is a single alphabet, digit or special symbol.
2.The length of character constant is 1 character.
3.Character constant is enclosed within single quotes (Example char c='A';).

Example  

int rollno=201;
float marks=85.6;
char grade='A';

Here 201 is integer constant,85.6 is  float constant and A is character constant.
 
Here I am requesting if you found this post was helpful for you then let me know by your comment in below comment box and share it with your friend. Also you can contact me on Instagram @rajusunagar_

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thank you!!.......

Post a Comment

0 Comments