Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Expressions
CSE 105
Structured Programming Language (C)
Presentation - 3
char
a single byte, holds one character in local character set
int
natural size of integers in the host machine
float
Single-precision floating point
double
double-precision floating point
2
CSE, BUET CSE-105 Structured Programming
Data type : char (K R 2.2) n
4
CSE, BUET CSE-105 Structured Programming
Data types: floating points (K R 2.2) n
float
single-precision
double
double-precision
long double
extended-precision
5
CSE, BUET CSE-105 Structured Programming
Caution with floating point (K R 2.2) n
value of x will be very close to zero e.g. 10-7. Hence this wont
work correct code is:
6
CSE, BUET CSE-105 Structured Programming
Constants : Numeric (K R 2.3) n
7
CSE, BUET CSE-105 Structured Programming
Constants : Character (K R 2.3) n
8
CSE, BUET CSE-105 Structured Programming
Constant : Escape Sequence (K R 2.3) n
9
CSE, BUET CSE-105 Structured Programming
Constant : String (K R 2.3) n
hello, world.
h e l l o , w o r l d \0
0 1 .. 12
10
CSE, BUET CSE-105 Structured Programming
Constant : Usage (K R 2.3) n
12
CSE, BUET CSE-105 Structured Programming
Operators
+ , , * , /, %
Division operator /
For ve operand, direction of truncation is m/c dependent
-7/2 = ? -3 or -4
Mod operator %
Cannot be applied on float or double
For ve operand, sign of result is m/c dependent
Precedence :
Highest : unary + and
2nd highest : * and /
Lowest : + and
Arithmetic operators associate left to right.
A * B * C = (A * B) * C
14
CSE, BUET CSE-105 Structured Programming
Relational & Logical Operators (K R 2.6) n
18
CSE, BUET CSE-105 Structured Programming
Type Conversions (K R 2.7) n
n = 0;
for (i = 0; s[i] >= `0` && s[i] <= `9`; ++i)
n = n * 10 + (s[i] - `0`);
return n;
}
0xAA | 1 << 2
10101010
00000100
10101110
21
CSE, BUET CSE-105 Structured Programming
Example: setting a bit to zero
value = value & ~ ( 1 << bit_number );
00001000
11110111
10101010
10100010
22
CSE, BUET CSE-105 Structured Programming
Bitwise Operators (K R 2.9) n
^ : bitwise XOR A B ^
0 0 0
Toggle (complements) some bits.
0 1 1
x = x ^ 0100; 1 0 1
Turns on the 6th (zero based) bit of x 1 1 0
24
CSE, BUET CSE-105 Structured Programming
Bitwise Operators (K R 2.9) n
Application:
/* getbits: get n bits from position p*/
Unsigned getbits (unsigned x, int p, int n)
{
return (x >> (p+1-n)) & ~(~0 << n);
}
if (a > b)
Only ternary operator ?: z = (a > b) ? a : b;
z = a;
else
expr1 ? expr2 : expr3; z = b;
26
CSE, BUET CSE-105 Structured Programming
Assignment Operators : LValue vs RValue
LValue
It is the result of an expression that has an address
usually used at the left side of assignment operator ( = )
RValue
It is the result of an expression that has a vlaue
Usually used at the right side of assignment operator (=)
Example: LValue RValue
a = a + 10;
a: 48357 55
[48375] 55 + 10;
27
CSE, BUET CSE-105 Structured Programming
How x = y = z = 17 works?
= is right-associative so the expression is interpreted as:
x = ( y = ( z = 17 )
z is assigned 17;
return value is 17
y is assigned 17;
return value is 17
x is assigned 17;
return value is 17
28
CSE, BUET CSE-105 Structured Programming
= vs. ==
It is easy to confuse equality with assignment
In C, the test expression of an if statement can be
any int expression including an assignment
expression
Assignment performed;
y set to 0 (oops)
if (y = 0)
printf("Sorry, can't divide by zero.\n");
else Expression returns
result = x / y; result of assignment:
0, or "false"
29
CSE, BUET CSE-105 Structured Programming
Assignment Operators (K R 2.10) n
x ^= y ^= x^= y;
x ^= y a^b b
y ^= x a^b a
x ^= y b a
31
CSE, BUET CSE-105 Structured Programming
Increment & Decrement Operators (K R 2.8) n
n = 5;
n = 5; x == 6
n = n+1;
x = ++n; n == 6
x = n;
32
CSE, BUET CSE-105 Structured Programming
Increment & Decrement Operators (K R 2.8) n
i = 4;
i = 4; s[5] == 8
i = i+1;
s[++i] = 8; i == 5
s[i] = 8;
b = b + 1; a == 1
int a = 0, b = 0, c = 0;
c = a + b; c == 1
c = a++ + ++b;
a = a + 1; b == 1
33
CSE, BUET CSE-105 Structured Programming
Increment & Decrement Operators (K R 2.8) n
DIY:
Exercise: 2-4
Exercise: 2-5
34
CSE, BUET CSE-105 Structured Programming
Increment & Decrement Operators (K R 2.8) n
36
CSE, BUET CSE-105 Structured Programming
Precedence (K R 2.12)n
Operators Associativity
() [] left to right
Higher Precedence
Relational < <= => > left to right
== !=
Bit-wise & left to right
^
|
Logical && left to right
||
Ternary ?: right to left