Thursday, February 12, 2015
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.
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 |
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.