This document discusses tokens, expression evaluation, and type casting in C programming. It defines tokens as the smallest meaningful elements of a program like keywords, identifiers, constants, strings, and operators. It describes expression evaluation using an example of evaluating the expression a = 5. It then explains type casting as converting one data type to another, like converting a long to an int, and gives examples of implicit and explicit type casting in C.
3. 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;
}
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 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
7. 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++.
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
13. 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.
14. 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*/
}
15. 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*/
}
16. Type Casting
❏ Implicit
❏ Example:
char val = ‘a’;
int num = val;
❏ Explicit
❏ Example:
char val = ‘a’;
int num =(int) val;
17. 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: