bbs 514 - yapısal programlama (structured programming)1 lexical elements kinds of tokens in c :...

21
BBS 514 - Yapısal Programlama (Structured Programming) 1 Lexical Elements Kinds of tokens in C : • Keywords • Identifiers • Constants • Operators • Punctuators

Post on 21-Dec-2015

228 views

Category:

Documents


5 download

TRANSCRIPT

BBS 514 - Yapısal Programlama (Structured Programming) 1

Lexical Elements

Kinds of tokens in C :

• Keywords

• Identifiers

• Constants

• Operators

• Punctuators

BBS 514 - Yapısal Programlama (Structured Programming) 2

Keywords

auto break case char const

continue default do double else

enum extern float for goto

if int long register return

short signed sizeof static struct

switch typedef union unsigned void

volatile while

BBS 514 - Yapısal Programlama (Structured Programming) 3

Identifiers

• Sequence of letters, digits, and the special character _.

• A letter or underscore must be the 1st character of an identifier.

– For this class, don’t use identifiers that begin with an underscore.

• C is case-sensitive: – Apple and apple are two different identifiers.

BBS 514 - Yapısal Programlama (Structured Programming) 4

Identifiers (cont.)

• Valid variable names:

n

x

_id

num1

a_long_identifier

• Invalid variable names:

var.1

num!2

not#this

126East

+more

BBS 514 - Yapısal Programlama (Structured Programming) 5

Declarations

• All variables must be declared before use. A declaration specifies a type, and contains a list of one or more variables of that type.

int lower, upper, step;

int c, line;• A variable may be initialized in its declaration.

int i = 0;• Variables for which there is no explicit initialization have

undefined (garbage) values.

BBS 514 - Yapısal Programlama (Structured Programming) 6

Constants

C manipulates various kinds of values.

• integer constants: 0, 37, 2001• floating constants: 0.8, 199.33, 1.0• character constants: ‘a’, ‘5’, ‘+’• string constants: “a”, “Monday”

BBS 514 - Yapısal Programlama (Structured Programming) 7

Arithmetic Operators

• The binary arithmetic operators are +, -, *, /, and the modulus operator %.

• x % y produces the remainder when x is divided by y.

11 % 5 = 1

20 % 3 = 2• Arithmetic operators associate left to right.

BBS 514 - Yapısal Programlama (Structured Programming) 8

Precedence and Associativity of Operators

Operator Precedence:x = 1 + 2 * 3; (What is the value of x?) x = 1 + (2*3); x is 7? orx= (1+2) * 3; x is 9?

Associativity: (left to right)10 + 3 + 710 – 3 + 715 / 5 * 2

BBS 514 - Yapısal Programlama (Structured Programming) 9

Assignment Operator

• The expression can simply be a constant or a variable:

int x, y;

x = 5;

y = x;

x = 6;• The expression can be an arithmetic expression:

x = y + 1;

y = x * 2;

variable = expression

BBS 514 - Yapısal Programlama (Structured Programming) 10

Assignment Compatibility

int x = 3;double y =12.8;x = y; truncates y!y = x; it is okay.x = 5 + 3.2; truncates the result!y = 5 + 3.2; it is okay.x = 10/4; x is 2y = 10/4; y is 2.0y = 10/4.0; y is 2.5

BBS 514 - Yapısal Programlama (Structured Programming) 11

Increment and Decrement Operators

• The increment operator ++ adds 1 to its operand.

++i; equiv. i = i + 1;

i++; equiv. i = i + 1;

• The decrement operator -- subtracts 1 from its operand.

--i; equiv. i = i - 1;

i--; equiv. i = i - 1;

BBS 514 - Yapısal Programlama (Structured Programming) 12

Increment and Decrement Operators

Suppose n = 5.

n++; /* sets n to 6 */

n = n + 1;

++n; /* sets n to 6 */

n = n + 1;

BBS 514 - Yapısal Programlama (Structured Programming) 13

Increment and DecrementOperators (cont.)

• When ++a is used in an expression, the value of a is incremented before the expression is evaluated.

• When a++ is used, the expression is evaluated with the current value of a and then a is incremented.

• Similarly, with --a and a--.

BBS 514 - Yapısal Programlama (Structured Programming) 14

Increment and DecrementOperators (cont.)

Suppose n = 5.

x = n++; /* sets x to 5 and n to 6 */

1. x = n;

2. n = n + 1;

x = ++n; /* sets x and n to 6 */

1. n = n + 1;

2. x = n;

BBS 514 - Yapısal Programlama (Structured Programming) 15

/* Preincrementing and postincrementing*/

#include <stdio.h>

int main(void){ int c;

c = 5; printf(“%d\n”,c); printf(“%d\n”, c++); printf(“%d\n\n”,c);

c = 5; printf(“%d\n”, c); printf(“%d\n”, ++c); printf(“%d\n”, c); return 0;}

556

566

BBS 514 - Yapısal Programlama (Structured Programming) 16

/*** increment and decrement expressions ***/#include <stdio.h>

int main(void){ int a =0 , b = 0, c = 0;

a = ++b + ++c; printf(“\n%d %d %d”, a,b,c); a = b++ + c++; printf(“\n%d %d %d”, a,b,c); a = ++b + c++; printf(“\n%d %d %d”, a,b,c); a = b-- + --c; printf(“\n%d %d %d”, a,b,c); return 0;}

2 1 12 2 25 3 35 2 2

BBS 514 - Yapısal Programlama (Structured Programming) 17

Arithmetic Assignment Operators

Assume:

int c = 3, d = 5, e = 4, f = 6, g = 12;

Operator Expression Explanation Assigns

+= c += 7 c = c + 7 10 to c

-= d -= 4 d = d - 4 1 to d

*= e *= 5 e = e * 5 20 to e

/= f /= 3 f = f / 3 2 to f

%= g %= 9 g = g % 9 3 to g

BBS 514 - Yapısal Programlama (Structured Programming) 18

Exercise 1

• Given

int i=2, j = 3, k =4;

Evaluate the following:

1. k *= i – j;

2. i += j = k;

3. j -= -k++ + ++i;

BBS 514 - Yapısal Programlama (Structured Programming) 19

Exercise 2

• Determine the value of the following expressions:

1. 2 + 2 * (2 * 2 – 2) % 2 / 2

2. 10 + 9 *( ( 8 + 7) % 6) + 5 * 4 % 3 * 2 + 1

3. 2 * (float) 6 / 5

BBS 514 - Yapısal Programlama (Structured Programming) 20

Exercise 3

1. Given double x;

int y = 4;

What is the result of the following?x = y/3+(y-1.0)/y;

2. Given double x = 2.5;

What is the value of x?x = (int)x*3+x;

BBS 514 - Yapısal Programlama (Structured Programming) 21

Programming Exercises

1. Write a program that reads a four digit number and finds the sum of individual digits.

2. The distance d between points (x1, y1) and (x2, y2) is given by

d = ( ( x1 – x2)2 + (y1 – y2)2) ½

Write a program that reads the coordinates of the two points and calculates the distance between them