Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

SESSION TWO

C CHARACTER SET AND C TOKENS

2.1 Introduction

2.2 Learning Outcomes

2.3 C Character Set

2.4 C Tokens

2.5 Summary

2.1 Introduction

Welcome to this session. The programming language that we shall use in this
course is C language. We shall start by looking at the set of characters used
in C language and the basic building block of C language commonly known
as C tokens.

2.2 Learning Outcomes

Upon successful completion of this session, you should be


able to:

1. Explain the c character set


2. Discuss the basic building blocks of C language
3. Apply tokens, keywords, comments and other essential
elements of C language in a program

2.3 C Character Set

Every computer program is written using a programming language. Examples


of these programming languages include Java, C, C++, PHP, Visual Basic,
Python, etc. The first thing that we learn in any language is the set of

1
characters used in that language. For example, when we start learning
English, firstly we require to learn English alphabets.

Similarly, there are many characters that can be used to write a C program.
The collection of those characters is referred as "C Character Set".

Following characters can be used to write a C program:

i. Alphabets - A,B,C ....Z, and a, b, c, ....z


ii. Digits - 0,1, ...9
iii. Blank Space -
iv. Special Symbols - #, (, ), {, }, [, ], +, -, *, / etc.

2.3 C Tokens

Tokens are the basic building blocks of C program. A C program consists of


a series of tokens which are part of C syntax and they are the smallest units
of a program that convey a specific meaning to the compiler.

There are 5 types of tokens in C. They are-

i. Keywords
ii. Identifiers

2
iii. Literals (or Constants)
iv. Operators
v. Separators (or Punctuators)

i. Keywords

Keywords are a set of reserved words that have a specific or special meaning
to C language. These words are called reserve words because they are reserve
for C program. There are total 32 keywords in C. They are always written in
small case alphabets. They are:

auto else long switch

break enum register typedef

case extern return union

char float short unsigned

const for signed void

continue goto sizeof volatile

default if static while

do int struct _packed

double

Table 2.1: Reserved Words in C

ii. Identifiers

These are names given to variables, functions, arrays, strings, structures


etc. A variable is a name given to a storage area or memory location that
programs can manipulate. It can also refer to a data item in a program
whose value can be changed or vary.

3
Naming Rules for Identifiers:

In real life, we can name our pet, our house or anything that we want to name
and there may be no specific rule for naming. Although, we take care that the
name we choose do not hurt the sentiments of some other person. Some other
names might be meaningful also. But in C there are some rules that need to
be followed for naming identifiers. When naming identifiers, the following
rules must be followed:

1. It consist of alphabets, digits or underscore ( _ ).

2. It should not start with digit.

3. It can be up-to 8 characters long (in DOS) and 256 characters long (in
Windows).

4. It should not be a Keyword.

5. It should not contain blank space.

6. It’s case sensitive: small case and capital case alphabets are distinct.

7. It should not contain special symbol (except underscore).

Example:

Table 2: Example of identifiers

4
iii. Literals (or Constants)

These are constant values that can be used in a C program. Constants are
fixed values that the program may not alter during its execution.

There are 4 types of literals in C:

a. Integer Literals – For example- 10, 100, 23456 etc.


b. Float (Real) Literals – For example 10.4, 100.354 etc.
c. Character Literals – For example ‘a’, ‘+’, ‘1’ etc. It means any single
character. They are always enclosed in single quotes.
d. String Literals - For example “college”, “a”, “what is your name?” etc. It
means any group of characters or single character. They are always
enclosed in double quotes.

iv. Operators

These are symbols used to perform operation on operands. Operands may


be variables or constants values. For example-

A + B, A+10 etc.

In above examples, A, B, 10 are operands and + is operator.

There are 3 categories of operators-

a. Unary operators - requires one operand.


Example: a++, -a

b. Binary operators - requires two operands.


Example: a+b, a*b, a-b

c. Ternary operators - requires three operands.


Example – Conditional operator ( ? :) operand1 ? operand2 : operand3

5
Types of operators in C

There are various types of operators as listed below:

a. Arithmetic operators

b. Assignment operator

c. Relational (or Comparison) operators

d. Logical operators

e. Increment and Decrement operators

f. Conditional operators

g. Bit-wise operators

h. Some special operators

We will discuss only Arithmetic operators and Assignment operator here.


Rest of the operators will be discussed in other sections to come.

Arithmetic Operators

These operators are used to perform arithmetic operations. All arithmetic


operators are binary except subtraction (-) which may be unary also. The
various arithmetic operators in C are:

i. Addition ( + ) - For example- 10+20 = 30, 10.5+20 = 30.5


ii. Subtraction ( - ) For example- 10-20 = -10, 10.5-20 = -9.5
iii. Multiplication ( * ) For example- 10*20 = 200, 10.5*20 = 210.0
iv. Division ( / )

Example 1: 10/20= 0 (because in C, integer/integer=integer)


Example 2: 10/20.0= 0.5 (because in C, integer/real=real)
Example 3: 10.0/20= 0.5 (because in C, real/integer=real)
Example 4: 10.0/20.0= 0.5 (because in C, real/real=real)

v. Modulus ( % )
This operator is used to find the remainder. Both of its operands must be
integer.

6
Example 1: 10%3 = 1

vi. Assignment Operator


There is only one assignment operator and its symbol is ( = ). It is used to
assign the value at its Right Hand Side (RHS) to the variable at its Left
Hand Side (LHS). It is a binary operator.

Example in Mathematics:

A = B + 20 (Correct)
B + 20 = A (Correct)

Example in C language:
A = B + 20 (Correct)
B + 20 = A (Incorrect)

vii. Compound assignment operators


These are mixture of arithmetic operators and assignment operator. They are
binary operators. There are 5 compound assignment operators:
a. + =
b. - =
c. * =
d. / =
e. % =

Example:

A+ = 10 It means A = A + 10
A* = B It means A = A * B
and so on…

viii. Separators (or Punctuators)

These are symbols used to separate a group of code from one another. The
most commonly used separator is a semicolon. Semicolon is used to terminate
the statement. Various separators in C are:

7
a. [ ] – Brackets
b. ( ) – Parentheses
c. { } – Braces
d. ; - Semi Colon
e. . - Period (Dot)
f. , - Comma

Example C Program (with reference to C tokens)

#include <stdio.h>
int main()
{
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);

// calculating sum
sum = number1 + number2;

printf("%d + %d = %d", number1, number2, sum);


return 0;
}

The above C program consist of following C tokens:

Keywords: int return

Identifiers: main number1 number2 sum printf scanf

Literals (Constants): "Enter two integers: " "%d %d" "%d + %d = %d" 0

Operators: & = +

Separators: () {} , ;

2.5 Summary

In this session, we have learnt about the set of characters used in C language.
We have also looked at C Tokens which are: keywords, identifiers, operators,
literals and separators. We also looked at various types of operators and
discussed in depth about arithmetic operators.

You might also like