W-02 - L-02 - Token, Operator and Expression
W-02 - L-02 - Token, Operator and Expression
Week-02, Lecture-02
• 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
Keywords ② Identifiers
③ Constants
④ Strings
⑤ Special symbols
⑥ Operators
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
③ Constants
④ Strings
⑤ Special symbols
⑥ Operators
Constant ④ Strings
⑤ Special symbols
⑥ Operators
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
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
Shorthand or
Increment and Decrement Arithmetic operators
Assignment operators
Operators Inc/dec operators
Relational operators
Logical operators
Bit wise operators
Operators
Logical operators
Bit wise operators
Conditional operators
Special operators
Operators
Bit wise operators
Conditional operators
Special 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,
• 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 operators are used to find the relation between two variables.
i.e. to compare the values of two variables.
Arithmetic 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
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?