Mek0101 - 04 - 25.10.2021
Mek0101 - 04 - 25.10.2021
Mek0101 - 04 - 25.10.2021
Bilgisayar
Programlama I
2021-2022 Güz
Hafta 4
2
• printf() and scanf() in C
• Memory Concept and value assignment
Last Week • Variables in C
o Defining variables
o Types of variables (local, global, static, automatic, external)
• Data Types
o Basic data types
o Derived data types
o void
3
Plan for Today
• Recap of the last week
• Data Types - revisited
• Operators
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
4
Data Types
Bits and Bytes; Numbers
• No matter what type of representation, most human beings can understand, at least the two types above.
➢ Unfortunately the computer doesn't.
5
Data Types
Bits and Bytes; Numbers
• Modern computers are built up with transistors.
• Whenever an electric current pass into the transistors either an ON or OFF status will be
established.
• Therefore the computer can only recognize two numbers, 0 for OFF, and 1 for
ON,
• which can be referred to as BIT.
• There is nothing in between Bit 0 and Bit 1
• eg Bit 0.5 doesn't exist.
• Hence computers can be said to be discrete machines.
• The number system consists only of two numbers is called Binary System. Binary (Base 2)
• And to distinguish the different numbering systems, the numbers human use,
ie 1,2,3,4..., will be called Decimals (since they are based 10 numbers)
6
Data Types
Bits and Bytes; Numbers
7
Data Types
Bits and Bytes; Numbers
10
Data Types
Bits and Bytes; Numbers
Base 10
11
Data Types
Bits and Bytes; Numbers
Base 2
12
Data Types
Bits and Bytes; Numbers
Base 10 to Base 2
Question: What is 6 in base 2?
13
Data Types
Bits and Bytes; Numbers
Base 10 to Base 2
Question: What is 6 in base 2?
Calculation Result Remainder
6:2 3 0
3:2 1 1
1:2 1
14
Data Types
Bits and Bytes; Numbers
Base 10 to Base 2
Question: What is 6 in base 2?
Calculation Result Remainder
6:2 3 0
Result: 1 1 0
3:2 1 1
1:2 1
15
Data Types
Bits and Bytes; Numbers
Base 2 to Base 10
Question: What is the base-2 value of 1010 in base-10?
1 0 1 0
16
Data Types
Bits and Bytes; Numbers
Question: What is the minimum and maximum base-10 value a single byte
(8 bits) can store?
17
Data Types
Bits and Bytes; Numbers
Question: What is the minimum and maximum base-10 value a single byte
(8 bits) can store?
Max:
Min: 0
18
Data Types
Bits and Bytes; Numbers
Hexadecimal (Base 16)
• When working with bits, oftentimes we have large numbers
• If we represent bits in base-16; this is called hexadecimal
19
Data Types
Bits and Bytes; Numbers
Hexadecimal (Base 16)
• Hexadecimal is base-16, so we need digits for 1-15. How do we do this?
20
Data Types
Bits and Bytes; Numbers
Hexadecimal (Base 16)
21
Data Types
Bits and Bytes; Numbers
Hexadecimal (Base 16)
• We distinguish hexadecimal numbers by prefixing them with 0x, and binary numbers
with 0b.
• E.g. 0xf5 is 0b11110101
22
Data Types
Bits and Bytes; Numbers
Hexadecimal to Binary
• What is 0x173A in binary?
23
Data Types
Bits and Bytes; Numbers
Binary to Hexadecimal
• What is 0b1111001010 in hexadecimal? (Hint: start from the right)
24
Data Types
Bits and Bytes; Numbers
26
Data Types
Bits and Bytes; Numbers
Integer Representations
27
Data Types
Bits and Bytes; Numbers
Integer Representations
28
Data Types
Bits and Bytes; Numbers
Integer Representations
29
Data Types
Bits and Bytes; Numbers
30
Data Types
Bits and Bytes; Numbers
• As we have seen, the computers do not really understand the (decimal) numbers
we use, but only binary representations of them.
31
Data Types
Bits and Bytes; Numbers
32
Data Types
Bits and Bytes; Numbers
ASCII Table:
33
Data Types
Bits and Bytes; Numbers
• Be careful!
34
Data Types
Bits and Bytes; Numbers
• Be careful!
Output:
35
Data Types
Bits and Bytes; Numbers
• Be careful!
Output: ?
36
Data Types
Bits and Bytes; Numbers
• Be careful!
Output:
37
Data Types
Formatting the output
• You can use more than the basic %f conversion character to format floating-point
values.
• The w sets the maximum width of the entire number, including the decimal
place. The p sets precision
• For example:
38
Example
39
Data Types
Formatting the output (cont.)
40
Plan for Today
• Recap of the last week
• Data Types – cont.
• Operators
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
42
Operators
An operator is a symbol that tells the compiler to perform specific mathematical or
logical functions. C language is rich in built-in operators and provides the following
types of operators:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
43
Arithmetic Operators
• Most C programs perform calculations using the C arithmetic operators
• The asterisk (*) indicates multiplication and the percent sign (%) denotes the
remainder operator
• In algebra, to multiply a times b, we simply place these single-letter variable
names side by side as in ab.
• In C, however, if we were to do this, ab would be interpreted as a single, two-
letter name (or identifier).
• Therefore, C requires that multiplication be explicitly denoted by using the *
operator as in a * b.
• The arithmetic operators are all binary operators.
• For example, the expression 3 + 7 contains the binary operator + and the
operands 3 and 7.
Details on
slide 75-78
45
Arithmetic Operators (cont.)
50
Arithmetic Operators (cont.)
51
© 2016 Pearson Education, Ltd. All rights reserved.
#include
main() {
int a
<stdio.h>
= 21;
Example
int b = 10;
int c ;
c = a + b;
printf("Line 1 - Value of c is %d\n", c );
c = a - b;
printf("Line 2 - Value of c is %d\n", c );
c = a * b;
printf("Line 3 - Value of c is %d\n", c );
c = a / b;
printf("Line 4 - Value of c is %d\n", c );
c = a % b;
printf("Line 5 - Value of c is %d\n", c );
c = a++;
printf("Line 6 - Value of c is %d\n", c );
c = a--;
printf("Line 7 - Value of c is %d\n", c );
}
52
Decision Making: Equality and Relational
Operators
• Executable C statements either perform actions (such as calculations or input
or output of data) or make decisions (we’ll soon see several examples of
these).
• We might make a decision in a program,
• for example, to determine whether a person’s grade on an exam is greater than or
equal to 60 and whether the program should print the message “Congratulations! You
passed.”
• This section introduces a simple version of C’s if statement that allows a
program to make a decision based on the truth or falsity of a statement of fact
called a condition.
Output
57
Logical Operators
• C provides logical operators that may be used to form conditions, even more
complex conditions by combining simple conditions.
• Following table shows the logical operators supported by C language:
Operator Description
&& Logical AND operator
|| Logical OR Operator
! Logical NOT (negation) Operator
• Suppose that we wish to ensure at some point in a program that either or both
of two conditions are true before we choose a certain path of execution. In this
case, we use the || operator:
if (semesterAverage >= 90 || finalExam >= 90)
printf("Student grade is A");
• This statement also contains two simple conditions:
• The condition semesterAverage >= 90 is evaluated to determine
whether the student deserves an “A” in the course because of a solid
performance throughout the semester.
• The condition finalExam >= 90 is evaluated to determine whether
the student deserves an “A” in the course because of an outstanding
performance on the final exam.
62
© 2016 Pearson Education, Ltd. All rights reserved.
Logical Operators (cont.)
Logical OR (||) Operator
63
© 2016 Pearson Education, Ltd. All rights reserved.
Logical Operators (cont.)
• The && operator has a higher precedence than ||.
• Both operators associate from left to right.
• An expression containing && or || operators is evaluated only until truth or
falsehood is known.
• Thus, evaluation of the condition
gender == 1 && age >= 65
• will stop if gender is not equal to 1 (i.e., the entire expression is false), and continue if
gender is equal to 1 (i.e., the entire expression could still be true if age >= 65).
• This performance feature for the evaluation of logical AND and logical OR
expressions is called short-circuit evaluation.
64
© 2016 Pearson Education, Ltd. All rights reserved.
Logical Operators (cont.)
Logical Negation (!) Operator
65
© 2016 Pearson Education, Ltd. All rights reserved.
Logical Operators (cont.)
66
© 2016 Pearson Education, Ltd. All rights reserved.
Logical Operators (cont.)
• In most cases, you can avoid using logical negation by expressing the condition
differently with an appropriate relational operator.
• For example, the preceding statement may also be written as follows:
if (grade != sentinelValue)
printf("The next grade is %f\n", grade);
67
© 2016 Pearson Education, Ltd. All rights reserved.
Example:
Logical Operators (cont.)
68
Assignment Operators
• We’ve already seen one assignment operator (=) several times.
= is a simple assignment operator and it assigns values from right
side operands to left side operand
• Additionally, C provides several assignment operators for abbreviating
assignment expressions.
• For example, the statement
c = c + 3;
• can be abbreviated with the addition assignment operator += as
c += 3;
• The += operator adds the value of the expression on the right of the
operator to the value of the variable on the left of the operator and
stores the result in the variable on the left of the operator.
71
© 2016 Pearson Education, Ltd. All rights reserved.
Confusing Equality (==) and
Assignment (=) Operators (cont.)
• Two aspects of C cause these problems:
• One is that any expression in C that produces a value can be used in the decision
portion of any control statement. If the value is 0, it’s treated as false, AND if the
value is nonzero, it’s treated as true.
• The second is that assignments in C produce a value, namely the value that’s
assigned to the variable on the left side of the assignment operator.
72
© 2016 Pearson Education, Ltd. All rights reserved.
Confusing Equality (==) and
Assignment (=) Operators (cont.)
• For example, suppose we intend to write: • This expression is a simple
if (payCode == 4) assignment whose value is the
printf(“%s“, "You get a bonus!"); constant 4.
but we accidentally write • Because any nonzero value is
if (payCode = 4) interpreted as “true,” the
printf(“%s“, "You get a bonus!"); condition in this if statement
• The first if statement properly awards a bonus to the is always true, and not only is
person whose paycode is equal to 4. the value of payCode
• The second if statement—the one with the error— inadvertantly set to 4, but the
evaluates the assignment expression in the if condition. person always receives a bonus
regardless of what the actual
paycode is!
73
© 2016 Pearson Education, Ltd. All rights reserved.
Confusing Equality (==) and
Assignment (=) Operators (cont.)
Confusing == and = in Standalone Statements • If x is equal to 1, the condition is true and
the expression returns the value 1.
• The other side of the coin can be equally • If x is not equal to 1, the condition is
unpleasant. false and the expression returns the
• Suppose you want to assign a value to a variable value 0.
with a simple statement such as • Regardless of what value is returned,
x = 1; there’s no assignment operator, so the
but instead write value is simply lost, and the value of x
x == 1; remains unaltered, probably causing an
• Here, too, this is not a syntax error. execution-time logic error.
• Rather the compiler simply evaluates the • Many compilers, however, will issue a
conditional expression. warning on such a statement.
74
© 2016 Pearson Education, Ltd. All rights reserved.
Increment and Decrement Operators
• C also provides the unary increment operator, ++, and the unary
decrement operator, --
• If a variable c is to be incremented by 1, the increment operator ++ can
be used rather than the expressions c = c + 1 or c += 1.
• If increment or decrement operators are placed before a variable (i.e.,
prefixed), they’re referred to as the preincrement or predecrement
operators, respectively.
• If increment or decrement operators are placed after a variable (i.e.,
postfixed), they’re referred to as the postincrement or postdecrement
operators, respectively.
Output
83