types, operators & expressions
DESCRIPTION
Types, Operators & Expressions. CSE 105 Structured Programming Language (C) Presentation - 3. Basic Data Types (K n R – 2.2). 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 - PowerPoint PPT PresentationTRANSCRIPT
CSE-105 – Structured ProgrammingCSE, BUET
Types, Operators & Expressions
CSE 105Structured Programming Language
(C)Presentation - 3
2
CSE-105 – Structured ProgrammingCSE, BUET
Basic Data Types (KnR – 2.2)
chara single byte, holds one character in local character set
intnatural size of integers in the host machine
floatSingle-precision floating point
doubledouble-precision floating point
3
CSE-105 – Structured ProgrammingCSE, BUET
Data type : char (KnR – 2.2)
Qualifier of sign
Basic type
Size (bits)
Range
char 8A single ASCII character
signed –128 to 127 (–27 to +27 – 1)
unsigned
0 to 255 (0 to +28 – 1)unsigned
signed
8-bits magnitude
7-bits magnitude
1-bit sign: 1 means negative and 0 means positive
4
CSE-105 – Structured ProgrammingCSE, BUET
Data type: int (KnR – 2.2)
Qualifier of sign
Qualifier of size
Basic type
Size (bits)
Range
int 16 or 32
host m/c dependent
signed short int 16 –32768 to 32767 (–215 to +215 – 1)
unsigned
short int 16 0 to 65535 (0 to +216 – 1)
singed long int 32 –2,147,483,648 to 2,147,483,647(–231 to +231 – 1)
unsigned
long int 32 0 to 4,294,967,295(0 to +232 – 1)
5
CSE-105 – Structured ProgrammingCSE, BUET
Data types: floating points (KnR – 2.2)
floatsingle-precision
doubledouble-precision
long doubleextended-precision
All of these types are implementation dependentThey can be of one, two or three distinct sizes.
6
CSE-105 – Structured ProgrammingCSE, BUET
Caution with floating point (KnR – 2.2)
Consider following code:
value of x will be very close to zero e.g. 10-7. Hence this won’t work correct code is:
7
CSE-105 – Structured ProgrammingCSE, BUET
Constants : Numeric (KnR – 2.3)
data type Example of constant
octal hex
int 1234 02322 0x4d2
long int 123456789L 0726746425L
0x75BCD15L
unsigned long int
123456789UL 0726746425UL
0x75BCD15UL
double 123.45 or 123.45e-3 …
N/A N/A
float 123.45F or 123.45e-3F
N/A N/A
long double 123.45L or 123.45-3L
N/A N/A
8
CSE-105 – Structured ProgrammingCSE, BUET
Constants : Character (KnR – 2.3)
A character constant is an integer, which can be presented in different formats:
`A` : represents ASCII 65th character i.e., A
int var = `A` + 10; // value of var will be 75
`0` : represents ASCII 48th character i.e., zero
int var = `0` + 10; // value of var will be 58
`\0101` : represent octal 101. `A` = 6510 = 1018
`\x41` : represent hexadecimal 41. `A` = 6510 = 4116
Character constants can participate in numeric operations just as any other integer.
9
CSE-105 – Structured ProgrammingCSE, BUET
Constant : Escape Sequence (KnR – 2.3)
Back slash “\” is called escape characterCharacters following “\” bear special meaning as follows:
\a bell character = `\007`
\0 null termination for string
\b backspace \\ back slash
\f formfeed \? question mark
\n new line \` single qoute
\r carriage return \`` double qoute
\t horizontal tab \ooo
octal number 0 o 7
\v vertical tab \xhh
hex number h {0..9, A..F}
10
CSE-105 – Structured ProgrammingCSE, BUET
Constant : String (KnR – 2.3)
String constant is a sequence of zero or more characters surrounded by double quote. E.g.,
I am a string /* the empty string */
String constants can be concatenated at compile time:
hello, world. hello, world.This is useful for splitting long strings in several lines
String is an array of characters terminated by `\0`Observe the difference between string and character constants below:
hello, world.
h e l l o , w o r l d \0
0 1 12………………..
11
CSE-105 – Structured ProgrammingCSE, BUET
Constant : Usage (KnR – 2.3)
Constant expressionContains only constantsCan be evaluated in compile time.
A and `A` are not the samechar v = `A` + 3; /* OK */char w = A + 3; /* Wrong */
Try these out:printf(\b Alert is %d, `\b`);printf(line is %c %d tested, `\n` , `\n`);printf(Backspace h\bas effect );printf (He said, \I am fine, thank %s.\, you);
#define LO `A`#define HI `Z`int cap_count[HI-LO+1]
A65 \0
`A`65
12
CSE-105 – Structured ProgrammingCSE, BUET
Constant: Enumeration (KnR – 2.3)
Enumerations are used to define multiple constantsAlternate to #define, but values are auto-generated.Examples:
enum { NO, YES }; #define NO 0#define YES 1
enum escapes { BELL = `\a`, BACKSPACE = `\b`, TAB = `\b` NEWLINE = `\n`, VTAB = `\v`, RETURN = `\r`};
enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };
/* FEB is 2, MAR is 3, etc. */
14
CSE-105 – Structured ProgrammingCSE, BUET
Arithmetic Operators (KnR – 2.5)
+ , – , * , /, %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 doubleFor –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
15
CSE-105 – Structured ProgrammingCSE, BUET
Relational & Logical Operators (KnR – 2.6)
Relational Operators: > >= < <=have lower precedence than arithmetic operatorsthus, i < lim – 1 i < (lim – 1)
Equality Operators:== !=has lower precedence than Relational operators
Logical Operators: && || … also !Left to right associativeEvaluation stops as soon as truth or falsehood is known
Example follows
16
CSE-105 – Structured ProgrammingCSE, BUET
&& and || have “short circuit” evaluation
left operand evaluated first; right operand evaluated only if necessary
&&: if left operand evaluates to 0, immediately return 0||: if left operand evaluates to nonzero value, immediately return 1
This allows you to write code like this with confidence:
if(y == 0 || x / y > MAX) ...
CS 3090: Safety Critical Programming in C
16
17
CSE-105 – Structured ProgrammingCSE, BUET
Relational & Logical Operators (KnR – 2.6)
for (i=0; i<lim—1 && (s[i]=getchar())!=`\n` && s[i]!=EOF; i++)
;
Condition in for loop is executed left to rightArray bound must be checked (i<lim-1) before s[i]=getchar( ) is executedIf the test i<lim-1 fails we cannot assign value to s[i]Similarly it would be unfortunate to check s[i]!=EOF before s[i]=getchar( ) is executedSince precedence of != is higher than assignment parentheses are needed in (s[i]=getchar())!=`\n`
s[i]=getchar()!=`\n` would be 1 or 0
DIY Ex. 2.2: Write a loop equivalent to above for loop without using && or ||
18
CSE-105 – Structured ProgrammingCSE, BUET
Type Conversions (KnR – 2.7)
Automatic conversions convert narrower operand into a wider one.Warnings are generated for expressions that may loose information.Binary operators (like +, *) with mixed operand types the lower or narrower type is converted to higher or wider type.
long double > double > float > long int > short int > char. unsigned > signed
-1L < 1U // 1U is promoted to signed long-1L > 1UL // -1L is promoted to unsigned long
Explicit type conversion can be specified as follows(type-name) expression
19
CSE-105 – Structured ProgrammingCSE, BUET
Type Conversions (KnR – 2.7)
char can be used just like int./* atoi: convert s to integer*/int atoi(char s[]){
int i, n;
n = 0;for (i = 0; s[i] >= `0` && s[i] <= `9`; ++i)
n = n * 10 + (s[i] - `0`);return n;
}
/* lower: convert ct to lower case; ASCII only; */int lower(int c){
if (c >= `A` && c <= `Z`)return c + `a` - `A`;
else return c;
}
20
CSE-105 – Structured ProgrammingCSE, BUET
Bitwise Operators (KnR – 2.9)
Bitwise operators can only be applied to integral operands: i.e., all versions of char and int& : bitwise AND
Mask off some bitsn = n & 0177;
sets all bit of n to zero except the rightmost 7 bits
| : bitwise ORTurn on some bitsx = x | 0100;
Turns on the 6th (zero based) bit of x
Must be distinguished from && and || operatorsint x = 1, y = 2; x && y 1
x & y 0x || y 1x | y 3
A B &
0 0 0
0 1 0
1 0 0
1 1 1
A B |
0 0 0
0 1 1
1 0 1
1 1 1
21
CSE-105 – Structured ProgrammingCSE, BUET
Example: setting a bit to onevalue = value | 1 << bit_number;
0xAA | 1 << 2
10101010
00000100
10101110
22
CSE-105 – Structured ProgrammingCSE, BUET
value = value & ~ ( 1 << bit_number );
0xAA & ~ (1 << 3)
00001000
11110111
10101010
10100010
Example: setting a bit to zero
23
CSE-105 – Structured ProgrammingCSE, BUET
Bitwise Operators (KnR – 2.9)
^ : bitwise XORToggle (complements) some bits.x = x ^ 0100;
Turns on the 6th (zero based) bit of x
~ : one’s complement (unary)x = x & ~077;
Sets the last six bits of x to 0Since sizeof(int) varies it is better than, x = x & 0177700
~ (bit-wise not) vs ! (logical not) operators int x = 0; ~x -1
!x 1
A B ^
0 0 0
0 1 1
1 0 1
1 1 0
A ~
0 1
1 0
24
CSE-105 – Structured ProgrammingCSE, BUET
Bitwise Operators (KnR – 2.9)
<< : Left shiftx = x << 3;
Shifts x left by 3 bits x = x * 23
>> : Right shiftx = x >> 3;
Shifts x right by 3 bits x = x / 23
If x is unsigned then vacated bits are filled with 0If x is signed then vacated bits are filled with 0 or 1 depending on the machine.
25
CSE-105 – Structured ProgrammingCSE, BUET
Bitwise Operators (KnR – 2.9)
Application: /* getbits: get n bits from position p*/Unsigned getbits (unsigned x, int p, int n){
return (x >> (p+1-n)) & ~(~0 << n);}
n=4, p=7 1111 ... 1111111111~0
1111 ... 1111110000~0<<n
0000 ... 0000001111~(~0<<n)
0110 ... 0101101011 ... 9876543210
x
00000110 ... 010110x>>(p+1-n)
00000000 ... 000110(x>>(p+1-n))&~(~0<<n)
DIY: Exercise 2-6, 2-7, 2-8
26
CSE-105 – Structured ProgrammingCSE, BUET
Conditional Expressions (KnR – 2.11)
Only ternary operator ?:expr1 ? expr2 : expr3;
The wider type of expr2 and expr3 is the result’s type
float f and int n(n > 0) ? f : n will be float regardless of the value of n
Example usage:printf(“You have %d item%s.”, n, n == 1 ?
“”, “s”);
if (a > b)z = a;
elsez = b;
z = (a > b) ? a : b;
27
CSE-105 – Structured ProgrammingCSE, BUET
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 ( = )
RValueIt is the result of an expression that has a vlaue
Usually used at the right side of assignment operator (=)
Example:
a = a + 10;
[48375] 55 + 10;
a: 48357
RValueLValue
55
28
CSE-105 – Structured ProgrammingCSE, BUET
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
29
CSE-105 – Structured ProgrammingCSE, BUET
= 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
if (y = 0)printf("Sorry, can't divide by zero.\n");
elseresult = x / y;
The compiler will not catch this bug!
Assignment performed;
y set to 0 (oops)
Expression returnsresult of
assignment:0, or "false"
else clause executed:
divide by 0!
30
CSE-105 – Structured ProgrammingCSE, BUET
Assignment Operators (KnR – 2.10)
For binary operators there exists short hand of the form:
+= –= *= /= %= <<= >>= &= |= ^= expr1 op= expr2 expr1 = (expr1) op (epxr2)
x *= y + 1 x = (x) * (y + 1)Ease: yyval[yypv[p3+p4] + yypv[p1+p2]] += 2;
DIY : Exercise 2-9.Faster version ofbitcount usingx &= (x – 1)
/* bitcount: count 1 bits in x */int bitcount (unsigned x){
int b;for (b = 0; x != 0; x >>= 1)
b += x & 1; return b;
}
31
CSE-105 – Structured ProgrammingCSE, BUET
Assignment Operators (KnR – 2.10)
Swap two integers in one statementx ^= y ^= x^= y;
Number of odd and even integers in input
a^b bx ^= y
a^b ay ^= x
b ax ^= y
a b
x y
32
CSE-105 – Structured ProgrammingCSE, BUET
Increment & Decrement Operators (KnR – 2.8)
Unary ++ (increment) and unary -- (decrement)Effect of Prefix and Postfix placement. (x++ vs. ++x)
At some places effect is sameif (c == `\n`) if (c == `\n`)
nl++; ++nl;
At some places it has different effect :
n = 5;x = n++;
n = 5;x = n;n = n+1;
x == 5n == 6
n = 5;x = ++n;
n = 5;n = n+1; x = n;
x == 6n == 6
33
CSE-105 – Structured ProgrammingCSE, BUET
Increment & Decrement Operators (KnR – 2.8)
Effect of Prefix and Postfix placement. (x++ vs. ++x)
A more complicated example:
i = 4;s[i++] = 8;
i = 4;s[i] = 8;i = i+1;
s[4] == 8i == 5
i = 4;s[++i] = 8;
i = 4;i = i+1;s[i] = 8;
s[5] == 8i == 5
int a = 0, b = 0, c = 0;c = a++ + ++b;
a = a + 1;c = a + b;b = b + 1;
a == 1c == 1b == 1
34
CSE-105 – Structured ProgrammingCSE, BUET
Increment & Decrement Operators (KnR – 2.8)
Proper application of these operators can produce efficient and concise code:
DIY: Exercise: 2-4 Exercise: 2-5
/* strcat: concatenate t to end of s; s must be big enough */void strcat(char s[ ], char t[ ]){
int i = 0, j = 0;
while (s[i++] != `\0`) ; /* find end of s */while ((s[i++] = t[i++]) != `\0`) ; /* copy t */
}
35
CSE-105 – Structured ProgrammingCSE, BUET
Increment & Decrement Operators (KnR – 2.8)
The operand of ++ and -- operator must have LValue
i++; // is ok(i + j)++; // is not ok
(i + j) has only RValue
(i = 0)++; // is oki = 0 returns the LValue of I
(i = j + 2)++; // is oki = j + 2 i is assigned to (j + 2) and LValue of i is returnedthen the i is incremented by 1
(i = j = 0) ++; // is oki = j = 0 i and j is assigned 0 and LValue of i is returnedi is then incremented by 1.
i: 48123 20
j: 48567 3050
36
CSE-105 – Structured ProgrammingCSE, BUET
Precedence, Associativity & Evaluation order
Precedence: given two operators of different types, which is evaluated first?Associativity: given a sequence of instances of the same operator, are they evaluated left-to-right or right-to-left?Evaluation order: given an operator with a number of operands, in what order are the operands evaluated?
37
CSE-105 – Structured ProgrammingCSE, BUET
Precedence (KnR – 2.12)
Operators Associativity
( ) [ ] left to right
Uniary ! ~ ++ -- + - (type) sizeof right to left
Arithmetic * / %+ -
left to right
Shift << >> left to right
Relational < <= => >== !=
left to right
Bit-wise &^|
left to right
Logical &&||
left to right
Ternary ? : right to left
Assignment = += -= *= /= %= &= ^= |= <<= >>=
right to left
Hig
her P
reced
en
ce