Tokens
by Shivu on Jan.31, 2010, under C Language Data Strucutres
Tokens:
Just like parts of speech in English , we have tokens in C . Tokens are nothing but individual units of a C program. Tokens are mainly classified into :
- Identifier
- keyword
- Variable
- Constant
- Operators
Identifier:
Identifier is a user defined name. While programming in C we name different things as and when required. These names are called identifiers. You can name variables, constants, data types, functions, etc.
When you name something in C you need to follow a few rules:
-
- Keywords should not be used.
- The starting letter of the identifier should be an alphabet.
- Special characters or symbols (except _) should not be used.
Examples
We can use
name,function,plus,variable100,v12,vv45,vvvv,iiiii,_newname,_sivaram,_anil,__name.
we cannot use
all keywords , 1var,2dva,3ddd,3rdfunction,&fun,$fun,*star ,.dat.
Keywords:
These are special words which have fixed meaning and their meaning cannot be changed. There are 32 key words. You can work wonders with just 32 key words. They are :
| int | float | char | short |
| long | double | signed | unsigned |
| struct | union | enum | const |
| void | static | extern | register |
| auto | volatile | sizeof | typedef |
| if | else | switch | case |
| default | while | for | do |
| continue | break | return | goto |
Note : Some compiler vendors like Microsoft, Borland, etc provide their own key words like near far etc. We will be following ANSIC standard keywords which will work on any compiler .
Variables :
Identifiers whose value changes during execution of a program are called variables. Basically we use variables in mathematics .In the same way we use variables to manipulate data. Each variable has a data type and a value. This name should be a valid C name. For example:
int sum=10;
here sum is variable name and 10 is the value it is storing in memory.
Constants:
Identifier whose value remains constant throughout the execution of program is known as a constant. Usually we have many mathematical constants like pi, e, m, etc. There is a provision in C to declare an identifier as a constant so that it remains constant throughout the program execution without getting changed even if someone unknowingly changes its value.
This is done by the key word const. We need to initialize the value. Constants should be a valid C name and it should be initialized to some value before you can use in program.Initialization means assigning a value at the time of declaration.
Constants can be classified into the following types:
- Integer constants
- Real constants
- Character constants
- String constants
Integer constants:
These are the constants which are used to represent the whole numbers. By default C takes every integer as a +ve integer. Unless we use a minus sign it will consider every integer as positive.
Real constants:
These are the constants which are used to represent the real values like heights and distances . They can take decimal values also. There are two notations for real constants. The first one is scientific form, i.e. the decimal values are represented with a help of powers of 10. The powers of ten are called exponents .
0.5 = 5/10 =5X10^-1 = 5e-1
0.05 = 5/100 =5X10^-2 =5e-2
50000 = 5X10000 =5X10^+4 =5e4
We can also represent the represent numbers in the conventional way as 0.005 , etc.
Character constant:
The identifiers which can hold a single character are known as character constants. Their values do not change during the execution of the program.These are enclosed in single quotes ‘ ‘ . It should be kept in mind that each alphabet and number have a separate code called ASCII code.For example:
variable=’a’;
variable2=’10’;
variable3=10;
String constants :
These are the identifiers which can hold a group of characters and the value remains constant during the execution of program. These are enclosed in “ ”.They are stored in the form of an array . We are going deal with these later.
Operators:
Operators are the symbols that instruct the compiler to do a specific action on variables or constants. There is a separate topic regarding operators which are really important to master C. C is a procedure oriented programming language i.e. we manipulate the data in C to serve our purpose, almost every manipulation is done using operators. Without operators we cannot do anything in C.