Objectives: Operators, Variables, and Strings in C
l
know what operators are available, and how to use theml
understand what a variable in C is, what its scope is, where in memory it may be stored,l
understand how to use and assign to variablesOperators
l
arithmetic:operator integer operands floating point operands
+ addition 23 + 10 = 33 22.5 + 10.3 = 32.8
− subtraction 23 − 10 = 13 22.5 − 10.3 = 12.2
∗ multiplication 23 ∗ 10 = 230 22.5 ∗ 10.3 = 231.75
/ division 23 / 10 = 2 22.5 / 10.3 = 2.18446(· · ·)
% remainder 23 % 10 = 3 N/A
The library
<math.h>
has a range of mathematical functions, e.g. cos(), sqrt()l
relational operators:operator description example
>
greater thanx > 100
>
=
greater or equal tox >= 20
<
less thanx < 100
<
=
less or equal tox <= 20
==
equalityx == 100
!=
inequalityx != 100
l
boolean operators:operator description example
&& logical and (0 <= x) && (x < 10)
|| logical or (x == ’a’) || (x == ’A’)
! logical negation !(x < 5)
Operators (II)
l
bitwise operators:operator description example (assume char x = 0x4A)
& bitwise and x & 0x0F
| bitwise or x | 0x0F
~ bitwise negate ~x
^ exclusive or x ^ 0x0F
<< shift left x << 4
>> shift right x >> 4
l
conditional operator: boolean expression?
expression1:
expression2int
absX = (x >= 0)? x:
−
x;
What does the following compute?
(x >= y)? x: y
l
classifications:n
unary operator: one operand, e.g.+(6)
,−
(10.43)
,~0xa
n
binary operator: two operands, e.g.23 + 10
,23 % 10
,0x17 ^ 0x23
,0x8
|
0x1
Variables
l
variables provide a reference to some stored informationn
it is almost always a location in memory (sometimes the data for a variable may be just in a register)n
it thus has an associated address, as well as a valuel
e.g.int
x = 0;
x
:0
x = x + 1;
x
:1
l
to obtain the value of a variable, you name in inside an expression e.g.printf(
"%d\n"
, 2
∗
x+1)
;l
to obtain the address of a variable, you use the&
operator e.g.printf(
"the address of x is %p\n"
, &x);
l
variables are just references to memory. The contents of memory can be changed in a number of ways; when this happens the values of your variables will change before your eyes!Scope of Variables
l
a variable defined within a block is local to that block, initialized every time the block is executed (if at all), and are temporary. These exist on the stack (the address may vary each time the block is executed)l
a variable defined within a block with astatic
keyword is also local to that block, but is initialized once and is permanent (in the global data area of memory)l
a variable defined outside a block is global and permanent (thestatic
keyword reduces the scope to the file it is within)int iam global ;
static int iam global with reduced scope ;
int function () {
int iam local to this function ;
static int also local but permanent ; }
Assignment Statements
l
lvalue = rvalue
e.g.year = 1900 + 99;
n
value of righthand side is stored in lefthand siden
rvalue
is a variable, constant or expressionn
lvalue
is a variablel
an assignment statement has a value (that of the righthand side)year2 = year = 1968;
l
minimal type checking! e.g. withint
year;
float
length;
:year = 1968.29;
/
∗
year becomes 1968
∗
/
length = 2 / 3;
/
∗
length becomes 0.0
∗
/
length = 2.0 / 3;
/
∗
length becomes 0.66...
∗
/
l
=
is different to the equality operator==
if
(year2 == 1968) ...
/
∗
possibly false
∗
/
if
(year2 = 2001) ...
/
∗
always true
∗
/
String Assignments and Operations
l
string operations require library calls:month =
"Dec"
+
"ember"
does not work – no in-built string operators!l
we have to use the string librarystring.h
(seeman string
on student system)# include
<
string .h>
...
char
month [9];
...
strcpy ( month ,
" D e c "
);
strcat ( month ,
" e m b e r "
);
if
( strcmp ( month ,
" D e c e m b e r "
) != 0)
printf (
" D o h ! \ n "
);
l
in C, strings are implemented as arrays ofchar
n
individual elements of the string variablemonth
:month[0]
,month[1]
, . . . ,month[8]
Precedence, Type Hierarchy and Shorthand Notations
l
general operator precedencen
arithmetic operator precedence: for∗
/%
, left to rightn
when in doubt, use()
’s!l
data type conversion hierarchyl
one of C’s features is that it permits very concise code, with a number of convenient shorthandsn
shorthand operators: e.g.i += 1;
Exercises
Write a program that calculates the area of a triangle given the lengths of its sides. The program should take input on a single line with three space separated floats which are the lengths of the sides of the triangle. It should output on a single line the area of the triangle. Hint: use Heron’s formula:
A
=√
s
(s
− a
)(s
− b
)(s
− c
) wheres
= a+2b+c anda,
b,
c
are the lengths of the sides.In the program below, work out the address location of each of the variables. What is the scope of each? Is that scope minimal? What are some problems with the code below?
# include<stdio .h>
static int count ; int i;
int add (int a, int b) {
static int x = b; for (i = 0; i < a; i ++) x = x + 1; return x; } void main () { int y; i = 6; y = add (i, 8); printf (" % d \ n " , y+ count ); }