???????
?
C
(
????????????
???) ???????????????????????????????
???????????????
??????????????????????????????? ????????????????????????????????
?????????
??????????????????
????????
???????????? **???? C ?????????????????????????????
????? ????
(Dennis Ritchie) ???????????????????? ??? ???? ?????????????????????Bell Telephone ? ???? C ?????????????????????????????????????????????????????????????????????
????? (Unix)
????????????????????????????????????????????????????????????????????????????????????????????????????????
??????
??????????????????
???????????
?(Compiler) ??????? ??????????? ???? C ?????????????????????????????????????????????????????
????????????????????????
(system programming language )?
????????
[
??????
]
???? C ??????????????????????
?????? ????
(Dennis Ritchie) ??????????????? ???? ??? ???? ?????????????????????Bell Telephone ?????????????????????????????? ???? ??????????????????????????
????? ????
? ???????????????????????????????? "???? C" ??????????????????????????????????????????????????????????
???? B
??????????????????
Ken Thompson
?
???? B
?????????????????????????????????????????????????????
???? BCPL
??????????????????????????
?????? ?????
(Martin Richards)? ??????? ???? C ????????????????????????????
???? BCPL
????????
???? B
?
???? C ????????????????????????????????????????????????????????????????????????????????
?????? (Unix)
??????????????????????????????????????
????? (Unix)
????????????????????
???????????(assembly)
???????? ???????????????????????????????????
PDP-7
????????????????????????????????????????????????????????????????????????????
PDP-11
? ???????? ???? ???? C ????????????????
?????????????(struct type)
??????????????????????????????????????????????
????????????????????????????????????????? (Unix Kenal)
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? (assembly)?
???????? ???? ???
????????? ????????? (Brian Kernighan)
???
????? ????? (Dennis Ritchie)
??????????????????
The C Programming Language
?????????? ????????????????????????? "K&R"?
???????? ????
?????????????????????????????????????????? American National Standards Institute (ANSI)
???????????????????????????????????? C ????????????? ???? ???????????????????????????????????? ANSI X3.159-1989 "Programming Language C" ??????????????????????????? ANSI C ????????? C, ? C89?
????????????? ??????? ANSI C ??????????????????????????
????????????????????????? ISO
?????????????????????????? "C89" ? "C90"?
????????????????"C89" ??????????????????????????????????????????????????? ????????????????????????????????????????????????????????? ISO (ISO/IEC 9899:1999)?????????? ???????????????????????????? C99 ?
???? C ???????????????????????
??????????????? (Procedural)
??????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????? (Linux, Mac, Window)? ???? C ???????????????????????????????
?????????? (compiler)
?????????????????????????????????????????????????????????????????????? ??????? ??????????????????????????????????????????????????????? (compiler) ??????????????????????
??????????? (Interpreter)
?
?????C ????????????????????????
- ????????????
???????????????
????? characters ??? Interger ??????????????????????????????????????????????????????????????????????????
- ?????????????????????????????????????????????????
???????????
(computer memory) ??????????????????? Pointer
- ????????????????????????? Polymorphism
- ??????????????????????????????????????????????????????? Standard Input Output (I/O)
- ????????????????????????????????????????? +=, -=, *=, ++ ?????????
?????????????????????????? C ???????????????????????? ?????????????????????????????? .h ??? ???????????? .c? ???????????? .h ????????????????????????????????????????????????????????????????????????????????(variable) ??????????? ????????????????? (struct type) ????????????????????????????? (Function) ??? ??? ??? ??????????? ???????????????????????????? .h ??? ???????????????????????????????????????????????????? (Function) ????????????????????????????????????? ? ??????????????????????????????????????????????????????????????????????????? ???????????? .c ????????????????????????????????????????????????? ???????????????????????????????????????????? .c ????????????????????????????????????????????? .h ?????????????????????????????????????????????????????????????????????????????????????? ?????????? c ????????????????????????????????????????? main ???????????????????????????????????????????????????????????????????????????????????????????
???????:
???????? Hello world
#include
<stdio.h>
int
main
(
)
{
printf
(
"Hello, World!"
);
/* ??????? Hello, World!*/
return
0
;
}
????????????????????????
??????????????????????????????
????????
???????????? stdio(Standard Input/Output) ????????????????????????????????? ???????????????????????????????????????????????????????????? ?
printf
(
"Hello, World!"
);
?????????????????????????????????????????????????? ????????(
Library
) Stdio ????????????????????? ???????(
string
) ?
?????????????
[
??????
]
(?). ???????????????????????????????? ?? ??? ????????????????????? a ??? b :
????????
#include
<stdio.h>
void
main
(
void
)
{
double
a
,
b
;
/* ??? a ??? b ??????????????????????*/
printf
(
"input the value of a and b"
);
scanf
(
"%lf %lf"
,
&
a
,
&
b
);
/*?????????? a ??? b*/
printf
(
"Addition = %lf
\n
"
,
a
+
b
);
/*????????????????????? a ??? b*/
printf
(
"subtraction = %lf
\n
"
,
a
-
b
);
/*???????????????????? a ??? b*/
printf
(
"multiplication = %lf
\n
"
,
a
*
b
);
/*????????????????????? a ??? b*/
printf
(
"Division = %lf
\n
"
,
a
/
b
);
/*????????????????????? a ??? b*/
}
(?) ????????????????????????????? ?????????????????????? ???????????????????? ????????? :
#include
<stdio.h>
#include
<conio.h>
#include
<math.h>
void
main
()
{
float
a
,
b
,
c
,
d
,
average
;
/*?????????????? a,b,c,d ??????????????????????? ???? average ????????????????*/
printf
(
"please input a:
\n
b:
\n
c:
\n
d:
\n
"
);
scanf
(
"%f%f%f%f"
,
&
a
,
&
b
,
&
c
,
&
d
);
average
=
a
+
b
+
c
+
d
/
4
;
if
(
average
>=
50
)
{
printf
(
"you are passed exam!!"
);
printf
(
"your score is %.2f:"
,
average
);
}
else
{
printf
(
"you are failed exam!!"
);
printf
(
"your score is %.2f:"
,
average
);
}
getch
();
/*?????????screen ???????back ?????*/
}
(?) ???????????????????????????????????? ????????????:
#include
<stdio.h>
#include
<conio.h>
#include
<math.h>
void
main
(){
float
a
,
b
,
c
,
delta
,
x1
,
x2
;
printf
(
"Please input value of a: "
);
scanf
(
"%f"
,
&
a
);
printf
(
"Please input value of b: "
);
scanf
(
"%f"
,
&
b
);
printf
(
"please input value of c: "
);
scanf
(
"%f"
,
&
c
);
delta
=
(
b
*
b
)
-
(
4
*
a
*
c
);
if
(
delta
>
0
)
{
x1
=-
b
-
sqrt
(
delta
)
/
2
*
a
;
//sqrt()????????????????
x2
=-
b
+
sqrt
(
delta
)
/
2
*
a
;
printf
(
"result of x1 :%f
\n
"
,
x1
);
printf
(
"result of x2 : %f
\n
"
,
x2
);
}
else
if
(
delta
==
o
){
x1
=-
b
/
2
*
a
;
printf
(
"result of x1=x2=%f"
,
x1
);
}
else
{
printf
(
"no answer"
);
}
getch
();
}
Loop ?????????????????????????????????????????????????????????????????????
?????????????? for loop? ???:
for
(
initial
;
condition
;
update
)
for (?????????????;? ????????; ????????????????)
?????????????:
for
(
i
=
1
;
i
<
5
;
i
++
)
???????????????????????????i? ???i??=1????i ?????????????????4????????????????????????????????
??????i++ ?????????????1?? i?????????? 5? ????? ??????i<5??
??????? :
#include
<stdio.h>
#include
<conio.h>
void
main
(){
clrscr
();
int
i
;
for
(
i
=
1
;
i
<
5
;
i
++
)
printf
(
"%d "
,
i
);
getch
();
}
?????????????????
?1 2 3 4
???????????????
????????????????????????y? ???y=x * 2?????????????????????8????????for loop:
????#
include
<
stdio
.
h
>
#include
<conio.h>
void
main
()
{
clrscr
();
int
x
,
y
;
for
(
x
=
0
;
x
<
5
;
x
++
)
//x++ ????????x???????????????????????
????????
printf
(
"y= %d "
,
x
*
2
);
//?????????????????(x*2)??????????????????statement(x=x*2)
getch
();
//wait for input
}
????????????????
y=0 y=2 y=4 y=6 y=8
?????????????????????????????? while loop:
#include
<stdio.h>
#include
<conio.h>
void
main
()
{
int
count
;
int
answer
;
count
=
1
;
while
(
count
<=
5
)
{
answer
=
count
*
2
;
printf
(
"%d*2 =%d;"
,
count
,
answer
);
count
=
count
+
1
;
}
getch
();
}
??????????????????
1*2=2;2*2=4;3*2=6;4*2=8;5*2=10;
????????????? switch
[
??????
]
switch ?????????????? ???????????????????????????????????????????
????????????? switch ???
switch
(
variable
)
{
case
const1
:
statement
;
break
;
case
const2
:
statement
;
break
;
...
case
constn
:
statement
;
break
;
default
:
statement
;
}
????????
???????????????????????????????????? ???????????????????????????:
#include
<stdio.h>
#include
<conio.h>
void
main
()
{
clrscr
();
float
score
;
char
grade
;
printf
(
"Input your score:"
);
scanf
(
"%f"
,
&
score
);
switch
(
score
/
10
)
{
/*(score/10) ???????????????????? */
case
10
:
case
9
:
grade
=
'A'
;
break
;
/* 90-100 ?????????????A ????????????????????????? ??????? case 10 ???*/
case
8
:
grade
=
'B'
;
break
;
/* 80-89.99 ?????????????B */
case
7
:
grade
=
'C'
;
break
;
/* 70-79.99 ?????????????C */
case
6
:
grade
=
'D'
;
break
;
/* 60-69.99 ?????????????D */
case
5
:
grade
=
'E'
;
break
;
/* 50-59.99 ?????????????E */
default
:
grade
=
'F'
;
}
printf
(
"Your score is %f
\n
"
,
score
);
printf
(
"Your grade is %c"
,
grade
);
getch
();
}