C
C
C
C
C
C
1.1 C
1.2 C
1960 ALGOL 60
1963 CPL
1967 Matin RichardsBCPL
1970 Ken ThompsonB
1972-1973 Dennis.M.RitchieC
Brian W.Kernighan D.M.RitchieC
1987ANSI C
1.3 C
1.4 C
CCmain),
int max(int x,int y)
mainmainmain
C
C
/*…………*/
1.5 C
01
1.6 C
521033
32
32
1.7
=+
1.7.1
2000-2500
y
S12000=>y
S2y4y“,S6
S3y4100y,S6
S4y100400y,y,S6
S5y“
S6:y+1=>y
S7:y<=2500S2y>2500,
1.7.2
1.7.3
C
C
C
2.1 C
(void)
(int)
(char)
(struct)
(union)
(float)
(double)
2.2
C
2.2.1
1,
0~9123-456
00~70123,-011
(0123)8 =1* 82+2*81+3*80=64+16+3=(83)10
0X0x0~9A~Fa~f0x2A-0x12
(-0x12)16=-(1*161+2*160)=-(16+2)=(-18)10
2.
Ll
u
2.2.2
C
0~9(.123-123.4567
e” E”
-2.1e22.1e-2
2.2.3
C a? $?
\”
2.1
\n
\t tab
\b
\r
\f
\\ \”
\?
\”
\ddd 138ASC
\xhh 1216ASC
2.1
main()
{printf(“?ab?c\t?de\rf\tg\n”);
printf(“h\ti\b\bj?k”);
}
2.1
fab?cgde
hjik
fgde
hj?k
2.2.4
C China”,“$3.232”
C
1 \0?
2.2.5
C
#define
2.2
#define PRICE 30
main()
{
int num,total;
num=10;
total=num*PRICE;
printf(“total=%d”,total);
}
2.3
3
a
2.1
i:
int i=10;
3.2(a)3.2(b)
i
10
(a)
i 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
(b)
2.2
1-10
0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1
1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0
10
1-10
.3.1
[signed] int
[signed] short [int]
[signed] long [int]
unsigned int
unsigned short [int]
unsigned long [int]
.
[signed] int -32768~32767 -215~(215-1) 2
unsigned int 0~65535 0~(216-1) 2
[signed] short [ int] -32768~32767 -215~(215-1) 2
unsigned short [int] 0~65535 0~(216-1) 2
[signed] long [int] -2147483648~2147483647
-231~(231-1)
4
unsigned long [int] 0~4294967295 0~(232-1) 4
2.1
2.
…...;
int a,b,c;
unsigned short x,y;
2.3
P342.3
2.3.2
float
47
double
816
67
2.
float s,t;
double h,k;
2.4
main()
{float a,b;
a=123456.789e5;
b=a+20;
printf(“%f,%f”,a,b);
}
12345678848.000000,12345678848.000000
b12345678920,720 1.0/3*31
2.3.3
1
char c1,c2;
1,
ASC ASC 97,b 98
97 98
c1 c2
0 1 1 0 0 0 0 1 0 1 1 0 0 0 1 0
c1 c1
2.3
ASC
2.5
main()
{char c1,c2;
c1=97;
c2=98;
printf(“%c,%c\n”,c1,c2);
printf(“%d,%d\n”,c1,c2);
}
a,b
97,98
2,
Turbo Csigned char-128~127ASC ~1270
%dASC ~2551%d
main()
{char c=130;printf(“%d”,c);}
-126unsigned char0~255signed charunsigned
char
2.6
main()
{char c1=130;
unsigned char c2=130;
printf(“c1=%d,c2=%d\n”,c1,c2);
}
c1=-126,c2=130
2.3.4
C
int a=3;
float f=3.56;
char c=?a?;
int a,b,c=5;
3
int a=3,b=3,c=3;
int a=b=c=3;
int a=3;
int a;
a=3;
2.4
1+2121+2
2.4.1
:(+)( )(*)(/)(,%)(++)( )
:(>)(<)(==)(>=)(<=)(!=)
1,
:(&&)(||)(!)
:(&)(|)(~)(^)(<<)(>>)
:(=)(+=,=,*=,/=,%=)(&=,|=,^=,<<=,>>=)
:(?:)
:(,)
(*)(&)
(sizeof)
(double)a; (int)(x+y); (float)(5%3)
:()[].
2.
C15115
x+y
3,
C
C()()
x y+zy,+”x y+z
x=y=z =”y=zx=(y=z)
1 [].
(e)
a[e]
x.y
p >x
2
-
++ --
!
~
(t)
&
sizeof
-e
++xx++
!e
~t
(t)e
p
&x
sizeof(t)
3 / % e1 e2
2.2 C
4 + e1+e2
5 << >> e1<<e2
6 < <= > >= e1<e2
7 = = != e1= =e2
8 & e1&e2
9 ^ e1^e2
10 | e1|e2
11 && e1&&e2
12 || e1||e2
13?,e1?e2:e3
14
= += =
*= /= =
>>= <<=
&= ^= |=
x=e
x+=e
x&=e
15,e1,e2
ai++ i(float)i
sizeof(int) p
2.2
( ) [ ],->
(!)
((~),(<<>>),(&^|))
2.4.2
+-*/%
5/31 5/3=1-5/3= 1,
%”-100%3-1100%-31
2.
i i ii()1
i i ii()1
++ii++i=i+1,++ii++++ii=i+1ii++ii=i+1
2.6
i3
j=++ii4jj4
3 3 4
2.7
P352.5
main()
{int c=5;
printf(“%d,%d,%d\n”,c+=c++,c+8,++c);
printf(“%d\n”,c+=c++,c+8,++c);
}
Turbo Cprintf()++c,c=6,c+814c+=c++,c=13
1.
main()
{int i=3;
printf(“%d,%d\n”,(i++)+(i++)+(i++),i);}
main()
{int i=3,k;
k=(i++)+(i++)+(i++);
printf(“%d,%d\n”,k,i);}

2.+
3.
(-++ ) /%+
4.
a b/c 1.5+?a?
( int,short,long,unsigned int,unsigned
short,unsigned long)( float,double)
double float
long
unsigned
int char,short
shortint,floatdouble(floatdouble)
intdoubleintdouble(double)doubleintunsigned intlong
doubleintdoubleintdouble
2.8
iintffloatddoubleelong
10+?a?+i f d/e
10+?a? a?97,107 +”i fifdoubledouble 107
i f107doubledouble edoubled/edouble 10+?a?+i fd/e
double
main()
{float x;
int i;
x=3.6;
i=(int)x;
printf(“x=%f,i=%d”,x,i);} x=3.600000,i=3
(int)x;xfloatintx x(float)2.9
2.4.3
a>3(>)a5 a>3” ( ) 1”a2
a>3” 0”
1,
C6
< ()
<= ()
> ()
>= ()
== ()
!= ()
(
(
<<=>>=)242
2.10
:
c>a+b c>(a+b)
a>b==c (a>b)==c
a==b<c a==(b<c)
a=b>c a=(b>c)
()
5==3” 5>=0” C1
0
2,
2.11
a=3,b=2,c=1,
a>b” 1;
(a>b)==c” (a>b1c)1;
b+c<a” 0;
d=a>b d1
f=a>b>c f 0( >”
a>b”
1 1>c”
0f)
2.12 P362.6
main()
{int a=5,b=3;
float x=3.14,y=6.53;
printf(“%d,%d\n”,a+b!=a b,x<=(y =6.1));
}
1,0
2.4.4
C&&() ||() ()
&&” ||”(a>b)&&(x>y),(a<b)||(x>y)
!”!(a>b)
1.
a&&b aba&&b
a||b aba||b
!a a!a
a b !a !b a&&b a||b
2.3
!a&&b||x>y&&c,
!&&||
!
2.
C1 0 0 0
0
a=4,!a0a0 0
a=4,b=5,a&&b1ab0 a&&b 1
aba||b
ab!a||b
4&&0||2
10(,)0( )
1
1
1
5>3&&8<4-!0
5>3”(&&)53 5>3”1 1&&8<4-!0”8
&&” <” <” 8<4-!0”4 <” -” -” <”
4-!0” !” !0”1 4-1”3 8<3”0 1&&0”0
a&&b&&c a(0)babcabc()abc
a||b||c a(0)bcababc
2.13
main()
{int w=1,x=2,y=3,z=4,a=5,b=6,c;
c=(a=w>x)&&(b=y>z);
printf(“a=%d,b=%d,c=%d\n”,a,b,c);}
a=0,b=6,c=0
main()
{int m,n,l,a,b,c,d;
m=n=a=b=c=d=0;
l=(m=a= =b)||(n=c= =d);
printf(“m=%d,n=%d,l=%d\n”,m,n,l);}
m=1,n=0,l=1
2.4.5
1.
=” a=3”3ax=a+b c
2.
=”
+= =*=/=%=
<<=>>=&=^=|=
3.
<><><>
a=5”5(a5)
a=(b=5) b=5”5 a=(b=5)”
b=5” a=b”a55
b=5” a=(b=5)” a=b=5”
b=5”(5)a
a=b=c=5 (5abc
5)
a=5+(c=6) (11a11c
6)
a=(b=4)+(c=6) (10a10
b4c 6)
a=(b=10)/(c=2) (5a5b
102)
a+=a =a aa12
a) a =a a”
a=a a*a=12 144= 132
b) a+= 132”
a=a+( 132)= 132 132= 264
I,()i i=3.56”i3
II,23floatff=232323.00000f23doubled,d=23,23
23.00000000000000,d
III,doublefloat7float
IV,12(8)88
unsigned char,80
char0(0~127)801( 128~ 1)8 1
V,intshortlongchar8char()
VI,intlong16long16int(0)long160int(1)long161
VII,unsigned intlong0
VIII,unsigned signed (:unsigned int=>int,unsigned
long=>long,unsigned short=>short,unsigned
char=>char)unsigned
IX,signedunsigned signedunsigned
main()
{unsigned int a=65535;
int b;b=a;
printf(“%d”,b);}
1
signedunsignedsignedunsignedsignedunsigned
signed
main()
{unsigned a;
int b= 1;a=b;
printf(“%u”,a);}
65535
2.4.6
1.
3C
2.
123
10()2210()33
main()
{int a=1,b=2,c=3,d=4,e;
e=a>b?a:c<d?c:d
printf(“%d\n”,e);}
a>b?a:c>d?c:da>b?a:(c>d?c:d)
2.4.7
C——3+5,6+8
12
122 3+5,6+8”14,
a=3 5,a 4
2.2a=3 515a 46060
123
int a,b,c;
printf(“%d,%d,%d”,a,b,c); a,b,c”printf3
2.4.8
1.,(&)
110
0&0=00&1=01&0=01&1=1
2.,(|)
11
0&0=00&1=11&0=11&1=1
3.,(^)
^XOR1
0^0=00^1=11^0=11^1=0
2.4.8
4.,(~)
~0110
2.15
P372.9
() ()
CC(),Cputchar()getchar()printf() scanf()
puts()gets()
C #include” stdio.h”stdiostandard
input&output h”head#include
#include<stdio.h>
printfscanf#include
3.1
Cputchargetchar
#include<stdio.h>
3.1.1(putchar)
putchar(),
putchar(ch);
chch()()
3.1
P493.7
#include<stdio.h>
main()
{char a=?Y?;b=?e?;putchar(a);putchar(b);
putchar(?s?);putchar(?\n?);putchar(?\141?);}
Yes
a
3.1.2(getchar)
getchar()
getchar();
getchar(),
3.2
P503.8
#include<stdio.h>
main()
{char c;c=getchar();putchar(c);}
3.3
P503.9
#include<stdio.h>
main()
{int c;c=getchar();
c>=?a?&&c<=?zputchar(c+?A?-?a?):putchar(c);}
3.2
Cscanfprintf
3.2.1(printf)
printf
printf(“,);
%
()(\n\t\r\101)
1.
%[][][.][]
d ()
o (0)
x (0x)
u ()
c
s
f
e
g %f%e
:-+#0
-
+ ()
# c,s,d,uo0xox;
0 0
0
.”
l”
3.4
main()
{unsigned int a=65535;
int b=-2;
printf(“a=%d,%o,%x,%u\n”,a,a,a,a);
printf(“b=%d,%o,%x,%u\n”,b,b,b,b);
}
a=-1,177777,ffff,65535
b=-2,177776,fffe,65534
3.5
main()
{printf(“%3s,%7.2s,%.4s,%-5.3s\n”,
“china”,“china”,“china”,”china”);
}
china,ch,chin,chi
3.6
main()
{float f=123.456;
printf(“%f%10f%10.2f%.2f
% 10.2f\n”,f,f,f,f,f);
}
123.456001123.456001123.46123.46123.46
3.2.2(scanf)
scanf
scanf(“,);
&”&aa
1.
%[ ][][]
d
o
x
u
c
s
f
e
scanf(“%d % d %d”,&a,&b);
1 2 31a23b
scanf(%3d%3d”,&a,&b);
123456123a,456b
l(%ld)(%lf);h(%hd)
2.scanf
scanf ab
scanf(“%d,%d”,a,b);a,b&a,&b
scanf(“%d,%d,%d”,&a,&b,&c);
,”1,2,3
scanf(“a=%d,b=%d,c=%d”,&a,&b,&c);
a=1,b=2,c=5
tab
scanf(“%c%c%c”,&a,&b,&c);
d ef d?a b,?e?c
scanf
scanf(“%7.2f”,&a);
3C
4.1 C
Cint a;
C5;”
a=3
a=3;;”
C9
if()
switch()
while
do while
for
goto()
break(switch)
continue(
return()
{}
{z=x+y;t=z/100;printf(“%f”,t);};”
4.2
4.2.1 if
if()
if3
1if()
4.1 ab
main()
{int a,b,t;
scanf(“%d,%d”,&a,&b);
if(a>b) {t=a;a=b;b=a;}
printf(“%d,%d\n”,a,b);
}
2if()
1;
else
2;
1 2
4.2if else4.1
main()
{int a,b;
scanf(“%d,%d”,&a,&b);
if(a<b)
printf(“%d,%d\n”,a,b);
else
printf(“%d,%d\n”,b,a);
}
3if(1)
1;
else if(2)
2;
else if(3)
3;
...…
else if(m)
m;
else
n;
n
1
1
2
2
3
3
m
m
4.3P624.3
#include<stdio.h>
main()
{char c;
printf(“input a character:”;c=getchar();
if(c<32)
printf(“This is a control character!\n”);
else if(c>=?0?&&c<=?9?)
printf(“This is a digit!\n”);
else if(c>=?A?&&c<=?Z?)
printf(“This is a capital letter!\n”);
else if(c>=?a?&&c<=?z?)
printf(“This is a small letter!\n”);
else
printf(“This is an other character!\n”);
}
if
ififif
if(1 )
if(2) 1;
else 2;
else
if(3) 3;
else 4;
if
if
1
1 2
2 3
3 4
ififelseifelse
elseelseif
4.4
if( )
if( ) 1;
else
if( ) 2;
else 3;
elseif(if)elseifelseif
if( )
{if( ) 1;}
else
if( ) 2;
else 3;
4.2.2 switch
if(90 A?80~89 B?70~79 C?…… )( )
ififCswitch
switch
switch( )
{case 11
case 22..
.
case nn
[defaule n+1]
}
casedefaultdefault
4.5
main()
{char grade;
printf(“input grade:”);
scanf(“%c”,&grade);
switch(grade)
{case?A?:printf(“90 100\n”);
case?B?:printf(“80 89\n”);
case?C?:printf(“70 79\n”);
case?D?:printf(“60 69\n”);
case?E?:printf(“0 59\n”);
default:printf(“error\n”); }
}
grade A?
90-100
80-89
70-79
60-69
0-59
error
caseswitchbreak
main()
{char grade;
printf(“input grade:”);
scanf(“%c”,&grade);
switch(grade)
{case?A?:printf(“90 100\n”);break;
case?B?:printf(“80 89\n”);break;
case?C?:printf(“70 79\n”);break;
case?D?:printf(“60 69\n”);break;
case?E?:printf(“0 59\n”);break;
default:printf(“error\n”); }
}
grade A?
90 100
case
main()
{char grade;
printf(“input grade:”);
scanf(“%c”,&grade);
switch(grade)
{case?A?:
case?B?:
case?C?:
case?D?:printf(“>=60\n”);break;
case?E?:printf(“<60\n”);break;
default:printf(“error\n”); }
}
grade A? B? C? D?
>=60
4.3
1 1+2 2+3 3=?
int s;
s=1 1+2 2+3 3;
Cwhiledo whileforCbreakcontinue
1 1+2 2+3 3+ +100 100=?
4.3.1 while
while();
while
while while
#include <stdio.h>
main( )
{
/*isum*/
int i,sum;
i=1;/*1*/
sum=0;/*sum 0*/
while(i<=100)/* 100*/
{
sum=sum+i;
/*1*/
i++;
}
/*100
sum */
printf(“1+2+3+… +100=%d”,sum);
}
while(i<=100)
while
i
15
#include<stdio.h>
main()
{
int i,s ;
s=0;
i=1;
while(i<6)
{s=s+i i;
i++;
}
printf(“1*1+2*2+…+5*5=%d”,s);
}
4.3.2 do while
do
while();
do while
dododowhiledo whiledo whiledo whilewhiledo
while
do while
do while
#include <stdio.h>
#define GOAL 39
main( )
{int date,count=0;
do
{printf(“Please input an integer:”);
scanf(“%d”,&data);
if(date>GOAL) printf(“%d too big,please try again!\n”,date);
else if(data<GOAL) printf (“%d too big,please try again!\n,date);
elselse printf(“You get it!\n”);
count++;
}while(data!=GOAL);
printf( You need %d times when you get it!\n,count);
}
4.3.3 for
for for(123)
for
122forfor322
2
1
3
15
#include <stdio.h>
main()
{
int i,s;
s=0;
for(i=1;i<6;i++)
s=s+i i;
printf(“1 1+2 2+… +5 5=%d”,s);
}
4.3.4 while,do while,for
forwhiledo while
/**/
for(i=0;i<10;i++)
printf(,”);
char c;
/**/
/* h?*/
do
{/**/
c=getchar();printf(“%c”,c);
}while(c!=?h
whiledo whilefor
do while while
char c;
/**/
/**/
do
{/*
*/
c=getchar();
printf(“%c”,c);
}while(c!=?h?);
char c;
c=getchar();
/**/
/* h
*/
while(c!=?h?)
{printf( %c,c);
/**/
c=getchar();
}
4.3.5
87
#include<stdio.h>
main()
{
int i;
/*1*/
for(i=0;i<7;i++);
printf(,”);
printf(“\n”);/**/
/*2*/
for(i=0;i<7;i++);
printf(,”);
printf(“\n”);/**/..
.
/*8*/
for(i=0;i<7;i++)
printf(,”);
printf(“\n”);/**/
}
whiledo whilefor
#include<stdio.h>
/*87*/
main()
{
int i,k;
/**/
for(i=0;i<8;i++)
{/* 7
*/
for(k=0;k<7;k++)
printf(,”);
printf(“\n”);/**/
}
}
k<7 k<=i
#include<stdio.h>
/**/
main()
{
int i,k;
/**/
for(i=0;i<8;i++)
{/**/
for(k=0;k<=i;k++)
printf(,”);
printf(“\n”);/**/
}
}
breakcontinue
/*break*/
#include <stdio.h>
#define PI 3.14159
/*110100*/
main()
{int r;float area;
for(i=1;r<=10;r++)
{area=PI*r*r;
if(area>100.0)
break;
printf( %f\n,area);
}
printf( r=%d\n,r);
}
/*continue*/
#include <stdio.h>
/*0*/
main()
{float f,s;
s=0;
do
{scanf(“%f”,&f);
if(f<0)
continue;
s=s+f;
}while(f!=0.0);
}
break
switchbreakswitchbreak
break
continue
continuecontinue
breakcontinue