Lec 03 Assignments
Lec 03 Assignments
Autumn 2020
Assignments
and Arithmetic
Operations
Assignments
Initialization during declaration helps one store
constant values in memory allocated to variables.
Later one typically does a sequence of the following:
Read the values stored in variables.
Do some operations on these values.
Store the result back in some variable.
This three-stage process is effected by an
assignment operation. A generic assignment
operation looks like: variable = expression;
Assignments are Imperative
Here expression consists of variables and constants
combined using arithmetic and logical operators.
The equality sign (=) is the assignment operator.
To the left of this operator resides the name of a variable.
All the variables present in expression are loaded to the
CPU. The ALU then evaluates the expression on these
values.
The final result is stored in the location allocated to
variable.
The semicolon at the end is mandatory and denotes that
the particular statement is over. It is a statement delimiter
Imperative Programming
A C program typically consists of a sequence
of statements. They are executed one-by-
one from top to bottom (unless some
explicit jump instruction or function call is
encountered). This sequential execution of
statements gives C a distinctive
imperative flavor.
This means that the sequence in which
statements are executed decides the final
values stored in variables.
Example
int x = 43, y = 15; /* Two integer
variables are declared and initialized */
x = y + 5; /* The value 15 of y is fetched and
added to 5. The sum 20 is stored in the
memory location for x. */
y = x; /* The value stored in x, i.e., 20 is
fetched and stored back in y. */
After these statements are executed both the
memory locations for x and y store the integer
value 20.
Another example
Let us now switch the two assignment operations.
int x = 43, y = 15; /* Two integer variables are declared
and initialized */
y = x; /* The value stored in x, i.e., 43 is fetched and
stored back in y. */
x = y + 5; /* The value 43 of y is fetched and added
to 5. The sum 48 is stored in the memory location for
x. */
For this sequence, x stores the value 48 and y the
value 43, after the two assignment statements are
executed.
Assignments with same
variables
The right side of an assignment operation may contain
multiple occurrences of the same variable.
For each such occurrence the same value stored in the
variable is substituted.
Moreover, the variable in the left side of the
assignment operator may appear in the right side too.
In that case, each occurrence in the right side refers to
the older (pre-assignment) value of the variable.
After the expression is evaluated, the value of the
variable is updated by the result of the evaluation.
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).
Floating point numbers, characters
and array locations may also be used
in assignment operations.
float a = 2.3456, b = 6.5432, c[5]; /* Declare float variables and arrays */
char d, e[4]; /* Declare character variables and arrays */
c[0] = a + b; /* c[0] is assigned 2.3456 + 6.5432, i.e., 8.8888 */
c[1] = a - c[0]; /* c[1] is assigned 2.3456 - 8.8888, i.e., -6.5432 */
c[2] = b - c[0]; /* c[2] is assigned 6.5432 - 8.8888, i.e., -2.3456 */
a = c[1] + c[2]; /* a is assigned (-6.5432) + (-2.3456), i.e., -8.8888 */
d = 'A' - 1; /* d is assigned the character ('@') one less than 'A' in the
ASCII chart */
e[0] = d + 1; /* e[0] is assigned the character next to '@', i.e., 'A' */ e[1]
= e[0] + 1; /* e[1] is assigned the character next to 'A', i.e., 'B' */ e[2] =
e[0] + 2; /* e[2] is assigned the character second next to 'A', i.e., 'C' */
e[3] = e[2] + 1; /* e[3] is assigned the character next to 'C', i.e., 'D' */
Implicit Conversion
An assignment does an implicit type conversion, if its
left side turns out to be of a different data type than
the type of the expression evaluated.
float a = 7.89, b = 3.21; int c; c = a + b;
Here the right side involves the floating point operation
7.89 + 3.21. The result is the floating point value
11.1. The assignment plans to store this result in an
integer variable.
The value 11.1 is first truncated and subsequently the
integer value 11 is stored in c.
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);
}
Typecasting Again
float a = 7.89, b = 3.21;
int c; c = (int)(a + b);
(binary)
+ Applicable for integers and real numbers.
addition
(binary)
- subtractio Applicable for integers and real numbers.
n
(binary)
* multiplicat Applicable for integers and real numbers.
ion
Operators in C
++ -- unary non-associative
-~ unary right
+- binary left
|^ binary left
Booleaan Values in C
Instead it uses any value (integer, floating point, character, etc.) as a
Boolean value.
Any non-zero value of an expression evaluates to "true", and the zero
value evaluates to "false". In fact, C allows expressions as logical
conditions.
Example:
0 False
1 True
6 - 2 * 3 False
(6 - 2) * 3 True
0.0075 True
0e10 False
'A' True
'\0' False
x = 0 False
x = 1 True
The last two examples point out the potential danger of mistakenly
writing = in place of ==. Recall that an assignment returns a value,
which is the value that is assigned.
Logical Operators
Logical operator Syntax True if and only if
NOT !C C is false
Examples
(7*7 < 50) && (50 < 8*8) True
(7*7 < 50) && (8*8 < 50) False
(7*7 < 50) || (8*8 < 50) True
!(8*8 < 50) True
('A' > 'B') || ('a' > 'b') False
('A' > 'B') || ('A' < 'B') True
('A' < 'B') && !('a' > 'b') True
Note
Notice that here is yet another source of
logical bug. Using a single & and | in order
to denote a logical operator actually means
letting the program perform a bit-wise
operation and possibly ending up in a
logically incorrect answer
Associativity of Logical
Operators
Operator(s) Type Associativity
! Unary Right
== != Binary Left
|| Binary Left
Examples
x <= y && y <= z || a >= b is equivalent to
((x <= y) && (y <= z)) || (a >= b).
C1 && C2 && C3 is equivalent to
(C1 && C2) && C3.
a > b > c is equivalent to
(a > b) > c.
The If Statement
C Statement:
if(Condition)
Block1;
scanf("%d",&x);
if (x < 0) x = -x;
x=x+1;
The If else Statement
C Statement:
if (Condition)
{ Block 1 }
else { Block 2 }
scanf("%d",&x);
if (x >= 0) y = x;
else y = -x;
x=x+1;
The ternary statement
Consider the following special form of the if-else
statement:
if (C) v = E1; else v = E2; Here depending upon the
condition C, the variable v is assigned the value of
either the expression E1 or the expression E2. This
can be alternatively described as:
v = (C) ? E1 : E2; Here is an explicit example.
Suppose we want to compute the larger of two
numbers x and y and store the result in z. We can
write:
z = (x >= y) ? x : y;