Week 3 Tokens in C
Week 3 Tokens in C
Introduction
As we all know, the English language comprises varied types of words like nouns,
adjectives, pronouns, articles, conjunctions, etc. These words make up the whole
language and are used according to the grammar rules.
Similarly, every programming language also has individual components that play
varied roles in a program called tokens. This lesson will discuss tokens in C, their
significance, their types, and the role they play within the C program.
Tokens in C Language
Tokens in C are the individual units of source code that make up the C programming
language. They are the smallest individual elements (or words or characters) that have a
specific meaning and purpose in any programming language.
In other words, tokens are essential components or the building blocks of C programs
and are processed by the C compiler to generate an executable program.
Use of Tokens in C
Tokens in C include identifiers, keywords, constants, operators, and punctuation
symbols. The C compiler reads the source code and breaks it down into these tokens,
which it uses to understand the program's structure and functionality. Tokens serve as
the foundation upon which C programs are built and executed.
a) Keyword Tokens in C
Keywords are reserved (or pre-defined) words in C that have predefined meanings
and are used to show actions or operations in the code. They are an integral part of the
C language syntax and cannot be used as identifiers (variable names or function
names) in the code. There are a total of 32 keyword tokens in C language.
Some Examples of keywords include if, while, for, int, else, switch, and return. It is
essential to use these keywords properly to stick to the syntax rules and ensure that the
code is interpreted correctly by the compiler by using the preprocessor directives. The
pre - processor is automatically used by the compiler and denotes that we are using a
header file. Let's look at a simple C program example that illustrates how the if
keyword from the if-statement.
Output:
Positive number
Explanation:
In the simple C code example, we first include the input-output header file <stdio.h>.
1. Then, we begin the main() function, which is the entry point for the program's
execution.
2. In main(), we declare a variable num of integer data type and assign the value
10 to it.
3. Next, we use an if-statement, which checks if the value of the num variable is
greater than 0, i.e., num>0.
If the condition is true, the if-block is executed. It contains a printf()
statement, which displays the string message- Positive number to the
console.
If the condition is false, the flow moves to the next line in the program.
4. After the if-statement, the main() function terminates with a return 0 statement,
indicating successful execution.
The if keyword is reserved for if-statements in a C program. Using this keyword any
other way will lead to errors. See the table below, listing various other reserved
keyword tokens in C and their significance.
b) Identifier Tokens in C
int main() {
return 0;
Output:
Age: 25
Salary: 5000.50
Explanation:
In the sample C code above-
1. Inside the main() function, we initialize two variables, i.e., age (an integer with
a value of 25) and salary (a floating-point number with a value of 5000.50).
2. The names- age and salary are identifiers that describe the type of value they
store.
3. Next, we use the printf() function to display the values of age and salary to the
console.
4. Here, the %d and %.2f format specifiers indicate integer and floating-point
values. The newline escape sequence (\n) shifts the cursor to the next line.
c) OPERATORS TOKENS IN C
Operators are symbols that are used to perform operations, manipulations and
modifications on data. More specifically, they perform arithmetic, logical, relational,
and other operations on variables and values in the code.
There are many types of operators in C, including arithmetic operators, logical
operators, relational/ comparison operators, assignment operators, bitwise
operators, increment/ decrement operators and conditional/ ternary operator.
Depending upon the number of operands an operator takes, they can be
classified as unary or binary.
The correct use of operator tokens in C is essential for performing desired
computations and manipulations on data in C programs.
Understanding their precedence and associativity rules is also important for
writing correct and efficient code.
The operators are types of symbols that inform a compiler for performing some specific
logical or mathematical functions. The operators serve as the foundations of the
programming languages. Thus, the overall functionalities of the C programming
language remain incomplete if we do not use operators. In simpler words, the operators
are symbols that help users perform specific operations and computations- both logical
and mathematical on the operands. Thus, the operators operate on the available
operands in a program.
Use of Operators in C
The operators basically serve as symbols that operate on any value or variable. We use
it for performing various operations- such as logical, arithmetic, relational, and many
more. A programmer must use various operators for performing certain types of
mathematical operations. Thus, the primary purpose of the operators is to perform
various logical and mathematical calculations.
The programming languages like C come with some built-in functions that are rich in
nature. The use of these operators is vast. These operators act as very powerful and
useful features of all the programming languages, and the functionality of these
languages is pretty much useless without these. These make it very easy for
programmers to write the code very easily and efficiently.
CATEGORIES OF OPERATORS
Operators can be categorized based on the number of operands it operates on i.e one (1)
operand, two (2) operands and three (3) operands.
I. Unary operators – have one (1) operand
II. Binary operators – have two (2) operands
III. Ternary operators – have three (3) operands
I. UNARY OPERATORS
These are the type of operators that act upon just a single operand for producing a new
value. All the unary operators have equal precedence, and their associativity is from
right to left. When we combine the unary operator with an operand, we get the unary
expression.
Unary Plus
This operator changes the sign of any negative argument. It means that a negative
number will become positive, and a positive number will also become positive.
But the unary plus is different from that of the addition operator. It is because we
require two operands for addition.
int p = -20;
int q = +p; // q = 20
NOT (!)
We use this operator for reversing the logical state of the available operand. It means
that the logical NOT operator will make an available condition to be false if it is already
true.
In simple words,
If p is true, then !p will be false.
If p is false, then !p will be true.
Increment
We use this operator to increment the overall value of any given variable by a value of
1. We can perform increment using two major ways:
Prefix increment
Postfix Increment
Prefix Increment
The operator in this method precedes the given operand (Example, ++p). Thus, the
value of the operand gets altered before we finally use it.
For instance,
int p = 1;
int q = ++p; // q = 2
Postfix Increment
The operator in this method follows the given operand (Example, p++). Thus, the value
of the available operand gets altered after we use it.
For instance,
int p = 1;
int q = p++; // q = 1
int r = p; // r = 2
Decrement
We use this operator to decrement the overall value of any given variable by a value of
1. We can perform decrement using two major ways:
Prefix Decrement
The operator in this method precedes the given operand (Example, --p). Thus, the value
of the operand gets altered before we finally use it.
For instance,
int p = 1;
int q = --p; // q = 0
Postfix Decrement
The operator in this method follows the given operand (Example, p--). Thus, the value
of the available operand gets altered after we use it.
For instance,
int p = 1;
int q = p--; // q = 1
int r = p; // r = 0
Sizeof ()
The function of the size of operator is to return the original size of the available operand
in terms of bytes. This type of operator always precedes the given operand. Here, the
operand can either be an expression or it can also be a cast. For instance,
float n = 0;
sizeof(n);
#include <stdio.h>
int main() {
int a = 5;
int b = 3;
int result = a + b; // Using the binary addition operator (+)
printf("The result of %d + %d is %d\n", a, b, result);
return 0;
}
Output:
The result of 5 + 3 is 8
1. ARITHMETIC OPERATORS:
The symbols of the arithmetic operators are:-
Operation Operator Comment Value of Value of
Sum before sum after
Multiply * sum = sum * 2 4 8
Divide / sum = sum / 2 4 2
Addition + sum = sum + 2 4 6
Subtraction - sum = sum - 2 4 2
Increment ++ ++sum 4 5
Decrement -- --sum 4 3
Modulus % sum = sum % 3 4 1
== equal to
!= not equal
< less than
<= less than or equal to
> greater than
>= greater than or equal to
3. ASSIGNMENT OPERATORS
We use these operators to assign specific values to variables.
Multiplication
*= Stores multiplied value in the left operand
assignment
4. LOGICAL OPERATORS
We use logical operators when we need to make decisions by testing one or more
conditions. Thus, logical operators work on Boolean values. The answers returned are
either true or false.
Bitwise operators in C
In C, the following 6 operators are bitwise operators (also known as bit operators as
they work at the bit-level). They are used to perform bitwise operations in C.
& bitwise AND Takes two numbers as operands and does AND on every bit of
two numbers. The result of AND is 1 only if both bits are 1.
| bitwise OR Takes two numbers as operands and does OR on every bit of
two numbers. The result of OR is 1 if any of the two bits is 1
^ bitwise XOR Takes two numbers as operands and does XOR on every bit of
two numbers. The result of XOR is 1 if the two bits are
different
<< left shift Left shifts the bits of the first operand, and the second operand
decides the number of places to shift
>> right shift Right shifts the bits of the first operand, and the second
operand decides the number of places to shift
~ bitwise NOT Takes one number and inverts all bits of it
Special Operators
C facilitates the usage of some special operators, which helps in reducing the hassle of
programmers. Some of them are:
• *(Pointer) => it stores the memory address of a variable.
• &(Pointer) => this points to the memory location where the computer stores the
operand.
• sizeof => this operator returns the space occupied by a particular data type in its
memory location e.g sizeof(int)
• comma (,) =>Used to link the related expressions together e.g value = (x=10, y=5)
Precedence of Operators in C
The precedence of the operators in C language helps in determining the preference in
which the operators must be evaluated in a program. Some operators display higher
precedence as compared to the others in the program. For instance, the precedence of
the multiplication operator is much higher than that of the addition operator. Let us
consider an example where a = 5 + 2 * 7; so here, the variable a gets assigned with the
value 19 and not 70. It is because the precedence of the multiplication operator (*) is
higher than that of the addition operator (+). Thus, the program would first multiply 2
with 7, produce 14, and then add it with 5 to get 19 as the final result.
The table below displays the operators according to their precedence. The ones with
higher precedence appear at the very top, and the ones with lower precedence appear at
the very bottom. In any given expression, the evaluation of the operators with higher
precedence occurs first.
Precedence Operator Description Associativity
* Dereference right-to-left
10 | Bitwise OR left-to-right
14 = Assignment right-to-left
*= , /= Multiplication/division right-to-left
assignment
Let's understand the precedence (or priority) operator with the help of programming
example.
#include <stdio.h>
int main() {
int a = 5;
a = 10,20,30;
printf("%d", a);
return 0;
}
Output:
10
Explanation:
Priority or precedence for the values assigned to any variable is given from left to right.
Example 2:
#include <stdio.h>
int main() {
int x = 5;
x = (10,20,30);
printf("%d", x);
return 0;
}
Output:
30
Explanation
Priority or precedence for the values inside brackets () assigned to any variable is given
from right to left.
d) Literal Tokens in C
Literals are used to represent constant values or fixed values that remain unchanged
during program execution. There are several types of literal tokens in C, including
numeric literals (e.g., integers or floating-point numbers), literal characters (e.g., \n for
newline), and string literals (e.g., Hello, World!).
Proper use of literals helps define the values of variables, constants, and other data
elements in the code, and their accurate representation is crucial for achieving the
desired behavior in code. Below is a sample C program that illustrates the creation of
various literal tokens in C.
#include <stdio.h>
int main() {
return 0;
}
Output:
Integer Literal: 42
Float Literal: 3.140000
Character Literal: A
Double Literal: 2.718280
String Literal: Hello
Explanation:
In the example C code above-
1. In the main() function, we declare and initialize different types of literal tokens,
including-
integerLiteral with an integer literal value of 42.
floatLiteral with a floating-point literal value of 3.14.
charLiteral with a character literal value of 'A'.
doubleLiteral with a double-precision floating-point literal value of
2.71828.
stringLiteral with a string literal value of "Hello".
2. We then use a set of printf() statements to display the values of these literals
with appropriate format specifiers.
3. Finally, the program returns 0, indicating successful execution, before exiting.
Types of Literals
Integer literals
Integer literals are used to represent integer values in C, which are whole numbers
without any decimal point. They can be written with or without a sign and can be
represented in different bases, such as decimal, hexadecimal, and octal. For example:
Decimal: 42, -10
Hexadecimal: 0xFF (which represents 255 in decimal)
Octal: 012 (which represents 10 in decimal)
Floating-point literals
Floating-point literals are used to represent floating-point values in C, which are
numbers with a decimal point. They can be written with or without a sign and can
have an optional exponent part. For example:
3.14, -0.5
2.0e-3 (which represents 0.002 in decimal)
Character literals
Character literals are used to represent individual characters in C, enclosed in single
quotes (' '). They can be regular characters, escape sequences representing special
characters (such as '\n' for a newline character), or octal or hexadecimal escape
sequences representing characters by their ASCII values. For example:
'a', '1', '\n' (newline character), '\x41' (which represents 'A' in ASCII)
String literals
String literals are used to represent sequences of characters in C, enclosed in double
quotes (" "). They are used to represent strings of characters, which are arrays of
characters terminated by a null character ('\0'). For example:
"hello", "world"
Boolean literals
Boolean literals represent boolean values, which can be either true or false. In C,
boolean values are typically represented using integer literals, where 0 represents false
and any non-zero value represents true. For example:
0 (false), 1 (true)
Binary literals
Binary literals are used to represent integer values in binary (base 2) notation and were
introduced in the C99 standard. They start with the prefix '0b' followed by a sequence
of binary digits (0 or 1). For example:
0b1010 (which represents 10 in decimal)
e) SPECIAL SYMBOLS IN C
Special symbol tokens in C include punctuation marks and other characters used to
structure and separate elements in code. Examples of special symbols include commas,
semicolons, parentheses, curly braces, and brackets.
#include <stdio.h>
int main() { //curly braces mark the enclosure of the code block for this function body
num2 = 20;
printf("num1 is greater\n");
} else {
printf("num2 is greater\n");
};
return 0;
Output:
num2 is greater
Explanation:
In this C code sample-
1. In the main() function, we declare two integer variables, num1 and num2, in a
single line using the comma symbol between them.
2. Then, we assign value 10 to num1 and value 20 to num2 using the assignment
operator (=).
3. Next, we create an if conditional / control statement, followed by a pair of
parentheses ( ). The if-statement allows for conditional execution of code based
on a condition.
4. We have a condition num1 > num2 inside the if-statement to check whether
num1 is greater than num2. If this condition is true, the code inside the
corresponding block (curly braces{}) will be executed.
If the condition is true, the if-block containing the printf() function prints
the message- 'num1 is greater' to the console.
If the condition is false, the else-block containing the printf() function
prints the message 'num2 is greater' to the console.
5. The program above showcases the use of multiple special characters and strings.
Braces ({ }): Braces or curly brackets are used to define blocks of code, such as
function bodies, loop bodies, and conditional statements. They enclose a group
of statements and define their scope. For example:
if (a > b) { // starts a conditional statement block
printf("a is greater than b");
} // ends the conditional statement block
Brackets ([ ]): Square Brackets are used to declare and access elements in
arrays and for specifying array sizes in function prototypes. They are also
known as opening and closing brackets. For example:
int arr[5]; // declares an integer array with size 5
printf("Value at index 2: %d", arr[2]); // accesses the value at index 2 in the
array
Ampersand (&): Used as the address-of operator to get the dynamic memory
address of a variable. For example:
int x = 10;
int *ptr = &x; // Pointer to x
f) CONSTANT TOKENS IN C
Constant tokens in C are values that do not change during the execution of a program.
They are also known as literal values or simply literals. In other words, constant tokens
in C represent fixed values, such as numbers, characters, and strings, that remain
unchanged throughout the execution of a C program.
Code Snippet:
// Example of constants in C
int num = 10; // integer constant
float pi = 3.14; // floating-point constant
char ch = 'A'; // character constant
char str[] = "Hello"; // string literal
g) STRINGS IN C
Code example:
#include <stdio.h>
int main() {
char greeting[] = "Hello, World!"; // string token
return 0;
Output:
Greeting: Hello, World!
Explanation:
In the C program sample-
1. We create a string called greeting and assign to it the character array- Hello,
World!.
2. Here, we use the double quote method, and the empty square brackets indicate
no fixed number of characters the literal can take.
3. Then, we use the printf() function to display the value of this string to the
console.
Character Pointers
C allows you to represent strings using character pointers, which point to the
first character of the string. The string is again terminated by a null character
('\0'). For example:
char *str = "Hello, World!";
String Constants
C allows you to directly specify string constants by enclosing them in double
quotes. String constants are treated as character arrays and automatically
terminated by a null character ('\0'). For example:
"Hello, World!"
Escape Sequences:
C provides escape sequences to represent special characters within strings.
Escape sequences start with a backslash (\) followed by a specific character
representing a particular value. Some commonly used escape sequences are:
\\ (Backslash) \b (Backspace)
In the C programming language, the character set is based on the ASCII (American
Standard Code for Information Interchange) standard. ASCII defines a set of 128
characters, including control characters (such as newline, carriage return, and tab) and
printable characters (letters, digits, punctuation marks, and special symbols).
The ASCII character set uses 7 bits to represent each character, allowing for a
total of 128 unique characters.
The characters are encoded using integer values ranging from 0 to 127. For
example, the character 'A' is represented by the integer value 65, 'B' by 66, and
so on.
In addition to the ASCII character set, the C language also supports escape
sequences that represent special characters and white space from memory, such
as '\n' for newline, '\t' for tab, and '\r' for carriage return.
Furthermore, C provides the concept of wide characters, which are used to
handle internationalization and localization.
Wide characters are represented by the wchar_t type and use a wider character
encoding, such as Unicode, to support a broader range of characters beyond the
ASCII set.