1 BBS 514 YAPISAL PROGRAMLAMA (STRUCTURED PROGRAMMING) LECTURE 3: ASSIGNMENT OPERATOR Lecturer: Burcu Can BBS 514 - Yapısal Programlama (Structured Programming)
2 Lexical Elements (Sözcüksel Elemanlar) Kinds of tokens in C (C de kullanılan sözcükler): Keywords (Anahtar sözcükler) Identifiers (Belirteçler) Constants (Sabitler) Operators (İşleçler) Punctuators (Noktalama işaretleri)
Keywords (Anahtar Sözcükler) 3 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
Identifiers (Belirteçler) 4 Sequence of letters, digits, and the special character _. (Harf, rakam ve _ dan oluşan bir dizi karakter) A letter or underscore must be the 1 st character of an identifier. (Mutlaka ilk karakter harf ya da _ olmalı.) For this class, don t use identifiers that begin with an underscore. (Ancak _ ile başlayan belirteçler bu derste kullanmayalım.) C is case- sensitive: (C büyük- küçük harf duyarlıdır!) Apple and apple are two different identifiers. n (Apple veapple iki farklı belirteçtir.)
Identifiers (cont.) (Belirteçler - devam) Valid variable names: (Geçerli değişken adları) n x _id num1 a_long_identifier Invalid variable names: (Geçerli olmayan değişken adları) var.1 num!2 not#this 126East +more BBS 514 - Yapısal Programlama (Structured Programming) 5
Declarations (Tanımlar) 6 All variables must be declared before use. A declaration specifies a type, and contains a list of one or more variables of that type. (Her değişken kullanılmadan önce mutlaka tanımlanmalıdır. Değişkenin türü ve ardından o türdeki bir ya da birden fazla değişken ismi sıralanır.) int lower, upper, step; int c, line; A variable may be initialized in its declaration. (Tanımlama aşamasında değişkene ilk değer de verilebilir.) int i = 0; Variables for which there is no explicit initialization have undefined (garbage) values. (İlk değer verilmeyen değişkenlerde rasgele bir değer bulunur.)
Constants (Sabitler) 7 C manipulates various kinds of values. (Farklı türde sabit değerler olabilir.) integer constants: 0, 37, 2001 (tamsayı sabitler) floating constants: 0.8, 199.33, 1.0 (ondalıklı sayı sabitler) character constants: a, 5, + (karakter sabitler) string constants: a, Monday (katar sabitler)
Arithmetic Operators (Aritmetik İşleçler) 8 (İkili işleçler) The binary arithmetic operators are +, -, *, /, and the modulus operator %. x % y produces the remainder when x is divided by y. (x in y ile bölümünden elde edilen kalan mod ile ifade edilir.) 11 % 5 = 1 20 % 3 = 2 Arithmetic operators associate left to right. (Aritmetik içleçler soldan sağa doğru çözümlenir.)
Precedence and Associativity of Operators (İşleçlerde Öncelik ve Birleşme Özelliği) 9 Operator Precedence: x = 1 + 2 * 3; (What is the value of x?) x = 1 + (2*3); x is 7? or x= (1+2) * 3; x is 9? Associativity: (left to right) 10 + 3 + 7 10 3 + 7 15 / 5 * 2
10 Assignment Operator (Atama İşleci) variable = expression değişken = ifade The expression can simply be a constant or a variable: (ifade, bir sabit ya da değişken olabilir.) int x, y; x = 5; y = x; x = 6; The expression can be an arithmetic expression: (ifade, aritmetik bir ifade olabilir.) x = y + 1; y = x * 2;
11 Assignment Compatibility (Atama Uyumu) int x = 3; double y =12.8; x = y; y = x; x = 5 + 3.2; y = 5 + 3.2; x = 10/4; x is 2 y = 10/4; y is 2.0 y = 10/4.0; y is 2.5 truncates y! (y yi daraltır) it is okay. (doğru) truncates the result! (sonucu daraltır) it is okay. (doğru)
Increment and Decrement Operators (Artırma ve Azaltma İşleçleri) 12 The increment operator ++ adds 1 to its operand. (İşlenenin değerini bir artırır) ++i; equiv. i = i + 1; i++; equiv. i = i + 1; The decrement operator - - subtracts 1 from its operand. (İşlenenin değerini bir azaltır.) - - i; equiv. i = i - 1; i- - ; equiv. i = i - 1;
Increment and Decrement Operators (Artırma ve Azaltma İşleçleri) 13 Suppose n = 5. n++; /* sets n to 6 */ n = n + 1; ++n; /* sets n to 6 */ n = n + 1;
Increment and Decrement Operators (cont.) (Artırma ve Azaltma İşleçleri) 14 When ++a is used in an expression, the value of a is incremented before the expression is evaluated. (++a ifadesinde, önce a nın değeri artırılır, ondan sonra ifade hesaplanır.) When a++ is used, the expression is evaluated with the current value of a and then a is incremented. (a++ ifadesinde, önce ifade hesaplanır, ondan sonra a nın o anki değeri bir artırılır.) Similarly, with - - a and a- -. (aynı durum - - a ve a- - için de geçerli.)
Increment and Decrement Operators (cont.) (Artırma ve Azaltma İşleçleri) 15 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;
/* Preincrementing and postincrementing*/ 16 #include <stdio.h> int main(void) { int c; c = 5; printf( %d\n,c); printf( %d\n, c++); printf( %d\n\n,c); 5 5 6 5 6 6 } c = 5; printf( %d\n, c); printf( %d\n, ++c); printf( %d\n, c); return 0;
/*** increment and decrement expressions ***/ 17 #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 1 2 2 2 5 3 3 5 2 2
Arithmetic Assignment Operators (Aritmetik Atama İşleçleri) 18 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
Exercise 1 19 Given int i=2, j = 3, k =4; Evaluate the following: 1. k *= i j; 2. i += j = k; 3. j - = - k++ + ++i;
Exercise 2 20 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
Exercise 3 21 1. Given double x; int y = 4; What is theresult of thefollowing? x = y/3+(y-1.0)/y; 2. Given double x = 2.5; What is the value of x? x = (int)x*3+x;
Programming Exercises 22 1. Write a program that reads a four digit number and finds the sum of individual digits. (4 basamaklı bir sayı alan ve basamaklardaki rakamların toplam sonucunu bulan programı yazın.) 2. The distance d between points (x 1, y 1 ) and (x 2, y 2 ) is given by (iki nokta arasındaki uzaklık şöyle bulunmaktadır:) d = ( ( x 1 x 2 ) 2 + (y 1 y 2 ) 2 ) ½ Write a program that reads the coordinates of the two points and calculates the distance between them (iki noktaya ait koordinatları kullanıcıdan alan ve bu iki nokta arasındaki uzaklığı hesaplayan programı yazın.)