In this chapter, I would like to explain variables and keywords in C language. I hope you have already gone through Chapter 1 – Data Types and Constants in C.
![variable-declaration in C variable declaration in C](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_s3pa1PHgDfAIbSXrwSM0DmwM2gXIqhb1WnMxJXTh1bKgWcaAIFsh-9lxDdHNLEmNBLgRMAKQuXDmTPus3jKNEcRhdCkbuGIUJl-XWbTK-WVqw6bL_hEb6GlH_U2KC1FAfJJlyzC05BdJQdpEcStMq9ye2m=s0-d)
Notes:
Variables
A variable can be defined in many ways.
At the basic level, a variable can be defined as a memory location
declared to store any kind of data (which may change many times during
program execution). It can also be defined as any quantity/entity which
may vary during program execution. To identify a variable (the declared
memory location), it can be assigned a name – known as variable name. The name given to a variable is known as an identifier.
An identifier can be a combination of alphabets,digits and underscore.
There are certain set of rules which must be observed while naming a
variable.
Rules for naming a variable:-
- A variable name (identifier) can be any combination of alphabets, digits and underscore.
- First character should be a letter (alphabet).
- Length of variable name can range from 1 to 8. (Note: Different compilers may allow different ranges, say upto 31. But it is a good practice to keep the variable name short.)
- A space in between is not allowed. Ex: A variable name can not be declared as var name
- Underscore can be used to concatenate name combinations. Ex: var_name , var_123 are valid identifiers.
- No commas or other special characters (other than underscore _ ) are allowed in a variable name.
- C is a case sensitive language – which means a variable name declared as flag is not same as FLAG. They both will be treated as different variables.
- There are certain reserved words in C language, known as keywords. Words similar to a keyword can not be used as a variable name. Ex:– In the previous article on data types, we saw int, char, float etc. These are actually keywords. So you can’t declare a variable with names int, char or float.
Variable Declaration
A variable must be declared first before
we can use it in a program for manipulations. A variable is declared
with its storage class, data type and identifier. The format is shown
below:-
Storage-class Data-Type Variable-name;
Storage class is some thing we will
learn in coming chapters. Let me brief it. There are 4 storage classes
namely Automatic, Static, External and Register storage classes. Each of
them has its own meaning and usage. Storage class is used to attribute
certain features to a variable like its scope (local or global), where
to store the variable (in memory or in register), the life time of a
variable etc. It is not necessary to specify a storage class while
declaring a variable. By default all variable declarations (without any
storage class specified) will be assigned to “automatic storage class”. We will discuss more about storage class in another chapter.
So our variable declaration would be like:-
Data-Type Variable-name;
Examples:-
int a;
int count;
char name;
Two or more variables of the same data
type can be declared in a single line, separating each variable names
with a comma and ending the line with a semicolon.
Examples:-
int n,count,flag,i,j;
char name,address,nick_nm;
Initial values can be assigned to variables while declaring it.
Examples:-
int a,num,count=0,flag=10,mark=100;
char name=Robert,sur_nam=Nero,mid_nam=de;
Let us examine what happens when we declare a variable.
int j=10;
Here we have declared a variable of data type integer with name as j and initial value as 10. This declaration tells the C compiler to:-
- Reserve space in memory to hold the integer value.
- Assign the name ‘j’ to that reserved memory space.
- Store the value 10 in this memory location.
Keywords
Keywords are reserved words in C which
has a predefined meaning. The compiler already knows the meaning of
these words and it will do a particular operation according to the
meaning of the keyword.
Example:- int – is a keyword used to identify an integer quantity. When we declare a variable as int a; the compiler assumes that variable a is used to store an integer quantity.
There are 32 keywords in C language. All of them are listed in the table below.
auto | do | if | struct |
break | else | long | sizeof |
char | enum | register | typedef |
case | extern | return | unsigned |
const | goto | short | union |
continue | float | signed | void |
default | for | switch | volatile |
double | int | static | while |
- A keyword name can not be used as a variable name.
- Keywords must be written in lower case.
No comments:
Post a Comment