1http://learn.tsinghua.edu.cn
Email,qiaolin@cic.tsinghua.edu.cn
Tel,62792961
2http://learn.tsinghua.edu.cn
– C
– C
– C

– C
3http://learn.tsinghua.edu.cn
C





4http://learn.tsinghua.edu.cn




5http://learn.tsinghua.edu.cn
6http://learn.tsinghua.edu.cn
– 1~9 0~9 9 –876 55
– 0x” 0X” 0~9 A/a~F/f 0x55
0x1F 0x2a
– 0x5516 = 8510
– 0 0~7 055
– 0558 = 4510
7http://learn.tsinghua.edu.cn

– 3.1416 –2.718281828
– E / e
– 3.14e3 = 3.14?103 –2.7E–2 = –2.7?10–2
8http://learn.tsinghua.edu.cn
– A’ c’
– ASCII
– \
\n’

9http://learn.tsinghua.edu.cn
0 1 2 3 4 5 6 7 8 9
0 \000 \001 \002 \003 \004 \005 \006 \a \b \t
10 \n \v \f \r \016 \017 \020 \021 \022 \023
20 \024 \025 \026 \027 \030 \031 \032 \033 \034 \035
30 \036 \037 space ! " # $ % & '
40 ( ) * +,–,/ 0 1
50 2 3 4 5 6 7 8 9,;
60 < = >? @ A B C D E
70 F G H I J K L M N O
80 P Q R S T U V W X Y
90 Z [ \ ] ^ _ ` a b c
100 d e f g h i j k l m
110 n o p q r s t u v w
120 x y z { | } ~ \177
10http://learn.tsinghua.edu.cn
\a
\b
\f
\n
\r
\t
\v
\0 ASCII0
\\ \
\ \
\ \
\ddd ddd?ASCII
11http://learn.tsinghua.edu.cn
T s i n g h u a \0
– Tsinghua” Programming in C”
– n n+1 NULL \0
– Tsinghua” 9
\084 \115 \105 \110 \103 \104 \117 \097
12http://learn.tsinghua.edu.cn
– #define
– #define PI 3.14




#include <stdio.h>
#define PI 3.14
int main()
{
float r,area;
r = 3.0;
area = PI * r * r;
printf(?%f?,area);
return 0;
}
13http://learn.tsinghua.edu.cn
14http://learn.tsinghua.edu.cn
– char int float double void
C
– CPU 4 8
– signed unsigned short long
– void
15http://learn.tsinghua.edu.cn
char 8 –128 127
unsigned char 8 0 255
signed char/char 8 –128 127
int 32 –2,147,483,648 2,147,483,647
signed int/int 32 –2,147,483,648 2,147,483,647
unsigned int 32 0 4,294,967,295
short int/short 16 –32,768 32,767
signed short int/short 16 –32,768 32,767
unsigned short int 16 0 65,535
long int/long 32 –2,147,483,648 2,147,483,647
signed long int/long 32 –2,147,483,648 2,147,483,647
unsigned long int 32 0 4,294,967,295
16http://learn.tsinghua.edu.cn
float 32 7 –3.40e38 –1.18e–38 1.18e–38 3.40e38
double 64 15 –1.80e308 –2.23e–308 2.23e–308 1.80e308
long double 80 19 –1.19e4932 –3.36e–4932 3.36e–4932 1.19e4932
void 32
17http://learn.tsinghua.edu.cn



VANT
– value
– address
– name
– type
18http://learn.tsinghua.edu.cn
#include <stdio.h>
int main()
{
char c;
int x,y,z;
unsigned long distance;
float m;
m = 1.11;
m = 2 * m;
m = m + 2.2;
printf(?%f\n?,m);
return 0;
}
x,y,z
c
distance
m
m
m
m
m
19http://learn.tsinghua.edu.cn
char c = ‘c’;
float x,y,z = 4.53;
double a = 15.5,b = 2.0;
z4.53x,y
c c’ASCII99
a,b


20http://learn.tsinghua.edu.cn
21http://learn.tsinghua.edu.cn



– 39 46
– 15
22http://learn.tsinghua.edu.cn
15,[] () {} ++{} – –{}
14 sizeof ++{} – –{} ~ ! + – &{} *{} (){}
13 *{} / %
12 + –
11 << >>
10 < <= > >=
9 == !=
8 &{}
7 ^
6 |
5 &&
4 ||
3?,
2 = *= /= %= += –= <<= >>= &= ^= |=
1,
23http://learn.tsinghua.edu.cn



floatdoublelong double

() b = c + (int)a;
24http://learn.tsinghua.edu.cn


– while( ( c = getchar() ) != EOF ){ /* < */ }

– = b = c + (int)a;


25http://learn.tsinghua.edu.cn

– y *= x + 1 y = y * (x + 1)
+= y += x; y = y + x;
–= y –= x; y = y – x;
*= y *= x; y = y * x;
/= y /= x; y = y / x;
%= y %= x; y = y % x;
26http://learn.tsinghua.edu.cn
– + – * / %



– x = ( –b + sqrt( b * b – 4 * a * c ) ) / ( 2 * a )
27http://learn.tsinghua.edu.cn
– > == < <= >= !=


– C 0 0 1
– x == y’a’ < ‘b’
– if( grade < 60 ) printf(?Fail\n?);
28http://learn.tsinghua.edu.cn
– && || !


x && y x y
x || y x y
!x x
– x > y || ’a’ < ‘b’ && 3 <= 2

29http://learn.tsinghua.edu.cn
– & | ^ ~ <<
>>
x y x & y x | y x ^ y ~y
0 0 0 0 0 1
0 1 0 1 1 0
1 0 0 1 1 1
1 1 1 1 0 0
30http://learn.tsinghua.edu.cn
– x = x & y x 0xB675y 0x00FF
– x 16 88 0
x = 0xB675 1 0 11 0 11 0 10 1 01 1 10
y = 0x00FF 0 0 00 0 00 0 11 1 11 1 11
x = x & y 1 0 11 0 11 0 10 1 01 1 100 00 00
0
– 4 0 y = x & 0x1000 y 0 0 1
31http://learn.tsinghua.edu.cn
1
– x = x | y x 0xB675y 0x00FF
– x 16818
x = 0xB675 1 0 11 0 11 0 10 1 01 1 10
y = 0x00FF 0 0 00 0 00 0 11 1 11 1 11
x = x | y 1 0 11 0 11 0 10 1 01 1 101 1 1
32http://learn.tsinghua.edu.cn
– x = x ^ y x 0xB675y 0x00FF
– x 16818
x = 0xB675 1 0 11 0 11 0 10 1 01 1 10
y = 0x00FF 0 0 00 0 00 0 11 1 11 1 11
x = x ^ y 1 0 11 0 11 0 10 1 01 1 1001 0 10 0 01
33http://learn.tsinghua.edu.cn
– x = ~x x 0xB675
x = 0xB675 1 0 11 0 11 0 10 1 01 1 10
x = ~x 1 0 11 0 11 0 10 1 01 1 100 1 00 1 00 1 01 0 10 0 01
34http://learn.tsinghua.edu.cn
<< 2n0
– <<
– y = x << n x 0xB675n 2
x = 0xB675 1 0 11 0 11 0 10 1 01 1 10
y = x << n 1 0 11 0 11 0 10 1 01 1 101 0 1 00 1 1 0 0
>> 2n0
– >>
– y = x >> n x 0xB675n 2
x = 0xB675 1 0 11 0 11 0 10 1 01 1 10
y = x >> n 1 0 11 0 11 0 10 1 01 1 100 0 1 0 1 01 0 11
35http://learn.tsinghua.edu.cn
– 1? 2,3
– 1 2
3
– x = ( a > b )? a,b;
– if( a > b ) x = a; else x = b;


– z = ( x = m + n,y = m – n,x / y );
36http://learn.tsinghua.edu.cn

– C
– stdio.h”



37http://learn.tsinghua.edu.cn
printf()
printf()
– printf(?,);
– printf(?age = %d\n?,age);
%y
– %d %i %f %e
%c %s %u %x
%o
– %[–]m[.n]y?m” %y”
.n” –
38http://learn.tsinghua.edu.cn
#include <stdio.h>
int main()
{
int x,y,z;
x = 10;
y = 15;
z = 25;
printf(?Results as follows:\n?);
printf(?x = %d,y = %d,z = %d\n?,x,y,z);
printf(?x + y = %d\nx + y + z = %d\n?,x + y,x + y + z);
return 0;
}
Results as follows:
x = 10,y = 15,z = 25
x + y = 25
x + y + z = 50
39http://learn.tsinghua.edu.cn
scanf()
scanf()
– scanf(?,);
– scanf(?%d,%c?,&age,&c);



Tab
40http://learn.tsinghua.edu.cn
#include <stdio.h>
int main()
{
char c,s[10];
int i;
float x;
scanf(?%d,%c,%f,%s?,&i,&c,&x,&s);
printf(?i = %d,c = %c,x = %f,s = %s\n?,i,c,x,s);
return 0;
}
10,a,3.14,hello<>
41http://learn.tsinghua.edu.cn
getchar()


– char c; c = getchar();
putchar(c)

– c
– char c = ‘c’; putchar(c);
42http://learn.tsinghua.edu.cn
41
– 5 6 13 14
42
– 1 4