Tokens in C
Tokens in C
Tokens in C
One can define tokens in C as the smallest individual elements in a program that is
meaningful to the functioning of a compiler. A token is the smallest unit used in a C program.
Classification of tokens in C:
1. Keywords in C
2. Special Characters in C
3. Constant in C
4. Identifiers in C
5. Strings in C
6. Operators in C
1. Keywords in C
Keywords in C can be defined as pre-defined or reserved words having their importance, and
each keyword has its functionality. Since keywords are the pre-defined words used by the
compiler, so they cannot be used as variable names.
do if static while
2. Special characters in C
Some special characters are used in C, and they have a special meaning which cannot be used
for another purpose.
Square brackets [ ]: The opening and closing brackets represent the single and
multidimensional subscripts.
Simple brackets ( ): It is used in function declaration and function calling. For
example, printf() is a pre-defined function.
Curly braces { }: It is used in the opening and closing of the code. It is used in the
opening and closing of the loops.
The comma (,): It is used for separating more than one statement and for example,
separating function parameters in a function call, separating the variable when
printing the value of more than one variable using a single printf statement.
Hash/pre-processor (#): It is used for pre-processor directives. It denotes that we are
using the header file.
Asterisk (*): This symbol is used to represent pointers and is also used as an operator
for multiplication.
Tilde (~): It is used as a destructor to free memory.
Period (.): It is used to access a member of a structure or a union
3. Constants in C
A constant is a value assigned to the variable which will remain the same throughout the
program, i.e., the constant value cannot be changed.
There are two ways of declaring constant:
1. Using the const keyword
C Constants
2. Using #define pre-processor
Types of constants in C:
Primary Constants Secondary Constants
Real Constant:Real Constants are also known as floating point constant. As integer constant
can not represent some quantity which are changing continuously such as distance, heights,
temperature, price, so for this purpose we are using real constants.
Examples: 45.8 7.989 0.65e4 1.5e+5 7.5E9 (7500000000)
Single Character Constants:A single character constant contains a single character enclosed
with in a pair of single quote marks.
Examples: ‘5’ ‘X’ ‘97’
String Constant:It is a sequence of characters enclosed in double quotes. The characters may
letters, numbers, special characters and blank spaces.
Examples: “Hello!” “1920” “GIET University” “5+3”
Backslash Character Constants:C supports some special backlash character constants that
are used in output functions. Below are the backslash character constants and their uses
4. Identifiers in C
Identifiers in C are used for naming variables, functions, arrays, structures, etc. Identifiers in
C are user-defined words. It can be composed of uppercase letters, lowercase letters,
underscore, or digits, but the starting letter should be either an underscore or an alphabet.
Identifiers cannot be used as keywords.
Rules for constructing identifiers in C are given below:
Identifiers should be written in such a way that it is meaningful, short, and easy to read.
4.1 Variables
A variable is a data name that may be used to store a data value. Unlike constants that remain
unchanged during the execution of the program, a variable may take different values at
different times during execution.
A variable can be chosen by the programmer in a meaningful way so as to reflect its function
or nature in the program.
Example: Average, height, Sum, Total, Counter_1, Class_strength, _add
Rules:
1. They may begin with a letter
2. ANSI standard recognize a length of 31characters
3. Uppercase and lowercase are significant.
4. White space is not allowed.
A data type, in programming, is a classification that specifies which type of value a variable
can store. According to the different data types we can insert to the respective variables. Also,
data types give an idea that which type of operations can be performed on that variable.
Different data types acquires different amount of memories to store the data. So a data type
tells us the size, range and the type of a value that can be stored in a variable.
In C language, data types are divided into three types. Those are primary data types, derived
data types and user-defined data types.
Primitive data types are the kind of data types which are declared or written by the developer
of the C language. To use a primitive data type we do not need to specify the work of the data
type. The works of these primary data types are previously defined so these data types are
also called as pre-defined data types. In C there are five basic data types and their extensions.
The basic/fundamental data types are integer (int), character (char), floating point (float),
double-precision floating point (double) and void. Many of them also offer extended data
types such as long int and long double, etc. In the below table all the data types, their sizes
and ranges are discussed.
A derived data type is a complex classification that identifies one or various data types and is
made up of simpler data types called primitive data types. Derived data types have advanced
properties and uses far beyond those of the basic primitive data types that operate as their
essential building blocks. Derived data types can be made up of simple integers or characters,
as well as other simple data types. Some examples of derived data types are
Array
Pointer
function
A user-defined data type (UDT) is a data type that derived from an existing data type. You
can use UDTs to extend the built-in types already available and create your own customized
data types. The User-Defined Data Types are basically defined by a user according to their
will. These offer various functions on the basis of how one defines them. Thus, these are
termed to be “User-Defined”. Some examples of user-defined data types are as follows.
Structures
Union
Typedef
Enum
5. Strings in C
Strings in C are always represented as an array of characters having a null character '\0' at the
end of the string. This null character denotes the end of the string. Strings in C are enclosed
within double quotes, while characters are enclosed within single characters. The size of a
string is the number of characters that the string contains.
Now, we describe the strings in different ways:
char a[10] = "HelloWorld"; // The compiler allocates the 10 bytes to the 'a' array.
char a[] = "HelloWorld"; // The compiler allocates the memory at the run time.
char a[10] = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '\0'}; // String is represented in the form of
characters.
6. Operators in C
Operators in C are special symbols, which are used to performed specific operations or work
upon the operands. The data items on which the operators are applied are known as operands.
The operators can performs mathematical and logical operations on the operands.
C operators can be classified into a number of categories. They include:
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operator
Increment and decrement operators
Conditional Operators
Bitwise Operators
Special Operator
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo division
6.2 Increment and decrement operators
The decrement (- -) and increment (+ +) operators are special types of operators used in
programming languages to decrement and increment the value of the given variable by 1
(one), respectively. These are also known as unary operators as they require only one operand
for the operation. These increment and decrement operators are generally used during loop to
control the flow of the data i.e. used to count the iteration that is undergoes in the loop
process.
This type of operator is used to transform as well as assign the values to any variable in an
operation. In any given assignment operator, the right side is a value, and the left side is a
variable. The value present on the right side of the operator must have the same data type as
that of the variable present on the left side. In any other case, the compiler raises an error.
Assignment operators supported by C are described in the below table.
Operator Statement with Meaning
shorthand operator
= a = 10 a = 10
+= a += 1 a=a+1
-= a -= 1 a=a-1
*= a *= 2 a=a*2
/= a /= 3 a=a/3
%= a %= 5 a=a%5
The relational operators are used to compare two quantities and depending on their relation
some certain decision is taken. In case the relation happens to be true, then it returns to 1. But
in case this relation turns out to be false, then it returns to the value 0. We use the relational
operators in loops and in the cases of decision making.
C supports six relational operators in all. These operators and their meanings are shown in the
below table.
Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to
6.5 Logical operators
We use logical operators to perform various logical operations on any set of given
expressions. The logical operators in C are used for combining multiple constraints/
conditions or for complementing the evaluation of any original condition that is under
consideration. The logical operators supported by C are studied with examples below.
A B A && B A || B !B
0 0 0 0 1
0 1 0 1 0
1 0 0 1 1
1 1 1 1 0
In the above table 1 represent true value and false value is represented by 0. We can see in the
relations that for different operators the resultant values are different. For logical and ( && )
if both the operand values are true (1) then only the result will be true (1), otherwise it is false
(0).
Similarly, for the logical or ( || ) if any one of the both operand is true (1) then it the result is
true and if both the operands are false (0) then only the result is false (0).
The logical not (!) reverse the value of the operand. That means if the value of the operand is
true (1) then after logical not the result value will be false (1) and vice-versa. The logical and
(!) operator is operate on only one operand so it can be consider under unary operator.
6.6 Bitwise Operators
The bitwise operators in C language are used to perform operations on the available data at a
bit level. Thus, performing a bitwise operation is also called bit-level programming. It is
mainly used in numerical computations for a faster calculation because it consists of two
digits 1 or 0. These operators are used for testing the bits, or shifting them right or left.
Bitwise operators may not be applied to float or double. The examples of bitwise operators
are shown in below table.
OPERATOR MEANING
& Bitwise AND
| Bitwise OR
^ Bitwise Exclusive OR
<< Shift Left
>> Shift Right
The operator which is used in case of decision making when longer conditional statements
like if and else exist. In simpler words, when we use an operator on three variables or
operands, it is known as a Ternary Operator. We can represent it using ? : .
Example: exp 1 ? exp 2 : exp 3
A = 10;
B = 15;
X = ( A > B ) ? A: B;
6.8 Special Operators
C supports some special operators of interest, those have some special work in the program.
The special operators are discussed in the below table.
OPERATOR MEANING
, Comma Operator
* Pointer Operater
Type Casting
Type Casting is basically a process in C in which we change a variable belonging to one data
type to another one. In type casting, the compiler automatically changes one data type to
another one depending on what we want the program to do.
For instance, in case we assign a float variable (floating point) with an integer (int) value, the
compiler will ultimately convert this int value into the float value. The process of casting
allows the programmers to make such types of conversions explicit or even force it in cases
where it wouldn’t happen normally.
Example:
int A = 11;
char B = ‘a’;
int C = A + B; C = 11 + 65
float D = A * 5.0; D = 11.0 * 5.0
Explicit Type Casting
In implicit type conversion, the data type is converted automatically. There are some
scenarios in which we may have to force type conversion. Suppose we have a variable div
that stores the division of two operands which are declared as an int data type.
In this case, after the division performed on variables var1 and var2 the result stored in the
variable “result” will be in an integer format. Whenever this happens, the value stored in the
variable “result” loses its meaning because it does not consider the fraction part which is
normally obtained in the division of two numbers.
To force the type conversion in such situations, we use explicit type casting. It requires a type
casting operator. The general syntax for type casting operations is as follows:
(type-name) expression
Here,
The type name is the standard ‘C’ language data type.
An expression can be a constant, a variable or an actual expression.
EXAMPLE:
#include<stdio.h>
void main()
{
int a = 12;
int b = 5;
int C = a / b;
float D = (float) a / b;
printf("Value of C is %d\n", C);
printf("Value of D is %f\n", D);
}
Output:
Value of C is 2
Value of D is 2.4