Programming and Data Structures (PDS) : CS11001/CS11002
Programming and Data Structures (PDS) : CS11001/CS11002
Programming and Data Structures (PDS) : CS11001/CS11002
Assignments
1
Assignments
2
Imperative Programming
Example
3
Another example
4
Example
int x = 5; x = x + (x * x);
The value 5 stored in x is substituted for each
occurrence of x in the right side, i.e., the expression
5 + (5 * 5) is evaluated.
The result is 30 and is stored back to x.
Thus, this assignment operation causes the value of
x to change from 5 to 30.
The equality sign in the assignment statement is not
a mathematical equality, i.e., the above statement
does not refer to the equation x = x + x2 (which
happens to have a single root, namely x = 0).
5
Implicit Conversion
Example
#include<stdio.h>
main()
{
float a = -7.89., b = 3;
int c;
typedef unsigned long newlong;
newlong d;
c = (int) a + b;
d=c;
printf("%d\n",c);
printf("%x\n",d);
}
6
Typecasting Again
int a, b, c; c = (a = 8) + (b = 13);
Here a is assigned the value 8 and b the
value 13. The values (8 and 13) returned by
these assignments are then added and the
sum 21 is stored in c.
The assignment of c also returns a value, i.e.,
21. Here we have ignored this value.
7
Assignment is right associative
For example,
a = b = c = 0;
is equivalent to a = (b = (c = 0));
Here c is first assigned the value 0. This
value is returned to assign b, i.e., b also gets
the value 0. The value returned from this
second assignment is then assigned to a.
Thus after this statement all of a, b and c are
assigned the value 0.
Generation of Expressions
A constant is an expression.
A (defined) variable is an expression.
If E is an expression, then so also is (E).
If E is an expression and op a unary operator defined in C, then
op E is again an expression.
If E1 and E2 are expressions and op is a binary operator defined
in C, then E1 op E2 is again an expression.
If V is a variable and E is an expression, then V = E is also an
expression.
8
Examples
53 /* constant */
-3.21 /* constant */
'a' /* constant */
x /* variable */
-x[0] /* unary negation on a variable */
x + 5 /* addition of two subexpressions */
(x + 5) /* parenthesized expression */
(x) + (((5))) /* another parenthesized expression */
y[78] / (x + 5) /* more complex expression */
y[78] / x + 5 /* another complex expression */
y / (x = 5) /* expression involving assignment */
1 + 32.5 / 'a' /* expression involving different data types */
Non-examples
9
Operators in C
Oper Meanin
Description
ator g
unary
Applicable for integers and real numbers. Does not make
- negatio
enough sense for unsigned operands.
n
(binary)
+ Applicable for integers and real numbers.
addition
(binary)
- subtract Applicable for integers and real numbers.
ion
(binary)
* multiplic Applicable for integers and real numbers.
ation
Operators in C
10
Examples
55 / 21 evaluates to 2.
55 % 21 evaluates to 13.
Bitwise Operators
11
Operator Meaning Example
a = 237 1 1 1 0 1 1 0 1
& AND b = 174 1 0 1 0 1 1 1 0
a & b is 172 1 0 1 0 1 1 0 0
a = 237 1 1 1 0 1 1 0 1
| OR b = 174 1 0 1 0 1 1 1 0
a | b is 239 1 1 1 0 1 1 1 1
a = 237 1 1 1 0 1 1 0 1
^ EXOR b = 174 1 0 1 0 1 1 1 0
a ^ b is 67 0 1 0 0 0 0 1 1
a = 237 1 1 1 0 1 1 0 1
~ Complement
~a is 18 0 0 0 1 0 0 1 0
a = 237 1 1 1 0 1 1 0 1
>> Right-shift
a >> 2 is 59 0 0 1 1 1 0 1 1
b = 174 1 0 1 0 1 1 1 0
<< Left-shift
b << 1 is 92 0 1 0 1 1 1 0 0
12