0% found this document useful (0 votes)
74 views40 pages

W-02 - L-02 - Token, Operator and Expression

The document provides an overview of C tokens, which are the basic building blocks of C programming, and categorizes them into six types: keywords, identifiers, constants, strings, special symbols, and operators. It explains the rules for creating identifiers, the types of constants, and the various operators available in C, including arithmetic, assignment, and increment/decrement operators. Additionally, it describes how operators function in expressions and highlights the importance of understanding these concepts for effective programming in C.

Uploaded by

2020-3-22-005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views40 pages

W-02 - L-02 - Token, Operator and Expression

The document provides an overview of C tokens, which are the basic building blocks of C programming, and categorizes them into six types: keywords, identifiers, constants, strings, special symbols, and operators. It explains the rules for creating identifiers, the types of constants, and the various operators available in C, including arithmetic, assignment, and increment/decrement operators. Additionally, it describes how operators function in expressions and highlights the importance of understanding these concepts for effective programming in C.

Uploaded by

2020-3-22-005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Token, Operator and Expression

Week-02, Lecture-02

Course Code: CSE122 Course Teacher: Masud Rabbani


Course Title: Programming and Problem Solving Designation: Lecturer
Program: B.Sc. in CSE Email: [email protected]
C Token
C tokens
• C tokens are the basic buildings blocks in C language which are
constructed together to write a C program.
• Each and every smallest individual units in a C program is known
as C token or a lexical unit.
• C tokens can be classified in six types as follows:
①Keywords (e.g., int, while, do …),
②Identifiers (e.g., main, total, my_var …),
③Constants (e.g., 10, 20 , - 25.5 … ),
④Strings (e.g., “total”, “hello” , “DIU” …),
⑤Special symbols (e.g., (), {} , [] …),
⑥Operators (e.g., +, /, - , * …)
C tokens
C tokens example program

• where,
• Main, x, y, total – identifier
• {,}, (,) – special symbols
• Int , return – keyword
• 10, 20 – constant
• =,+ – operator
• “Total 30” – String
• main, {, }, (, ), int, x, y, total etc– all these are various tokens
① Keywords

C Keywords
② Identifiers
③ Constants
④ Strings
⑤ Special symbols
⑥ Operators

• There are some reserved words in C language whose meaning


are predefined in C compiler, those are called C keywords.
• Each keyword is meant to perform a specific function in a C
program.
• Each keywords has fixed meaning and that cannot be changed
by user.
• We cannot use a keyword as a variable name.
• Since upper case and lowercase characters are not considered
same in C, we can use an uppercase keyword as an identifier.
But it is not considered as good programming practice.
① Keywords

Keywords ② Identifiers
③ Constants
④ Strings
⑤ Special symbols
⑥ Operators

• There are 32 keywords in C which are given below.


keywords are all lowercase.
① Keywords

Identifiers
② Identifiers
③ Constants
④ Strings
⑤ Special symbols
⑥ Operators
• Each program elements in a C program are given a name called identifiers.
• Names given to identify variables, functions etc. are examples for identifiers.
• e.g., int x ; here x is a name given to an integer variable.
• Rules for constructing identifier name in C:
• An identifier can be composed of letters (both uppercase and lowercase letters),
digits and underscore only.
• The first character of identifier should be either a letter or an underscore(not any
digit). But, it is discouraged to start an identifier name with an underscore
though it is legal. It is because, identifier that starts with underscore can conflict
with system names. In such cases, compiler will complain about it.
• Punctuation and special characters are not allowed except underscore.
• Identifiers should not be keywords.
• Identifiers are case sensitive.
• There is no rule for the length of an identifier. However, the first 31 characters of
an identifier are discriminated by the compiler. So, the first 31 letters of two
identifiers in a program should be different.
① Keywords

Constants ② Identifiers
③ Constants
④ Strings
⑤ Special symbols
⑥ Operators

• Constants in C refer to fixed values that do not change


during the execution of a program.
• Example: 1, 2.5 , "Programming is fun." etc. are the
example of constants.
• In C, constants can be classified as follows:
• Numeric constants
• Integer constant (Ex: 102, - 5 )
• Real constant (Ex: 3.14, 5.5 )
• Character constants
• Single character constants (Ex: ´A` , ´;` , ´5` )
• String constants ( Ex: “Hello” , “5+4”)
• Backslash character constants (Ex: \n, \r)
① Keywords

Integer constants ② Identifiers


③ Constants
④ Strings

• Integer constants are the numeric constants (constant ⑤ ⑥


Special symbols
Operators
associated with number) without any fractional part or
exponential part.
• There are three types of integer constants in C language:
• decimal constant(base 10),
• octal constant(base 8) and
• hexadecimal constant(base 16).
• For example:
• Decimal constants: 0, -9 , 22 etc
• Octal constants: 021, 077, 033 etc
• Hexadecimal constants: 0x7f, 0x2a, 0x521 etc
• Note: Every octal constant starts with 0 and
hexadecimal constant starts with 0x in C programming.
① Keywords

Real Constants ② Identifiers


③ Constants
④ Strings
⑤ Special symbols
⑥ Operators

• Real constant, also called Floating point constants are


the numeric constants that has either fractional form
or exponent form.
• For example:
• -2.0
• 0.0000234
• -0.22E-5
• Note: Here, E-5 represents 10-5. Thus, -0.22E-5 = -
0.0000022.
Single Character
① Keywords
② Identifiers

Constants
③ Constants
④ Strings
⑤ Special symbols
⑥ Operators

• Single character constants are the constant which use


single quotation around characters.
• For example: 'a', 'l', 'm', 'F' etc.
• All character constants have an equivalent integer
value which are called ASCII Values.
① Keywords
② Identifiers

String constants ③ Constants


④ Strings
⑤ Special symbols
⑥ Operators

• A string is a sequence of characters enclosed in double


quotes.
• The sequence of characters may contain letters,
numbers, special characters and blank spaces.
• String constants are the constants which are enclosed
in a pair of double-quote marks.
• For example:
• "good" // string constant
• "" // null string constant
• " " // string constant of six white space
• "x" // string constant having single character.
① Keywords

Backslash Character ② Identifiers


③ Constants

Constant ④ Strings
⑤ Special symbols
⑥ Operators

• Sometimes, it is necessary to use newline(enter), tab, quotation


mark etc. in the program which either cannot be typed or has
special meaning in C programming.
• In such cases, backslash character constant ( escape sequence)
are used.
• For example: \n is used for newline.
• The backslash( \ ) causes "escape" from the normal way the
characters are interpreted by the compiler.
① Keywords
List of Escape Sequences ② Identifiers
③ Constants
④ Strings
⑤ Special symbols
⑥ Operators
① Keywords
② Identifiers

Special Symbols ③ Constants


④ Strings
⑤ Special symbols
⑥ Operators

• The following special symbols are used in C having some


special meaning and thus, cannot be used for some other
purpose.
• [] () {} , ; : * … = #
• Braces{}: These opening and ending curly braces marks the
start and end of a block of code containing more than one
executable statement.
• Parentheses(): These special symbols are used to indicate
function calls and function parameters.
• Brackets[]: Opening and closing brackets are used as array
element reference. These indicate single and
multidimensional subscripts.
① Keywords
② Identifiers

Operators ③ Constants
④ Strings
⑤ Special symbols
⑥ Operators
• C operators are symbols that triggers an action when applied to C variables and other
objects. The data items on which operators act upon are called operands.
• Depending on the number of operands that an operator can act upon, operators can be
classified as follows:
• Unary Operators: Those operators that require only single operand to act upon are
known as unary operators.
• Binary Operators: Those operators that require two operands to act upon are called
binary operators.
• Ternary Operators: These operators requires three operands to act upon.
• There are many operators, some of which are single characters
~ ! @ % ^ & * - + = | / : ? < >
• While others require two characters ++ -- << >> <= += -= *= /= == |
= %= &= ^= || && !=
• Some even require three characters <<= >>=
• The multiple-character operators can not have white spaces or comments between the
characters.
Operators And
Expression
Operators and
Expressions
• Consider the expression A + B * 5 , where,
 +, * are operators,
 A, B are variables,
 5 is constant,
 A, B and 5 are called operand, and
 A+B*5 is an expression.
Types of C
operators
• C language offers many types of operators, such as:
• Arithmetic operators
• Assignment operators
• Increment/decrement operators
• Relational operators
• Logical operators
• Bit wise operators
• Conditional operators (ternary operators)
• Special operators
Arithmetic operators
Assignment operators

Arithmetic Operators Inc/dec operators


Relational operators
Logical operators
Bit wise operators
Conditional operators
Special operators

• C Arithmetic operators are used to perform mathematical


calculations like addition, subtraction, multiplication, division
and modulus in C programs.
Arithmetic Operators Arithmetic operators
Assignment operators
Inc/dec operators
• There are three types of arithmetic operations using Relational operators
Logical operators
arithmetic operators: Bit wise operators
Conditional operators
Special operators
① Integer arithmetic : when all operands are integer. If a=15 ,
b=10,
• a + b =25
• a / b =1(decimal part)
• a % b =5 (remainder of division)
② Real arithmetic : All operands are only real number. If a=15.0 ,
b=10.0
• a / b = 1.5
③ Mixed model arithmetic : when one operand is real and
another is integer. If a=15 and b= 10.0
• a / b = 1.5 whereas, 15/10=1
• Note: The modulus operator % gives you the remainder
when two integers are divided: 1 % 2 is 1 and 7 % 4 is 3.
• The modulus operator can only be applied to integers.
Arithmetic Arithmetic operators
Assignment operators

Operators Inc/dec operators


Relational operators
Logical operators
Bit wise operators

①Integer arithmetic : Conditional operators


Special operators

When an arithmetic operation is performed on two whole


numbers or integers than such an operation is called as integer
arithmetic.
It always gives an integer as the result.
Let x = 27 and y = 5 be 2 integer numbers. Then the integer
operation leads to the following results.
• x + y = 32
• x – y = 22
• x * y = 115
• x%y =2
• x/y =5
Example program for C
Arithmetic operators
Assignment operators
Inc/dec operators

arithmetic operators
Relational operators
Logical operators
Bit wise operators
Conditional operators
Special operators

Output:
Addition of a, b is : 60
Subtraction of a, b is : 20
Multiplication of a, b is : 800
Division of a, b is : 2
Modulus of a, b is : 0
Arithmetic operators

Arithmetic Operators Assignment operators


Inc/dec operators
Relational operators
Logical operators
Bit wise operators
Conditional operators
Special operators
② Real arithmetic :
When an arithmetic operation is preformed on two real
numbers or fraction numbers such an operation is called real
or floating point arithmetic.
The modulus (remainder) operator is not applicable for
floating point arithmetic operands.
Let x = 14.0 and y = 4.0 then
• x + y = 18.0
• x – y = 10.0
• x * y = 56.0
• x / y = 3.50
Arithmetic operators

Arithmetic Operators Assignment operators


Inc/dec operators
Relational operators
Logical operators
Bit wise operators
Conditional operators

③ Mixed mode arithmetic :


Special operators

When one of the operand is real and other is an integer and if


the arithmetic operation is carried out on these 2 operands
then it is called as mixed mode arithmetic.
If any one operand is of real type then the result will always
be real
Let x = 15 and y = 10.0 then
• x / y = 1.5
Note that: 15 / 10 = 1 (since both of the operands are
integer)
Assignment Arithmetic operators

Operators Assignment operators


Inc/dec operators
Relational operators
• In C programs, values for the variables are assigned Logical operators
Bit wise operators
using assignment operators. Conditional operators
Special operators
• For example, if the value “10″ is to be assigned for the
variable “sum”, it can be assigned as sum = 10;

Shorthand or
Increment and Decrement Arithmetic operators
Assignment operators
Operators Inc/dec operators
Relational operators
Logical operators
Bit wise operators

• There are two more shorthand operators: Conditional operators


Special operators
• Increment __
• Decrement ++
• These two operators are for incrementing and decrementing a
variable by 1.
• For example, the following code increments i by 1 and
decrements j by 1.
Arithmetic operators
Assignment operators

Increment and Decrement Inc/dec operators


Relational operators

Operators
Logical operators
Bit wise operators
Conditional operators
Special operators

• The ++ and - - operators can be used in prefix or suffix mode,


as shown in Table
Arithmetic operators
Assignment operators
Inc/dec operators

Increment and Decrement Relational operators


Logical operators

Operators
Bit wise operators
Conditional operators
Special operators

• If the operator is before (prefixed to) the variable, the variable is


incremented or decremented by 1, then the new value of the
variable is returned.
• If the operator is after (suffixed to) the variable, then the variable
is incremented or decremented by 1, but the original old value of
the variable is returned.
• Therefore, the prefixes ++x and --x are referred to, respectively,
as the preincrement operator and the predecrement operator;
and the suffixes x++ and x -- are referred to, respectively, as the
postincrement operator and the postdecrement operator.
Arithmetic operators
Assignment operators

Increment and Decrement Inc/dec operators


Relational operators

Operators
Logical operators
Bit wise operators
Conditional operators
Special operators

The prefix form of ++ (or --) and the suffix form of ++ (or --) are the same if they are
used in isolation, but they cause different effects when used in an expression. The
following code illustrates this:

In this case, i is incremented by 1, then the old value of i is returned and used in the
multiplication. So newNum becomes 100. If i++ is replaced by ++i as follows,

i is incremented by 1, and the new value of i is returned and used in the


multiplication. Thus newNum becomes 110.
Arithmetic operators
Assignment operators

Exercise on ++ and - - Inc/dec operators


Relational operators
Logical operators
Bit wise operators
Conditional operators
Special operators

• int x=2 , y = 5 , z = 0;
• x++ ; y++ ;
• x=y++ + x++;
• y=++y + ++x;
• y=++y + x++;
• y += ++y;
• y += 1 + (++x);
• y += 2 + x++;
Arithmetic operators

Relational Assignment operators


Inc/dec operators
Relational operators

Operators Logical operators


Bit wise operators
Conditional operators
Special operators

• Relational operators are used to find the relation between two variables.
i.e. to compare the values of two variables.
Arithmetic operators

Logical Operators Assignment operators


Inc/dec operators
Relational operators
Logical operators
• These operators are used to perform logical operations Bit wise operators
Conditional operators
on the given expressions. Special operators

• There are 3 logical operators in C language. They are,


logical AND (&&), logical OR (||) and logical NOT (!).
Example program for
Arithmetic operators
Assignment operators
Inc/dec operators

logical operators in C
Relational operators
Logical operators
Bit wise operators
Conditional operators
Special operators

Output:
&& Operator : Both conditions are true
|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is inverted as false
Bit wise Operators Arithmetic operators
Assignment operators
Inc/dec operators
Relational operators
Logical operators
Bit wise operators
• One of C’s powerful features is a set of bit manipulation operators. These Conditional operators
permit the programmer to access and manipulate individual bits within a Special operators

piece of data to perform bit operations. The various Bitwise Operators


available in C are shown in Figure
• Decimal values are converted into binary values which are the sequence of
bits and bit wise operators work on these bits.
• These operators can operate upon ints and chars but not on floats and
doubles.
Arithmetic operators
Assignment operators
Inc/dec operators

Special Operators Relational operators


Logical operators
Bit wise operators
Conditional operators
Special operators
Example program for Special
operators in C
Example program for sizeof()
operator in C

Output:
Storage size for int data type:4
Storage size for char data type:1
Storage size for float data type:4
Storage size for double data type:8
Thank you
&
Any question?

You might also like