Professional Documents
Culture Documents
Pli10 D PDF
Pli10 D PDF
Tfi '
K
HPOOPIKH
E
'
&
ATPA 2000
HPOOPIKH
'
&
Y
ANAITH INTEA
T M
IANNH EOIA
...
TYPORAMA
/ 19972000
ISBN: 9605380846
K : H 10/4
Copyright 2000
& , 26222 : (0610) 314094, 314206 : (0610) 317244
. 2121/1993,
.
fi
K 1
, ,
E ............................................................................................................... 11
1.1
1.2
1.3
.................................................................................................... 13
, ,
E ............................................................................................... 13
1.1.1 ....................................................................................... 15
1.1.2
....................................................................................................................... 15
1.1.3
................................................................ 16
1.1.4
................................................... 18
..................................................................................................... 19
, ,
E ............................................................................................... 19
1.2.1 .................................................... 20
1.2.2 ................................................................................ 21
1.2.3 .............................................................................................................. 22
1.2.4 .............................................................................................................................. 23
1.2.5 .................................................. 25
...................................... 27
, , ............................................................... 27
1.3.1 ................................................................................ 27
1.3.2
...................................................................................................... 30
1.3.3 C ................................................................................................................. 31
1.3.4 C; ................................................................................................................................ 32
..............................................................................................................................................................
33
B ..................................................................................................................... 34
E P O PA M M AT I M O Y
K 2
fi
, ,
E ............................................................................................................... 37
2.1
2.2
2.3
2.4
2.5
2.6
2.7
T ; ....................... 39
H ............................................................................................... 39
T ............................................................................................... 40
................................................................................................................ 41
........................................................................................................................ 42
A ............................................................................................................................. 43
......................................................................... 44
..............................................................................................................................................................
46
B ..................................................................................................................... 47
K 3
M T
, ,
E ............................................................................................................... 49
3.1
E ,
.................................................................................................. 51
, ,
E ............................................................................................... 51
3.1.1 ; .................................................................................................. 52
3.1.2 ............................................................................................... 53
3.1.3 ; ............................................................................ 54
3.1.4 C .............................................................................. 55
3.1.5 ......................................................... 55
3.1.6 .......................................................................................... 56
3.2
O C ......................................................................... 56
, ,
E ............................................................................................... 56
3.2.1 ....................................................................................................... 57
3.2.2 .................................................................................. 60
3.2.3 ........................................................................................................... 62
3.2.4 ........................................................................... 64
..............................................................................................................................................................
66
B ..................................................................................................................... 67
EPIEXOMENA
K 4
, ,
E ............................................................................................................... 69
4.1
...............................................................................................................................
4.1.1
..........................................................................................................
4.1.2
..............................................................................
71
71
71
4.1.3 .................................... 72
4.1.4
.....................................................................................
73
4.1.5 ......................................................................................... 74
4.2
.................................................................................................................
75
4.2.1 ............................................................................................................. 76
4.2.3 ..................................................................................... 77
4.2.4 ...................................................... 77
4.2.5
..................................................................................
79
..............................................................................................................................................................
80
B ..................................................................................................................... 80
K 5
T E
, ,
E ............................................................................................................... 81
5.1
5.2
.......................................................................................................................................... 84
.....................................................
5.2.1
83
..........................................
84
5.2.2 C
........................................................................
85
5.2.3
........................................................................
86
5.2.4 ....................................................................... 89
5.3
.................................................................................................................
5.3.1
................................................................................
90
90
5.3.2 ....................................................................................................... 91
5.4
..........................................................................................................................................
5.4.1
.......................................................................................................
5.4.2
5.5
O C
91
92
............................................................................
93
....................................................................................................................
93
E P O PA M M AT I M O Y
5.5.1
5.5.2
.............................................................................................
94
...................................................................................................
95
5.5.3
.........................................................................................
97
......................................................................................................
5.5.6
.......................................................................
98
98
.............................................................................................
...........................................................................................................................................................
99
100
B .................................................................................................................. 101
K 6
Afi
, ,
E ............................................................................................................ 103
6.1
6.2
6.3
6.4
A .............................................................................. 105
A ...................................................................................................... 106
............................................................................................................................... 107
6.3.1 ........................................................................................... 107
6.3.2 ............................................................................................... 109
6.3.3 .......................................................................................... 109
6.3.4 ...................... 111
H C ......................................................................................... 112
...........................................................................................................................................................
114
B .................................................................................................................. 114
K 7
E P
, ,
E ............................................................................................................ 115
7.1
H ..................................... 117
7.1.1 ...................................................................... 117
7.1.2 ............................................. 118
7.1.3 ..................................................................................... 119
7.1.4 ........................................... 121
EPIEXOMENA
7.2
C ................................................................................ 123
E ............................................................................................ 123
7.2.1 if .................................................................................................................... 124
7.2.2 switch .................................................................................................. 126
7.2.3 while ......................................................................... 129
7.2.4 do while ................................................................. 132
7.2.5 for ............................................................................... 133
7.2.6 ........................................................................................................ 134
7.2.7 .......................................................................................................... 135
7.2.8 break, continue goto ....................................................... 136
...........................................................................................................................................................
137
B .................................................................................................................. 137
K 8
, ,
E ............................................................................................................ 139
8.1
8.2
8.3
E .............................................. 141
8.1.1 .............................................................................................. 141
8.1.2 ......................................................................................... 143
8.1.3 ............................................................................... 145
M ............................................................................................. 149
A .................................................................................................................... 152
...........................................................................................................................................................
154
B .................................................................................................................. 156
K 9
Afi
, ,
E ............................................................................................................ 157
9.1
9.2
9.3
9.4
9.5
9.6
9.7
H ............................................................................................................. 159
........................................................................................................................ 159
......................................................................................................... 160
................................................................................................ 161
.............................................................................................. 162
......................................................................................................................... 162
................................................................................. 162
E P O PA M M AT I M O Y
10
9.8
9.9
........................................................................................................................ 163
.................................................................................................................... 164
...........................................................................................................................................................
165
B .................................................................................................................. 166
K 10
H Pascal
, ,
E ............................................................................................................ 167
10.1
10.2
10.3
10.4
10.5
............................................................................................................................................. 169
................................................................................................................... 169
Pascal .................................................................................... 170
.............................................................................. 172
.................................... 174
...........................................................................................................................................................
176
B .................................................................................................................. 176
A ....................................................................... 177
A .................................................................................................... 198
B ............................................................................................................................................ 219
...................................................................................................................................... 223
E
fi
, , .
:
,
,
,
.
. , , , . , -
12
KEAAIO 1: EIAH
,
.
.
1.1
fi
.
:
3 ,
,
,
,
6 ,
,
4 .
, . ,
, ,
. , , , ,
. , , ,
. .
.
.
13
14
KEAAIO 1: EIAH
. , ,
, , . , .
, .
, ,
, , , ,
(). ,
. , ,
, , .
,
, .
,
, [1]
(Software Engineering).
[1] X , . .
1.1.1
. ,
4.1 T
, ,
(Software life cycle models), .
,
(waterfall model) [Boehm 76] (spiral model)
[Boehm 88], , ,
. , :
(Analysis), .
(Design), .
(Implementation),
.
, . , (Structured Analysis/SA)
.
1.1.2
,
codeandfix . :
.
, , -
15
KEAAIO 1: EIAH
16
, . , () , .
. ,
, . , .
1959 , (stagewise),
(waterfall model). ,
,
. ,
.
1.1.3
1.1. ,
(conceptual model)
(implementation
model). ,
,
(machine language)[1].
: .
, ,
,
[2].
[1] M .
[2] H
.
1. I
17
2. M
6U
Get_Input
Error_Message
Input
Kind_Of_Input
Error_Message
Number
Activate_Display_Result
3U
Process_Inputs
Number
Number
Number
2U
Store_Number
4U
Display_Result
Output
7U
Get_Stored_Number
Error_Message
Number
Number
Error_Message
Numbers_To_Be_Processed
type
exe
lator.
Calcu
num
num
num
atof()
push()
1. M
pop()
3. M
1.1.
[1]
KEAAIO 1: EIAH
18
1.1.4 fi fi
.
5
.
.
. :
1. H .
2. H
.
3. H ,
.
4. H .
5. H .
1, 2 3, 4 5 ,
. , ,
.
,
. ,
, (, , ..).
, .
,
, , . , .
19
, , .
,
(notation) .
.
.
1.2
fi
,
.
:
,
,
,
.
,
[1]
.
.
, , 1.2, ,
. , , .
[1] .
fi
1.1
KEAAIO 1: EIAH
20
text Editor
Compiler
Linker
Debuger
MTA
Send autoU
responce
get message
handleU
deliveryU
options
update alt.U
recipient
update autoU
responce text
Prepear autoU
responce
Put autoU
responce
PrepearU
deliveredU
message
Put messageU
in mailbox
design model
1.2
implementation model
1.2.1
21
[1]. ,
. , ,
(linking) .
A
E U
example.c
MU
example.obj
BU
object
example.exe
E
1.2.2
,
[2] (text editor),
. C
[1] T CASE
.
[2] text editor word processor.
.
1.3
KEAAIO 1: EIAH
22
[1] .c. ,
[2] . example.c.
/* A Simple C Program */
#include <stdio.h>
main( )
{
printf("hello, world");I
}
1.2.3
, , .
. ,
. , ..,
, ..,
online . ,
. , (command line) :
< > [< >] < >
, WATCOM [3] MSDOS,
wcc example
[1] ,
, .
[2] T Dennis Ritchie
[Kernighan 74].
[3] Watcom
C/C++.
, example.c,
, :
WATCOM C Optimizing Compiler Version 8.5e
Copyright by WATCOM Systems Inc. 1984, 1991. All rights reserved.
WATCOM is a trademark of WATCOM Systems Inc.
example.c: 4 lines, 0 warnings, 0 errors
Code size: 17
example.obj.
, ,
.
(syntax errors). ,
.
example.c(4): Error! E1077: Missing '}'
example.c: 4 lines, 0 warnings, 1 errors
, ,
, text
editor . ,
.
1.2.4
H (Linker).
.
,
[1].
, ,
.
.exe ( Dos Windows).
[1] E .
23
KEAAIO 1: EIAH
24
(object code), ,
.
, ( . ).
WATCOM ,
wcl example
, :
WATCOM Linker Version 7.0
Copyright by WATCOM Systems Inc. 1985, 1991. All rights reserved.
WATCOM is a trademark of WATCOM Systems Inc.
loading object files
searching libraries
creating a DOS executable
example.exe, , , , .
example
DOS
example.exe. :
hello, world
, , .
, Basic, Pascal, C, . ,
.
25
(reuse) , .
(, )
,
.
. C
(standard C library),
. printf, ( ). C,
(header files),
. , printf
#include <stdio.h>.
1.2.5
,
,
, . , ,
,
, . ,
,
. ,
. A
.
KEAAIO 1: EIAH
26
fi
1.2
.
1.
2.
3.
4.
5.
6.
7.
8.
fi
1.1
.
#include <stdio.h>
main()
{
int num1 = 10;
int num2 = 20;
int sum;
sum = num1 + num2;
printf( %d %d : %d, num1, num2, sum);
}
, .
.
fi
1.2
C .
.
. ,
math.h. . .
1.3
fi
(programming paradigms)[1] , , C,
.
, :
4 ,
,
4
,
Pascal Prolog,
6 C .
1.3.1
(programming paradigm/programming
style) , , , , . ,
. ( 1.1), , .
,
[Ambler 92]:
[1] .
27
KEAAIO 1: EIAH
28
1.1
FORTRAN
Lisp
Simula
Prolog
Operational,
. (imperative), (objectoriented)
(functional) .
Demonstrational, programming by example[1],
. operational . PT [Hsia 88] Metamouse [Maulsby 89].
Definitional,
, ,
. , (logic programming),
(constraint programming),
(Constraint Logic
Programming) [2].
(function) . , . E, , [1] Programming by Example Home Page
http://lcs.www.media.mit.edu/people/lieber/PBE/.
[2] Haskell.
. , . ,
, . , C++
. ML , CLOS Lisp
, Prolog++
Prolog , ...
Pascal, Ada, Algol, Fortran C .
, .
, (actions), , [Sethi 97]. , Prolog definitional
declarative . , ,
.
[1]. , (declarative) ,
(.., , ,
..) .
, ,
( )
[2],
[3] (). [1] A inference Engine
Prolog.
[2] B (codesign) .
[3]
.
29
30
KEAAIO 1: EIAH
(objects) (classes), . ,
, , .
, 6.4, Smalltalk, C++, Object Pascal
Java.
1.3.2
,
1957 FORTRAN (FORmula TRANslation),
. . H FORTRAN, ALGOL,
60 ALGOL ,
ALGOLlike. , , , Pascal C, . 70 ,
Pascal Nicklaus Wirth,
C (system programming)
Dennis Ritchie. , (general purpose)
Modula, Concurrent
Pascal Ada ( Pascal) C++ Java ( C).
COBOL (Common Business Oriented Language) , BASIC (Beginners
Allpurpose Symbolic Instruction Code) , , . 1.4,
,
.
31
BASIC
Lisp
BCPL
Pascal
Simula
Prolog
Mesa
Modula
Smalltalk
Ada
CommonU
Lisp
Eiffel
ML
Self
UCSDU
Pascal
ObjectU
Pascal
CLOS
StandardM
ML
C++
C
ObjectiveC
Ada 95
Visual C++
Java
Delphi
(1 2 ), , ,
.
1.3.3 C
1.4.
fi
1.3
KEAAIO 1: EIAH
32
C ( B).
C , 1973 UNIX PDP11. ,
, system programming
UNIX.
1977, The C Programming Language Brian
Kernighan Dennis Ritchie. H
C white book
K&R [Kernighan 78].
,
system programming , ,
90
. , ,
, , ,
Fortran, Basic, Pascal. ,
K&R .
ANSI C [ANSI 88] [Kernighan 88]. [Ritchie 93].
1.3.4 C;
C , :
E .
Y topdown modular
(structured) .
E (efficient)
.
E (portable), (flexible) (powerful).
,
.
A C++ .
Y .
H C
overhead . , ,
(Application Software).
H C Java.
, . ,
, ,
, .
. , Fortran, Lisp, Simula
Prolog, , . C++ C
Simula.
Fortran. C Pascal
. , C, ,
,
(C++) (Java)
.
33
KEAAIO 1: EIAH
34
[ANSI 88]
ISO/IEC 9899
http://wwwold.dkuug.
dk/JTC1/SC22/WG14/.
[Bergin 96]
Bergin Thomas, Gibson Richard, History of Programming Languages,
Addison Wesley, 1996.
.
ACM SIGPLAN History of
Programming Languages.
,
: Frederick Brooks, Alain
Colmerauer, Richard Gabriel, Ralph Griswold, Per Brinch, Hansen, Alan
Kay, C. H. Lindsey, Barbara Liskov, Richard Nance, Elizabeth Rather,
Dennis Ritchie, Jean Sammet, Guy Steele, Bjarne Stroustrup, William
Whitaker, Niklaus Wirth. C, C++, Smalltalk,Pascal, Ada, Prolog, Lisp, ALGOL
68, FORMAC, CLU, Icon, Forth Concurrent Pascal.
[Boehm 76]
Boehm W. Barry, Software Engineering IEEE Trans. Comput., vol.
C25, pp 12261241, Dec. 1976.
[Boehm 88]
Boehm W. Barry, A Spiral Model of Software Development and
Enhancement IEEE Computer, vol. 21, no 5, p.6172, May 1988.
[Horowitz 84]
Horowitz Ellis, ,
, 1993.
Fundamentals of Programming Languages Horowitz Ellis, Computer
Science Press, 1984
B I B I O PA I A
[Horowitz 95]
Horowitz Ellis, Fundamentals of Programming Languages, Third
edition, Computer Science Press, 1995.
[Hsia 88]
Hsia, Y. T., Ambler A., Programming Through Pictorial
transformations, Proc. Intl Conf. Computer Languages 88, IEEE CS
Press, Los Alamitos, Calif., Order No. 1988, pp. 1016.
[Kernighan 88]
The C Programming Language 1988 ANSI C.
ISO/IEC 9899,
. 1990.
[Maulsby 89]
Maulsby D. L., Witten H., Inducing Programs in a Direct
Manipulation Environment, Proc. CHI89, ACM Press, New York,
1989, pp. 5762.
[Ritchie 93]
Ritchie M. Dennis, The Development of the C Language, ACM
SIGPLAN Notices, March 93, p. 207.
C.
[Sethi 97]
Sethi Ravi, Programming Languages: Concepts and Constructs 2nd
Edition, Addison Wesley, 1996. Reprinted with corrections, April, 1997.
[Wexelblat 81]
Wexelblat L. Richard, History of Programming Languages, Los
Angeles, 1981.
ACM SIGPLAN History
of Programming Languages Los Angeles on June 13, 1978.
60 (1967) 1977, -
35
36
KEAAIO 1: EIAH
.
: ALGOL, APL, APT, BASIC, COBOL,
FORTRAN, GPSS, JOSS, JOVIAL, LISP, PL/I, SIMULA, AND
SNOBOL.
fi
fi
,
.
, C.
:
2 ,
3 ,
3 ,
4 ANSI C,
C ,
,
4 C,
3 .
(keyword)
BNF
ASCII
(syntax error)
Unicode
(semantic
error)
(reserved word)
case sensitive
K E A A I O 2 : Y N TA K T I K O A
38
,
, ,
. , ,
,
.
,
. .
. , . .
, , ,
C.
T ; / H
2.1 T ;
(.., , , ..)
,
.
, .
C, ANSI [ANSI 88],
96 .
, (tab), , (form feed) (newline),
91 :
a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
0 1 2 3 4 5 6 7 8 9
_ { } [ ] # ( ) < > % : ; . ? * + / ^ & | ~ ! = , \ " '
, C
. ,
ASCII (American Standard Code for Information Interchange) [1].
IBM , EBCDIC (Extended Binary
Coded Decimal Interchange Code), , ,
unicode
[Unicode].
2.2 H
. , , . H
.
[1] A C
ASCII .
39
K E A A I O 2 : Y N TA K T I K O A
40
. ,
[Horowitz 84] [Horowitz 95].
,
. .
fi
BackusNaurForm (BNF)
BNF 1963
ALGOL60. (.. EBNF) . . Augumented BNF ( BNF) RFC 2234
http://www.nexor.com/public/rfc/index/
cgibin/search/form?2234
(syntax graph)
BNF,
Wirth [Wirth76].
BNF .
(contextfree grammars)
Chomsky . [ http://ftp.cs.rochester.edu/u/leblanc/csc173/grammars/]
2.3 T fi
,
, .
,
:
(reserved words)
(keywords)
(operators)
(identifiers)
, ,
T /
41
. . , ,
,
. *,
, .
+, , *, /, <.
2.4
(reserved words)
. C 6 :
1.
2. (runtime function names)
printf, isdigit, abs .
.
.
3. Macro names
, , , EOF,
INT_MAX.
4. (type names)
, , , time_t, va_list.
fi
2.1
fi
2.2
K E A A I O 2 : Y N TA K T I K O A
42
5. (preprocessor)
C .., include, define.
6. _
.. _DATE_ ,
_FILE_.
C
, [Darnell 1991]
http://www.concentric.net/~Brownsta/cpredef.
htm#ReservedIdentifiers, Identifiers NOT To Use in C
Programs The Reserved Identifiers.
2.5
(keywords)
.
int C, , , if else if else .
, ,
, ,
. , ,
,
.
if, else, for, case, while, do
.
C, , ,
.
ANSI C
auto
double
int
struct
break
else
long
switch
case
enum
register
typedef
char
extern
return
union
const
float
short
unsigned
continue
for
signed
void
default
goto
sizeof
volatile
do
if
static
while
2.6 A
(identifiers)
() .
, ,
. (uniquely
identifies) , , (identifier).
C ,
. :
1. ,
, .
2.
3. C
, case sensitive.
4. C . , , ANSI C
31 . 8 . ,
.
. , , .
.
,
.
. . .
-
43
K E A A I O 2 : Y N TA K T I K O A
44
.
.
fi
A . i, j, l, m. ,
, .
X . :
) velocity
max_velocity maxVelocity
) display_error
displayError.
,
. .
X . ,
.
2.7
,
, ,
. ,
, . ,
.
,
, . , , (semantic
errors), . -
45
. . ,
.
1
i = 120;
velocity = 120;
if(i<j)
func1();
fi
2.1
decrease_velocity();
else
else
func2();
increase_velosity();
C
. .
.
J
$amount
find_max#of_lements
5j
set_password
get_words
lname
int
isdigit
_Fname
MaXvElOcItY
get@name
fi
2.2
K E A A I O 2 : Y N TA K T I K O A
46
, . , ,
. ,
,
. , ,
. , .
, ,
, , .
B I B I O PA I A
[AhoUllman72]
Aho J. ., Ullman The theory of Parsing, Translation and Compiling
PrenticeHall 1972.
[ANSI 88]
ISO/IEC 9899
http://wwwold.dkuug.dk/JTC1/SC22/WG14/.
[Darnell 1991]
Darnell P., Margolis P. C: A Software Engineering Approach,
SpringerVerlag, New York 1991.
[Horowitz 84]
,1993.
Fundamentals of Programming Languages Ellis Horowitz,
Computer Science Press, 1984. ,
, ,
.
.
.
[Horowitz 95]
Horowitz Ellis, Fundamentals of Programming Languages, Third
edition, Computer Science Press, 1995.
[Rojiani 96]
Rojiani K. B., Programming in C with numerical methods for
Engineers, PrenticeHall, 1996.
[Unicode]
To unicode ISO 10646 ,
16 bit ( 8 bit -
47
K E A A I O 2 : Y N TA K T I K O A
48
),
. unicode http://www.unicode.org/.
[Wirth 76]
Wirth ., lgorithms + Data Structures = Programs, PrenticeHall,
1976.
M T
fi
,
. ,
, C
.
:
,
4
,
,
.
K E A A I O 3 : M E TA B H T E TA E P E T Y O I E O M E N N
50
.
.
,
. .
. , ( ) . ,
C .
fi
3.1
; (15 ) .
E ,
3.1 E ,
fi
.
:
,
,
,
,
,
3 ,
,
,
2 .
, . ,
, , .,
. , , ,
-
51
K E A A I O 3 : M E TA B H T E TA E P E T Y O I E O M E N N
52
.
(, , ), . ,
, .
, ,
.
, , .
3.1.1 ;
. , .
. ,
,
. ,
, count
12 C:
count = count + 1;
E ,
Horowitz, , ,
[Horowitz 95].
,
.
.
() . ,
. width, height area
. ,
, *,
area = width * height;
width height
area.
, .
3.1.2
, . ( Horowitz),
, , . ,
,
, ..
C .
. .
53
54
K E A A I O 3 : M E TA B H T E TA E P E T Y O I E O M E N N
. ,
:
,
(compiletime),
,
(runtime).
,
, .
.
3.1.3 ;
, ,
. , ,
.
, ,
.
C ,
char, int float . (struct student),
. .
(scalar) (aggregate). ,
, , ,
(boolean). ,
.
(record) Pascal, (struct) C.
E ,
3.1.4 C
C :
< > < >;
/* fi count */
int num;
/* fi num */
int count, num;
/* fi */
*/count num */
,
.
num 20.
int num = 20;
3.1.5
,
. , const. :
const float pi = 3.14; /* pi fi, */
/* */
,
, , pi,
. pi
, .
55
K E A A I O 3 : M E TA B H T E TA E P E T Y O I E O M E N N
56
3.1.6
.
, , l = 2r, 3.14. C :
perimetros = 2 * 3.14 * radius;
perimetros radius
2 3.14 . , ,
,
.
fi
3.1
A
.
( C)
( C)
3.2 O C
fi
C, . ,
,
.
, :
4 C
,
,
O C
,
C
,
.
H C . , (int) (float double), (char), (pointers)
(enum) .
, (struct) (union). ,
C char, int,
float, double. .
,
C. ,
,
. , ,
.
,
.
C. , . ,
, .
3.2.1
, char,
.
char . : 'C', '2', '*', ')'.
57
K E A A I O 3 : M E TA B H T E TA E P E T Y O I E O M E N N
58
. , choice :
char choice = A;
printf
%c.
printf(o %c\n, choice);
o ASCII 65
printf scanf
(standard input)
.
scanf(%c,&ch);
O byte[1] ,
(word).
[1] A , ANSI , 8bits.
O C
59
, (newline) (tab).
( ), C (escape sequences) '\n' '\t', .
.
printf(Write, \a \\ is a backslash. \\n);
Write, a \ is a backslash.
65
%c
1) AU
Z U
Z ASCII 65
3) U
Z U
Z
%d
4) U
Z U
Z
2) ASCII
1
3.1
ASCII .
[1] K SCII
.
K E A A I O 3 : M E TA B H T E TA E P E T Y O I E O M E N N
60
3.2.2 fi
C.
, ,
, ASCII .
, , . ,
. ,
. .
1. Z .
2. .
3. T ASCII .
4. B .
5. T .
.
C , ,
.
C.
1;
(
), .
,
.
printf
. :
printf( :\t);
\t
.
2 .
, (, ;)
char ch;
O C
, scanf
.
scanf(%c,&ch);
/* scanf */
*/ ch. */
3 :
printf(o ASCII %c %d\n, ch, ch);
4 C . , :
next_ch = ch + 1;
, next _ch .
5 . ,
:
/* fi */
fi ASCII */
#include <stdio.h> /* */
/* stdio.h */
main()
{
char ch, next_ch;
printf( :\t);
scanf(%c,&ch);
printf(o ASCII %c %d\n, ch, ch);
next_ch = ch + 1;
/* fi */
, /* */
.
. main, ,
. , ,
{ } .
61
K E A A I O 3 : M E TA B H T E TA E P E T Y O I E O M E N N
62
fi
3.2
. ,
printf(o ASCII %c
%d\n, ch+1, ch+1); 4 5,
4'. T fi fi .
fi
3.3
fi
3.4
. .
.
66 90. ,
ASCII ,
ASCII .
.
3.2.3
, int,
.
,
, bits
. , 16 bit,
int 32767 +32768
0 65535 . long
int . , unsigned
int .
unsigned int count; /* count fi . */
O C
63
, printf . %d, %x %o , , .
printf(dec=%d;, octal=%o;,hex=%x\n, num, num, num);
num ,
. l,h u
long, short unsigned, . .
: .
%d.
scanf(%d,&num);
num.
,
, ,
. .
fi
3.5
K E A A I O 3 : M E TA B H T E TA E P E T Y O I E O M E N N
64
fi
3.6
limits.h
: short, long, signed,
unsigned. , , ,
. ;
.
3.2.4
C , 2 [1]. float
double .
float plank = 6.63e34;
plank
6.63e34.
long double
, ..,
long double plank;
float, , 6
7 , double 14
15 .
float.h
.
, printf %f, %e %g. O %f
fixed point , %e %g , . , float val = 32000.0;
printf(%f %e %g\n, val, val, val);
[1] , .
O C
65
32000.000000 3.200000e+004 32000
printf,
.
, :
0.12
45.68
, . double,
, .
ANSI C
,
(portability). , float,
32 bits 8
24 , double, 64 bits, 32 bits
.
%c
%f
%s
%o
%x
%g
%d
%e
fi
3.2
K E A A I O 3 : M E TA B H T E TA E P E T Y O I E O M E N N
66
fi
3.7
,
,
. Pascal
. 2 ( 93). , 2.3 C ( , ,
1990) C . .
, .
.
, , . ,
, .
, C, , ,
, ,
.
B I B I O PA I A
[Horowitz 95]
Horowitz Ellis, Fundamentals of Programming Languages, Third edition,
Computer Science Press, 1995.
B
[Darnell, 1991]
Darnell P., Margolis P. C: A Software Engineering Approach,
SpringerVerlag, New ork, 1991.
[Horowitz 84]
,1993.
[Kernighan 88]
The C Programming Language 1988 ANSI C. ,
,1990.
[Rojiani 96]
Rojiani K. B., Programming in C with numerical methods for
Engineers, PrenticeHall, 1996.
[Waite 90]
Waite Mitchell, Prata Stephen New C Primer Plus Waite Group Inc.,
1990.
67
fi
C,
.
, .
:
,
,
,
,
,
,
& *.
&
. ,
. , ,
, C.
,
, .
.
70
C,
. , , . , , , C,
.
,
, ,
. , ,
. ,
.
71
4.1 T
O , . . C. 1 .
4.1
. , , ,
.
, , 31 float. 31
, , .
.
4.1.1
4.1.
float temp[31];
T U
H , temp 31
float, (temp), (31), (float).
4.1.2
/ -
4.1
31 float.
72
. , temp:
temp[0] ,
temp[1]
temp[30] (! )
.
temp[0] temp[1]
temp[2] temp[3]
temp[30]
. O
.
float ar[5] = {1 , 2, 3.5, 4, 5};
ar ,
. . , ISBN
[1] 0, , (global).
73
.
. , , .
, Pascal,
. , C
.
C (null)
. ASCII 0 \\0.
, ISBN
:
char book_title[30], isbn[12];
4.1.4
, .
C 1.2.2, ( C).
,
. , Hello 6 bytes :
H
\0
. 1 byte ,
, null.
. ,
char isbn str[] = {0,,3,8,7,,
9,7,6,,1,\0};
74
,
. , \0.
Afi
4.1
Afi
4.2
fi
4.1
fi
4.2
'Hello'
isbn . C ,
, printf.
isbn.
. ,
.
,
.
.
.
1/.4
. strlen
. .
4.1.5
, . :
int count[20]; /*
count 20 */
12 , */
/* */
T / O
int *ar[10];
75
/* ar 10 , */
/* [1] */
, .
.
int array[4][12];
array 4 ,
12 int.
C .
10 4
0 10.
: , , .
.
5 . , 1 .
4.2 O
,
.
. ,
,
. ,
,
,
.
, ,
, [1] [ ] *.
Afi
4.3
Afi
4.4
76
.
.
4.2.1
,
. C :
< > *< >;
4.2 ,
, . num_ptr
. *
int *num_ptr;
:
) num_ptr int
) num_ptr int.
_ptr
. .
.
num ptrU
U
U
4.2
int
num ptr;
4.2.2
,
.
77
. , :
< > *< > = <>;
<> :
) [1]
int *num_ptr = 1000;
/* num_ptr */
/* 1000 */
) &,
/* num_ptr num */
4.2.3
,
,
< > = <>;
, num_ptr = 1000; num_ptr = #
.
, , .
4.2.4
* ;. . , . , , ,
.
.
[1] H byte.
78
Afi
4.5
&
year_ptr
&year
*year
1821
*year_ptr
year_ptr
year
&year_ptr
Afi
4.6
num2
pt1
pt2
Afi
4.7
. num_ptr = #
. *num_ptr = num;
79
4.2.5
, C ,
. , arr
int arr[10]; p int *p; p arr
p = &arr[0];
,
p = arr;
arr[1]
arr[2]
arr[3]
4.3.
p+1
p+2
p+3
arr[n] *(arr + n). :
*(arr + n) <> arr[n]
arr + n <> &arr[n]
80
. C,
.
0 1, n1
n . , ,
.
B
[King 96]
King K.N., C Programming: A modern approach, W.W.Norton &
Company, Inc.
T E
fi
, . .
:
4 ,
,
,
,
3
,
2 ,
,
,
2 C.
K
(infix)
(implicit conversions)
(prefix)
(explicit
conversions)
(postfix)
(applicative
order)
K E A A I O 5 : T E E T E E K PA E I P O T A E I
82
(abstract syntax tree)
(precedence)
(side effects)
(statement)
(associativity)
(command)
(100+45)*4
, (x + y) * z 10. . ,
,
.
,
.
, .
, x + y * z,
, * +.
y z ,
, x. , ,
. . , , . ,
(x + y) * z +
, , *.
,
.
.
.
,
,
.
. C. ,
C.
5.1 O
(operator) , ,
. (operands)
, .
(expressions).
num + 12 +
, num 12.
.
. , , num1 num2
num1 > num2 >,
ar , C,
ar[4] [].
,
, (unary), (binary)
(ternary). , ,
5.1.
C,
Pascal.
83
K E A A I O 5 : T E E T E E K PA E I P O T A E I
84
5.1
+*/%
&& || !
> >= == !=
bits byte
>> & | ^ ~
& [] . >
T
. , ,
.
5.2
, , , , .
, , ,
,
. ,
,
,
.
12 * 20
count
count + 1
func()
num / (count + 1)
MAX_VALUE
func() / 4
5.2.1 fi
. E,
1. , x + y,
(infix notation),
2. +x y,
(prefix notation)
3. x y +,
(postfix notation)[1].
, .
.
x + y [2] (x + y).
,
, 8.1.3.
5.2.2 C
C Darnel
[Darnel 91] :
. .
A .
.
E . .
, &, . , mum_ptr
num ,
[1] O (parenthesisfree) ,
.
[2] A Lisp, .
85
K E A A I O 5 : T E E T E E K PA E I P O T A E I
86
num_ptr
num_ptr + 1
0xFF00[1]
&num
Hello World
&num + 2
5.2.3 fi
,
(precedence) (associativity),
.
, ,
. ,
*
/ , + [2]. C , Pascal, .
, ,
. ,
(left associative),
, . , 10 8 2 (10 8)
2. +, , * / .
, C,
(=). ,
num1 = num2 = 10
, 10 num2. ,
num2
num1. , C
10.
5.2 C.
[1] A .
[2] H Smalltalk80 .
x + y * z (x + y) * z.
87
5.2.
C.
( ) [ ] > .
! ~ ++ + * & ()
sizeof
*/%
<< >>
== !=
&
&&
||
?:
Pascal,
Ada, Fortran, PL/1, ALGOL Java. . ,
,
(2 3) * 4,
2
(3 * 4), .
,
.
. ,
x + y + z * w x + y z * w , ,
(x + (y + (z * w))) z * w,
K E A A I O 5 : T E E T E E K PA E I P O T A E I
88
y x.
1 2
1 2
. (applicative order).
, , ,
(short circuit evaluation).
y
) x and y, x
) x or y x .
C AND OR, Ada, ,
.
Afi
5.1
C ;
Afi
5.2
Afi
5.3
4 + 6
true (1)
c=3+8
17
x
2
sqrt(16.0)
x
1821
false
5>3
4.0
8<1
11
6 + (c=3+8)
false (0)
(5> 1) && (6>7)
1821
89
5.2.4
,
5.1. (node)
,
(leaves).
. ,
.
, (abstract syntax
trees),
.
5.1
5.1
Prefix notation :
* y y * * 2 x z
Infix notation:
y * y 2 * x * z
Postfix notation:
y y * 2 x * z *
y * y 2 * x * z
max(a,b), max
,
. , , , ,
C.
2 + ((4 +2)/(75) 6) .
*
y
*
2
Afi
5.4
K E A A I O 5 : T E E T E E K PA E I P O T A E I
90
5.3 M
:
;
;.
2 3
,
5 .
. , ,
. , C ,
. , , ,
.
(implicit
conversions), , (explicit conversions).
5.3.1
, , ,
. , 3.0 + 1 / 2
3.5, , 3.0,
1 / 2 0.
C ,
.
(char < int < long < float < double).
, int i; float f; f = i = 3.3 3
i , , 3.0 f. ,
C ,
char int float double.
M /
91
5.1
5.3.2
, C
.
casting cast. , , , . , j =
(float)2, 2 float j. , j , float int j.
res1 = 2.56 + 3/2; res2 = 2.56 + 3.0 / 2;
. res1 = (float)(2.56 + 3/2);
B. res1 = 2.56 + (float)(3/2);
. res1 = 2.56 + (float)3/2;
5.4 fi
(command)
. (;) C.
C ; .
Afi
5.5
K E A A I O 5 : T E E T E E K PA E I P O T A E I
92
,
5.3 .
5.3
int num;
printf(Hello World);
num = 21;
5.4.1 fi
, module, , ..
. Pascal, , begin
end . C { }. C .
int num;
num = max(12,13);
int count = 1;
printf(Hello);
count = count + 1;
, .
) {
) {
int num;
num = max(12,13);
int count = 1;
, .
/ O C
5.4.2
C , [1].
#include
< >
,
.
define. define
. :
#define
PI
3.141592654
C 6 . : ,
[1] C .
.
93
K E A A I O 5 : T E E T E E K PA E I P O T A E I
94
, , , .
, .
.
5.5.1
+, , *, /
%. ,
, ,
1.
. + , , * / %
. %
. count % 3 count
3 count.
, , .
, .
. ,
. C
++ ,
1 .
( , .. ++count) ( , .. count++).
, .
, ,
, ,
. 5.2, x y
O C
95
10
20
++x;
11
20
y = x;
10
10
y = x + y;
20
y = y x++;
10
11
5.2
, , 1 byte,
, . ,
int temp[20];
int *temp_ptr = temp; /* temp_ptr */
/* temp */
x z
, ,
x y, 10 20
) z = ++x + y;
) z = x + y;
) z = x++ + y;
) z = x + y;
5.5.2
= , C
,
. , +=, =, *= /= +,,* /.
-
Afi
5.6
K E A A I O 5 : T E E T E E K PA E I P O T A E I
96
. , x *= 10;
x 10 x.
x = x * 10; x *= y
+1 x = x * (y + 1). ! x = x * y + 1.
(bitwise operators): >>= <<= &= ^= |=
(side effect operators),
(side effects).
i
i = v[i++];
i = ++i + 1;
/* */
/* */
. 5.2
5.4 [Darnel 91]
.
Afi
5.7
int count = 10;
printf(%d %d\n,count,count*count++);
5.2
C
a = b += c++ d + e / f
O C
97
5.5.3
true
1!= 1
0>1
0 == 0
1>= 1
false
1>10
100
100
:
) count = 8
) num = count + 4
Afi
5.8
) num = = 9
) count = = num 4
count num
8 12 .
Afi
5.9
K E A A I O 5 : T E E T E E K PA E I P O T A E I
98
5.5.4 &&, || !
x y y
z.
Afi
5.10
x, y, z
(x++ < y ) && (y < ++z)
) x= 3, y = 4 z= 4
) x= 5, y = 4 z= 4.
5.5.5
: & * [ ] . >
.
.
5.5.6
cast , ( ).
.
5.3.2.
5.5.7 sizeof
sizeof :
) .. sizeof(x+y)
) .. sizeof(int).
O C
99
, bytes .
,
.
printf (%d\n, sizeof(int));
bytes
int.
bytes
char, short, int, float,
double .
5.5.8 bits
bits
.
(low level programming).
.
[Darnell 91] low level programming
20 [King 96].
5.5.9 fi
(? : ) ,
,
postfix, prefix infix .
,
(mixfix notation). , x > z ? x : z
? : x > z, x z.
1 ? 2 : 3
2, 1 ,
3. 1 ,
5.3
K E A A I O 5 : T E E T E E K PA E I P O T A E I
100
.
1.
, x>z ? x : z x x > z , z.
fi
5.1
, , . . ,
. .
, , .
,
. , ,
.
.
,
.
.
,
and
or. C , Pascal,
, ,
.
,
.
B I B I O PA I A
[Corriveau 98]
Corriveau Jean Paul, A stepbystep Guide to C Programming,
Prentice Hall.
[Darnell 1991]
Darnell P., Margolis P. C: A Software Engineering Approach,
SpringerVerlag, New ork.
[Kernighan 88]
The C Programming Language 1988 ANSI C.
[King 96]
King K. N., C Programming: A modern approach, W.W.Norton &
Company, Inc.
[Rojiani 96]
Rojiani K. B., Programming in C with numerical methods for
Engineers, PrenticeHall.
101
Afi
fi
.
:
(abstraction),
,
,
(implementation)
(interface),
,
4
(module),
4
,
,
.
(formalactual arguments)
(return value)
(modular
design)
(function
(function)
(procedure)
prototype)
K E A A I O 6 : A A I P E T I K O T H T A T I I E P A I E
104
(abstraction).
, .
,
. :
) (procedural abstraction)
) (data abstraction).
,
. .
.
.
6.1 Afi
. ,
.
. , .
:
1.
2.
3.
( ).
(, ) ( ).
, ,
.
. , . , , ,
, ,
,
.
;
.
: (notation)
.
; . , -
105
K E A A I O 6 : A A I P E T I K O T H T A T I I E P A I E
106
. , , . ; (
), ( ) ,
,
.
(implementation) , (, , )
, (interface)
.
, , .
,
, printf . printf .lib C,
stdio.h
include .
6.2 Afi fi
(modular design)
, , (modules), ,
C.
,
[Rojiani 96],
module . module :
1. ,
2. ,
3. ,
4. ,
5. (interface).
A /
. C . (subroutine)
(operation)[1]. Pascal,
. ,
.
6.3
C
.
. , , , .
C
main , , .
:
1.
2.
3. .
6.3.1
,
, ,
, .
:
<> < >(<1> [< 1>],
,<N> [< N>]);
H (function prototype), [1] H (method).
107
K E A A I O 6 : A A I P E T I K O T H T A T I I E P A I E
108
ANSI C,
, ,
, , . ,
(procedure function Pascal), ( ( ) C). (formal arguments),
[1]. ,
. , ,
int max(int a, int b);
Afi
6.1
, .
Afi
6.1
xn, x n .
Afi
6.1
[1] .
109
6.3.2
.
printf scanf . H ,
(actual arguments), :
< >(1, 2, , );
, ( )
. ,
. , ,
.
draw_circle(a/2.0, 2.0*b, c/3.0);
, ,
draw_circle, x, y r.
, :
max_num = max(num1,num2);
result = num1 + max(num2,num3);
printf( fi %d\n, max(num1,num2));
6.1. 6.2 6.3.
.
6.3.3 fi
fi
6.1
K E A A I O 6 : A A I P E T I K O T H T A T I I E P A I E
110
.
area .
float area(float width, float height)
{
double result;
result = width * height;
return(result);
}
width height
,
. , ,
, , . result,
(local variable)
(global variables).
. ( )
. .
, . , return. return
, area.
return .
Afi
6.4
, . .
.
float triangle_area(float base, height)
float ginomeno;
{
ginomeno = base * height;
return(ginomeno/base)
}
6.3.4
result = max(max(15,13),17);
,
, ,
.
max, max(15,13) 17. C ,
[1].
max(15,13), ,
15 13 a b
, 6.1(),
max. To max (a > b) ? a : b
a b 15 13, . return
15 . max. max
, 6.1(), 15
17 a b, , max , ,
max .
[1] H ANSI C .
111
Afi
6.5
K E A A I O 6 : A A I P E T I K O T H T A T I I E P A I E
112
(a>b) ? a : b, a b 15 17, 17
return
.
183 [Sethi 97] Procedure
Call and Return in C.
6.4 H C
C . ,
, .
,
. , , , , ,
, .
15
result = max (max (15, 13), 17);
15
int max (int a, int b)U
17
{U
{U
return(a>b?a:b);U
return(a>b?a:b);U
6.1.
max(max(15,13),17).
, .
H C
strcpy(), strcat(), strlen(), strcmp(), strchr() . .
isalnum(), isalpha(), isdigit(), islower(), isspace(), tolower(),
toupper(). .
gets(), puts(), getchar(), putchar().
.
113
fi
6.2
fi
6.3
fi
6.4
fi fi
:
1.
( #include #define)
main
2. 23 ,
.
3.
max_velocity, max_temp, word_len, display_error(...), isdigit(...),
read_ten_chars(...).
4.
.
5. .
6. .
7.
.
K E A A I O 6 : A A I P E T I K O T H T A T I I E P A I E
114
.
,
. , .
, () ,
. , C ,
,
.
, , .
B
[Darnell 1991]
Darnell P., Margolis P. C: A Software Engineering Approach,
SpringerVerlag, New ork.
[Rojiani 96]
Rojiani K.B., Programming in C with numerical methods for
Engineers, PrenticeHall.
[Sethi 97]
Sethi Ravi, Programming Languages: Concepts and Constructs 2nd
Edition, Addison Wesley 1996. Reprinted with corrections April 97.
E P
fi
,
.
:
,
,
,
,
C,
,
.
(structured programming)
if
if else
switch case
for
while
(conditional loop)
(counting loop)
do while
break
continue
K E A A I O 7 : P O TA E I E E X O Y P O H
116
. , . ,
, .
To . ,
, . ,
, , . ,
C, .
117
7.1 H fi
7.1.1 fi
. ,
7.1. , 1; 2; 3; 1, 2 3.
.
,
.
.
,
,
, (structured programming).
.
(singleentry/singleexit). C Pascal
, .
goto,
.
, ,
(looping) (conditional
branching). ,
, , .
(efficiency) . -
7.1.
K E A A I O 7 : P O TA E I E E X O Y P O H
118
true
. ,
, .
7.1.2 fi
, , . if,
:
7.2.
if then 1 else 2
, 7.2
, .
,
, 1
2. ,
.
if ( ) , . ,
:
true
if
else if
else if
:
else if
else
E1
true
7.3.
E2
E1 then 1
E2 then 2
E3 then 3
En then n
0
.
, , 1 2 3
, 3.
0.
, flowchart 7.3,
2,
.
119
if
if E then
,
, ,
if.
if
, .
(concurrent
programming), Ada [Watt 90].
if
. , C ( Pascal Ada) , ,
[1].
switch ( 7.2.2), ,
. , case, ALGOL. Pascal
case, C switch case.
. if then else %.
.
7.1.3
fi
7.1
K E A A I O 7 : P O TA E I E E X O Y P O H
120
E
loop
true
7.4.
.
, , (loop
body).
. ,
, .
(indefinite iterations), ,
.
while Pascal
while E do
H , 7.4.
. .
, , .
C
do while E
7.5,
,
. E .
Pascal
loop
true
repeat until
loop .
7.5.
repeat while :
; while not E do
, while repeat,
,
(control
variable). , (control
sequence) . H Pascal
for <> := 1 to 2 do
,
1 , ,
,
2. [Sethi 97].
C
.
7.1.4
121
K E A A I O 7 : P O TA E I E E X O Y P O H
122
continue . , ,
.
break continue, :
while () {
if ( ) {
continue;
}
}
continue
:
while () {
if ( )
else
}
7.6.
break
E1
7.6 while
break.
true
while(E1) {
if(E2) {
true
E2
break;
}
2
}
H / C
return, , return. ,
, return
.
goto <>
<> :
C goto .
,
, . [Kernighan 88]
[Sethi
97]. ; goto
C . ; . goto.
7.2 C
H C
. . ,
.
,
. :
: ifelse, switch case
: while, do while, for
: break, continue, goto
123
K E A A I O 7 : P O TA E I E E X O Y P O H
124
7.2.1 fi if
if ,
:
if
(1)
1;
, ,
( 0 C,
) 1,
. if
, C
, , .
if (light == red)
stop();
else
go();
1 if .
, if :
if () {
fi1;
fiN;
}
To else.
if, :
) if( (num >10) && (num < 14))
) if(!found)
7.1
.
, -
125
C :
#include <stdio.h>
#include <math.h> /* */
main()
{
double num;
printf( fi fi:);
scanf(%lf,&num);
if(num<0)
printf( : fi fi\n);
else
printf(H %lf
%f\n,sqrt(num));
exit(0);
}
if else
.
.
7.2
.
. :
int max(int a, int b);
:
int max(int a, int b) int max(int a, int b) int max(int a, int b)
{
int max;
if (a>b)
return (a>b) ? a : b ;
if (a>b)
max = a;
return a;
else
else
return b;
max = b;
return(max)
}
K E A A I O 7 : P O TA E I E E X O Y P O H
126
max, return , , ,
, .
,
.
? : if else.
if else.
fi
7.2
. ( ? : ); .
7.2.2 fi switch
H switch .
switch 7.7, . ,
(1, 2, 3,).
, 1, 2, .
,
default, ,
switch.
case switch
.
break, goto return. break,
switch, switch. ,
break switch,
,
case . ,
break , ,
. ,
break.
switch , :
K case int char
.
case .
O default
case .
Case default .
default .
, case .
H break
.
7.3
, ,
.
E .
1.
2.
3.
4.
5.
6.
7.
127
switch () {
case 1 :
1
case 2 :
2
case 3 :
3
default :
}
7.7
K E A A I O 7 : P O TA E I E E X O Y P O H
128
1 C
printf scanf .
H 2 ,
. display_menu
.
:
void display_menu();
H ,
3 choice ,
, [1] 1,
2, 3 4.
4 5
switch case. H ,
choice. case
1, 2, 3 4. , switch 7.8(). 7.8()
.
[1] .
switch(choice) {
case 1: ;
case 2: ;
case 3: ;
case 4: ;
default :
;
}
129
switch(choice) {
break;
break;
break;
break;
case 1:
result =
num1 + num2;
break;
:
/* cases */
default :
printf(
break;
\n);
break;
()
()
7.8: switch.
3. . 10 0.
.
.
.
(ADD, SUB,
MUL DIV) .
, #define ,
C.
.
7.2.3 fi while
while
.
.
:
while ( )
fi
7.3
fi
7.4
K E A A I O 7 : P O TA E I E E X O Y P O H
130
, , ( ). ,
while, . , while. 7.9 while.
while (count<limit) {
while
count++;
false
printf(count is %d\n,count);
count<limit;
true
< fi fi>
count++;U
printf(count is %d\n,count);
: limit 12 count
12,
.
< >
7.9
while.
4
.
strlen
. '\0' .
while 0
C. while.
int i = 0;
int i = 0;
int i = 0;
while(str[i]!=\0)
while(str[i])
while(str[i++]);
++i;
++i;
return i;
return i;
return i;
}
131
5
.
n
n! = n(n1)(n2) (3)(2)(1) 0! = 1
factorial num,
num:
factorial = 1.0;
count = 1;
while(count < num) {
factorial *= i;
count++;
}
,
.
.
:
) while(++count<12)
1;
fi
7.5
Afi
7.1
, , .
,
,
Fibonacci. 1.
Fibonacci.
. , 254 [Rojiani
96] .
7.1
K E A A I O 7 : P O TA E I E E X O Y P O H
132
fi
7.6
(spaces) (),
. getchar() \n .
. .
7.2.4 fi do while
while ( );
7.10
do while.
7.10 do
while.
do {
do
count++;
printf(count is %d\n,count);
} while (count<limit)
printf(count is %d\n,count);U
count++;
while
< fi fi>
7.2
count<limit;
true
false
< >
7.6.
do while.
. , 98 [Darnell 1991] .
133
7.2.5 fi for
, , ,
.
for :
for (1 ; 2 ; 3 )
:
1. 1. ,
, , ' .
2. 2, . , ,
.
3. , 3.
, ' (update).
2 ( 2).
7.11 for,
for [Waite 90].
(,). (,)
.
. ,
for
. ,
for(i=0,j=10; i<8; i++,j++)
t[j] = s[i];
s t
s, .
for(ch = getchar(), j = 0; ch!=EOF; j++, putchar(ch), ch = getchar())
K E A A I O 7 : P O TA E I E E X O Y P O H
134
H , , .
[Waite 90].
for(count=1;count<max_count;count++){
for
printf(count is %d\n,count);
count=1;
< >
}
< fi fi >
false
count<U
maxcount;
count++
true
printf(count is %d\n,count);U
< >
7.11
for.
< >
7.2.6 fi
:
1. , ,
.
2. while for , ,
. ,
for , ,
.
for( ; ; )
while()
initialize;
1;
while(test) {
1;
update;
}
, . for. .
while. .
10
, , .
. 142 [King 96].
135
fi
7.7
7.3
7.2.7 fi
C ,
. .
1 ,
2 .
. 3 . )
)
.
2
,
, . .
fi
7.8
Afi
7.2
fi
7.9
K E A A I O 7 : P O TA E I E E X O Y P O H
136
7.1.3. ,
C,
7.1.3. , 279 [Rojiani 96] break,
111 [Darnell 1991]
break continue.
break.
break
while (( ch = getchar()) != \n) {
if (ch == \t)
break; /* fi while */
putchar(ch);
}
while((ch=getchar()) != \n &&
ch != \t)
putchar(ch);
fi
7.10
7.4
. , , .. . .
Y N O H B I B I O PA I A
.
, .
:
1.
, . .
, .
2. goto.
.
3. (while) (dowhile), .
4. break switch , , , continue .
.
(flag) .
5. .
6.
[Rojiani 96]
B
[Darnell '91]
Darnell P., Margolis P. C: A Software Engineering Approach,
SpringerVerlag, New ork.
[Rojiani 96]
Rojiani K.B., Programming in C with numerical methods for
Engineers, PrenticeHall.
137
K E A A I O 7 : P O TA E I E E X O Y P O H
138
[Sethi 97]
Sethi Ravi, Programming Languages: Concepts and Constructs 2nd
Edition, Addison Wesley 1996. Reprinted with corrections April 97.
[Waite 90]
Waite Mitchell, Prata Stephen New C Primer Plus Waite Group Inc.
[Watt 90]
Watt David, Programming Language Concepts and Paradigms
Prentice Hall.
fi
, ,
.
:
,
,
3 ,
static ,
,
,
,
,
,
,
,
.
(scope rules)
K E A A I O 8 : P O X P H M E N A E M AT A Y N A P T H E N
140
(call by value)
(call by
(recursion)
reference)
static
6
.
. ,
. ,
.
.
. ,
.
.
.
,
,
. ,
.
,
. .
.
8.1 E
8.1.1
,
, ,
, . C,
, ,
.
, ;
;.
(scope rules), .
. , (lexical) (dynamic).
, ,
. [1].
, , 6 [Horowitz 84] 5.3 Scope Rules for Names
[Sethi 97].
C .
.
Darnell [Darnell 91]:
1. .
(global) .
[1] H Lisp
.
141
K E A A I O 8 : P O X P H M E N A E M AT A Y N A P T H E N
142
, .
, ,
static.
,
static .
2. . ,
. static
, , .. static int velocity;
3. .
.
goto .
4. (block).
.
. , .
.
C , ,
(name conflict). ,
(hides) .
fi
8.1
8.1 , . .
, . , .
.
143
11 int c = 13;
int a;
13 return (a>b?a:b);
static int b;
14 }
main(){
a=12;
16 int b=20;
8
9
10
b = a ;
\n", a,b,c,max(b+5,a));
func(a+b);
max(x,b):%d\n", a,b,c,x,max(x,b));
19 }
}
8.1: C.
.
. ,
.
static.
.
188 [King 96] 355 [Rojiani 96]
8.1.2
(duration). H
. , . , ,
. , .
,
K E A A I O 8 : P O X P H M E N A E M AT A Y N A P T H E N
144
. , ,
,
. .
, C
, static . num
, temp .
func(int x)
{
int temp;
static int num;
:
}
. ,
, . , ,
.
7.1.2. [Darnell 91] 13 [Waite 90],
(random number function).
Afi
8.1
) static
. ) count num;
static int num;
void func(int) {
static int count = 0;
int num=100;
increment
main.
.
,
main increment,
.
145
void increment(void);
main()
{
increment();
increment();
increment();
}
void increment(void)
{
int j =2;
static int k =2;
printf("j:%d\tk:%d\n",j++,
k++");
}
8.1.3
C , . , ,
. .
, , , , ( ), .
C .
, , C.
,
static.
1
Afi
8.2
146
K E A A I O 8 : P O X P H M E N A E M AT A Y N A P T H E N
func4().
x y buf. , main
func1 func2, func1 func2, func2 func3 func4
, , main func1 y, func2, func3
func4 buf, main, func2 func3 x, ,
.
8.2
file1.c file2.c.
. 8.3
file1 file2.
file1.c : 3 . func2
file2.obj. H 4
func1 (static). func1
file1. H 6 x . x
file1,
file2, file2 6, x , ,
o (extern). 7 y . , y
file1. H 21 k
, , static
i 20
func1.
file2.c : 3 4
func3 func4 .
file2. H 7, buf ,
file2, file1.
147
file1.c
file2.c
#include <stdio.h>
#include <stdio.h>
void func2(void);
int x=1;
extern int x;
void main()
10
10
void func2(void)
11
int i=30;
11
12
printf("func2\tbuf:%s\t",buf);func3();
13
func4();
14
12
13
printf("y:%d\tfunc(i+1):%d\tfunc1(i):%d
14
\n\n",y,func1(i+1),func1(i));
15
func2();
15
16
16
17
17
18
18
printf("func3\tbuf:%s\t",buf);
19
19
20
int i=10;
20
21
21
22
23
printf("func4\tbuf:%s\tx:%d\n\n",buf,x++);
24
22
23
printf("func1\ta:%d\ti:%d\tk:%d\tx:%d\n"
24
,a,i,k,x++);
24
func2();
26
return(k++);
27
}
8.2
13 file1.
printf. func1 i (30).
1 2
8.3. , func
i+1(31).
4 5 8.3.
K E A A I O 8 : P O X P H M E N A E M AT A Y N A P T H E N
148
static 21 file1. i
10 func1, k
. ,
printf 7 8.3.
1
func1
a:30
i:10
k:1
x:1
func2
buf:klea
func3
buf:klea
func4
func1
a:31
i:10
k:2
x:3
func2
buf:klea
func3
buf:klea
func4
buf:klea x:4
y:13
func(i+1):2
func1(i):1
func2
buf:klea
func3
buf:klea
func4
buf:klea x:5
buf:klea x:2
6
7
8
9
8.3
1/.8
1,
.
,
. 10.2
[King 96] .
.
.
5. ,
(3 + 6 ) * (8 6) 3 6
+ 8 6 *
, , .
E / M
,
.
4.3 [Kernighan 88].
getop,
. . . ,
,
. ,
4.4 EPMH C (http://www.
ee.upatras.gr/prof/thrambo/C2Java.htm)
.
149
8.1
8.2 M
. ,
. .
.
void swap(int a, int b);
fi
8.2
K E A A I O 8 : P O X P H M E N A E M AT A Y N A P T H E N
150
num1
num2
12
102
swap(num1,num2);
a
a = b;
b = temp;
}
temp
, swap
swap(num1, num2);
8.4
num1 num2
12 102, .
swap num1 num2 12 102. swap. swap
. , swap printf a b,
. ;
num1
1000:
12
1002:
num2
102
1000
1002
temp
swap(&num1,&num2);
8.5
, . a
b
. , ,
8.4,
num1 num2
.
a
b , num1 num2. To
swap. ;
,
.
swap
num1 num2 &num1 &num2, &.
:
swap(&num1,&num2);
. swap
void swap(int *a, int *b);
, :
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
8.3 swap,
num1 num2 1000 1002.
swap
a b. num1 num2, 1000 1002, 8.5. , swap num1 num2
. temp = *a;
swap temp 1000 ( 1). *a = *b; num2 num1
, , *b = temp; temp num2 .
.
,
.
, 5.2 [Sethi 97]
callbyvalue,
callbyreference callbyvalueresult
. callbyname. ,
151
K E A A I O 8 : P O X P H M E N A E M AT A Y N A P T H E N
152
Afi
8.3
swap. .
, ,
.
8.3 Afi
(recursion) .
,
.
. , ,
, .
fi
8.4
1 n.
.
for(i=0;i=<n;i++)
total +=n;
return total;
.
n
n1 n. O n1 ,
n1. ,
return (sum(n1) + n);
; sum , .
sum ,
.
n
.
n1.
sum n .
.
.
. sum
n 1,
sum. ,
sum, ,
, :
int sun(int n)
{
if(n =< 1)
return n;
else
return (sum(n1) + n);
}
153
K E A A I O 8 : P O X P H M E N A E M AT A Y N A P T H E N
154
stack overflow ,
.
2
printf
putchar ,
.
printd n .
. 1821. 182 1821/10 , , 1
1821%10. 1 ,
putchar(1) , , putchar(1821%10) , , putchar(n%10) n printd.
182 18 (182/10) , , 2 (182%10).
printd(n/10);
.
/* printing a number as a character string */
#include <stdio.h>
void printd(int n)
/* print n in decimal */
{
int i;
if(i<0) {
putchar();
n = n;
}
if((i = n/10) != 0)
printd(i);
putchar(n%10 + 0);
}
A / Y N O H
sum 5
. .
xn. x n , , xn .
.
n Fibonacci.
n, Fibonacci .
.
. , , .
.
,
.
, ,
,
.
155
Afi
8.4
fi
8.5
fi
8.6
K E A A I O 8 : P O X P H M E N A E M AT A Y N A P T H E N
156
[Darnell 1991]
Darnell P., Margolis P. C: A Software Engineering Approach,
SpringerVerlag, New ork.
[Horowitz 84]
1993.
[Kernighan 88]
The C Programming Language 1988. ANSI C.
1990.
[King 96]
King K.N., C Programming: A modern approach, W.W.Norton &
Company, Inc.
[Rojiani 96]
Rojiani K.B., Programming in C with numerical methods for
Engineers, PrenticeHall.
[Sethi 97]
Sethi Ravi, Programming Languages: Concepts and Constructs 2nd
Edition, Addison Wesley 1996. Reprinted with corrections April 97.
[Waite 90]
Waite Mitchell, Prata Stephen New C Primer Plus Waite Group Inc.
Afi
fi
.
:
,
,
,
,
,
,
.
struct
, (abstraction),
: )
) .
, .
158
K E A A I O 9 : A A I P E T I K O T H TA TA E O M E N A
, , .
.
(types)
Pascal modes ALGOL68. , SIMULA67
. ,
,
.
C
, , 6.4. C struct, record
Pascal,
. , ,
.
9.1
.
, ,
. , .. ,
. ,
,
.
(structure) (record)
Pascal, , ,
,
. struct ,
.
, ,
,
, (field),
. int,
char, float double,
.
.
9.2 fi
:
[< >] {
< 1 > < 1 >;
< 2 > < 2 >;
< 3 > < 3 >;
struct
159
K E A A I O 9 : A A I P E T I K O T H TA TA E O M E N A
160
/* */
/* */
int line_color;
int fill_color;
};
9.3
C
:
struct
struct
< > ;
, :
struct [< >] {
< 1 > < 1 >;
< 2 > < 2 >;
< 3 > < 3 >;
161
struct rectangle {
int x1, y1; /* */
int x2, y2; /* */
int linecolor;
int fillcolor;
} rect1, rect2;
,
struct rectangle rect1, rect2;
rectangle.
.
9.4 fi
.
, :
struct rectangle {
int x1, y1; /* */
int x2, y2; /* */
int line_color;
int fill_color;
}
, , :
struct rectangle rect1= {10,10, 40,40,RED,GREEN},
rect2 = {5,5,50,50,RED,BLUE};
(2.0,3.0) 1.0
(4.0,6.0) 2.0, .
Afi
9.1
Afi
9.2
K E A A I O 9 : A A I P E T I K O T H TA TA E O M E N A
162
9.5
.
(.) :
< >.< >
,
rect1.x1
x1 rect1,
rect2.line_color
line_color rect2.
9.6
.
C . person, , addr address
birthday day.
struct person {
/* */
B, address day.
H city emp
person
emp.addr.city
9.7
ANSI C . , , ,
.
/ /
. , ,
.
.
9.8
H :
struct address addr[10];
. , addr :
struct address addr[10] =
{
{Kleanthis, Telou Agra, 10, 24662,
Patras },
{Nikos,Marathona,12,12345,
Athens },
{kostas,Korinthou,340, 24561,
Aigio }
};
addr .
, . H
addr[0].name
,
addr[1].name
.
addr[1].name[0]
addr.
163
Afi
9.3
K E A A I O 9 : A A I P E T I K O T H TA TA E O M E N A
164
9.9
, addr1
addr_ptr addr1, .
addr_ptr>name
name addr_ptr,
addr_ptr>zip
zip addr_ptr.
C .
6.5 [Kernighan
88]
17.5 [King 96],
(linked lists).
C.
Afi
9.4
3, void. .
YNOH
read_circle 3 4,
. ,
. .
:
struct address addr[10];
struct address *addr_ptr = &addr[0];
/* struct address *addr_ptr = addr; */
addr_ptr++;
addr_ptr;
.
,
. C, Pascal, , , .
,
C Pascal.
struct C
.
, ,
, ,
.
165
fi
9.1
Afi
9.5
K E A A I O 9 : A A I P E T I K O T H TA TA E O M E N A
166
C, , .
B
[Kernighan 88]
The C Programming Language 1988. ANSI C.
1990.
[King 96]
King N. K., C Programming: A modern approach, W.W.Norton &
Company, Inc.
10
H Pascal
fi
, Pascal,
C.
:
Pascal ,
Pascal C ,
,
Pascal,
Pascal C.
(declaration
part)
(program
body)
(function)
(procedure)
Ada
Modula
Object Pascal
repeat until
Pascal (procedural) . Pascal
C
. C , . ,
[Kernighan 81].
Pascal
168
K E A A I O 1 0 : H A P O PA M M A T I M O Y P A S C A L
C .
Pascal
Pascal , , .
C .
.
169
10.1
Pascal : Real (
), Integer ( ), Char (
) Blean ( /).
(literal).
, Pascal , , (record).
10 :
int a [10];
/* C */
{ Pascal }
C, -
K E A A I O 1 0 : H A P O PA M M A T I M O Y P A S C A L
170
. O 10.1
.
10.1
Pascal C
C struct
Pascal record
struct rec{
type
int a,b,c;
rec = record
float d,e,f;
a,b,c:integer;
};
d,e,f:real;
end;
struct rec r;
var
r:rec;
H
C.
10.3 Pascal
Pascal :
(declaration part) (program body).
, , (functions) (procedures).
10.1
(program heading), Pascal
. , CircleArea
.
, ,
begin end. C ( ).
, , Writeln Readln , printf scanf C.
Pascal
C . H Pascal
PA S C A L
171
{ }
area : Real; { }
begin
{ }
Writeln ( >);
Readln(Radious);
{ }
Area := Pi * radious * radious;
{ }
10.1
Writeln(H , Area)
Pascal .
end.
:
[Horowitz 95].
Pascal
10.2 C Pascal.
10.2: C Pascal.
Pascal
Pascal
:=
&&
and
==
||
or
!=
<>
not
mod
/ ( )
div
,
C.
K E A A I O 1 0 : H A P O PA M M A T I M O Y P A S C A L
172
Afi
10.1
Pascal 6.
.
{ Program to find factorial of 6 }
program factorial;
const
value=6;
var
i,j:integer;
begin
j:=1;
for i:=1 to value do
j:=j*i;
writeln('The factorial of ',value,' is ',j);
end.
10.4 fi
. . Pascal
. , , (program unit or module), . [1] [1] , , .
,
. :
procedure <> ( );
begin
end;
.
Pascal . , Pascal ,
C .
,
.
function
:
function < >( ) : ;
begin
end;
return
C. Pascal
, , . ,
10.2
.
.
. (value parameter), ,
(variable parameter), -
173
K E A A I O 1 0 : H A P O PA M M A T I M O Y P A S C A L
174
.
var .
function ComputeAve
(NumItems: Integer;
Sum: Real) : Real;
begin
if NumItems > 0 then
ComputeAve := Sum / NumItems
else
ComputeAve := 0
10.2
ComputeAve.
Afi
10.2
fi
10.1
end;
C
10.2. , .
10.5 fi
Pascal C .
if
if then
1
else
2
,
begin end. if,
C else if.
X X X
175
, Pascal
case :
case of
1 : 1;
2 : 2;
n : n;
end
, .
end.
while
while do
for
counter
initial final . to
downto .
7 ( ), Pascal
repeat . :
repeat
until
,
.
Pascal repeat dowhile C.
Afi
10.3
K E A A I O 1 0 : H A P O PA M M A T I M O Y P A S C A L
176
Pascal
.
Pascal , , ,
, .
, . Pascal , begin end
, function procedure
, var .
B
[Kernighan 81]
Kernighan W. Brian, Why Pascal is not my favorite programming
language AT&T Bell Laboratories, Computer Science Technical Report
No.100.
http://netlib.belllabs.com/cm/cs/cstr/100.ps.gz
C Pascal.
[Koffman 95]
Koffman B. Elliot Pascal 5th Edition, AddisonWesley Publishing
Company.
Pascal,
ACM. , Pascal, ,
problem solving software engineering.
[Horowitz 95]
Horowitz Ellis, Fundamentals of Programming Languages, Third
edition, Computer Science Press, 1995.
A fi
1.1
: ) ,)
) .
, ,
, ,
1.1.4, .
, ! , , , , .
1.2
:
1. ,
2. ,
3. ,
4. ,
5. ,
6. ,
7. ,
8. .
, .
, , 1.2 1.
.
2.1
, +, , *,
/, < , , , .
E P O PA M M AT I M O Y
178
2.2
. ,
.
3.1
( C)
( C)
, .
(date) ,
C . , date
.
, . , , , , 3.1.3
3.2
%c
%f
%s
%o
%x
%g
%d
%e
%g
.
, ,
. , ,
. , ,
A A N T H E I A K H E N AY T O A I O O H H
( printf, ).
4.1
printf
. .
printf(Hello);
printf
s.
printf(O ISBN fi : %s\n, isbn);
O ISBN fi 03879761
,
.
, .
, .
4.2
scanf C.
.
scanf
printf.
scanf(%s,isbn);
isbn.
:
) & isbn,
, ..,
,
179
E P O PA M M AT I M O Y
180
) 11
, . .
141 [King 96].
scanf , 3.2 scanf .
&, ,
.
, .
4.3
, 40 , . ! , , . 10, 4 40; . , 40 .
int bathmos[40];
! . ,
;
; ;
,
int bathmos[10][4];
bathmos 10 ( ), 4 (
).
, ,
, , -
A A N T H E I A K H E N AY T O A I O O H H
,
. , bathmos[0][1] ([1]) ([0]) ,
bathmos[1][0]
.
4.4
float temp[5][12][31]; temp[4][11][0]
, . ,
4.1.6 , , 4.3
4.5
year
year_ptr
&year
*year
1821
*year_ptr
year_ptr
year
&year_ptr
,
, *year . , &
,
.
! C. ,
. .
4.6
num1
num2
pt1
pt2
181
E P O PA M M AT I M O Y
182
4.7
. . ,
, num (&num) (=) *num_ptr,
int *num_ptr.
. ! & num,
= num_ptr;
.
.
.
5.1
.
, , .
.
, 2
.
, . .
5.2
. (4 +2)
(75) , 2 + (6/2 6).
, / 2 + (3 6).
, , 1.
, 5.2.3
,
.
5.3
:
A A N T H E I A K H E N AY T O A I O O H H
183
true (1)
17
2
x
false
4.0
11
false (0)
1821
4 + 6
c=3+8
x
sqrt(16.0)
1821
5>3
8<1
6 + (c=3+8)
(5> 1) && (6>7)
, ,
,
. :
) . H
, . :
1821
1821
sqrt(16.0)
4.0
12 + max(1,5)
17
sqrt , . max
.
) .
. :
num = 21 12
num1 = num2 = 23
23
6 + (c=13+6)
25
) .
E P O PA M M AT I M O Y
184
(true) (false)
. :
5<6
true
5>6
false (0)
false
(5> 1) || (6>7)
true
.
. .
.
, . .
5.4
2 + 1 1 (26), 2
3/4 3 4 4+2 75, .
+
2
E1
E2 6
E1 tree
E3 E4
E2 tree
+
+
4
2
E3 tree
7
5
E4 tree
, ,
, .
4+2 75 . , .
4 + 2 7 5.
A A N T H E I A K H E N AY T O A I O O H H
185
.
, ,
.
.
, ! .
5.5
. res1 = 2.56 + (float)3/2;
, . 3 2 1 , 2.56 3.56. ; .
, . 3 2 1
. float 1 .
, . , , , .
(float) 3
. , /
2 float. / float . , + float float,
= float res1.
5.6
H :
z = ++x + y;
11
31
z = x + y;
29
z = x++ + y;
11
30
z = x + y;
30
E P O PA M M AT I M O Y
186
). ,
( ).
, , ,
.
printf
.
++ --.
.
, .
.
5.7
int count = 10;
printf(%d %d\n,count,count*count++);
10 100 11 100
, ANSI C, .
int num, count = 11;
num = count/2 + 5* (1+ count++);
A A N T H E I A K H E N AY T O A I O O H H
187
5.8
true
1 < 0
0>1
1!= 1
1>= 1
false
0 == 0
100
1>10
100
, ! .
, , true false.
.
5.9
:
) count = 8
) num = count + 4
12
) num == 9
) count == num 4
, , .
(=) (==).
. num == 20 1
() num 20. , num = 10
10 , , num.
, , , !
5.10
:
x
true
false
, , .
,
. ,
E P O PA M M AT I M O Y
188
false , , false ,
(y < ++z) , , y
z. ;
.
(x++ < y)
, !
C .
6.1
: int max(int a, int b, int c);
, ,
. , , , 6.3.1.
, , .
6.2
: double power(double x, int n);
, , !
.
, , .
.
6.3
char *strchr(char *str,char ch);
.
.
str. -
A A N T H E I A K H E N AY T O A I O O H H
189
, int size.
, , .
4.2.
, , .
6.4
.
int max(int a, int b)
int max;
return((a>b) ? a : b);
max = (a>b) ? a : b;
return(a);
}
, ,
,
.
, . A
, , C .
6.5
, .
ginomeno
. . , ; . ginomeno/base.
, , ,
6
.
E P O PA M M AT I M O Y
190
7.1
. ,
. count
12,
count 12 , , .
1 .
,
4.
5.5.1 , ,
while.
, , .
7.2
for(year=0;year<YEARS;year++){
subtotal = 0;
for(month=0;month<MONTHS;month++)
subtotal+=temp[year][month];
avg_temp[year] = subtotal/MONTHS;
}
for(month=0;month<MONTHS;month++){
subtotal = 0;
for(year=0;year<YEARS;year++)
subtotal += temp[year][month];
printf(" %d : %.1f\n",
subtotal/YEARS);
}
, ,
for.
. -
A A N T H E I A K H E N AY T O A I O O H H
3
.
, for, ,
, .
8.1
) static num . ,
static count
, .
) count
, num , func , func.
, .
,
, . . .
8.2
.
7.1.1 [Darnel 91]
.
, ,
. 8.1.1 8.1.2
.
j:2
j:2
j:2
k:2
k:3
k:4
191
E P O PA M M AT I M O Y
192
8.3
int. .
, .
int max(int *,int *);
main()
{
:
max_value = max(&num1,&num2);
}
int max(int *a, int *b)
{
return(*a>*b?*a:*b);
}
, , .
, , ,
, 4.2.
8.4
. ,
1 .
. :
sum(n1) + n
15
10
A A N T H E I A K H E N AY T O A I O O H H
, , .
, .
, , .
.
9.1
. , :
struct circle {
float x,y;
/* */
float r;
/* */
struct circle .
struct circle
c1 = {2.0,3.0,1.0},
c2 = {4.0,6.0, 2.0};
, 9.29.4. .
9.2
, 7, 24662
addr1 .
struct address {
char name[40];
char street[12];
int number;
int zip;
char city[15];
};
/* fi */
193
E P O PA M M AT I M O Y
194
/* */
struct address addr1 = { , , 7, 24662,
};
.
,
9.29.4. , ,
.
, .
.
9.3
,
point .
struct point {
float x,y;
/* */
};
struct circle {
struct point p;
/* */
float r;
/* */
};
H , circle,
.
struct circle read_circle(struct circle c1)
{
printf( :);
scanf(%f %f, &c1.p.x, &c1.p.y);
printf( :);
scanf(%f,&c1.r);
return c1;
}
A A N T H E I A K H E N AY T O A I O O H H
c = read_circle(c);
c ,
.
, circle
.
struct circle read_circle(void)
{
struct circle c;
/* fi c */
return c;
}
return, c1 ,
c , , c.
, .
9.4
void,
, .
.
void read_circle(struct circle *c1)
{
printf( :);
scanf(%f %f,&c1>p.x, &c1>p.y);
printf( :);
scanf(%f, &c1>p.r);
}
read_circle(&c);
c circle,
.
195
E P O PA M M AT I M O Y
196
scanf, ,
,
, ,
.
, ,
,
.
9.5
, , , . ,
, , addr[1].
, 4.2.5
.
10.1
To value ,
Readln.
write('Enter a value:');
Readln(value);
value.
, ,
Pascal. 10.3.
, . Pascal .
10.2
,
:
procedure ComputeAve (Num1, Num2 { } :Real;
var Sum, Average { } : Real) ;
A A N T H E I A K H E N AY T O A I O O H H
begin
Sum := Num1 + Num2;
Average := Sum / 2.0;
end;
var ,
.
, var,
10.3
C.
, . C, .
10.3
, ,
.
dowhile ,
repeat .
, . 7
.
, .
.
197
A
1.1
,
, :
10 20 : 30
. ,
. .
1.2
: \BIN ,
..., \LIB \ . math.h, H,
, , cos, sin, tan, , .
3.3
printf(O , \ \\ backslash.\\n );
O , \ backslash.
3.4
:
#include <stdio.h>
main()
{
int num;
printf( fi fi 66 90:);
scanf(%d,&num);
A A N T H E I PA T H P I O T H T N
199
, 66,
:
:
ASCII fi : 65
ASCII fi : 66
: C
ASCII fi : 67
, ,
.
3.2.2
.
, , .
3.5
\t .
1 2, :
3
3.6
short 32767
/* toobig.c Integer Overflow
#include <stdio.h>
main()
{
short i = 32767;
printf(%d \t%d \t%d\n,i,i+1,i+2);
}
:
32767
32768
32767
H count -
E P O PA M M AT I M O Y
200
. .
(integer
overflow).
4.1
.
printf
printf("%s\n%c\n%c\n%c\n%c\n", name,name[1],name[2],name[3],name[9]);
#include <stdio.h>
char name[20] = "";
main()
{
printf("%s\n",name);
printf("%c\n%c\n%c\n",name[1],name[2],name[3]);
printf("%c\n",name[9]);
}
4.2
string.h. strlen. strlen bytes
\0.
#include <stdio.h>
#include <string.h>
char name[20];
main()
{
printf(" fi:");
scanf("%s",name);
printf("%s\n",name);
printf("%c\n%c\n%c\n",name[1],name[2],name[3]);
printf("%c\n",name[strlen(name)1]);
}
A A N T H E I PA T H P I O T H T N
5.1
num1 num 2 ,
max = num1>num2 ? num1 : num2;
printf(o %d\n, max);
printf(o %d\n, num1>num2 ? num1 : num2);
max1 = num1>num2 ? num1 : num2;
max2 = max1 > num3 ? max1 : num3;
printf(o %d\n,max2);
max1 . ,
num1>num2 ? (num1>num3? : num1 : num3):
(num2>num3 ? num2 : num3);
. , .
, .
6.1
.
, .
91 [Darnell 1991]
.
201
E P O PA M M AT I M O Y
202
6.2
string.h.
strcpy ,
. !
.
, . strcpy(ch_ptr, str1) C.
#include <stdio.h>
#include <string.h>
char ch = 'a';
char bigstr[50];
main()
{
char *ch_ptr;
A A N T H E I PA T H P I O T H T N
printf("bigstr:%s\n",bigstr);
strcpy(bigstr,str1);
printf("bigstr:%s\n",bigstr);
strcat(bigstr,str2);
printf("bigstr:%s\n",bigstr);
printf("str1 %s str2\n", strcmp(str1,str3)? "Not equal":"equal");
printf("str1 %s \"Klea\"\n", strcmp(str1,"Klea")?
"Not equal":"equal");
ch_ptr = strchr(str1,ch);
printf("%c\n",ch_ptr?*ch_ptr:'#');
ch_ptr = strchr(str2,ch);
printf("%c\n",ch_ptr?*ch_ptr:'#');
}
; ch_ptr ,
strcpy, . ,
ch_str = bigstr;, ch_str
50 bytes
bigstr.
, , strchr
.
. , *.
%c printf.
6.3
ctype.h.
isalnum(), isalpha(), isdigit(), islower(), isspace()
, , true false (0).
#include <stdio.h>
#include <ctype.h>
203
E P O PA M M AT I M O Y
204
main()
{
int ch;
printf("%c \t%3s\n",65,isalnum(65)?"AN":"");
printf("%c \t%3s\n",222,isalpha(222)?"A":"");
printf("%c \t%3s\n",55,isdigit(55)?"D":"");
printf("%c \t%3s\n",92,islower(92)?"L":"");
printf("%c \t%3s\n",32,isspace(32)?"Space":"");
printf("%c \t %c\n",'A',tolower('A'));
printf("%c\t%c\t%c\n",'b', toupper('b'),toupper(tolower('B')));
}
tolower() toupper()
, .
6.4
stdio.h.
. . 6.3 4.1.4 4.1.5 .
#include <stdio.h>
char ch;
char line[80];
main()
{
printf(" fi:");
gets(line);
printf(" fi :");
puts(line);
printf(" :");
ch=getchar();
A A N T H E I PA T H P I O T H T N
printf(" :");
putchar(ch);
putchar('\n');
}
.
7.1
year, :
if((year % 400) == 0) then
else if ((year % 100) == 0) then
else if (year % 4) == 0 then
else
, . , , ,
,
.
, , . .
7.2
205
E P O PA M M AT I M O Y
206
if(a>b)
if(a>b)
if(a>b)
if(a>c)
return a;
return a;
else
return a;
else
else
return c;
else
if(a>c)
if(a>c)
return c;
else if(b>c)
else
if(b>c)
return c;
if(b>c)
return b;
return b;
else
return b;
else
else
return c;
return c;
) max
) max
return c;
}
) max
, ; ,
. , .
. ;
,
. :
if .
,
. else if. , , else
if;
else if
: else if
, , [1].
,
. else
if .
[1] A .
A A N T H E I PA T H P I O T H T N
207
; , .
4.
, ,
. .
, .
,
C .
7.3
#include <stdio.h>
( )
switch(choice) {
void main()
case 1:
float num1,num2;
break;
float result;
case 2:
int choice;
printf(" fi:");
case 3:
scanf("%f",&num1);
printf(" fi:");
break;
scanf("%f",&num2);
case 4:
if(num2)
printf("\n\n");
printf("\n 1 fi \n");
else
printf("\n 2 \n");
printf( -
printf("\n 3 fi \n");
\n);
printf("\n 4 \n");
break;
printf(" fi 14:");
default:
scanf("%d",&choice);
printf(" \n");
()
}
printf("To %f,%f",result,result);
exit(0);
}
E P O PA M M AT I M O Y
208
if(choice>=1 && choice=<4)
printf("To %f,%f",result,result);
1, 2, 3 4. A,
.
display_menu
menu(), , ,
choice = menu();
, switch 3.
, . .
, .
.
7.4
, #define ,
, ,
. define
:
#define ADD
#define SUB
#define MUL
#define DIV
choice
choice[1]
enum choice {ADD=1,SUB,MUL,DIV}choice;
switch
[1] C .
. .
A A N T H E I PA T H P I O T H T N
209
switch(choice) {
case ADD: fi ;
break;
case SUB: ;
break;
case MUL:
break;
case DIV:
break;
default : fi ; break;
}
7.6
#include <stdio.h>
main()
{
int ch, num_of_spaces = 0;
printf( fi:\n);
ch = getchar();
while(ch != \n) {
if(ch == )
num_of_spaces++;
ch = getchar();
}
printf( fi %d.\n, num_of_spaces);
exit(0);
}
ch = getchar(); . . , do while ,
.
, while. , .
, .
E P O PA M M AT I M O Y
210
7.7
#include <stdio.h>
main()
{
int i,n;
double factorial;
printf( fi fi:);
scanf(%d,&n);
factorial = 1;
for(i=2;i<=n; ++i)
factorial *= i;
printf(\n To fi %d %.0f, n, factorial);
}
5 for , ,
.
,
7.2.5. 5. , do.
, .
do.
7.8
for.
void func(int num[], int items)
{
for(i=0;i<items;i++)
if (num[i]<0)
num[i]++;
A A N T H E I PA T H P I O T H T N
else
num[i] = 2;
}
for, .
, , i<items
i<=items.
for ,
while do. ,
10
. for while
do, .
, ,
.
7.9
, .
#include <stdio.h>
#define YEARS 3
#define MONTHS 12
float temp[YEARS][MONTHS]= {
{10,12,13, 14, 20, 30, 32, 36, 24, 20, 15, 10},
{12,14,15, 16, 22, 32, 34, 38, 26, 22, 17, 12},
{ 8,10,11, 12, 18, 28, 30, 34, 22, 18, 13, 8},
};
float avg_temp[3];
void main()
{
int year,month;
float total,subtotal;
211
E P O PA M M AT I M O Y
212
total = 0;
for(year=0;year<YEARS;year++){
subtotal = 0;
for(month=0;month<MONTHS;month++)
subtotal+=temp[year][month];
avg_temp[year] = subtotal/MONTHS;
printf(" %d : %.2f\n",
year,avg_temp[year]);
total += avg_temp[year];
}
printf(" %d %.4f:\n",
YEARS,total/YEARS);
for(month=0;month<MONTHS;month++){
subtotal = 0;
for(year=0;year<YEARS;year++)
subtotal += temp[year][month];
printf(" %d : %.1f\n",
subtotal/YEARS);
}
}
7.10
143 [King 96]
.
typedef
int. C Boolean . typedef int Bool;
Boolean.
,
#include <stdio.h>
#define TRUE
#define FALSE 0
A A N T H E I PA T H P I O T H T N
main()
{
Bool digit_seen[10] = {0};
int digit;
long int n;
printf( fi: );
scanf(%ld, &n);
while(n>0) {
digit = n % 10;
if(digit_seen[digit])
break;
digit_seen[digit] = TRUE;
n /= 10;
}
if (n>0)
printf( \n);
else
printf( \n);
return(0);
}
, .
%.
break
.
7.2.3
while 7.2.8 break , , .
8.1
8.1, a b
.
H
,
213
E P O PA M M AT I M O Y
214
, . 3
a .
c
( 11) ,
main . , b 4 , static. T a b max ( 12)
max a b.
H 6 a b 12 11, .
8 max a b max
17 11, . max 17,
printf. , func
23 x .
b 16,
func b.
a, func.
c printf main
( 7), :
a:11
b:12
max(b+5,a):17
a:11
b:20
c:13
x:23
max(x,b):23
,
. .
8.5
float power(float x, int n)
{
if (n==0)
return(1.0);
else if (n>0)
return(x * power(x,n1));
else
return (1.0 / power(x,n));
A A N T H E I PA T H P I O T H T N
, n
0 1.0.
8.11 [Rojiani 96]
.
8.6
O fibonacci
float fibonacci(int n)
{
if(n<=2)
return(1.0);
else
return(fibonacci(n1) + fibonacci(n2));
}
PI 3.14
struct point {
float x;
float y;
};
struct circle{
struct point p;
215
E P O PA M M AT I M O Y
216
float r;
};
main()
{
struct circle c1;
c1 = read_circle(c1);
report_circle(c1);
c1 = read_circle2();
report_circle(c1);
read_circle3(&c1);
report_circle(c1);
}
A A N T H E I PA T H P I O T H T N
main()
{
int num1, num2;
int sum, average;
num1 =100;
num2 = 200;
ComputeAve(num1, num2, &sum, &average);
printf("sum is %d and average is %d\n", sum, average);
}
217
E P O PA M M AT I M O Y
218
,
C.
Pascal, var .
,
6 8, .
, .
B I B I O PA I A
B
[Horowitz 84]
, , 1993.
Fundamentals of Programming Languages Ellis Horowitz, Computer
Science Press, 1984.
, , , . .
.
[Kernighan 88]
The C Programming Language 1988 ANSI C.
1990.
ISO/IEC 9899,
[Waite 90]
Mitchell Waite, Stephen Prata New C Primer Plus Waite Group Inc 1990.
1991
C:.
C.
(1998) Teach Yourself in 21
Days 4th edition, 1997 Sams Publishing.
B fi
[AhoUllman72]
Aho, ., Ullman, J. The theory of Parsing, Translation and Compiling
PrenticeHall 1972.
[ANSI 88]
ISO/IEC 9899
http://wwold.dkuug.dk/JTC1/SC22/WG14/www/standards
[Bergin 96]
Bergin Thomas, Gibson Richard, History of Programming Languages,
Addison Wesley 1996.
219
E P O PA M M AT I M O Y
220
.
ACM SIGPLAN History of Programming Languages.
,
:
Frederick Brooks, Alain Colmerauer, Richard Gabriel, Ralph Griswold, Per
Brinch, Hansen, Alan Kay, C. H. Lindsey, Barbara Liskov, Richard Nance,
Elizabeth Rather, Dennis Ritchie, Jean Sammet, Guy Steele, Bjarne
Stroustrup, William Whitaker, Niklaus Wirth.
C, C++, Smalltalk,Pascal, Ada, Prolog, Lisp,
ALGOL 68, FORMAC, CLU, Icon, Forth, Concurrent Pascal
[Boehm 76]
Boehm W. Barry, Software Engineering IEEE Trans. Comput., vol. C25,
pp 12261241, Dec. 1976.
[Boehm 88]
Boehm W. Barry, A Spiral Model of Software Development and
Enhancement IEEE Computer, vol. 21, no 5, p.6172, May 1988.
Corriveau Jean Paul, A stepbystep Guide to C Programming, Prentice
Hall.
[Darnell 1991]
Darnell P., Margolis P.: C: A Software Engineering Approach,
SpringerVerlag, New York 1991.
[Hanly 99]
Hanly J.R. and Koffman E.B., Problem Solving & Problem Design in C,
third edition, AddisonWesley, 1999.
, C,
C. .
[Horowitz 84]
Horowitz Ellis Fundamentals of Programming Languages, Computer
Science Press, 1984.
. .
.
B I B I O PA I A
[Horowitz 95]
Horowitz Ellis, Fundamentals of Programming Languages, Third edition,
Computer Science Press 1995.
[Hsia 88]
Hsia Y.T., Ambler A., Programming Through Pictorial
transformations,Proc. Intl Conf. Computer Languages 88, IEEE CS Press,
Los Alamitos, Calif., Order No. 1988, pp.1016.
[Kernighan 81]
Kernighan W. Brian, Why Pascal is not my favorite programming
language AT&T Bell Laboratories, Computer Science Technical Report
No.100.
http://netlib.belllabs.com/cm/cs/cstr/100.ps.gz
.
[Kernighan 88]
The C Programming Language 1988 ANSI C.
ISO/IEC 9899,
[King 96]
King K.N., C Programming: A modern approach, W.W.Norton &
Company, Inc.
[Koffman 95]
Koffman B. Elliot Pascal 5th Edition, AddisonWesley Publishing
Company.
Pascal, ACM.
, Pascal, ,
problem solving software engineering.
[Maulsby 89]
Maulsby D.L., Witten H., Inducing Programs in a DirectManipulation
Environmant, Proc. CHI89, ACM Press, New York, 1989, pp. 5762.
[Ritchie 93]
Ritchie M. Dennis The Development of the C Language, ACM SIGPLAN
Notices, March 93, p.207.
C.
221
E P O PA M M AT I M O Y
222
[Rojiani 96]
Rojiani K.B., Programming in C with numerical methods for Engineers,
PrenticeHall 1996.
[Sethi 97]
Sethi Ravi, Programming Languages: Concepts and Constructs 2nd
Edition, Addison Wesley 1996. Reprinted with corrections, April 1997
[Unicode]
To unicode ISO 10646 16 bit,
. unicode http://www.unicode.org/.
[Waite 90]
Waite Mitchell, Prata Stephen New C Primer Plus Waite Group Inc 1990.
[Wexelblat 81]
Wexelblat L. Richard History of Programming Languages, Los Angeles,
1981.
ACM SIGPLAN History of
Programming Languages Los Angeles on June 13, 1978.
60 (1967) 1977, .
:
ALGOL, APL, APT, BASIC, COBOL, FORTRAN, GPSS, JOSS, JOVIAL,
LISP, PL/I, SIMULA, AND SNOBOL.
[Wirth 76]
Wirth ., Algorithms + Data Structures = Programs, PrenticeHall,
1976.
fi
M C. 1
.
O NOT C.
&
M C.
.
&&
O AND C.
C ( )
.
?:
O C.
||
O OR C.
++
M C. 1
.
T C.
.
==
T C .
abstraction
A.
ALGOL
algorithmic language.
.
FORTRAN
60.
ANSI
ANSI C
O C 1983
ANSI.
E P O PA M M AT I M O Y
224
applicative order
E .
ASCII
Basic
Beginners Allpurpose
Symbolic Instruction Code.
, ,
.
binary operator
BNF
break
C
switch .
call by reference
K .
call by value
K , .
char
C
.
compiler
M.
conditional loop
E .
continue
C
.
API OPN
225
counting loop
A .
data abstraction
A .
data type
T .
declaration part
T Pascal
(functions) (procedures).
define
E C.
do while
C ( )
(conditional
loop).
double
C
.
float
C
.
for
C ( )
(counting loop).
FORTRAN
FORmula TRANslation.
.
.
.
function prototype
E P O PA M M AT I M O Y
226
goto
C .
.
identifier
A.
if else
C ( )
.
include
E .
.
int
C
.
ISO
International Standards
Organization. .
K&R C
keyword
linker
local variable
T .
machine language
Modula
Niklaus Wirth
70
Pascal.
, ,
Software Engineering
(real time
embedded systems).
Object Pascal
E Pascal
.
API OPN
227
OO
ObjectOriented. .
OO programming
ObjectOriented programming. .
Pascal
Y ,
Nicklaus Wirth
70
, . ,
, Modula,
Concurrent Pascal Ada.
printf
C
.
procedure
program body
T Pascal
begin,
end.
programming
paradigm/style
. , , , ,
E P O PA M M AT I M O Y
228
, .
recursive function
A .
reserved word
return
H C
.
scanf
C
.
scope rules
K .
semantic error
sizeof
M C.
bytes .
Smalltalk
.
Palo Alto
Xerox 70. Alan Kay
Simula,
LISP SketchPad.
,
C++ Java.
standard C library
B C.
statement
static
C ,
.
string
A.
string.h
A C.
API OPN
strlen
229
C.
.
struct
C
.
structured programming .
switch case
C
.
syntax error
ternary operator
T .
unary operator
M .
Unicode
. ISO/IEC 10646
16
.
.
variable
M.
while
C ( )
, (conditional
loop).
(string) .
(identifier)
, ,
.
E P O PA M M AT I M O Y
230
M/
(object),
.
. .
T
(modules).
(abstraction). A
.
T
. .., ,
, .
H -
ANSI C
C ANSI .
M , , , .
K .
T
.
API OPN
231
T
.
O
,
,
.
H ,
.
M
1 2 1 2 , ,
.
M
.
T
(identifier)
. ,
.
E P O PA M M AT I M O Y
232
K , , . ,
.
E
,
C, Pascal, Ada, FORTRAN.
T .., + C.
T
. ,
,
.
(side effect)
.
(actual argument) (
)
.
(preprocessor) C .
.
M (command)
.
M
, ,
API OPN
233
.
.
. , .
H .
H .
H .
T
. .., ,
, .
(function) .
,
, ,
.
(linker)
.
({ } C, begin end
Pascal)
.
.
.
E P O PA M M AT I M O Y
234
(operator)
,
,
(operands) , .
(local variable)
.
(ternary operator)
.., ? : C.
(formal arguments)
C
.
C
.
(data type)
, , .
M .
y ) x and y x
) x or y x .
K
.
(runtime) .
(compiletime) .