📄 04.txt
字号:
CS 1355
Intro to Programming in C
Thursday 2006.9.21 (Week 2)
Lecture Notes (at http://r638-2.cs.nthu.edu.tw/C/notes/04.txt)
Introduction to C Programming (Chapter 2 cont'd)
Concepts
- Arithmetic expressions
- Operator associativity (left associative, right associative)
- Operator precedence
- Relational operators
- if statements
- statement blocks
Arithmetic expressions
- Expression: something that yields a value
- basic: literals (1, 2, 3, 100, 2345, ..), variables (a, b, x,...)
- compound: operators (+ - * / ...), function calls ( f(x), ...)
- Actually, there are two kinds of numbers:
- int: integers (whole numbers) e.g., 1 2 3 100 2034
- float: floating-point (fraction) e.g., 3.1415 7.8899 0.0002324
- Arithmetic expression: evaluates to an arithmetic value (number)
Arithmetic operators:
- Binary operators
add: a + b
subtract: x - y
multiply: i * j
divide: m / n (in case of int, the integer part of the quotient)
(if either m, n is a float, then result is a float)
modulo: m % n => "remainder" (for integer division only)
- examples (can try this out in Ch)
> 7 / 4
1
> 7 % 4
3
> 7.0 / 4
1.7500
- Unary operators:
negation: -a (if a is 3, then -a is -3)
positive: +a (doesn't do anything)
Operator associativity
x = 2 + 3 + 4;
==> x is 9. Do (2+3)+4 = 5+4 = 9
x = 8 - 2 - 1;
==> x is 5. Do (8-2)-1 = 6-1 = 5.
* This is called "left associative" (evaluate the left pair, then next, next,..)
* If right associative, then it would be
(8 - (2 - 1)) = 8 - 1 = 7, which would be incorrect.
Operator Precedence
x = 2 + 3 * 4;
==> x is 14, because it's implicitly
2 + (3 * 4) = 2 + 12 = 14.
It is NOT
(2 + 3) * 4 = 5 * 4 = 20.
Operator precedence overrides associativity
+ - are both left associative and equal precedence
* / % are all left associative, equal precedence,
and higher precedence than + -
Q: what is the evaluation order of
x = 8 * 7 / 4 * 3 % 5 + 6 / 2 - 7;
Answer:
It is evaluated as
(((((8 * 7) / 4) * 3) % 5) + (6 / 2)) - 7
= ((((56 / 4 = 14) * 3 = 42) % 5 = 2) + (6 / 2 = 3) = 5) - 7
= -2
Function evaluation:
- parameters are evaluated first
- the function is called
- then the return value of the function is used.
Example
#include <stdio.h>
int sum(int x, int y) {
return x + y;
}
int main() {
int a = 2 + sum(3*2-4, 4%3+5) * 4;
printf("%d\n", a);
}
Run it
./a.out
34
Reason: 2 + sum(3*2-4, 4%3+5) * 4
= 2 + sum(2, 6) * 4
= 2 + 8 * 4
= 34
Relational operators
- compare two numeric values
- Result: true or false value
- Six comparison operators: == != > < >= <=
x == y x is "equal to" y
x != y x is "not equal to" y
x > y x is "greater than" y
x < y x is "less than" y
x >= y x is "greater than or equal to" y
x <= y x is "less than or equal to" y
where x and y are arithmetic expressions.
True/False value:
- what is the result of the relational operator?
- in C, anything zero is considered false
- integer value 0
- empty string ""
- null character '\0'
- in C, anything else non-zero is considered true
- by default, you get 1 for a true value
- but you probably should not assume it's 1
Example: (try it in Ch, prompt is >)
> int x = 2;
> x > 3
0 /* This means false */
> x < 4
1 << This means true
> x == 2
1
etc
Operator precedence
- relational operators have lower precedence than arithmetic
=> evaluate arithmetic operators before comparing
- example (assume x = 2)
x * 3 - 4 <= 3 + x * 2
This is evaluated as
(2 * 3 - 4) <= (3 + 2 * 2)
(6 - 4) <= (3 + 4)
2 <= 7
True
- so, not necessary to put parentheses around the arithmetic expressions
If statements
- a way to decide whether to do something
- decision is based on a true/false value
Example
#include <stdio.h>
int main() {
int age;
printf("how old are you? ");
scanf("%d", &age);
if (age < 20) {
printf("No, you are too young to drink alcohol\n");
} else {
printf("Yes, you are old enough to drink alcohol\n");
}
}
Run it
% ./a.out
how old are you? 30
Yes, you are old enough to drink alcohol
% ./a.out
how old are you? 12
No, you are too young to drink alcohol
% ./a.out
how old are you? 20
Yes, you are old enough to drink alcohol
% ./a.out
how old are you? 19
No, you are too young to drink alcohol
%
How it works
- if, else are keywords
- The structure looks like
if (condition) {
statement block S1
} else {
statement block S2
}
- meaning:
if the condition expression has a true value,
then statement block S1 is executed;
otherwise (condition is false), statement block S2 is executed.
- Note:
the ( ) around condition are required!!
=> it is a syntax error to say
if age < 20 { ... }
you must have ( ) around condition, as in
if (age < 20) { ... }
- statement blocks S1 and S2 each can contain a sequence of statements
actually, { } enclose a statement block
but { } itself is considered to be one (compound) statement.
Spacing and indentation:
- in C, spaces, tabs, and newlines are ignored
(unless you are inside a string!)
- Example: the following programs are equivalent
/* program 1 */
#include <stdio.h>
int main() {
int age;
printf("how old are you? ");
scanf("%d", &age);
if (age < 20) {
printf("No, you are too young to drink alcohol\n");
} else {
printf("Yes, you are old enough to drink alcohol\n");
}
}
/* program 2 */
#include <stdio.h>
int
main()
{ int age; printf("how old are you? "); scanf("%d",&age);
if (age<20) { printf("No, you are too young to drink alcohol\n"); }
else { printf("Yes, you are old enough to drink alcohol\n"); }}
/* program 3 */
#include <stdio.h>
int main() { int age;
printf("how old are you? ");
scanf("%d",
&age);
if (age<20)
{
printf("No, you are too young to drinnk alcohol\n");
}
else
{
printf("Yes, you are old enough to drink alcohol\n");
}
}
However, better to follow the first style, as recommended by the
inventor of C
1. put { at the end of a line to save some space
2. put } else on the same line to save space
3. Statements in a statement block should be indented by one extra level.
** you can have a statement block within another statement block!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -