• No results found

Objectives: Operators, Variables, and Strings in C

N/A
N/A
Protected

Academic year: 2021

Share "Objectives: Operators, Variables, and Strings in C"

Copied!
9
0
0

Loading.... (view fulltext now)

Full text

(1)

Objectives: Operators, Variables, and Strings in C

l

know what operators are available, and how to use them

l

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 variables

(2)

Operators

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 than

x > 100

>

=

greater or equal to

x >= 20

<

less than

x < 100

<

=

less or equal to

x <= 20

==

equality

x == 100

!=

inequality

x != 100

l

boolean operators:

operator description example

&& logical and (0 <= x) && (x < 10)

|| logical or (x == ’a’) || (x == ’A’)

! logical negation !(x < 5)

(3)

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

:

expression2

int

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

(4)

Variables

l

variables provide a reference to some stored information

n

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 value

l

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!

(5)

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 a

static

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 (the

static

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 ; }

(6)

Assignment Statements

l

lvalue = rvalue

e.g.

year = 1900 + 99;

n

value of righthand side is stored in lefthand side

n

rvalue

is a variable, constant or expression

n

lvalue

is a variable

l

an assignment statement has a value (that of the righthand side)

year2 = year = 1968;

l

minimal type checking! e.g. with

int

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

/

(7)

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 library

string.h

(see

man 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 of

char

n

individual elements of the string variable

month

:

month[0]

,

month[1]

, . . . ,

month[8]

(8)

Precedence, Type Hierarchy and Shorthand Notations

l

general operator precedence

n

arithmetic operator precedence: for

/%

, left to right

n

when in doubt, use

()

’s!

l

data type conversion hierarchy

l

one of C’s features is that it permits very concise code, with a number of convenient shorthands

n

shorthand operators: e.g.

i += 1;

(9)

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

) where

s

= a+2b+c and

a,

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 ); }

References

Related documents

Two-factor authentication can provide the three guarantees I de- scribed for both online and offline PINs.. Consider signing into your Gmail account using an iPhone when

The study is helpful for many players in Home Appliances to make following strategies related to advertising for any particular company: -..  Advertising

Struggling with each of once upon a time promo: canceled or renewed for season four seasons on both sides of the abc.. Seen making great tool to

Equipment or jdbc to the contracting officer to create clauses: who shall be subject and therefore, regardless of twelve months as design, section a clause is in the contract as

All global variables declared within the declaration of declaring local variable using negative or keyword avoids this block in england, they are available outside the numbers..

An initial eff ort was made to compare the gene and repeat content of the tomato and potato genomes, based on the available BAC-end sequences for both species (Datema et al.,

The study will focus on the passive in-situ mode of the device, which will provide a stepping stone for the development of performance-based design procedures for its semi-active

This study has examined how indigenous student migrants from the Chittagong Hill Tracts who live in Dhaka experience increased mobility and how access to new lifestyles and global