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

As its name suggests constants are the values which will never change during the execution of program.
Sounds confusing? Let’s try to make things more clear with a simple example.
Variables 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

1. Primary constants
2. Secondary constants (We will learn them later)
At this stage we will only discuss primary constants. Primary constants are of three types.

a. Integer constants
b. Real constants
c. Character constants
Let’s discuss them one by one.

Integer Constant

Yes it will contain only integers. Remember an integer constant will never contain any decimal point. Eg: 1, 2, -43 etc

Character Constant

It is single (remember) alphabet, number or any special symbol which is enclosed in an inverted commas. Eg: ’+’, ’1’, ’a’, etc.

Real Constant or Floating Point Constant

A real constant may have any digit but it must contain one decimal point. Eg: 1.22, -54.5, 3432.13

Types of Variables

As I said earlier Variable are name of locations in memory. In that location we can store any constant like integer, character or Real. But there is some limit that an integer variable can only store integer constant, character variable can only store character constant and real variable can only store real constant.
So it is quite obvious types of variables is similar types of constants. Eg: int x= 1;

Here “int” is a keyword, “x” is an integer variable and it can only store integer constant, “1” is a integer constant.

Rules for writing variable names

1. A variable name may contain alphabets, numbers and underscores.
2. No other special character (other than underscore) can be used for writing variable name.
3. A variable name should start with either underscore or alphabets.
4. No space is allowed while writing variables.

Keywords in C

Keywords are the words whose meaning is already explained to the compiler. They cannot be used as a variable name.
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
Keywords in C

4 thoughts on “Variables, Constants and Keywords in C

  1. vishnu kumar

    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

    Reply
    1. Admin Post author

      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.

      Reply
    2. Abhishek

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

      Reply
  2. akash kumar

    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.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *