In this tutorial you will learn about variables, constants and keywords in C.
Variables in C
In a typical C program we have to do a lot of computation. Of course there will be storing of some data in different locations of memory in computer. These memory locations are identified by their address like 56234. Suppose if programmer wants to access the particular locations like 10 times in a program to store another value at that location.
So It will become a tedious job for a programmer if he have to memorise the numerical address name. So to make this job easy we use variables.
So variables are nothing but the name of the locations or addresses of memory which is given by programmer.
Constants in C
In the above picture (1st) we have stored the constant value 3 at x location. The name of that location is x. It’s a variable. We can also store another value at x location.
Here X = variable (location or memory address name)
3 = constant
Try to understand the second example yourself if you have any doubt, do comment below.
There are two type of Constants
- Primary constants
- Secondary constants (We will learn them later)
At this stage we will only discuss primary constants. Primary constants are of three types.
- Integer constants
- Real constants
- Character constants
Integer Constant
Character Constant
Real Constant or Floating Point Constant
Types of Variables
So it is quite obvious types of variables is similar types of constants. Eg: int x= 1;
Rules for writing variable names
- A variable name may contain alphabets, numbers and underscores.
- No other special character (other than underscore) can be used for writing variable name.
- A variable name should start with either underscore or alphabets.
- No space is allowed while writing variables.
Keywords in C
A question which may arise in your mind that, how computer will know that its integer variable or character variable or anything else?
The simple answer is with the help of keywords. In one of the above example I have used “int” keyword. Eg: int x=1
In this example “int” is a keyword and it will tell the computer that “x” will be an integer variable and it will only store integer constant.
There are 32 keywords used in C language which are given below. We will discuss them in later tutorials.
Keywords in C |
Video Tutorial
Watch below video tutorial to learn more about variables and keywords in C.
hi, i am vishnu
..i am confusing in constant here is variable is same x but the constant is changed 3 to 5 … but according to the definition constants do not change …….
please help me i could not understand by this example
Don’t get confused, it is constant value. You are right but that is constant variable, its value do not change. Constant value and constant variable, both the things are different.
bro don’t get confuse constant means —- whose value will not change in execution ……….so in 1st example we have taken 3 as a constant ….so 3 will not change at the time of execution…..and in 2nd example 5 is taken as constant…….. so 5 will not change at the time of execution … I hope it helps……
there are two examples,on is x=3 where 3 is const,and another one is x=5 where 5 is constant.
dear vishnu dont get confused that is the memory location that can store any number, but if we put the number 5 than this could not be changed by the compiler.
Is there any difference between variable and identifier????
Identifier can be variable names, function names, array names, etc. Means all comes under category of identifier.