Tokens, Expression
Evaluation & Type
Casting
Agenda
❏ Tokens
❏ Expression Evaluation
❏ Type Casting
Tokens
❏ A token is the smallest element of a program that is
meaningful to the compiler. Tokens can be classified
as follows:
❏ Keywords
❏ Identifiers
❏ Constants
❏ Strings
❏ Special Symbols
❏ Operators
#include<stdio.h>
int c, d;
int main()
{
int a, b;
return 0;
}
Keywords
Identifiers / Variables
❏ There are certain rules that should be followed while
naming c identifiers:
❏ They must begin with a letter or underscore(_).
❏ They must consist of only letters, digits, or
underscore. No other special character is allowed.
❏ It should not be a keyword.
❏ It must not contain whitespace.
❏ It should be up to 31 characters long as only first
31 characters are significant.
Constants
❏ Constants are also like normal variables. But, only
difference is, their values can not be modified by the
program once they are defined. Constants refer to
fixed values. They are also called as literals.
❏ Constants may belong to any of the data
type.Syntax: const data_type variable_name; (or) const
data_type *variable_name;
Example : #define pi = 3.1416
Strings
❏ Strings are nothing but an array of characters ended
with a null character (‘0’).This null character
indicates the end of the string. Strings are always
enclosed in double quotes. Whereas, a character is
enclosed in single quotes in C and C++.
Special Symbols
❏ Brackets[]
❏ Parentheses()
❏ Braces{}
❏ comma (, )
❏ semi colon (;)
❏ asterick (*)
❏ assignment operator(=)
❏ pre processor(#)
Operators
❏ Unary Operators (++, --)
❏ Binary Operators
❏ Arithmetic operators
❏ Relational Operators
❏ Logical Operators
❏ Assignment Operators
❏ Conditional Operators
❏ Bitwise Operators
❏ Ternary Operators (? :)
Bitwise Operators vs. Logical Operators
(*Class Discussion Slide with O4)
❏ Logical Operators
&& ||
A = 10 , B = 20
# A > 10 && B <= 20
= F && T
= F
= 0
❏ Bitwise Operators
& |
A = 15 , B = 15
# A & B
= 15
Expression Evaluation
Expression Evaluation Let, a = 5
Type Casting
Converting one data type into another is known as type casting or, type-conversion. For example, if you
want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'. You can convert the
values from one type to another explicitly using the cast operator as follows −
(type_name) expression
Consider the following example where the cast operator causes the division of one integer variable by
another to be performed as a floating-point operation −
#include <stdio.h>
Int main() {
int sum = 17, count = 5;
double mean;
mean = (double) sum / count;
printf("Value of mean : %fn", mean );
return 0;
}
When the above code is compiled and executed, it produces the following result −
Value of mean : 3.400000
It should be noted here that the cast operator has precedence over division, so the value of sum is first
converted to type double and finally it gets divided by count yielding a double value.
Implicit Type Casting
When the type conversion is performed automatically by
the compiler without programmers intervention, such type
of conversion is known as implicit type conversion or type
promotion.
int x;
for(x=97; x<=122; x++) {
printf("%c", x); /*Implicit casting from int to char thanks to %c*/
}
Explicit Type Casting
The type conversion performed by the programmer by posing the data type of
the expression of specific type is known as explicit type conversion. The explicit
type conversion is also known as type casting.
Type casting in c is done in the following form:
(data_type)expression;
where, data_type is any valid c data type, and expression may be constant,
variable or expression.
For example,
int x;
for(x=97; x<=122; x++) {
printf("%c", (char)x); /*Explicit casting from int to char*/
}
Type Casting
❏ Implicit
❏ Example:
char val = ‘a’;
int num = val;
❏ Explicit
❏ Example:
char val = ‘a’;
int num =(int) val;
Example
Consider the following code:
If we want to get the exact value of 7/5 then we need explicit casting from int to float:
Thanks

L3_Tokens, Expression Evaluation.pptx

  • 1.
  • 2.
    Agenda ❏ Tokens ❏ ExpressionEvaluation ❏ Type Casting
  • 3.
    Tokens ❏ A tokenis the smallest element of a program that is meaningful to the compiler. Tokens can be classified as follows: ❏ Keywords ❏ Identifiers ❏ Constants ❏ Strings ❏ Special Symbols ❏ Operators #include<stdio.h> int c, d; int main() { int a, b; return 0; }
  • 4.
  • 5.
    Identifiers / Variables ❏There are certain rules that should be followed while naming c identifiers: ❏ They must begin with a letter or underscore(_). ❏ They must consist of only letters, digits, or underscore. No other special character is allowed. ❏ It should not be a keyword. ❏ It must not contain whitespace. ❏ It should be up to 31 characters long as only first 31 characters are significant.
  • 6.
    Constants ❏ Constants arealso like normal variables. But, only difference is, their values can not be modified by the program once they are defined. Constants refer to fixed values. They are also called as literals. ❏ Constants may belong to any of the data type.Syntax: const data_type variable_name; (or) const data_type *variable_name; Example : #define pi = 3.1416
  • 7.
    Strings ❏ Strings arenothing but an array of characters ended with a null character (‘0’).This null character indicates the end of the string. Strings are always enclosed in double quotes. Whereas, a character is enclosed in single quotes in C and C++.
  • 8.
    Special Symbols ❏ Brackets[] ❏Parentheses() ❏ Braces{} ❏ comma (, ) ❏ semi colon (;) ❏ asterick (*) ❏ assignment operator(=) ❏ pre processor(#)
  • 9.
    Operators ❏ Unary Operators(++, --) ❏ Binary Operators ❏ Arithmetic operators ❏ Relational Operators ❏ Logical Operators ❏ Assignment Operators ❏ Conditional Operators ❏ Bitwise Operators ❏ Ternary Operators (? :)
  • 10.
    Bitwise Operators vs.Logical Operators (*Class Discussion Slide with O4) ❏ Logical Operators && || A = 10 , B = 20 # A > 10 && B <= 20 = F && T = F = 0 ❏ Bitwise Operators & | A = 15 , B = 15 # A & B = 15
  • 11.
  • 12.
  • 13.
    Type Casting Converting onedata type into another is known as type casting or, type-conversion. For example, if you want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'. You can convert the values from one type to another explicitly using the cast operator as follows − (type_name) expression Consider the following example where the cast operator causes the division of one integer variable by another to be performed as a floating-point operation − #include <stdio.h> Int main() { int sum = 17, count = 5; double mean; mean = (double) sum / count; printf("Value of mean : %fn", mean ); return 0; } When the above code is compiled and executed, it produces the following result − Value of mean : 3.400000 It should be noted here that the cast operator has precedence over division, so the value of sum is first converted to type double and finally it gets divided by count yielding a double value.
  • 14.
    Implicit Type Casting Whenthe type conversion is performed automatically by the compiler without programmers intervention, such type of conversion is known as implicit type conversion or type promotion. int x; for(x=97; x<=122; x++) { printf("%c", x); /*Implicit casting from int to char thanks to %c*/ }
  • 15.
    Explicit Type Casting Thetype conversion performed by the programmer by posing the data type of the expression of specific type is known as explicit type conversion. The explicit type conversion is also known as type casting. Type casting in c is done in the following form: (data_type)expression; where, data_type is any valid c data type, and expression may be constant, variable or expression. For example, int x; for(x=97; x<=122; x++) { printf("%c", (char)x); /*Explicit casting from int to char*/ }
  • 16.
    Type Casting ❏ Implicit ❏Example: char val = ‘a’; int num = val; ❏ Explicit ❏ Example: char val = ‘a’; int num =(int) val;
  • 17.
    Example Consider the followingcode: If we want to get the exact value of 7/5 then we need explicit casting from int to float:
  • 18.