Вы находитесь на странице: 1из 267

...................................................................................................................................

3
......................................................................................................4
................................................................................................................................5
.......................................................................................................................................6
1. .................................................................................................7
2. /++........................................................................................................ 22
3. .................................................................................................. 31
4. /++ ............................................................................................................ 47
5. /++ .............................................................................................................. 71
6. ...................................................................................................................... 89
7. ............................................................................................... 107
8. ...................................................................... 129
9. ........................................................................................................................... 149
10. ......................................................................................................... 171
11. ................................................................................................. 196
12. .............................................................................................. 218
13. ................................................................................................. 246
I............................................................................................................................. 262
II............................................................................................................................ 295

www.books-shop.com


,
, . . :

++
,
++, .

, ,

/++. ,
!

. .

,
. , - .

, - .
.
, I.

. ++, , , .

www.books-shop.com

.
.
. , ,
.
C WARE Corporation , .
, , , , , ,
.
.
SYBEX, .
. ,
, . , ++. !

www.books-shop.com


, - , . . ,
- .
. , , , , ,
.
, .
, . , . , ++
, BASIC, ,
WordPerfect, Lotus, Excel.
, , ,
, .
/++ ,
K&R ANSI . , ++. , , ,
, ++. ++, , , .


, . .
. , , , , . .
, .
. ,
.
1 2 , ++. 3 ,
.
, 4, , . 5 . 6 , , .
7, , , ,
. 8 , 9 , , .
, 10 , 11
. 12 ,
. , .
, 13 , , .
, ,
.
I , ,
II 13.
.
, , .


++, ,
. , ANSI ++.

www.books-shop.com

1

, .
, , . , . , , , , , . , , , ,
, .
- , , , , . , , , , , , .
, ,
, , - , .
, , , , , .
,
, .
- , , , . , , , . . , , .


, : ,
, , - , , . , ,
, , .
, , , , , , . , , . , , . , , ,
.
, - , ? , -, ? . , , , , , ,
, . .
* , ,
. ,
, . , , , - . ,
, , , , , . . , .
.
, ?
: , .
. , :
, -. , ,
.

www.books-shop.com

++
++ , . , ++ . , , .
, . , :

;
90 ;
50 ;
90 ;
;
.
, , , .
* , , - ,
, ++. (..)
, , , , , .
. A, B, C, . -! , - , -
-? , . : 50 , 25% ,
, 15%, , , .

www.books-shop.com

. 1.1. ,
, , , , , .
, , , , . ,
, , . OK, ,
, .


, .
. , , (.1.1).
,
, ,
. , , . , ,
, .
. , , , -

www.books-shop.com

, .
, .
, , ( ): (
) ( ).
,
. .1.2 , , *.
, 0, , 1,
* ,
. (..)

. 1.2. ,
,
. ()* , , , , .
, . ,
, , , .
- , , .
* binary digit, bit. (..)

www.books-shop.com

. 1.3.
,
.
0 1,
, .
, , . , . , MOV ,
, JMP , . , , 0 1, , , .
. 1.3, , .

. 1.4. ,
, , , ,
.

%RRNVVKRS

piracy@books-shop.com

, , 0 1.
, . ,
puts()* , .
.
. 1.4 , ++ -
, ,
. , .
, , puts()
, , ,
.
* put string. (..)
,
.

,
. ,
.
, , -,
. , ,
. ,
. ,
. ,
,
. .
. 1.5 . , , ( , ) , .

. 1.5.
, ++,
.
, ,

++
, ++
. , ,
, ++.

www.books-shop.com

.
( , ).
.
, , . ,
, . , . ,


, .C, .OBJ, ..
, , , .
, ++, , .

. .
- .
, . ,
, - , .

. 1.6.
, , .
. .1.6 , . , ,
. BASIC, MS-DOS, .
? , , .
, ,
, , , .

www.books-shop.com

? , , , , .
, , , . ,
, .

++
, ,
++. /++,
. ,
, .
, .
, . ,
, .

/++?
. ,
,
. ,
. : , .

, , ,
. . , ,
,
.
, . ,
? , . , .

, , . .
IBM PC ,
Apple Macintosh, .
. , ( )
. , , : , , IBM, , Apple. .
, , ,
. . , ,
, .

, . BASIC, .
- . , . , , , . , , . ,
.

www.books-shop.com

. ,
( , ). , 27 (keywords) *.
*
. (..)

. 1.7. ,
, . . , .
, .
,
, , .
, , , puts(). ,
,
( , ).
.
,

www.books-shop.com

. 1.8. /
, . ( .1.7) ( , )
. , .
, .
, , . , (header
file) .H. , .

.
. 1.8 /.
, , .
, , .
, .
, .
, ,
, , , , .
, .
, . , , , A, B,
C. , , , , , , ,
.
. IBM ,
IBM. Mac , Mac.
,
.

www.books-shop.com


. 1972 .
. ,
, K&R ( ). K&R- , , , , K&R-, .
, , , , -. , * 1983 ,
* American National Standards Institute. (..)
ANSI . ANSI-
.
, ++ . , , .
, ++. ++ ,

. , ++ .

-
, , -
, . .
, , : , , .

- , ,
.
. , :



, 12



555-1234




, , , , , . , : , .
- (), .
. ( ), :

www.books-shop.com




, ,
. ,
:

: (, , 12)
: (, 555-1234)
: (, )*
, . - , . , ,
- .


, ,
, . , ,
. ++.
, , ,
. , ,
. ,
.
. , , .
,
.
.
* :
Member_cards.change_address(Smiths, 12 West Avenue)
Member_cards.change_phone(Doe, 555-1234)
Member_cards.change_status(Jones, inactive)
++. (..)


. , . .


, .
. , , .
, , .
?
, : . , .
, , ,
, .
. , ,
.
. .
, :

www.books-shop.com

, .
.
,
.


, .
. ,
, .


,
. - ,
.
, . , , .
, ,
.
, . , .


, . , . , ,
.


, . , . : .
, , . , .
.
, ACCOUNT.DAT, . , ,
. , .
, , , , .
, .
.
. , , , .
, , , , . , , , , 6%
100 1666.66 ! ,
, .
,
.


, .

www.books-shop.com

-, , . .
-, , , - ,
. ,
. , , , .
, . ,
.

,
++, , .
,
ASCII DOS TEXT. .
,
, ,
. ,
.
.
, , ,
. , , . ,
*.
, , . (debugger) . , .
, , . (profiler) . (assembler)
, , , .
(IDE). , , , ,
.
: , ,
, , .

/++
, , , . . , , .
DOS, Windows . . -, ,
.

1.

2.

3.

4.

5.

6.

? .

www.books-shop.com

7.

1.

2.

, ,
( 65
).

** ,
ANSI . , Borland, Borland C 1.0 .
(..)

%RRNVVKRS

piracy@books-shop.com

2
/++
, , , , .
(, for)
( scanf, scan format) ( ), , , -.
, .
, ,
.
.

++
++ .
, ++.


, , . , ,
, , , . , , , ,
.
, .
( ++) , main(). :

main()
, , , main. . ,
. ,
, .
main() .
, . , ({) , , (}) *. , ,
, :

main()

.....;
.....;

.....;
}

* , , .
(..)

www.books-shop.com

. 2.1. /++

. , . ,
.
main().
/++, OK:

main()
{
puts("OK");
}
, , ,
. , .
, ,
, OK ( , 3). .2.1 .
.
, . :

main()
{
puts(" ");
puts(" ?");
}
, .
, .
puts(),
, , puts(),
.

www.books-shop.com

++ . , , .
:

main(){puts("OK");}
, . , , :

main() ;

. , , .
puts() .
4.

. ,
, .
, , . , , .
, ,
, .


, , .
, puts(), PUTS() Puts(). , ,
, . .

return
, , ? , , MS-DOS, ,
Windows, .
, , . , , ,
. return(0), , main():

main()
{
puts(" ");
puts(" ?");
return(0);
}
return(0) , . , , . 0
. return(0); return; . , , .
- return(0), , . , :

main()

www.books-shop.com

{
puts(" ");
return(0);
puts(" ?");
}

puts(), " ?".


,
. ,
.
, , ,
, . , .
. , .
. ,
.
, , ,
. , , , , , . ,
, ,
.
/* */.
:

/* */
main()
{
puts("OK");
return(0);
}
/* , */ . ,
, . ,
, . , .
, :

/* */
main()
{
/* "OK"*/
puts("OK");
return(0); /* */
}
( ) .
, . */,
,
:

/* ,
return(0) ,

www.books-shop.com

*/
:

/* ,
* return(0) ,
*
*/
:

/*
******************************
* , *
* return(0) , *
*

******************************
*/
. ,
, , .

++
, ,
, */ ,
. ++
//, . , :

//
main()
{
puts("OK");
return(0); //
}
,
//:

// ,
// return(0) ,
//
++ - /* */.


, puts(), ,
. , . .
, , *. , puts()
, ,
. ?

www.books-shop.com

. . ,

puts("");
, . .2.2, , puts(), .

* , , , , , .
(..)

. 2.2.
, , -----, - ( 3).
puts("") .
, , puts(), , , puts("") , puts()
.
. puts()
: , , .
, .
.
.
, main() ,
puts(). main() , . main(), , puts(). , puts() , main().

#include
,
, STDIO.H. :

#include
main()

www.books-shop.com

{
puts("OK");
return(0);
}
#include , , STDIO.H. stdio /
(standard input/output). STDIO.H , .
, , main().

?
, (< >), , INCLUDE. ,
.
, INCLUDE.

#include "stdio.h"

, .
, ,
.
* STDIO.H.
? , getc(),
, , . , getchar().
* . getchar() getc() , . ,
, ?
STDIO.H, , getchar(), STDIO.H #include.
, , STDIO.H, .
* , . (..)


.
,
. .
, ,
, .
. ,
, ? ,
, . ,
, . , .
:
, , .
, . ,
.
main(), ( 7
).

www.books-shop.com

* get a character. (..)


. , , . , , . , ,
, , , : ?
, .

1.

, ?

2.

3.

return?

4.

5.

6.

1.

, :

.
.
2.

, ,
.

3.

, :

4.

main()

5.

6.

puts(" ");
}

www.books-shop.com

3

. ,
, , . ,
, . , , , ,
, ,
.
, , . , , , . , .
-, . . -,
. , , .
, ,
, .
, .
, . ,
, ,
, .

++
++ , .
++ , K&R .


(char, character) , . ,
*, ,
, (.3.1).
, 26 26 :

. 3.1.

* char , ,
1 (8 ). (..)

ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghigklmnopqrstuvwxyz
:

1234567890
:*

!@#$%^&*()_+-=|\}{"':;?/>.<,~`
, , , , , .

www.books-shop.com

, ,
, .
, 1, 2, 3,
1 1. , . , , , 1 , 1.

, , . ,
. , . , , , .
++

.
, . .
. 123 , 1, 2, 3.
* : 33 33 . (..)
, , 10. , , , , .


, .
, , .
(int, integer) , .
, ,
. , : . , ,
, .
. 3.2, ,
, ( 2, 2000 ).
, .
( int) ,

. 3.2.
32768 +32767. , , .
,
.
.
short int

: 0 255

int

: 32768 +32767

%RRNVVKRS

piracy@books-shop.com

long int

: 2147483648 +2147483647

unsigned long : 0 4294967295


, (),
(floating-point values).
(float). , , ,
3.4+38. : 38
, . :
float

3.438 3.4+38

double

1.7308 1.7+308

long double 3.44932 1.1+4932


, . , 6.12345678912345 float 6.12345. , ,
, , . double
, 1516 .

. 3.3.
; , (long double) .
. 3.3
.

?
, , , . ,
, ,
. ?
? , !
. , , . . , . int float char .
, , . , , . , , , , .
, , . ,
, , .


, , ,
. , ,
, , .

www.books-shop.com

.
, , , ,
, . , , . ,
, . , 5%. ,
0.05, , .
, . . , , . , , , ,
.
.
, , . ,
.


.
: ,
, 32 .
,
. , , 32 , , , accountspayable
accountsreceivable, .
,
(_). , . , , , city_tax citytax.
, , . city_tax , ctax, amt_due
, due. ,
.
.
.
++, , K&R
, , ANSI, , ++. , , ,
, . , , , ,
.
K&R :

auto
break
case
char
continue
default
dodouble
else

entry
extern
float
for
goto
if
int
long
register

return
short
sizeof
static
struct
switch
typedef
union

ANSI :

const
enum
signed

www.books-shop.com

void
volatile

. 3.4.
++ :

catch
cin
class
cout
delete
friend

inline
new
operator
private
protected

. TAX,
Tax tax. , TAX, Tax tax, , ,
, .
,
, .
(. 3.4) , . ,
.
, .
.

www.books-shop.com

. 3.5.


. main() #define, :

#define NAME VALUE


, , ,
, . : ,
(.3.5).
, ( ,
#define ( 7).

. 3.6.
). , .
. ,

www.books-shop.com

. 3.7.

#define PI 3.14
, PI, 3.14 (.3.6).
, (, ) PI,
3.14 (.3.7).
.
, #define. , PI
, , 3.14, .

#define COUNT 5
COUNT , 5 .
, float, , , , . , :

#define RATE 5.00


, int, float. , :

#define RATE 0.56


, #define RATE .56, .
:

#define UNIT 'A'


, , :

#define MY_FRIEND "George"


main()
{
puts(MY_FRIEND);
}
, .
, puts() ,
. , ,

www.books-shop.com

, (), . , MY_FRIEND, "George",


M-Y-_-F-R-I-E-N-D. MY_FRIEND,
:

puts("MY_FRIEND");
#define MY_FRIEND" George", MY_FRIEND, "George",

puts(MY_FRIEND);
puts()

puts("George");
George.

#define MY_FRIEND ""


. ? #define
MY_FRIEND, "".

++
++ .
, , const.
, , :

main()
{
const int CHILDREN = 8;
const char INIT = 'C';
....;
....;
}
const ,
, . ++, #define.
8 ,
'C'. :

#define CHILDREN 8
#define INIT 'C'

?
, ,
? ? ,

#define PHONE "555-1234"


, , :

puts(PHONE);
, ,

puts("555-1234");

www.books-shop.com

. 3.8.
#define .
. , 20 . , ,
, , 20 puts().
19 puts(), , .
, , #define.
.3.8, puts() .
.

#define TAX 0.06


TAX, 0.06
. , , 6 6.5 , ,
, , , ,
#define*.


, , , . :

type name;
, , , . :

main()

www.books-shop.com

. 3.9.
* , , (, , ), , , . (..)
main() int , tax_rate (.3.9).
, ,
:

main()
{
int count, children, year;
float tax_rate, discount;
}
: . main()
. main():

int count;
main()
{
.....
}
, ,
( 7).


, , , , ,
. , .
( ) :

main()
{
int count = 5;
char initial = 'A';
float rate = 0.55;
}
count
5. initial, '' , float rate 0.55. ,
.

www.books-shop.com

, , (=):

count = 5;
initial = 'A';
rate = 0.55;
,
.
,
, . .

. 3.10.
\0 .
,
. , , .
. , , .
. .3.10 , , "". , ,
puts(). \0 ,
. , puts(),
.
#define,
:

#define CLIENT " "


, char
, . , :

char var_name[N];
var_name , N , : [ ].
, , , , ,
. , , , :

char state[3];

www.books-shop.com

. 3.11.

. 3.12. , ,

, .3.11, , state , , (\0).
, , , . , , , .
, . , , . :

char client[10];
, .
, - ,
.3.12.
, :

char client[80];
, .
, , . , , , ( , , ). , :
%RRNVVKRS

piracy@books-shop.com

main()
{
char client[15];
client = "";
puts(client);
}

client = "";
, ,

. 3.13.
. main():

char client[] = "";


main()
{
puts(client);
}
,
. , (.3.13). , puts()
- .
main() :

main()
{
static char greet[] = "";
puts(greet);
}
main(), static, . , . , , ( 7).


, , , . . , puts() ,

#define PI 3.14

www.books-shop.com

main()
{
puts(PI);
}
. puts() .

:

#define INITIAL "A"


, ,
, char. , , .
, , .

* ,
. , , . :

count = 5;
5 . , , . :

#define INIT 'C'


rate = 0.55;
client = "";
puts("555-1234");
C, 0.55, 555-1234 .


.
, .
,
, , . , .
, . ,
, , .
, . .
1.

2.

, :

, : . , ,
float. sale. , . TAX_RATE.

. , 25.00 , 0.05, 1.25


. float. sales_tax.

, . 26.25 .
float. total.

#define : #define TAX_RATE 0.05

www.books-shop.com

3.

: float sale, sales_tax, total;

, , .
* . (..)

1.

char?

2.

'3' 3?

3.

4.


long int?

5.

double float?

6.

7.

8.

9.

10. ?

1.

, , , , ( 40 ).

2.

, ,
.

3.

, :

4.

char client[3]="Ajax";

5.

main()

6.

float tax_due;

7.

char name(10);

8.

int count(5);
tax_due = "$1635.00";

www.books-shop.com

4
/++
.
, . ,
.
, , - .
.

++.
, , . .

puts()
puts(), . (, , )
:

puts(" !");

#define MESSAGE " "

main()

puts(MESSAGE);
}

char greeting[]=" ";

main()

puts(geering);
}
, ( )
,
++ .

, .
, , .
puts(). ,
, , .
.
\n , ( ). , .
, puts()
, \n , .

www.books-shop.com

, , , ,
\n , puts(). , , , .

putchar()
putchar() .
:

putchar('H');

#define INITIAL 'H'

main()

putchar(INITIAL);
}

main()

char letter;

letter='G';

putchar(letter);
}

putchar() .

putchar('Hi');
.

.
putchar(),
, .
\n, .
putch(), putchar().


putchar()
STDIO.H #include. putchar()
putc(). putc(), 11, , . STDIO.H , putc() putchar().
putc() , ( , putchar()) . ,
, , . putc() putchar() , . int , , , K&R , :

main()

www.books-shop.com

{
int letter;
letter='G';
putchar(letter);
}
, letter ,
. .
K&R putchar() int. .


, , escape- (escape sequences). (\), , ,
, escape- (escape character). escape , . , , , .


\n, , .
putchar(). :

putchar('A');
putchar('\n');
A, . ,
\n, , putchar() ,
. n , , .
puts() escape- .

puts(" \n");
, . .
puts(),
. , :
1.

2.

\n escape- ,
, .

3.

puts() . , .

\n ,
.

puts("A\nB\nC");
:

A
B
C
? : , , B, C. ,
C.

www.books-shop.com

\t . ,
, :

main()
{
puts("123456789012345678921234567893123456789412345\n");
puts("0\t1\t2\t3\t4\t5\n");
}
, ,
0 1 5, .

123456789012345678921234567893123456789412345
0

, ,
:

main()
{
puts(", :\n");
puts("\t\t\t\t\t\t\n");
puts("$1.50\t\t$2.45\t\t$6.24\t\t$3.56\n");
puts("10 \t\t5 \t\t15 \t\t1 \n");
}
.4.1. ,
, .
, , .

. 4.1. ,


\r ,
. - , , ,
.
. 4.2 escape- . , Enter , ,
.
, CR/LF ( carriage return/line feed).
.

www.books-shop.com

. 4.2.
\r :

puts("\r");
. : , \r ,
. \r
, , .


\r, , \b
. , .
, ,
, .


( , ), \f . .
, ,
,
. puts()
putchar(), ,
, .


escape- . ,
:
Escape-

\'

\"

\\

, :

""

www.books-shop.com

puts(" """);
. , , puts(),
, , . ,
"", , ,
puts(). .
:

puts(" \"\"");

. 4.3.
. 4.3 , .
,
. IBM PC ,
ASCII-. , , , *. , , 3 ( ). , ( ) :

putchar('\003');
. , :
* . (..)

main()
{
puts(" \003\n");
puts(" \004\n");
puts(" \005\n");
puts(" \006\n");
}
puts()
(.4.4). .4.1 , .

www.books-shop.com

. 4.4.
putchar()
. int, , ,
:

int count;
count=5;
, putchar()

putchar(count);
, , .
4.1. .

ASCII , ! escape- \007


. ,
:

#define BELL '\007'

/* BELL , \007 */

main()
{
putchar(BELL);

/* */

putchar(BELL);

/* */

puts(" !\n");
puts(" \
\n");
}
#define BELL \007.
, . , BELL putchar().

++
,
.
ASCII-
. .
, K&R ANSI
. %RRNVVKRS

piracy@books-shop.com

( .4.1) .
ASCII- , , , .
, ASCII-,
0 127 .
, .

printf()
puts() putchar() , , , . ,
(). , .
++ , printf().
. ,
printf() .
printf() puts() :

#define MESSAGE "!"


main()
{
printf(MESSAGE);
printf(" , ");
}
puts(), printf() , ,
.
++ cout.
++ .


, , printf(), (.4.5).

. 4.5. printf()
*.
, .
, ** , .
* control string format string. (..)
** format specifier. (..)
(%), , :
%d

%u

%f

float double

www.books-shop.com

%e

%g

, %f
%e, ,

%c

%s

, printf() :

printf("%d")
, ( , : printf("%%");).
d , ,
(.4.6).

. 4.6.
, ,
, .
. .
,
.
printf() :

printf("%d", 12);
12
(.4.7).

. 4.7.
printf() : 12.
. , :

printf(" %d ", 12);

www.books-shop.com

. 4.8.

" %d "
, %d, , " "
"" (.4.8). 12 , :

12
, .
, , ,
:

printf(" 12 ");
puts(" 12 ");
,
printf() , , , :

main()
{
int age;
age = 12;
printf(" %d ", age);
}
(.4.9).

. 4.9.

www.books-shop.com

printf() , .
.
, : , .
:

main()
{
int lucky_1, lucky_2;
lucky_1 = 12;
lucky_2 = 21;
printf(" \
%d %d", lucky_1, lucky_2);
}
lucky_1 lucky_2 . printf() (),
, .
int, %d, .4.10.

. 4.10.
, :

12 21
lucky_1, , ,
( lucky_2) . :

printf(" \
%d %d", lucky_2, lucky_1);
:

21 12
. , float int printf():

main()
{
int count;
float amount;
count = 5;
amount = 45.48;
printf("
%f %d ", amount, count);

www.books-shop.com

}
%f.
%d. , . .4.11, float

. 4.11.
%f, int %d. :

45.580000 5
, ( , ).
:

printf(" \
%f %d ", count, amount);
, :

-2.00214937 16454
.
printf() , .


printf() .
, , .
, \n:

printf(" %f %d
\n", amount, count);
\n , ( ), ,

printf(" %f\n
%d \n", amount, count);
:

45.580000 5
escape- , .

www.books-shop.com

,
int :
%o

, , 0

%x

, %o
%x , , .
, 17
:

main()
{
printf("%d %x %o \
\n", 17, 17, 17);
// ++
// ,
// (.)
}
:

17 11 21

. , , , .

putchar() ,
. , int putchar() printf():

main()
{
int a;
a = 'A';
putchar(a);
putchar('\n');
printf("%c", a);
}
: , putchar(), , printf() %c. , char int,
ASCII-:

main()
{
char a;
a = 'A';
printf("ASCII- %c %d", a, a);
}

%c %d. :

www.books-shop.com

ASCII- 65
, %c, 65, ASCII- , %d.
, ,
int.


printf() .
*.
, ,
.

printf(" %f
%d ", amount, count);
:

45.580000 5
, ,
- :

45.579998 5

. 4.12.

* field-width specifiers. (..)


, , .
, %.nf, n (.4.12). , :

printf(" %.2f", amount);


float :

45.58
, :

printf(" %.3f", amount);


:

45.580
.4.13 .

www.books-shop.com

. 4.13.

, ( ) ,
, :

%N.nf
N .

printf(" %8.2f", amount);


45.58
. , ,
.4.14: ,
. ,
, , .

. 4.14.

, , , , , , , .

printf(" %2.2f", amount);

www.books-shop.com

45.58
, ,
.4.15. , , 0:

. 4.15.

printf(" %08.2f", amount);


00045.58
(
), %

%-8.2f
.

printf(" %-8.2f
", amount);

45.58
. 4.16, 45.58 ,
, .
, . , . , , message, ,

. 4.16.

printf(" , %8s", message);


,
. 4.17, message

www.books-shop.com

. 4.17.


, ,
.
, puts() putchar().
, ,
, printf(). puts(),
, .
, , .

. 4.18. printf()

, printf(),
.
printf() ,
, ,
. printf(), ,
, .
.4.18 , , ,
printf().

++
,
++. ++ .
%RRNVVKRS

piracy@books-shop.com

++ cout,

(<<)
, ,*
.
++, . , ,
* insertion operator. .
(..)

. 4.19. cout

cout
cin ( ). , , STREAM.H #include ( #include 2).
, cout, .4.19. cout

<
. cout . ( ) .

cout << ", .


";
, .

int count;
count = 4509;
cout << count

www.books-shop.com

. 4.20. cout

count 4509.
cout, .
, .4.20. ,

int age;
age = 43;
cout << " " << age << " .";

43 .
cout , ,
, .
, printf(), cout . , , , \n,
.4.20.
cout , . ,
.
cout . ++,
.

++
++
. printf(), ,
cout
. .
, cout.


, , - , , .
, :

www.books-shop.com

puts(" \n");
puts(" .\n");
puts(" ,\n");
puts(" .\n");
.
, , ( ),
. , ,
:

puts(", :");
, :

printf(" :

%7.2f\n", princ);

printf(":

%7.2f\n", interest);

printf(" :

%7.2f\n", total);

, , :

256.25

92.12

34.37

, , , :

printf("%f %f %f", prins, inter, total);


:

256.25 92.12 34.37


. , . , .
. 4.2 , .

www.books-shop.com

4.2. /++.

1.

2.

puts()?

3.


putchar()?

4.

escape-?

5.

escape-
\n \r?

6.

7.

printf()?

8.

printf() puts()?

9.

10. ?

1.

www.books-shop.com

puts().
2.

printf().

3.

puts(), . 80 .

4.

printf(),
.

5.

printf(),
:

float length, width, height, volume;


6.

7.

.
printf(), :

char name[12];
int age;

8.

9.

har item[] = "";

10.

float cost = 3.55;


float markup = 0.75;
printf(),
:

1 : 3.55
:

0.75

.
11. :

int count = 30;


count , ,
:

! ! ! !
.
30 , .

www.books-shop.com

5
/++
, . , , ,
, , . . 5.1 , .
, . , . , . ,
.
. 12 ,
.

. 5.1. ,
, :
, , ,
, .

++
++ .
, . . ,
. - ,
, (.5.2).

www.books-shop.com

. 5.2.

gets()
gets() . .
:

main()
{
char name[15];
gets(name);
puts(name);
}
gets() 14 , , name. ,
, ,
(\0), . name , 15 , 16:

char name[16];
gets() . , - . ,
Enter. , , , , .
Enter .
gets(),
( ),
,
,
Enter.
:

main()
{
char name[25];
printf(", : ");
gets(name);
printf(", : %s", name);

www.books-shop.com

}
, :

, :
printf()
puts() ,
,
. ,
.
printf() ,
, ,
printf() . , , . , , .
, . ,
, Enter,
Backspace . Esc ,
.
Enter name
. printf()
(, ):

, :
, :
, , ,
, , Enter.
name
printf().
, gets() .

getchar()
getchar() . ,
(char int) , K&R- ( 4).
:

int letter;

char letter;

letter = getchar();

letter = getchar();

, getchar() , , . ,
. :
letter , getchar(). ,
getchar() (.5.3). getchar() , .
getchar() . .
, getchar() . Enter , getchar()

www.books-shop.com

. 5.3. getchar()
, .
, - .
++ getch(),
Enter.
, getchar(),
Enter .
.
? , ,
, . getchar() , Enter, .
getchar(), , ,
int char, putchar() printf(). %c:

/*getchar.c*/
main()
{
int initial;
puts(", .");
initial=getchar();
putchar('\n');
putchar(initial);
putchar('\n');
printf("%c", initial);
}
initial, , putchar() printf(). '',
, .

www.books-shop.com

, . ,
.
%d, ASCII- :

/*ascii.c*/
main()
{
int letter;
letter=getchar();
printf("ASCII- \
%c %d\n", letter, letter);
}
letter, getchar(), ,
printf(), , . %c , ,
%d , ASCII- .
, getchar(), , , , int ,
.

Enter
getchar() ,
. . ( 25).
puts(), , ,
, , .
,
, - puts(), , , , , :

printf(" Enter");
getchar();
getchar() , (.5.4). Enter,
, . getchar() . , , Y,
, .

. 5.4. getchar()

&
%RRNVVKRS

piracy@books-shop.com

, , ,
, (.3). 0
. 2 , 0
2097151, .5.5. .

. 5.5.
, . , count, () . .
. 5.6 count, 2341. , 21560 21561, count ( !) 21560.
, , . count int, ,
21560, 21561.
.
21561 .

. 5.6.
(, ) .
, (&) ,
, : &count. & . , , , , . , count 2341,
&count 21560.

www.books-shop.com

. , , 10.
scanf().

scanf()
scanf() , . () ,
, , ,
(SCAN Formatted characters). , printf(), scanf() ,
,
.
++ ,

cin. ++ .
, printf(), scanf() :
(.5.7). , *, ,
. ,
.

. 5.7. scanf()
, printf():
%d

%u

%f

, float

%e

%g

%f %e

%c

%s

%o

%x

, scanf() , :

main()
{
float amount;
scanf("%f", &amount);
}
* conversion characters. (..)

www.books-shop.com

scanf() , amount. , .
scanf(), , . Enter.
scanf() gets() getchar().
, scanf(), .


gets(), ,
Enter, .
getchar(), .
scanf() -. ,
scanf() , .
, scanf() . , . scanf()
. Enter , ,
scanf() , ,
. scanf() , , , . ,
,
. .5.8 .
scanf() : , ,
, , char. :

main()
{
int count;
puts(", : ");
scanf("%d", &count);
printf(" %d", count);
}

www.books-shop.com

. 5.8. scanf() , ,
, ,
, . scanf(). ( ), . , , ,
. , 123abc, 123 count, abc , .5.9.
. , 123,
12, 3 .

www.books-shop.com

. 5.9. scanf() ,
scanf() . ,
ABC123, , .
, .
%d, . %x,
0123456789ABCDE,
. %c,
, scanf() . :

char letter;
scanf("%c", &letter);
, scanf() , . , char, .
, scanf() ,
, , . :

main()
{
char name[25];
puts(", : ");
scanf("%s", name);
puts(name);
}
, .
Enter, name.
, scanf()
, , ,
(.5.10).

www.books-shop.com

. 5.10. scanf()
- scanf(), . gets().

scanf()
scanf() . :

main()
{
int count;
printf(",
Enter: ");
scanf("%d", &count);
printf(" : %d", count);
}
?
1.

printf() :

, Enter:
, .
2.

, .

3.

Enter.

4.

scanf() , ,
. , , .
. , .

5.

6.

printf() , , : .

scanf(), .
5.1 , : int,
float, char . , , .
5.1. , . /*scanf3.c*/

main()
{
int count;
float amount;

www.books-shop.com

char letter;
char name[15];
puts(" Enter");
scanf("%d", &count);
puts("
Enter");
scanf("%f", &amount);
puts(" Enter");
scanf("%c", &letter);
puts(" Enter");
scanf("%s", name);
printf("%d %6.2f %c %s", count,
amount, letter, name);
}
scanf() .
, , .
,
scanf(), .
int,
%c, %d. %d , ASCII-
- printf().


, scanf() , , - , . , scanf(),
.
, , :

main()
{
float amount;
puts(" : ");
scanf("%f", &amount);
printf("\n %f", amount);
}
, , , : $45.65. , , .
, scanf(), , scanf() . !

scanf():

puts(", . \n");
puts(" . \n");
puts(" .
: 4567.87\n");
puts(" : $4567,87\n");

www.books-shop.com

puts(" \n");
, , . , printf(), ,
. printf()
, , ,
. scanf() . , scanf():

scanf("$%f", &amount);
$ ,
, , $45.65, , scanf() ,
amount.
, , ,
, .
, . ,
$ , , . - , ,
, :

puts(", . \n");
puts(" .
: $4567.87\n");
puts(" .\n");
, printf(). , , ,
. , , printf():

printf(" $%f", amount);


$, printf(). , %f.

scanf()
, , , . scanf() ,
, . ,
, , ,
.
scanf() .
, ,
.

++
++ gets(), getchar() scanf(),
. , ++ .
cin (>>), *, .

int count;
cin >> count;
count. cin
, .
cin . cin .
. - cin
, scanf().
:
* extraction operator. (..)

www.books-shop.com

. 5.11.

cin >> amount >> count >> age >> name;


, >>
<<. .5.11 ,
, .
>> , . ,
. cin , , .
<< , . ,
. cout , ,
, .


, , , . , scanf() , , .
. , ,
, , . ?
, ,
. ,
, . , , ,
, . . , ,
, , , .
, .
:

int count = 0;
char initial = ' ';
float rate = 0.0;
, .
, , , , , .
, , ,
. : .
. 5.1 , .

www.books-shop.com

5.1. /++.

(, BASIC)

.


. ,
, ,
, . , , , , , 90 . , , .
.
, .
, . ,
, ? , ( 8). :

cache = amount;
amount cache, , amount , . cache
, . , , .
, 5.2, (
).
5.2. .

/*storage.c*/
main()
{
%RRNVVKRS

piracy@books-shop.com

int number, storage;


puts(" ");
scanf("%d", &number);
storage = number;
puts(" ");
scanf("%d", &number);
printf(" : %d\n", storage);
printf(" : %d", number);
}

. 5.12.
number :

puts(" ");
scanf("%d", &number);
number storage :

storage = number;
number ,
. , , . , ,
.5.12.

1.

2.

getchar() Enter?

www.books-shop.com

3.

getchar().

4.

5.

scanf()?

6.

7.

gets(),
scanf()?

8.

scanf()?

9.

10.
scanf() char?

1.

, ,
.

2.

, ,
, .

3.

, ,
, ,
.

4.

, getchar(),
gets() scanf().

5.

, :

6.

main()

7.

8.

char initial;

9.

initial = gets();

10.

puts(initial);
}

www.books-shop.com

, - ? ,
, . , ,
.
, , ,
. ,
. , , , ,
. ,
.
. , ,
. , , .
.6.1, :

=
= +

. 6.1.
, , .
, , .
, .

++
++ . , ++
. 10 , + ++ .


:

+

%

, ( x),
( \).
, .

www.books-shop.com

. , :

sales_tax = 4500;
, .
, :

sales_tax = amount * tax_rate;


price = cost + shipping + insurance;
per_unit = total / count;
, :

sales_tax , amount tax_rate;


price , : cost,
shipping insurance;
per_unit , total count.

, ,
. , , , :

sales_tax = amount * 0.06;


price = 56.90 + shipping + 7.87;
per_unit = 156.65 / 12.50;
6.1 . , . (10 ) (6 ). , puts()
printf().
6.1. .

/*invoice.c*/
#define TAX_RATE 0.06
#define SHIPPING 0.10
main()
{
char name[15], address[20], city[15],
state[3], zip[6];
float order, total, tax, ship;
printf(" : ");
gets(name);
printf(": ");
gets(address);
printf(": ");
gets(city);
printf(": ");
gets(state);
printf(":");
gets(zip);
printf(" : ");
scanf("%f", &order);
tax = order * TAX_RATE;
ship = order * SHIPPING;
total = order + tax + ship;
puts("\n\n\n\n");
puts(" \n");
printf("%s\n%s\n%s, %s %s\n", name,
adress, city, state, zip);
printf("\t\t\t\t%-10s\t%10.2f\n", ":", order);
printf("\t\t\t\t%-10s\t%10.2f\n", ":", tax);
printf("\t\t\t\t%-10s\t%10.2f\n",
":", ship);
printf("\t\t\t\t\t\t______________\n);
printf("\t\t\t\t%-10s\t%10.2f",": ",total);
}

www.books-shop.com

% .
(/) , . ,
12 5 (12/5) 2, 2.4. , 0.4,
.
, .
, 0.4, float.
. , 12
5, . 2 ,
%.
.
6.2. , , , .
, , , , . , , .
6.2. , .

/*change.c*/
main()
{
int amount, twenties, tens, fives, ones, r20, r10;
printf(" : ");
scanf("%d", &amount);
twenties = amount / 20;
r20 = amount % 20; /*r20 - */
tens = r20 / 10;
r10 = r20 % 10;
/*r10 - */
fives = r10 / 5;
ones = r10 % 5;
putchar('\n');
printf(" %d \
, :", amount);
printf("%d () 20 ", twenties);
printf("%d () 10 ", tens);
printf("%d () 5 ", fives);
printf("%d () 1 ", ones);
}
, , 57, :

57 , :
2 () 20
1 () 10
1 () 5
2 () 1
. 6.2 .
twenties = amount/20. amount, twenties int, . , 20 amount. , . twenties ,
, 20 , .

www.books-shop.com

. 6.2. ,
, , 20 , :
? , .
20 , ,
10 . .
, %. r20 = amount % 20 r20 , amount 20.
, , 20,
. 20 40 , 17 . 17 tens.
, twenties, tens fives.
ones 5.


,
. , float, , , float. :

main()
{
float cost, shipping, total;
cost = 56.09;
shipping = 4.98;
total = cost + shipping;
printf("
%.2f \
", total);
}
, , :
61.07
. . :

main()
{
int total;
float cost, shipping;
cost = 56.09;
shipping = 4.98;
total = cost + shipping;
printf("
%d ", total);
}

www.books-shop.com

float (cost shipping),


total. ,
61.07, total ,
. %d 61.

. 6.3. ,

, ,
(.6.3). ,
60 (56+4).
, float:

main()
{
int cost, shipping;
float total;
cost = 56;
shipping = 4;
total = cost + shipping;
printf(" %.2f \
", total);
}
, total %f,
. ,
, 60.00.
float int ,
.
:

main()
{
int shipping;
float total, cost;
cost = 56.09;
shipping = 4;
total = cost + shipping;
printf(" \
%.2f ", total);
}
int float,
foat (60.09).
. : ,
, float, , , () . .6.4 , . float.
, .

www.books-shop.com

. 6.4.

, .
. :

main()
{
int count;
count = 5;
printf(" %d", count+19);
}
:

24
printf() count+19.
() .
%d.
. ,
count+19, count 5. , , .
, , :

printf("%d", count+number);
printf("%d", 16-4);
printf(%f", amount*TAX_RATE);
, .
, ,
. :

main()
{
float cost, shipping;
printf(" : ");
scanf("%f", &cost);
printf(" : ");
scanf("%f", &shipping);
printf(" %f", cost+shipping);
}

printf(" %f", cost+shipping);


, :

total = cost + shipping;


/*total float*/
printf(" %f", total);
. , , ,
. , printf(). , -

www.books-shop.com

, , , , cost+shipping.
, , .
, .
, -
, - .
, .


, .
, . ,
, . , , ( ).
,
, .
, . , *.
, , .
:

a = 1 + 4 / 2 * 5 + 3;
* , , , , .
(..)
, ,
15.5 (.6.5).
, 14 ( .6.6 , ). , ,
.

. 6.5.

www.books-shop.com

. 6.6.
.
6.3 ,
. , . , , ?
6.3. .

/*average.c*/
main()
{
float number_1, number_2, number_3, average;
printf(" : ");
scanf("%f", &number_1);
printf(" : ");
scanf("%f", &number_2);
printf(" : ");
scanf("%f", &number_3);
average = number_1 + number_2 + number_3 / 3;
printf(" %f", average);
}

average = number_1 + number_2 + number_3 / 3;


, :
1.
2.

number_3 3.
number_1 number_2 .

100, 233.33. :

average = (number_1 + number_2 + number_3) / 3;


. , , , . , , 3 (.6.7).
! .

%RRNVVKRS

piracy@books-shop.com

. 6.7.

. 6.8.
, (.6.8). .
. , , 40- . , 40 , ,
40 ,
:

40 * rate
/* */
hours - 40 /* */
rate * 2
/* */
rate 1 , hours . , . , :

total = 40 * rate + hours - 40 * rate * 2


, 48 , 10 . :

40 * rate 400
40 * rate 400
400 * 2

800

400 + 48 448
448 800 352
, 352 . , : total = (40 * rate) +((hours - 40) * (rate * 2))
( 6.4). , . , .
. :

40 * rate

400

hours 40

rate * 2

20

8 * 20

160

400 + 160
560
6.4. .

/*payroll1.c*/
main()
{

www.books-shop.com

float rate, hours, total;


printf(" : ");
scanf("%f", &rate);
printf(" : ");
scanf("%f", &hours);
total = (40 * rate) + ((hours -40) * (rate*2));
printf(" : %f", total);
}
, , . 6.5 ,
, ,
.
, . .
6.5. , .

/*payroll2.c*/
main()
{
float rate, hours, total, regular, extra, d_time, overtime;
printf(" : ");
scanf("%f", &rate);
printf(" : ");
scanf("%f", &hours);
regular = 40 * rate;
extra = hours - 40;
d_time = rate * 2;
overtime = extra * d_time;
total = regular + overtime;
printf(" : %f", total);
}


. , .
.

, ,
. :

variable = variable + 1
, ,
, .

. 6.9.
, (.6.9). , . , :

www.books-shop.com

, , .6.10. count, , :

int count;
count=0;

count = count + 1;

. 6.10.
:

count = 0 + 1
count, 0, , 1.
1, count.
0 1. :

count = count + 1;

count = 1 + 1
count, 1, 1.
2, . , count ().
, . count , 1,

count = count + 2;
: 1, 3, 5, 7 . count,
, count = count + 5, , count = count + 10, .
, :

variable = variable - 1
().


,
. ++variable . ,

variable = variable + 1;
, :

/*count.c*/
main()
{
int count = 0;
printf(" \
count %d", count);
printf(" \
count %d", ++count);
printf(" \
count %d", count);
}
:

count 0
count 1
count 1

www.books-shop.com

printf() count 1.

count = count + 1;
printf("
count %d\n", count);

.
, . , ++count ,
:

printf("
count %d\n", count+1);
count+1 , count.
, , ,
. , , 6.6.
6.6. .

main()
{
int count = 0;
printf(" \
count %d", count);
printf(" \
count %d", count+1);
printf(" \
count %d", count);
}
:

count 0
count 1
count 0
count+1 1,
. printf()
, 0.
, ,
, . , count, number:

number = ++count;
:

count = count + 1;
number = count;
++ :

++number;
++ ,

variable++;
. 6.6:

main()
{
int count = 0;
printf(" \
count %d", count);
printf(" \
count %d", count++);
printf(" \
count %d", count);
}
++ count ,
printf(). printf() , , , :

count 0
count 0
count 1
printf(), 0
1 printf().

www.books-shop.com

,
, :

main()
{
int number, storage;
puts(" :");
scanf("%d", &number);
storage = number++;
printf(" : %d\n", storage);
printf(" : %d", number);
}

storage = number++;
, -, number storage, -,
number .
, .
:

--variable
1

variable- 1

. , , , (
) . :

variable = variable + other_variable;


, .
:

int total, number;


total = 0;
scanf("%d", &number);
total = total + number;
, number 10.

total=total+number;
total 10, ,
:

total=0+10;
, scanf()
, number 15:

scanf("%d", &number);
total = total + number;
:

total = 10 + 15;
number.
6.7 ,
. :

average = (A + B + C) / 3
, , ( ). 9 .
6.7. ,
.

/*average1.c*/
main()
{
float number, total, count, average;
total = 0.0;
count = 0.0;
printf(" : ");
scanf("%f", &number);
total += number;
++count;

www.books-shop.com

printf(" : ");
scanf("%f", &number);
total += number;
++count;
printf(" : ");
scanf("%f", &number);
total += number;
++count;
average = total / count;
printf(" %f", average);
}


+=

total += amount total = total + amount

total = discount total = total discount

*=

total *= tax_rate total = total * tax_rate

/=

total /= count

total = total / count

%=
total %=count
total = total % count
, , . , , .6.11. .

total *= rate;

total = total * rate;

. 6.11.


, . ,
( ) . , , . , Clear .
6.7, . , , total, 1827.

total = 0;
, 75, :

total = 1827 + 75
0 , :

total = 0 + 75

www.books-shop.com


, , , , , . , ,
, , .
, , .


(. 6.4). , , , , , , , 40 , , , . , . , ?
, 40, .

total = (40 * rate) +((hours - 40) * (rate * 2));


-, - 40 , 40, 40 * rate. -, hours - 40 . ,
, .
,
, 40.
.
, .


, . , , , . - ,
, ,
.
, , (. 6.2).
:

twenties = amount / 20;


r20 = amount % 20;
tens = r20 / 10;
r10 = r20 % 10;
fives = r10 / 5;
ones = r10 % 5;
, :
(/),

. 6.12. 6.2
(%). ,
. .6.12
.

www.books-shop.com

. , 50 ,
, :

fifties = amount / 50;


r50 = amount % 50;
twinties = r50 / 20;
r20 = r50 % 20;
, .


, , printf(). , . , ,
printf(), , .
, printf()
, .
. , :

regular 400
extra -2
d_time 40
extra, , . , -
printf(), . .

1.
2.

/ %?
? , ?
3. ?
4. ?
5. .
6. ?
7. count=count+1 count++.
8. ?
9. .
10. --count count--?

1.

5.

, ,
2000 .
,
.

(F) (C). C=(5.0/9.0)(F32).
3 , , .
, :

6.
7.
8.
9.
10.
11.
12.
13.

#define TAX_RATE 0.06


main()
{
float cost, total;
printf(" : ");
scanf("%f", &cost);
printf(" : ");
scanf("%f", &shipping);

2.
3.
4.

www.books-shop.com

14. total = cost + cost * tax_rate + shipping;


15. printf(" %f", total);
}

7

,
. ,
, , - , ,
( , ).
. , , main(), . , , , .
, . ,

++. ,
.
, . ,
,
. , , . ,

. 7.1.
, (.7.1).
. , - . ,
, . , . .
main() !


main().

www.books-shop.com

. 7.2.
, main() (.7.2): , , , , .
, ,
, .
return (0); .
.
. 7.3 , .
.

answer();
main() answer() , , ,
answer()

. 7.3.
, .
puts(). answer() , , . puts() answer() return(0); , main() answer(). return(0); main() .
. 7.3 :

puts(" ?");
puts(" Enter");
%RRNVVKRS

piracy@books-shop.com

pause = getchar();
answer();
puts(" 1024 .");
return(0);
return(0);
, , .
return(0); , .
7.1 .
question() main(). answer()
question(). return(0) answer() question(),
main(). , , .
.

main()

Microsoft. question()
Enter.

question()

- Windows.

answer()

main()

7.1. , .

/*quiz2.c*/
main()
{
puts(" .\n");
question();
puts(" .\n");
return(0);
}
question()
{
int move_on;
puts(" \
Microsoft.\n");
puts(" \
Enter.\n");
move_on = getchar();
answer();
return(0);
}
answer()
{
puts(" - Windows.\n");
return(0);
}

www.books-shop.com

. main()
,
. 7.2, .
main() . , main().
7.2. , main() .

/*quiz3.c*/
main()
{
welcome();
question();
answer();
the_end();
return(0);
}
welcome()
{
puts(" .\n");
return(0);
}
question()
{
int move_on;
puts("
Microsoft.\n");
puts("
Enter.\n");
move_on = getchar();
return(0);
}
answer()
{
puts(" - Windows.\n");
return(0);
}
the_end()
{
puts(" .\n");
return(0);
}
. , , answer().
. 7.2, , :

main()

www.books-shop.com

answer()
welcome()
the_end()
question()
,
main().


, main(), ,
. . , .

()
. .
, :

printf(" ");
move_on = getchar();
, , . , , . getchar() , , , pause():

pause()
{
int move_on;
printf(" ");
move_on = getchar();
return(0);
}
move_on pause(). ,
, . , . .
. - ,
. ,
main().

main()
{
int move_on;
puts(" \n");
/* , */
pause();
}
pause()
{
printf(" ");
move_on = getchar();
return(0);
}

www.books-shop.com

, pause()
move_on. main(),
main() . move_on pause(),
.

. 7.4.
, , . .7.4 age, main(), spouse().
( ) , ,
. , . , age , , printf();. printf(), spouse(),
age, . printf(),
main(), age, main(). ,
age.

()
, . . , main():

int temp;
main()
temp , , . , 7.3. temp
main(), convert(), freeze() boil(). ,
main(), .
celsius. celsius convert(), , , , , convert().
7.3. , .

/*f_to_c.c*/
int temp;
main()
{

www.books-shop.com

printf(" : ");
scanf("%d", &temp);
convert();
freeze();
boil();
}
convert()
{
float celsius;
celsius = (5.0 / 9.0) * (temp - 32);
printf("%d
%6.2f \
\n", temp, celsius);
return(0);
}
freeze()
{
printf(" %d
\n", temp-32);
return(0);
}
boil()
{
printf(" %d
\n", 212-temp);
return(0);
}
, , ,
.


() ,
. .
,
.
, ,
, . , , , .
, , , , . :

myfunc()
{
static int count;
}
. , , -

www.books-shop.com

, .
, . , .
, , .
, 7.4. .
doit() , 0, printf(). ,
.
, 0,
:

autovar 0 statvar 0
autovar 0 statvar 0
autovar 0 statvar 0
autovar 0 statvar 0
7.4. , .

/*stat.c*/
main()
{
doit();
doit();
doit();
doit();
}
doit()
{
int autovar = 0;
int statvar = 0;
printf(" autovar %d statvar \
%d\n", autovar, statvar);
++autovar;
++statvar;
return(0);
}
doit(), :

static int statvar = 0;


:

autovar 0 statvar 0
autovar 0 statvar 1
autovar 0 statvar 2
autovar 0 statvar 3
statvar , 0. statvar , .
statvar , 1, ++statvar;. 1 0 .

www.books-shop.com

,
.


, . , puts(), , *, , , .
puts() , :

puts("!");
"!" puts() ,
.
. ,
, . , , **.
* . , ,
, , , . . (..)
** ,
. (..)
, , . , puts() , :
. , , ,
. ,
(.7.5), ,

. 7.5.
, , .
, *.
,
, .
, . :

main()
{
int count;
count = 5;
doubles(count);
}
* , .

www.books-shop.com

. 7.6.

doubles(num)
int num;
{
printf("%d", num * 2);
return(0);
}

doubles(count);
main() count. doubles() num. num, , , count. , , num,
, doubles().
. ,
, . , ,
, .
, .
doubles() (
.7.6).

++
++ :doubles(int num)
1.

doudles(), count.

2.

doubles() num 5.

3.


printf().

7.5 ,
, . area() .
, main(),
area() :

area(length, width, fnum);

www.books-shop.com

7.5. .

/*area.c*/
main()
{
float length, width;
int fnum;
printf(" : ");
scanf("%d", &fnum);
printf(" : ");
scanf("%f", &length);
printf(" : ");
scanf("%f", &width);
area(length, width, fnum);
}
area(size, wide, num)
float size, wide;
int num;
{
float area;
area = size * wide;
printf(" %d %f.2f", num, area);
return(0);
}
, . length
size, width wide, fnum
num. :
float, int .
area() - .
area() area.
:

area(width, length, fnum);


width size, length wide. - ,
. , ,
, . ,
:

area(fnum, width, length);


fnum wide, width size, num length. , , . , .
, 7.6. : (cost) (discount). cost
discount price() amount mrkdown. reduce
net price() .
7.6. .

/*discount.c*/
main()

www.books-shop.com

{
float cost, discount;
printf(" : ");
scanf("%f", &cost);
printf("
( ): ");
scanf("%f", &discount);
price(cost, discount);
}
price(amount, mrkdown)
float amount, mrkdown;
{
float reduced, net;
reduced = amount * mrkdown;
net = amount - reduced;
printf("
%.2f ", net);
return (0);
}
price() ,
.
:

: 100
( ): 0.05
95
, :

price(discount, cost);
, float float. , , : discount
amount, cost mrkdown.
cost 100, discount 0.05, , , 10000 .
, 4.95, 95.50.


, . , getchar(), :

key = getchar();
getchar(), .
key.
.
, .
,
. :

main()
{
char letter;
letter = getlet();
%RRNVVKRS

piracy@books-shop.com

putchar('\n');
printf(" %c", letter);
}
char getlet()
{
printf(" : ");
return(getchar());
}

letter = getlet();
getlet().
letter main().
,
.

char getlet();
, , getlet(),
. , , return(). ,

return(getchar());

++
ANSI,
, . void:

void myfunct()
,
. ++
void , .
. , getchar() ,
.
getchar()

www.books-shop.com

. 7.7. ,
, return() printf() ,
letter, main().
return() . ,
, return() getchar().
, , . ,
.7.7, ,
.

number = square(value);
square(), value.

++
++ ( ) .
, main(). :

void square(int num);


main()
, .

int square(num)
, square()
num. return() .

return(num * num)
num * num, number, square() main().

float
, , , , .
, int char, , , , int char.

www.books-shop.com

++
++,
. , :

int doubles(int num);


float doubles(float num);
doubles(), int, float.

. 7.8. float
, :
1.

float .

2.

main() , . .7.8 , .

float square();
, float.
, .
main():

main()
{
float number, value, square();
, main().

return() main()
, , return(0) main(). , , 0, ? : .
, main().
, return() . -

www.books-shop.com

return() , , ,
. return(0) , . ,
. , *.


, , #define, . ,
#define PI 3.14, 3.14 PI.
#define , .
- , .
, ENTER printf():

#define ENTER printf(", : ")


, , printf(),
ENTER:

#define ENTER printf(", : ")


main()
{
int age, size;
ENTER;
scanf("%d", &age);
ENTER;
scanf("%d", &size);
}

. 7.9.
, :
, main() , printf()
(.7.9).
, , ,
*. ,
. , ENTER , , .

www.books-shop.com

,
, , . , , CONVERT :

#define ENTER printf(",


: ")
#define CONVERT(temp) (5.0 / 9.0) * (temp - 32)
main()
{
float climate;
ENTER;
scanf("%f", &climate);
printf(" %f
\n", CONVERT(climate));
}
* . (..)

. 7.10.
#define CONVERT, . , CONVERT, (5.0 / 9.0) * (temp - 32)
temp. , , , (.7.10). 212, CONVERT printf()
(5.0 / 9.0) * (212 - 32).
, . ,
, ENTER . ,
, , .


, , , ,
main(). , , . , - ,
, -

www.books-shop.com

main(). , , ,
.
, , main(), , - , .
, : ? , .

?
, , . , , 7.3, temp ,
, .
, .
, . ,
. , - , .
.
, . , printf() . .


, , , .
.
, , . , 7.6,
scanf().
, , , 0.05 5 .
, :

( ):
, 5 0.05. , , 5 , 500 ,
, , . .
, . , ,
, . ,
8.

1.


, ?

2.

main()?

3.

, ?

4.

5.

6.

www.books-shop.com

7.

8.

9.

10. float?

1.

, ; .

2.

, ,
.

3.

2 , , main() .

4.

, :

5.

dothis()

6.

7.

puts(" ");

8.

main()

9.

return;

10.

11.

main()

12.

13.

puts(" ");

14.

return(0);
}

www.books-shop.com

8

, . , , , , , . .
, , , , .
, .
/C++, . , , . ,
, .
, , . , , ,
, .
, , , , .

If
if. , ,
. if , , (if - ). if :

if (condition)
instruction;
: ( ), (.8.1). , , if, ,
, .

. 8.1. if
, , if,
. if ,
:

, ;

, . , .

if : , .
:

www.books-shop.com

==

if (tax == 0.06)

>

if (hours > 40)

<

if (hours < 40)

>=

if (salary >= 10000)

<=

if (cost <= limit)

!=

if (count != 1)

: , ,
, (==) .
, , , , . (=)
.
if :

if (time > 11)


puts(" , .");
: time 11,
. time 11, .
if , . , , :

gets(name);
if (name == "")
puts(" ");
10.
, 8.1, if.
, . .
, 40000 . :

if (cost > 40000.00)


luxury = cost * 0.005;
8.1. .

/*luxury1.c*/
main()
{
float cost, tax, luxury, total;
luxury = 0.0;
printf(" : ");
scanf("%f", &cost);
tax = cost * 0.06;
if (cost > 40000.00)
luxury = cost * 0.005;
total = cost + tax + luxury;
printf("
%.2f", total);
}

www.books-shop.com

: cost 40000, luxury , cost, 0.5 . , ,


, , if.
, luxury , 0, , , tax. , tax
, tax = cost * 0.06,
luxury , 40 . , . , luxury ,
, . ,
, if,
, .
, ,
, . , 40 , 40 1 . 40 , . , 40
, :

if (cost >= 40000.00)



, , .


if . , , . , . .
, 8.2.
.
,
. , ,
. , , if. .
8.2. .

/*luxury2.c*/
main()
{
float cost, tax, luxury, total;
luxury = 0.0;
printf(" : ");
scanf("%f", &cost);
tax = cost * 0.06;
if (cost > 40000.00)
{
luxury=cost*0.005;
printf("
%.2f\n", luxury);
}
total = cost + tax + luxury;
printf("
%.2f", total);
%RRNVVKRS

piracy@books-shop.com

if...else
if , . , , . , , ,
- , ,
: .
if:

if (cost > 40000.00)


{
luxury = cost * 0.005;
printf(" \
%.2f", luxury);
}
if (cost < 40000.00)
puts(" \
");
. , ,
: 40
, . , , ,

. 8.2. , else
, else ( ):

if (condition)
instruction;
else
instruction;
: , , if, , else. , else, , . , ,
, if. else.
, , .8.2. , -

www.books-shop.com

luxury , if .


7 ,
. c if,
. , 8.3,
, , , , , .
, , , . , ,
. , , ask():

ask("9+5= ", 14);


8.3. .

/*score*/
int correct, wrong;
main()
{
char question[15];
int answer;
correct = 0;
wrong = 0;
ask("4 + 5 = ", 9);
ask("6 + 2 = ", 8);
ask("5 + 5 = ", 10);
ask("4 + 7 = ", 11);
printf (" : %d.\n", correct);
printf (" : %d.\n", wrong);
}
ask(quest, ans)
char quest[15];
int ans;
{
int guess;
printf("%s", quest);
scanf("%d", &guess);
if (guess == ans)
++correct;
else
++wrong;
return(0);
}


, if
. ,

www.books-shop.com

. .
, 8.4. ,
. , , ,
, 6 .
8.4. .

/*iftax.c*/
main()
{
int taxable;
float cost, tax;
tax = 0.0;
printf(" : ");
scanf("%f", &cost);
printf(" Y,
, N, : ");
taxable = getchar();
if (taxable == 'Y')
tax = cost * 0.06;
printf("\n
%f", cost + tax);
}
, . , Y, . y, , , if
.

getchar()
,
getchar(), . , , Enter.
, , getch() getche(). , ,
, Enter.
, getchar()
scanf() . 5 ,
, (
), scanf() , .
getchar() ,
. ,
scanf() gets() , , ,
getch() getche().

getchar(). #include ,
fflush(stdin) getchar().
fflush() , .

www.books-shop.com

,
Y. if.
, :

if (taxable == 'Y' || taxable == 'y')


: taxable Y y, ... ,
, ,
. , ,
. ,
taxable . (taxable == 'Y' || 'y') .
: (||), (&&) (!). , if ( ) . , . , if , .

( ), . ,
,
(8.5).
8.5. .

/*twovars.c*/
main()
{
int depents;
float income;
puts(" ");
scanf("%f", &income);
puts(", ");
scanf("%d", &depents);
if (income < 20000 && depents > 2)
puts("
");
}
if : income depents. ,
if, , income
20 , depents . , , 20001 , puts() .
, , ,
. , .

if (taxable == 'Y' && taxable == "y")


, .
, , ,
.
, , 40 60 . :

if (cost >= 40000.00 && cost <= 60000.00)


if, ,
.
40000 60000 , (.8.3).

www.books-shop.com

, , cost . ,

. 8.3. ,
, - , .
if , , ,
:

if (income <= 20000.00 || income >= 60000.00)


puts(" ");
, ,
, . () ,
, if, 0. , , , . , : ,
count , 0:

int count;
count = 0;
if (! count) puts(" ");

if (count == 0)
, : ,
count , :

int count;
count = 1;
if (count) puts(" ");
if (count) if (count != 0), , . , .

if
if...else . , .
if. .
, if :

if (income > 100000)


if (status == 'S')
taxrate = 0.35;

www.books-shop.com

, ,
taxrate . :

if (income > 100000 && status == 'S')


taxrate = 0.35;
, ,
&&, , if. , : income
100000 status "S", taxrate 0.35.
, if ,
. , , if, ,
. :

main()
{
float income;
scanf("%f", &income);
if (income >= 20000.00)
if (income <= 100000.00)
puts("
22%");
else
puts("
20000$ - 15%");
}
, -, : income 20000 100000, , income
20000, .
, , , .
, 20000, puts(),
100000, 15.
, else if , . , else if, . else
if, , income
20000 100000.
, ,
:

if (income >= 20000.00)


{
if (income <= 100000.00)
puts("
22%");
}
else
puts(" 20 , \
15%");
, else
if.
if:

www.books-shop.com

if (income >= 20000.00 && income <= 100000.00)


puts("
22%");
else
puts(" 20 , \
15%");
if
.
, , . , , , 100 . . , , ,
income, 150000, . ,
?
8.6.
if...else. , , . (income < 20000.00),
puts(), . else if,
(income < 100000.00) , .
8.6. .

/*brackets*/
main()
{
float income;
puts(" : ");
scanf("%f", &income);
if (income < 2000.00)
puts("
15%");
else
if (income < 2000.00)
puts("
22%");
else
puts("
35%");
}
, , if. if...else
, . , ,
, if .
,
if...else.

switch/case/default
, if...else .
switch. switch ,
, ,
. 8.7.

www.books-shop.com

8.7. , switch.

/*switch.c*/
main()
{
int answer;
puts(" : \n");
puts("1. ,
\n");
puts("2. ,
\
\n");
puts("3. ,
\n");
puts("4. \n");
puts(" \n");
answer = getchar();
putchar('\n');
switch (answer)
{
case '1':
puts(" , , \
\n");
break;
case '2':
puts(" ,
\n");
puts(" \n");
break;
case '3':
puts(" ,
\n");
puts("
\n");
puts("
\n");
break;
case '4':
puts(" ,
, - 3\n");
break;
default:
puts(" ,
\

www.books-shop.com

\n");
}
switch int char, , , case.
case , .
, ,
.
case '1':, , , switch '1'.
, case.
, case, ,
. break case switch, , case,
switch .
case, , default. . default break, switch. default , ,
.
break, , case, , break.
,
, , :

case 'Y':
case 'y': puts (" \"\"");
break;
case 'N':
case 'n': puts (" \"\"");
break;


, case .
case 12.87: case "Adam".
10. float, ,
- .
if...else. 8.8 , if...else level, float, income.
8.8. if
.

/*switch.c*/
main()
{
float income;
char level;
printf(" : ");
scanf("%f", &income);
if(income<= 20000.00)
level = '1';
else
if(income<= 60000.00)
level = '2';

www.books-shop.com

else
if(income<= 120000.00)
level = '3';
else
level = '4';
switch(level)
{
case '1':
puts(" = 15%");
break;
case '2':
puts(" = 28%");
break;
case '3':
puts(" = 32%");
break;
case '4':
puts(" = 36%");
break;
default:
puts("
");
}
}
, if...else
switch, if...else. , , , , , ,
, , if,
switch , , . case , . if...else , .


, : if, if,
switch. 8.9 , (
switch) if...else.
. ,
, , , .
, ,
, ,
.
8.9. , if...else.

/*quiz4.c*/
main()
{
int answer;
puts(" : \n");
puts("1. , \n");
%RRNVVKRS

piracy@books-shop.com

puts("2. ,
\
\n");
puts("3. ,
\n");
puts("4. \n");
puts(" \n");
answer = getchar();
putchar('\n');
if(answer == '1')
{
puts(" , .\n");
puts("
\n");
}
else
if(answer == '2')
{
puts(" ,
\n");
puts(" \n");
}
else
if(answer == '3')
{
puts(" ,
\n");
puts("
\n");
puts("
\n");
}
else
if(answer == 4)
puts(" ,
, - 3\n");
else
puts(" ,
\n");
}
, . , , , . , , .

www.books-shop.com


, , . , 7 , . , 5 0.05 .
:

if (mrkdown >1)
mrkdown = mrkdown / 100;
6 . , , ,
40- . , , 8.10. if : ,
, ,
40. , , ,
.
8.10. .

/*allhours.c*/
main()
{
float rate, hours, total,
regular, extra, d_time, overtime;
printf(" : ");
scanf("%f",&rate);
printf(" : ");
scanf("%f", &hours);
d_time = rate * 2;
if(hours <= 40)
{
regular = hours * rate;
extra = 0.0;
overtime = 0.0;
total = regular;
}
else
{
regular = 40 * rate;
extra = hours -40;
overtime = extra * d_time;
total = regular + overtime;
}
printf("
: %.2f\n", regular);
printf("
: %.2f\n", extra);
printf("
: %.2f\n", d_time);

www.books-shop.com

printf("
: %f.2\n", overtime);
printf("
: %f.2\n", total);
}
( )
d_time. if, , . , - , if.
, . , -
(, 2500 ). , , .

1.

= ==?

2.

3.

else?

4.

,
?

5.

if.

6.

switch if?

7.


switch?

8.

, if
?

1.

, , , .

2.

,
, 1 100.

3.

, , , :
0, 0 50, 51 100, 101 150, 150.

4.

, lownum highnum. lownum


highnum. , ,
lownum, highnum. .

5.

, :

6.

main()

7.

8.

int age;

www.books-shop.com

9.

printf( );

10.

scanf("%f", &age);

11.

if age < 18 then

12.

puts("

13. ");
14.

else

15.

if age > 18 then

16.
17.

puts("
");
}

www.books-shop.com

, ,
.
. , .
++ , ,
:

for;
do...while;
while.
,
.

for
for , , . .9.1. ,
, for. .
for ,
1 10, .

main()
{
int repeat;
for (repeat = 1; repeat <= 10; repeat++)
printf("%d\n", repeat);
}
repeat, *. , .
for :
repeat=1

repeat

repeat <=
10

, repeat
10

repeat++

repeat

* . (..)

. 9.1. for

www.books-shop.com

++
++
for:

for (int repeat=1; repeat <= 10; repeat++)


repeat.
, repeat ,
1. , , 10.
, , ,
.

(.9.2). , , .

. 9.2.
, 11. , repeat <= 10 , .
.
:

main()
{
int repeat;
char letter;
puts(" 10 ");
for(repeat = 1; repeat <= 10; repeat++)
letter = getchar();
}
getchar() 10 , ,
repeat 1 11. . :

for (repeat = 101; repeat <= 110; repeat++)


letter = getchar();
}
10 , 101 110. , .


for , :

for (delay = 1; delay <= 1000; delay++);

www.books-shop.com

, , , , 1000 ,
, delay 1 1001. .
, , . , Enter .
, , . . 1
2 *. , , .
* . 1 . (..)


. ,
, .
101 ( 32 132) :

main()
{
int temp;
float celsius;
puts(" \t \n");
for (temp = 32; temp <= 132; temp++)
{
celsius = (5.0 / 9.0) * (temp - 32);
printf("%d\t\t%6.2f\n",
temp, celsius);
}
}
printf()
. (\t\t) . (%6.2)
, , .
.
, , . :

main()
{
int temp, repeat;
float celsius;
puts(" \t \n");
temp = 10;
for (repeat = 1; repeat <= 10; repeat++)
{
celsius = (5.0 / 9.0) * (temp - 32);
printf("%d\t\t%6.2f\n",

www.books-shop.com

temp, celsius);
temp += 10;
}
}
. , , temp. 10 : 10 20, 30 , 100.


, , for, ,
. . ,
, , , , :

main()
{
int temp, start, end;
float celsius;
printf("
: ");
scanf("%d", &start);
printf("
: ");
scanf("%d", &end);
puts(" \t \n");
for (temp = start; temp <= end; temp++)
{
celsius = (5.0 / 9.0) * (temp - 32);
printf("%d\t\t%6.2f\n",
temp, celsius);
}
}
,
. start end for
. , temp end. , 20 43,
20 43 . 24
, .


for , , . .
for , .
:

main()
{
int row, column;
for (row = 1; row <= 10; row++)
{
for (column = 1; column <= 10; column++)

www.books-shop.com

printf("*");
putchar('\n');

/*

, */
}
}
10 , 10 . row column. row
. , , 10 , column

. 9.3.
( , , : row - , column ).
.9.3 .

for (column = 1; column <= 10; column++)


printf("*");

www.books-shop.com

. 9.4. ,
100 : 10 10
, 10 10 . , , .9.4.
putchar('\n');. ,
, . , , .
9.1 .
, .
, row column.
9.1. .

/*timestab.c*/
main()
{
int row, column;
puts("\t\t \n\n");
for(row = 1; row <= 10; row++)
%RRNVVKRS

piracy@books-shop.com

{
for(column = 1; column <= 10; column++)
printf("%6d", row * column);
putchar('\n');
}
}

. 9.5. ,
, TIMESTAB.C, .9.5.
, :

main()
{
int row, column;
for (row = 1; row <= 10; row++)
{
for (column = 1; column <= row; column++)
printf("*");
putchar('\n');
}
}
, .9.6.

www.books-shop.com

. 9.6.
, . ,
, :
, , .
. , row
.
,
. ,
. .
, , for.
100 100 , , 10 !

do...while
do...while , ,
, , , .
do...while .9.7.
, , , , while. , ,
, , , .

. 9.7. do...while
do...while, , . ,
Reset Ctrl+Alt+Del.

www.books-shop.com

do...while , ,
:

main()
{
int temp;
float celsius;
char repeat;
do
{
printf(" : ");
scanf("%d", &temp);
celsius = (5.0 / 9.0) * (temp - 32);
printf("%d
%6.2f \n",
temp, celsius);
printf(" ?");
repeat = getchar();
putchar('\n');
}
while (repeat == 'y' || repeat == 'Y');
}
,
getchar() scanf(), getchar()
getch() getche(),
fflush(stdin) .
8.
, ,
do...while. , Y
y. , , , .
do...while , . ,
, . , , , 0
1, , do...while, ,
:

do
{
printf(" :");
scanf("%f", &discount);
}
while (discount < 0 || discount >= 1);
, while , , . , . .
,
, , . ,
, , . .
, :

www.books-shop.com

main()
{
int count;
float discount;
count=0;
do
{
printf(" : ");
scanf("%f", &discount);
count++;
}
while ((discount < 0 || discount >= 1)
&& count < 20);
if (count == 20)
puts ("");
}
20 , . count
.
do...while,
. , ,
Y:

main()
{
int a;
do
a = getchar();
while (a != 'y' && a != 'Y');
}

www.books-shop.com

. 9.8. do...while

do
do...while , .
, , , ,
- , ,
.9.8. ,
0 100. ,
. ,

while (repeat == 'y' || repeat == 'Y');


- , ,
.

while
while , , . while :

while (condition)
instruction;
.9.9. :

while (condition)
{
instructions;
}

www.books-shop.com

. 9.9. while
, do, while , ,
do...while, , . , .
, . , . , : , do, while ,
, do...while,
, . ,
.

printf(" : ");
scanf("%f", &discount);
while (discount < 0 || discount >= 1)
{
printf(" ");
scanf("%f", &discount);
}


: while, for do...while,
. , , while, for.
, 0 100. .

/*mixed.c*/
main()
{
int temp, count;
float celsius;
for (count = 1; count <= 10; count++)
{
printf("
1 100: ");
scanf("%d", &temp);
while (temp < 0 || temp > 100)
{
printf(",
: ");
scanf("%d", &temp);
}
celsius = (5.0 / 9.0) * (temp - 32);
printf("%d
%6.2f \n",

temp, celsius);

www.books-shop.com

}
}
for 10 . , .


. , ,
.
-, , for, do...while while .
:

, , , ?

, for. ,
:

, , , ?

, do...while, while.
,
. ,
do...while while, for. , , ,
. , while.
? , - ,
. ,
, ,
.
, , , , Y (y), .
, , , , . , , 9.2.
. ,
, .
, . , , while .
,

if (count > 0)
, . ,
, .
9.2. .

/*average.c*/
main()
{
float number, total;
int count;
total = 0.0;
count = 0;
printf(" .
: ");
scanf("%f", &number);
while (number >= 0 )

www.books-shop.com

{
count++;
total += number;
printf(" .
: ");
scanf("%f", &number);
}
if (count > 0)
{
number = total / count;
printf(" = %.2f = %d
= %.2f", number, count, total);
}
}
, , :

while (a >= 0)
{
scanf("%f", &a);
count++;
total += a;
}
,
, while .
,
. , , , do...while, 9.3.
-

total = 0.0;
count = 0;
9.3. .

/*ave_bad.c*/
main()
{
char repeat;
float number, total;
int count;
total = 0.0;
count = 0;
do
{
printf(" .
: ");
scanf("%f", &number);
while (number >=0)
{

www.books-shop.com

count++;
total += number;
printf(" .
: ");
scanf("%f", &number);
}
if(count > 0)
{
number = total / count;
printf(" = %.2f = %d
= %.2f", number, count, total);
printf("
?");
repeat = getchar();
putchar('\n');
}
}
while ((repeat == 'y') || (repeat == 'Y'));
}
- , ,
, . , ,
, . , .
.
:

do
{
total = 0;
count = 0;
. , while, do...while.


(flag) , ,
- , , , , . -
, , , .
, do...while :

do
{
printf("
0 100: ");
scanf("%d", &temp);
}
, . , , -

www.books-shop.com

, . , ,
9.4.
9.4. , .

/*flag.c*/
main()
{
int temp;
float celsius;
char repeat;
char flag;
do
{
flag = 'n';
do
{
if (flag == 'n');
printf("
0 100: ");
else
printf("
, : ");
scanf("%d", &temp);
flag = 'y';
}
while (temp < 0 || temp > 100)
celsius = (5.0 / 9.0) * (temp - 32);
printf("%d
%6.2f \n",

temp, celsius);

printf(" ?");
repeat = getchar();
putchar('\n');
}
while (repeat == 'y' || repeat == 'Y');
}
flag 'n'. . 'n',
, .
'n', .
, 'y'.
, , (flag == 'n')
, .
,
, . .
, ,
.
%RRNVVKRS

piracy@books-shop.com

,
int char. , , . 'n', , 'y', . , .

break
,
, if...else.
, while.
9.5.
9.5. while break.

/*break*/
main()
{
int temp;
float celsius;
printf("
0 100.
555 : ");
scanf("%d", &temp);
while (temp != 555)
{
while ((temp < 0 || temp > 100)
&& temp != 555)
{
printf(",
: ");
scanf("%d", &temp);
}
if (temp == 555)
break;
celsius=(5.0/9.0)*(temp-32);
printf("%d
%6.2f \n",

temp, celsius);

printf(" ,
555: ");
scanf("%d", &temp);
}
}
: , . , . , 555.
, 0 100
555, , :

if (temp == 555)
break;

www.books-shop.com

break , , , while for .


, , . .
, , ,
.
, 555 , . , , . , , , ?
,
, .

1.


for, do while?

2.

for?

3.

for?

4.

5.

6.

break?

1.

8.10 ( 8)
, ,
.

2.

, 6-
,
1 50 ,

3.

4.

.06

1.06

.12

2.12

5.

, 0
25.

6.

, :

7.

*****

8.

****

9.

***

10.

**

11.

12.

**

13.

***

www.books-shop.com

14.

****
*****

15. , :

16.

main()

17.

18.

float row, column;

19.

puts("\t\t \n\n");

20.

for (row = 1; row <= 10; row++)

21.

22.

for (column = 1;

23.
24.

column <= 10; column+)


printf("%d", row * column);

25.

26.

putchar('\n');
}

www.books-shop.com

10

, ,
. , , , 31 , 31 , ,
. , , , , . ,
, .
, 31 31 .
, ,
(array). , .

, , , .
, , , . , -
, . ,
, , , .
, , , ( ). , . ,
, ,
, .
. ,
.
, , ,
, . , , , .
. , . puts() , .
. -, ,
-, , , , .


, , , .
.10.1. , , , 31 , :

int temps[31];
, , . , , main(), ( ), main(), .
,
.10.2. temps temps[0], temps[1],
temps[2] . , 0,
, , , 0 4. temp[5] . , , (subscript),
temps, temps .

www.books-shop.com

. 10.1.

. 10.2.
,
. , 10,
9. , , ,
.
:

char name[5];
gets() puts() , .


, . :

int temps[5] = {45, 56, 12, 98, 12};


, , :

temps[0]

45

www.books-shop.com

temps[1]

56

temps[2]

12

temps[3]

98

temps[4]

12

main(). main(), ,
:

int temps[5] = {45, 56, 12, 98, 12};


main()
{
static float prices[3] = {23.45, 34.56, 12.34};
main() ,

temps[0] = 45;
, . , , for:

main()
{
int temps[31];
int index;
for (index = 0; index 31; index++)
{
printf("
#%d: ", index);
scanf("%d", &temp[index]);
}
}
index , .
31 , .
:

#N
N , .
, index 0
30, 31, ( 0 30). index
0, temps[index] temps[0]. , , temps[index] scanf(),
temps[0]. for index ,
scanf() .
.10.1.
10.1. .

www.books-shop.com

, , , . ,
, .

. 10.3.
.10.3. 0, temps[0] 75.
, . temps[1] , temps[0].
, , , ,
. ,
:

for (index = 0; index < 31; index++)


temps[index] = 0;
, , .


, :
, . .
, , , .
, 10.1 ,
. -, ,
-, -

www.books-shop.com

. , for, index 0 .
10.1. .

/*array1.c*/
main()
{
int temps[31];
int index, total;
float avarage, celsius;
total = 0.0;
/* */
for (index = 0; index < 31; index++)
{
printf("
#%d: ", index);
scanf("%d", &temp[index]);
}
/* */
for (index = 0; index < 31; index++)
total += temps[index];
average = total / 31.0;
printf("
: %f\n\n", average);
puts(" \t\t \n");
/* */
for (index = 0; index < 31; index++)
{
celsius = (5.0/9.0)*(temps[index]-32);
printf("%d\t\t%6.2f\n",
temps[index], celsius);
}
}
, , , , ,
, 31. , ,
, ,
, .
, 10.1, ,
31. for do...while,

index = 0;
, index, , . for , for.
10.2. do...while .

/*array2.c*/
main()
{

www.books-shop.com

int temps[31];
int index, total;
float avarage, celsius, count;
total = 0.0;
/* */
index = 0;
do
{
printf(" #%d, \
555: ", index);
scanf("%d", &temp[index]);
}
while (index < 31 && temps[index-1] != 555);
/* */
index = 0;
do
{
total += temps[index];
index++;
}
while (index < 31 && temps[index-1] != 555);
count = index;
average = total / count;
printf("
: %f\n\n", average);
puts(" \t\t \n");
/* */
index = 0;
do
{
celsius = (5.0 / 9.0) * (temps[index] - 32);
printf("%d\t\t%6.2f\n",
temps[index], celsius);
index++;
}
while (index < 31 && temps[index-1] != 555);
}
, , , 555,

while (index < 31 && temps[index-1] != 555);


, index . , .
, 555,
.

www.books-shop.com

555 . ,
, ,
.
, do...while
, , 555.
index, , , . float,
float. , float- count, :

count = index;
average = total / count;
index, , ,
. ,
0 4, index .
, .
, , . while. 31 ( 0 30), index,
temps[31], 31,

. 10.4.
. , :

while (index < 31 && temps[index] != 555);


, , ,
(.10.4). , , .
31 , (index < 31) . ,
, , , .
, :

while (temps[index] != 555 && index < 31);


, 31 , ,
, .
.
, , , .
, 32, 31.


,
. :

/*highlow*/
%RRNVVKRS

piracy@books-shop.com

...
high = temps[0];
low = temps[0];
index = 1;
while (index < 31 && temps[index] != 555)
{
if (temps[index] > high)
high = temps[index];
if (temps[index] > low)
low = temps[index];
index++;
}
printf(" %d\n", low);
printf(" %d\n", high);
...
, high low .
. ,
high, low. , ,
. ,

. 10.5.
, . - high, , low, . .10.5 .


.
. , , , , .
, ,
999 .
:

www.books-shop.com

/*found.c*/
...
printf(" , : ");
scanf("%d", &num);
index = 0;
found = 0;
while (! found && index < 31 && temps[index] != 555)
{
if (temps[index] == num)
found = 1;
else
index++;
}
if (! found)
puts(" ");
else
printf("
%d\n", index);
...
, found. , ,
.
found .

while (! found)
: found . ,
found .
, , ( 0).
, , , , if, found 1. ,
found , . ,
index , , .
if ,
.
, while , .
, : ,
, 555.


, ++
. , , , ,
. , , , .
. ,
( ) . 10.3 , . , :

convert(temps)
10.3. .

www.books-shop.com

/*arr_pass.c*/
#define COUNT 31
main()
{
int temps[COUNT];
int index;
float celsius;
/* */
for (index = 0; index < COUNT; index++)
{
printf("
#%d: ", index);
scanf("%d", &temp[index]);
}
/* convert()*/
convert(temps);
}
/* */
convert(heat)
int heat[];
{
int index;
float celsius;
for (index = 0; index < COUNT; index++)
{
celsius = (5.0 / 9.0) * (temps[index] - 32);
printf("%d\t\t%6.2f\n",
heat[index], celsius);
}
}


K&R
,
++, . K&R- , , . , 11. ANSI- ++
,
.

++
, ++

www.books-shop.com

, , :

convert(heat[])
,
. ,
, :

convert(heat);
int heat[];
,
.

. 10.6.
,
convert() heat,
temps. .10.6 , convert()
heat . heat , temps,
temps .


, ,
. , . , :

, ;

, .

. , 20
, , , .
:

www.books-shop.com

int room[20] = {102, 12, 107, 43...


, . 102
12 , 107 43 . ,
, , .
. , .
.
, . ?
. , .
. , . , .
, , , , .
- .
10.4 , . room , , max , . , room,
, , max.
10.4. .

/*rooms.c*/
int room[10] = {102, 107, 109, 112,
115, 116, 123, 125, 127, 130};
int max[10] = {12, 43, 23, 12, 20, 15, 16,
23, 12, 15};
main()
{
int index, choice, num, rooms, flag, found;
rooms = 10;
puts("1. \n");
puts("2.
\n");
puts("3. \n");
printf(" 1 3: ");
scanf("%d", &choice);
if (choice == 1)
{
for (index = 0; index < rooms; index++)
printf(" #%d
%d \n", room[index], max[index]);
}
if (choice == 2)
{
printf(" : ");
scanf("%d", &num);
index = 1;

www.books-shop.com

found = 0;
while (! found && index < rooms)
if (room[index] == num)
found = 1;
else
index++;
if (! found)
puts("
\n");
else
printf(" #%d
%d \n", room[index], max[index]);
}
if (choice == 3)
{
flag = 0;
printf("
:");
scanf("%d", &num);
for (index = 0; index < rooms; index++)
if (max[index] >= num)
{
flag = 1;
printf(" #%d
%d \n", room[index], max[index]);
}
if (flag == 0)
puts("
\n");
}
}
rooms,
. , .
if.
switch if...else.
for .
, room,
max.
, , , .
, , . . , . flag ,
.

www.books-shop.com

.
, . ,
(\0),
.
++ .
, (.10.7). ,
,
:

main()
{
char name[20];
int index;
printf(" : ");
scanf("%s", name);
for (index = 0; index < 20; index++)
printf("%c\n", name[index]);
}

. 10.7. , ,

, , , .
gets(), ,
10.5.
10.5. gets().

/*getstr.c*/
main()
{
char name[10], letter;
int index;
index = 0;
puts(" , Enter\n");
do
{
letter = getchar();
name[index] = letter;
index++;

www.books-shop.com

}
while (letter !='\r' && index < 9);
name[index] = '\0';
putchar('\n');
puts(name);
}
, ,
. -
. , , ++ . , ,
. .10.2 , ++.
10.2. .

,
, .


++ , , :

if (string1 == string2)

www.books-shop.com

. 10.8. strcmp()
strcmp(),
, , , , .
strcmp() .10.8. :

if (strcmp(name1, name2) == 0)
puts(" ");
else
puts(" ");
, , , .
strcmp(), ,
,
:

main()
{
int index, flag;
char name[10], name1[10];
gets(name);
gets(name1);
flag = 0;
for (index = 0; index < 10; index++)
if (name[index] != name1[index])
{
flag = 1;
break;
}
if (flag == 1)
puts(" ");
else
puts(" ");
}

www.books-shop.com


. , name, 20, , .
++ strlen(),
, :

gets(name);
count = strlen(name);
printf(" %s %d ", name, count);
, (
name), count. strlen() :

main()
{
int index;
char name[10];
gets(name);
for (index = 0; index < 10; index++)
{
if (name[index] == '\0')
break;
}
printf("%d", index);
}
. , , , , .
, ,
, :

main()
{
char name[10];
int index, count;
gets(name);
count = strlen(name);
for (index = count; index > 0; index--)
putchar(name[index-1]);
putchar('\n');
}
for,
. 5, 5, index 5 1. 0 4, ,
, index .

name = "";
strcpy(), . :
%RRNVVKRS

piracy@books-shop.com

strcpy(name, "");
strcpy(name, name1);
"" name. , name1, name.
, :

char name[] = "";


main()
{
char person[10];
int count, index;
count = strlen(name);
for (index = 0; index <= count; index++)
person[index] = name[index];
puts(person);
}
.

. 10.9. strcat()

++
++ +
:

new_string = string1 + string2;


, , ,
, . (concatenation). K&R strcat(), .10.9. , , ,
.

www.books-shop.com


, . ,
, . , , .
, . , , . , , 10
20 , 200 :

int table[10][20];
,

. 10.10.
1020. table[0][0] ,
table[0][1] .
, .
, . ,

char names[10][20];
names 19 (.10.10).
, . 10 , , 19 . .10.11 ,
10 ( ) , 10
for .
, :

name[index][index2] = letter;
,
. , .10.12 name[0][3] ( ) ("")

www.books-shop.com

. 10.11.

. 10.12.
. , ,
:

puts(name[index]);
, ++ , , :

main()
{
char name[10][20];
int index;

www.books-shop.com

for (index = 0; index < 10; index++)


gets(name[index]);
for (index = 0; index < 10; index++)
puts(name[index]);
}



, , ,
, .
, ,
.
, , , . , float ,
. , , , ,
, .
, . . , , , .

1.

2.

3.

4.

5.

6.

7.

8.

1.

, , 20 .

2.

1 ,
.

3.

, :

4.

main()

5.

6.

int temps(31);

7.

int index, total;

8.

for (index = 0; index < 31; index++)

9.

www.books-shop.com

10.

printf("

11. #%d: ", index);


12.

scanf("%d", &temps(index));

13.

14.

high = temps(0);

15.

low = temps(0);

16.

index = 1;

17.

while (index < 31)

18.

19.

if (temps(index) > high)

20.

high = temps(index);

21.

else

22.

low = temps(index);

23.

index++;

24.

25.

printf("

26. %d\n", low);


27.

printf("

28. %d\n", high);


}

www.books-shop.com

11

, , , , , ,
. .
, , .
, : . , , . ,
, .
, .


, . , , .
, float, int, char . ,
, . ,
-. , , , ,
, , (.11.1).

. 11.1.
, :

char name[20], description[40], category[12];


float cost;
int number;
,
, ,

++
++ .
, ,
. , , , , :

struct square {

www.books-shop.com

float number;
void assignumber(double);
float squareit(void);
}

amount;

void square :: assignumber(float num)


{
number = num;
}
void square :: squareit(void)
{
float toreturn;
toreturn = number * number;
return(toreturn);
}
square.assignumber(25.0);
cout << "" << square.squareit();
:: (- scoping) .
, , ,
.
, , -
. , ++
,
. .


.
, . ,
(.11.2).

. 11.2.

www.books-shop.com

struct, ,
(tag). , ,
.
.
. . .
(template). . ,
. .
,
.
- :

struct CD
{
char name[20];
char description[40];
char category[12];
float cost;
int number;
};
, CD, : , float . , , 78 (.11.3).

. 11.3.


. ,
, . float,
, , float-. , ,
. .11.4.
struct , , , . ,

www.books-shop.com

. , CD,

struct CD disc;

. 11.4.
disc, . , disc . disc
CD, 78 , : float int (.11.5).

. 11.5.
(, CD),
:

struct CD disc, cdrom;


, disc cdrom, CD.
, . , CD VIDEO, :

www.books-shop.com

struct CD disc, cdrom;


struct VIDEO movies, vacation;
, . ( )
:

struct CD
{
char name[20];
char description[40];
char category[12];
float cost;
int number;
} disc;
,
:

CD ; , ;

, , ; , ;

disc , ; CD, , ;
.


, ,
. ,
:

struct CD
{
char name[20];
char description[40];
char category[12];
float cost;
int number;
} disc = {" ", " ",
"-", 12.50, 12};
CD, disc (.11.6).

%RRNVVKRS

piracy@books-shop.com

. 11.6.
, :

struct CD disc = {" ", " . ",


" ", 24.99, 213};
,
, .
, main(), , main() . ! ,
, main(), :

static struct CD;


. . , cost CD,

cost = 23.13;
, , cost . . , , disc, cost.
, , :

structure_variable.member_name
, , ,
, :

gets(disc.name);
gets(disc.description);
gets(disc.category);
disc.cost = 16.95;
disc.number = 5;
, , , name disc.
,
. 11.1 , ,

www.books-shop.com

-. , .
, , .
11.1. .

/*CD1.c*/
struct CD
{
char name[20];
char description[40];char category[12];
float cost;
int number;
} disc;
main()
{
puts(" \n\n");
printf(" : ");
gets(disc.name);
printf(" : ");
gets(disc.description);
printf(" : ");
gets(disc.category);
printf(" : ");
scanf("%f", &dics.cost);
printf(" : ");
scanf("%d", &disc.number);
puts(" :\n\n");
printf(":

%s\n", disc.name);

printf(":

%s\n", disc.description);

printf(":

%s\n", disc.category);

printf(":
printf(" /:

%6.2f\n", disc.cost);
%d\n", disc.number);


.
, .
, . , .
, :

struct CD disc[10];
, , , (.11.7). , :

gets(disc[0].name);
gets(disc[1].name);

www.books-shop.com

11.2 , , , , , -.
11.2. .

/*CD2.c*/
struct CD
{
char name[20];
har description[40];
har category[12];
float cost;
int number;
} disc[10];
main()
{
int index, repeat;
har flag;
flag = 'Y';
index =0;
do
{
puts("
#%d\n", index);
printf(" : ");
gets(disc[index].name);
printf(" : ");
gets(disc[index].description);
printf(" : ");
gets(disc[index].category);
printf(" : ");
scanf("%f", &dics[index].cost);
printf(" : ");
scanf("%d", &disc[index].number);
index++;
if(index < 10)
{
printf("
? -Y, -N");
scanf("%C", &flag);
}
}
while (index < 10 && (flag == 'Y' || flag == 'y'));
puts("

/");

for (repeat = 0; repeat < index; repeat++)

www.books-shop.com

printf("%s %d\n", disc[repeat].name,


disc[repeat].number);
}

. 11.7.
, 10 disc, :

} disc[10];
flag , , , 10. do : ( ) ( ). index .
, , . 10 .
do :

while (index < 10 && (flag == 'Y' || flag == 'y'));


, :
index , flag 'Y' 'y'.
, (.11.8).

. 11.8.
flag 'y',
.
(), index .

www.books-shop.com

for, - .
, c
.


K&R- .
,
. , , ,
:

cdrom = disc;
++ .
.
++ , ANSI, . 11.3 , . main(),
putdisc() . disc:

putdisc(disc);
, . ( ), .
:

putdisc(disk)
struct CD disk;
disk, disc.
disk, .
11.3. .

/*CD3.c*/
struct CD
{
char name[20];
char description[40];
char category[12];
float cost;
int number;
} disc;
main()
{
puts(" \n\n");
printf(" : ");
gets(disc.name);
printf(" : ");
gets(disc.description);
printf(" : ");
gets(disc.category);
printf(" : ");
scanf("%f", &dics.cost);
printf(" : ");
scanf("%d", &disc.number);

www.books-shop.com

putdisc(disc);
}
putdisc(disk)
struct CD disk;
{
puts(" :\n\n");
printf(":

%s\n", disc.name);

printf(":

%s\n", disc.description);

printf(":

%s\n", disc.category);

printf(":
printf(" /:

%6.2f\n", disc.cost);
%d\n", disc.number);

}
11.4 , . getdisc(), main()

return(inputdisc);
, (CD) , getdisc(). , , int
char, .
CD, :

struct CD getdisc();
, , CD, :

struct CD inputdisc;
, getdisc() , disc. , .
11.4. .

/*CD4.c*/
struct CD
{
char name[20];
char description[40];
char category[12];
float cost;
int number;
} disc, getdisc();
main()
{
disc = getdisc();
puts(" :\n\n");
printf(":

%s\n", disc.name);

printf(":

%s\n", disc.description);

printf(":

%s\n", disc.category);

printf(":
printf(" /:

%6.2f\n", disc.cost);
%d\n", disc.number);

}
struct CD getdisc()

www.books-shop.com

{
struct CD inputdisc;
puts(" \n\n");
printf(" : ");
gets(inputdisc.name);
printf(" : ");
gets(inputdisc.description);
printf(" : ");
gets(inputdisc.category);
printf(" : ");
scanf("%f", &inputdiscdics.cost);
printf(" : ");
scanf("%d", &inputdisc.number);
return(inputdisc);
}

, ( ), return() . ,
.
, . , .
. ,
, . (&), , .

tax = 35;
, , . , ,
tax, 35. , .

printf("%d", &tax);
, tax, , .
(&tax) . ,

&tax = 25;
.
, ,
. tax , 21260, tax 21260*.
, , .11.9.
, , . , . . ,

int *taxptr;
( ) taxptr,
.

float *net;

www.books-shop.com

. 11.9.
net, float.
, - . :

taxptr = &tax;
, tax,
taxptr (.11.10). tax 21260, taxptr
21260.
*
( ). ,
"%p", "%d", .
(..)

. 11.10.
:

main()

www.books-shop.com

{
int *taxptr;
int tax;
taxptr = &tax;
tax = 35;
printf(" tax %d\n", tax);
printf(" tax
%d\n", taxptr);
}
:

tax 35
tax 21260
, : ? , , tax , -
. ,
*taxptr. , , . 21260,
. taxptr 21260,
*taxptr 35.
, taxptr, .

taxptr = 21260;
, , . , taxptr, , :

taxptr = &tax;
*taxptr,

*taxptr = 35;
: 35 ,
taxptr. 21260,

. 11.11.

www.books-shop.com

35 21260, tax, 35 (.11.11).


, , . tax . , , , ,
.


. , . , .
. , , . ,
, .
, 11.5, . ,
, .
11.5. , .

/*letcount.c*/
main()
{
char name[20], letter;
int number, start;
puts(" \n");
gets(name);
printf(" ");
letter = getchar();
countlet(name, letter, &number, &start);
printf("\n %c
%s %d \n", letter, name, number);
printf("
%d ", start);
}
countlet(ndplume, alpha, count, first)
char ndplume[], alpha;
int *count, *first;
{
int index, flag;
*count = 0;
index = 0;
flag = 0;
*first = 0;
while (ndplume[index != '\0')
{
if (ndplume[index] == alpha)
{
*count = *count+1;
if (flag == 0)
%RRNVVKRS

piracy@books-shop.com

{
*first = index+1;
flag = 1;
}
}
index++;
}
}
countlet() : , count start, .
: number *count,
start *first (.11.12).

. 11.12.

www.books-shop.com

. 11.13.
,
while:

while (ndplum[index] != '\0')


,
. , , .
while ,
if (.11.13). if , , . , ,
, :

*count = *count + 1;
, , ,
. number, number 1, , main(). ,

*count++;
, , *.
if flag. ,
. *first ,

www.books-shop.com

. , ,
1 ( ).
flag 1,
.
if , . ,
index++; if, , while, . - ,
.
countlet() main(), , .
* (*count)++;.(..)
, .
, return.
number start . , .
, number start
. main(), countlet() , . , .
, 12.

1.

2.

3.

4.

5.

6.

7.

float *num,
num *num?

8.

9.

1.

, .
, , , .
1 , , 20 .

2.

2 , .

3.

, float main() , ,

www.books-shop.com

.
4.

, :

5.

main()

6.

7.

struct CD

8.

9.

char description[40];

10.

char category[12];

11.

char name[20];

12.

float cost;

13.

int number;

14.

} disc;

15.

puts(" ");

16.

printf(" : ");

17.

gets(name);

18.

printf(" : ");

19.

gets(description);

20.

printf(" : ");

21.

gets(category);

22.

printf(" : ");

23.

scanf("%f", &cost);

24.

printf(" : ");

25.

scanf("%d", &number);

26.

puts(" : ");

27.

printf(":

%s\n", name);

28.

printf(":

%s\n", description);

29.

printf(":

%s\n", category);

30.

printf(":

31.

printf(" /:

%6.2f\n", cost);
%d\n", number);

www.books-shop.com

12

, . , , :
,
.
, , , .
, . , ,
, - , , ,
.

, . , .



. ,
, . , (.12.1).
, .
,
.
.
, .
,

. 12.1.
, .
, , , , , , .
, , ,
, (.12.2).

www.books-shop.com

. 12.2. ,

++ , , STDIO.H. , .
, . ,

#include
.
, ,
, .

++
++ ,
.
IOSTREAM.H, FSTREAM.H , , , . .
. , .
.


,
.
. :

FILE *file_pointer;
FILE ,
. .
,
. , , ,
. ,
:

FILE *infile, *outfile;

www.books-shop.com


fopen(), .12.3.
.
,

. 12.3. fopen()
. , MS-DOS , , ( ). , , "PRN". .
, , . ++ ,
, . :

r ,
.
,
.
w ,
.
,
.
,
.
a ,
. ,
.
,
.

www.books-shop.com

++ ANSI , .
"r+", "w+" "a+".
, CD.DAT , :

FILE *cdfile;
cdfile = fopen("CD.DAT", "w");
, ,
:

FILE *cdfile;
cdfile = fopen("CD.DAT", "r");
, , , , . , fopen() .
, ,
, .
, :

FILE *cdfile;
cdfile = fopen("PRN", "w");
, "w".

/++
, .
, , . "r", . ,
, . , (.12.4). , , , . , , *.
.

www.books-shop.com

. 12.4.
"w", , .
.
"w" ,
, . ,
, , . , "w" . , ,
- .
"a",
. , , ,
.
* .(..)


, , fopen(). . ,
. ,
, , .

www.books-shop.com

, ,
. ,
if, . , if, , :

if ((cdfile = fopen("CD.DAT", "w")) == NULL)


{
puts(" ");
exit();
}
NULL , STDIO.H.
, NULL , .
: if ((cdfile = fopen("prn", "w")) == NULL) { puts(" "); exit(); }

++
++ cin cout . ,
>> <<. ,
, :

ifstream file_pointer(file_name)
:

ofstream file_pointer(file_name)
.
, .
, , , . while, :

while ((cdfile = fopen("prn", "w")) == NULL)


{
puts(" Enter");
flag = getchar();
}
, .
, ,
(abort), (retry). R,
.


, .

fclose(file_pointer);
, , , , . , -
, . ,
, .

%RRNVVKRS

piracy@books-shop.com

, ,
. ,
, , , . , 12.1.
12.1. .

FILE *cdfile;
if((cdfile = fopen("CD.DAT", "w")) == NULL)
{
puts(" ");
exit();
}
/* */
fclose(cdfile);
if((cdfile = fopen("CD.DAT", "r")) == NULL)
{
puts(" ");
exit();
}
/*
*/
fclose(cdfile);
"w", .
"r",
.

flush()

.

putc()
fputc();

getc() fgetc();

fputs();

fgets();

,
fprintf();

, fscanf();

fwrite();

fread().

www.books-shop.com

. , , .
, , Enter:

/*fputc.c*/
#include
main()
{
FILE *fp;
char letter;
if((fp = fopen("MYFILE","w"))==NULL)
{
puts(" ");
exit();
}
do
{
letter=getchar();
fputc(letter, fp);
}
while(letter != '\r');
fclose(fp);
}
"w". MYFILE , . do, getchar(), ,


getchar()
getchar() , ,
,
getche(). do...while while:

while ((letter = getche())) != '\r')


putc(). putc() :

putc(char_variable, file_pointer);
fputc().
, Enter, (\r),
.


getc() fgetc(). :

char_variable = getc(file_pointer);
, , fp,
:

www.books-shop.com

letter = getc(fp);
, , :

/*fgets.c*/
#include
main()
{
FILE *fp;
int letter;
if((fp = fopen("MYFILE","r"))==NULL)
{
puts(" ");
exit();
}
while((letter = fgetc(fp)) != EOF)
printf("%c", letter);
fclose(fp);
}
"r", while
. while.

letter = fgetc(fp)
, , letter. ,
EOF* ( STDIO.H), , ,
.

, . .
, , .


,
. fputs() fgets().
fputs() :

fputs(string_variable, file_pointer);
,
. ( ) , . , :

/*fputc.c*/
#include
main()
{
FILE *fp;
char flag;
char name[20];
if((fp = fopen("MYFILE","w"))==NULL)
{
puts(" ");

www.books-shop.com

exit();
}
flag = 'y';
while(flag != 'n')
{
puts(" ");
gets(name);
fputs(name, fp);
fputs("\n",fp);
printf(" ?");
flag=getchar();
putchar('\n');
}
fclose(fp);
}
* end-of-file. (..)
while , n.
gets(), fputs(). , , ,
, .
strlen(), ,
:

printf(", : ");
gets(name);
while(strlen(name) > 0)
{
fputs(name, fp);
fputs("\n", fp);
printf(", : ");
gets(name);
}
, , name, , 0. Enter,
. Enter
, .
, , :

printf(", : ");
while(strlen(gets(name)) > 0)
{
fputs(name, fp);
fputs("\n", fp);
printf(", : ");
}
while.
, "prn".
, :

www.books-shop.com

if ((fp = fopen("prn", "w")) == NULL)


81 , , Enter. 12.2
, , . , Enter, Backspace
.
12.2. .

/*wp.c*/
#include "stdio.h"
main()
{
FILE *fp;
char line[81];
if ((fp = fopen("prn", "w")) == NULL)
{
puts(" ");
exit();
}
puts(" ,
Enter\n");
puts("
Enter \n");
gets(line);
while (strlen(line) > 0)
{
fputs(line, fp);
fputs("\n", fp);
gets(line);
}
fclose(fp);
}


fgets(). :

fgets(string_variable, lenght, file_pointer);


, , lenght . lenght
, .
, ,
:

/*fgets.c"/
#include "stdio.h"
main()
{
FILE *fp;
char name[12];
if ((fp = fopen("MYFILE", "r")) == NULL)

www.books-shop.com

{
puts(" ");
exit();
}
while(fgets(name, 12, fp) != NULL)
{
printf(name);
}
fclose(fp);
}
while , NULL.
, NULL. NULL, EOF
.
, ,
lenght 80.
, , printf()
. , ,
, fputs("\n", fp);, printf() .


, , .
, ,
, fprintf() fscanf(). , printf() scanf(),
, , :

fprintf(file_pointer, control_string, data_list);


fscanf(file_pointer, control_string, data_list);
, 12.3,
. gets() while. , . , Enter.
12.3. .

/*fprintf.c*/
#include "stdio.h"
main()
{
FILE *fp;
char name[20];
int quantity;
float cost;
if ((fp = fopen("MYFILE", "w")) == NULL)
{
puts(" ");
exit();
}
printf(" : ");
gets(name);

www.books-shop.com

while (strlen(name) > 0)


{
printf(" : ");
scanf("%f", &cost);
printf("
: ");
scanf("%d", &quantity);
fprintf(fp, "%s %f %d\n", name,
cost, quantity);
printf(" : ");
gets(name);
}
fclose(fp);
}
, .
Enter. ,
, :

do
{
printf(" : ");
gets(name);
printf(" : ");
scanf("%f", &cost);
printf(" : ");
scanf("%d", &quantity);
fprintf(fp, "%s %f %d\n", name, cost, quantity);
}
while (strlen(name) > 0);
, ,
Enter :
.
while
scanf(),

fprintf(fp, "%s %f %d\n", name, cost, quantity);


, . TYPE MS-DOS, :

1.120000 100
7.340000 150
75.000000 3
, , ,
:

1.120000 100 7.340000 150 75.000000 3


, , , . , , ,
-? :

www.books-shop.com

1.120000 Memoryex
7.340000 Okaydata 75.000000 HP

. , :

1.120000 Memoryex
, int float, .
.


fscanf().
scanf() , fscanf()
,
. , fscanf() , scanf().
, , , .
12.4 , , . , while , fscanf() EOF, .
12.4. .

/*fscanf.c*/
#include "stdio.h"
main()
{
FILE *fp;
char name[20];
int quantity;
float cost;
if ((fp = fopen("MYFILE", "r")) == NULL)
{
puts(" ");
exit();
}
while (fscanf(fp, "%s%f%d",
name, &cost, &quantity) != EOF)
{
printf(" :
%s\n", name);
printf(":
%.2f\n", cost);
printf(" :
%d\n", quantity);
}
fclose(fp);
}


scanf()
, . fwrite() fread().

www.books-shop.com

fwrite() :

fwrite(&structure_variable, structure_size,
number_of_structures, file_pointer);
, ,
:

&structure_variable ,
, ;

structure_size ; ,
sizeof(), :

sizeof(structure_variable)
;

number_of_structures , ,
; 1,
;

file_pointer .

, . CD, 11, :

. 12.5. fwrite() CD

fwrite(&disc, sizeof(disc), 1, fp);


.12.5.
, CD, , 12.5. gets(). ,
, fopen() , .
CD ,
.
12.5. CD.

/*fwrite.c*/
#include "stdio.h"
main()
{
FILE *fp;
struct CD

www.books-shop.com

{
char name[20];
char description[40];
char category[12];
float cost;
int number;
} disc;
char filename[25];
printf(" ,
: ");
gets(filename);
if ((fp = fopen(filename, "w")) == NULL)
{
printf("
%s\n", filename);
exit();
}
puts(" \n");
printf(" : ");
gets(disc.name);
while (strlen(disc.name) > 0)
{
printf(" : ");
gets(disc.description);
printf(" : ");
gets(disc.category);
printf(" : ");
scanf("%f", &disc.cost);
printf(" : ");
scanf("%d", &disc.number);
fwrite(&disc, sizeof(disc), 1, fp);
printf(" : ");
gets(disc.name);
}
fclose(fp);
}


, fread(). :

fread(&structure_variable, structure_size,
number_of_structures, file_pointer);
fwrite(). , CD, 12.6. while:
%RRNVVKRS

piracy@books-shop.com

while (fread(&disc, sizeof(disc), 1, fp) == 1)


fread() , .
, , 1. while , .
, , ,
0, .
12.6. CD .

/*fread.c*/
#include "stdio.h"
main()
{
FILE *fp;
struct CD
{
char name[20];
char description[40];
char category[12];
float cost;
int number;
} disc;
char filename[25];
printf(" ,
: ");
gets(filename);
if ((fp = fopen(filename, "r")) == NULL)
{
printf("
%s\n", filename);
exit();
}
while (fread(&disc, sizeof(disc), 1, fp) == 1)
{
puts(disc.name);
putchar('\n');
puts(disc.description);
putchar('\n');
puts(disc.category);
putchar('\n');
printf("%f", disc.cost);
putchar('\n');
printf("%d", disc.number);
}
fclose(fp);
}

www.books-shop.com

. 12.1 , .
12.1. .


, , . , , , .
12.7 , ,
-, CD (, 20). , disc. ,
, ,
:

total = total + disc[index].cost;


index++;
count++;
,
, ,
total count. , .
, ,
, .
12.7. .

/*rarray.c*/
#include "stdio.h"
main()
{
FILE *fp;
struct CD
{
char name[20];
char description[40];
char category[12];
float cost;
int number;
} disc[20];
int index, count;
float total;
count = 0;
total = 0;

www.books-shop.com

char filename[25];
printf(" : ");
gets(filename);
while ((fp = fopen(filename, "r")) == NULL)
{
printf("
%s\n", filename);
printf(" : ");
gets(filename);
}
index = 0;
while (fread(&disc[index],
sizeof(disc[index]), 1, fp) == 1)
{
puts(disc[index].name);
putchar('\n');
puts(disc[index].description);
putchar('\n');
puts(disc[index].category);
putchar('\n');
printf("%f", disc[index].cost);
putchar('\n');
printf("%d", disc[index].number);
total = total + disc[index].cost;
index++;
count++;
}
fclose(fp);
printf("
%.2f\n", total);
printf(" %.d \n", count);
}

++
++ , (ifstream ofstream), << >>. :

file_pointer >> variable;


:

file_pointer << variable;


, , "w", , , .

www.books-shop.com

, "a". , , . "a" , , , , .
, , . , ,
, .
, , , , - .


putc(), fputc() fputs() . , TYPE, , . ,
, getc(), fgetc() fgets().
, , , fputs(). , ,
, ,
.
12.8 , , . , ,
. ,
.
12.8. .

/*filecopy.c*/
#include "stdio.h"
main()
{
FILE *fp1, *fp2;
char infile[25], outfile[25];
int letter;
printf(" : ");
gets(infile);
if ((fp1 = fopen(infile, "r")) == NULL)
{
printf("
%s", infile);
exit();
}
printf(" : ");
gets(outfile);
if ((fp2 = fopen(infile, "w")) == NULL)
{
printf("
%s", outfile);
fclose(fp1);
exit();
}
while ((letter = fgetc(fp1)) != EOF)
{
putchar(letter);

www.books-shop.com

fputc(letter, fp2);
}
fclose(fp1);
fclose(fp2);
}
"r", .
, . "w",

. 12.6. fprintf()
. , . , , , .
fprintf() . , fprintf()
34.23 , , .12.6.
fscanf(),
.
, fprintf() , getc(), fgetc() fgets().
. , fgets(), , . ,
fgets() fgetc(),
.


fwrite().

++ ,
ANSI ,
.
"wb" "rb". b .
, putw() getw().
, . TYPE,
. ASCII-,
.
, fwrite(), fread(). , , .
, , ,
.

www.books-shop.com

: , , . , ,
"prn" "w".
fwrite() , .
fprintf(), 12.9.
: , .
12.9. .

/*fread1.c*/
#include "stdio.h"
main()
{
FILE *fp;
struct CD
{
char name[20];
char description[40];
char category[12];
float cost;
int number;
} disc;
char filename[25];
printf(" : ");
gets(filename);
if((fp = fopen(filename, "r")) == NULL)
{
printf("
%s\n", filename);
exit();
}
if((ptr = fopen("PRN", "w")) == NULL)
{
printf("
\n", filename);
fclose(fp);
exit();
}
while (fread(&disc, sizeof(disc), 1, fp) == 1)
{
fprintf(ptr, "
%s\n", disc.name);
fprintf(ptr, ":
%s\n", disc.description);
fprintf(ptr, ":
%s\n", disc.category);

www.books-shop.com

fprintf(ptr, ":
%6.2f\n", disc.cost);
fprintf(ptr, " /:
%d\n", disc.number);
fprintf(ptr, "\n\n");
}
fclose(ptr);
fclose(fp);
}
fread(),
fprintf(). fread() , ,
, fscanf().

fprintf(ptr, "\n\n");
CD.


, , . ,
, . ,
- .
, .
"r",
, ,
, .


++ ,
ANSI, . ,
, . fseek(). , , fread(), , ,
. , ftell() , rewind()
.
. strcmp(), , . ,
, .
, , . , . "a" , ,
, .
, , , . ,
.
, .

www.books-shop.com

1.

2.

3.

4.

,
"r", "w" "a"?

5.

6.

7.

8.

fprintf() fwrite()?

9.

10. sizeof()?

1.

, fputs()
, .

2.

, (1) .

3.

, fprintf()
, , ,
.

4.

, , 3.

5.

3 4 , .

6.

, :

7.

#include "stdio.c"

8.

main()

9.

10.

FILE fp;

11.

char letter;

12.

if ((fp = fopen("MYFILE", "w")) == NULL)

13.

14.

puts(" ");

15.

exit();

16.

17.

do

18.

19.

letter = getchar();

20.

fputc(letter, fp);

21.

22.

while(letter != '\n');

23.

fclose(fp);

www.books-shop.com

www.books-shop.com

13

, ++, .
, .
, , , .
, , , ,
.


-. , , -.
.
, . ,
.
, , , :

, ;


-,
(record). , , , ,
.
, .
CD,
,
.
. , . , , , -, .
,
. , , . , ,
, .
. , , ,
, , . , , : ,
. , %RRNVVKRS

piracy@books-shop.com

. , , ,
.
II . II , , .


.
, STDIO.H:

#include "stdio.h"
, :
. , . , , :

#define FILENAME "Cdfile"


#define TEMPFILE "Temp"
,
, :

#define FILENAME "C:\DATA\CD.DAT"


, , 20
, . , 20:

#define MAX 20
, .
:

FILE *fp, *tp, *printer;


CD. , 11:

struct CD
{
char name[20];
char description[40];
char category[12];
float cost;
int number;
} disc;
, , -.
, , ,
- :

int slots[MAX];
int count;

main()
. main() , , .
.
, main() :

, , ;

www.books-shop.com

, ;

, .

main()
. ( ) main()
, . , main()

char select;
, , ,
. , getslots(). ,
disc.number slots[],
, :

getslots()
{
int index;
index = 0;
count = 0;
if ((fp = fopen(FILENAME, "r")) != NULL)
{
while (fread(&disc, sizeof(disc),
1, fp) == 1)
{
slots[index] = disc.number;
index++;
count++;
}
fclose(fp);
}
}
,
. , .
, , ,
, .
, do.
, ,
. puts() printf(), , , getchar(). getchar()
Enter .
, , . , main() :

main()
{
char select;
do
{
puts("

-\n");

puts("1 \n");
puts("2 \n");

www.books-shop.com

puts("3
\n");
puts("4 /\n");
puts("5 \n");
puts("6 \n");
puts("7 \n");
puts("8 \n");
printf(",
: ");
select = getchar();
putchar('\n');
, main() .
if.
, , switch, . default
, , .
main() :

switch(select)
{
case '1' :
addcd();
break;
case '2' :
delcd();
break;
case '3' :
chcd();
break;
case '4' :
chloc();
break;
case '5' :
sort();
break;
case '6' :
locate();
break;
case '7' :
plist();
break;
case '8' :
break;
default:
puts(", \n\n");
}

www.books-shop.com

}
while (select != '8');
return(0);
}
do...while ( switch) , 8, .
, ,
.

: addcd()
, -, , ,
12 .
, 12, , , ,
, . , , . getslots()
, . , :

int pause;
if (count >= MAX)
{
puts(" , \n");
pause = getchar();
return;
}
, "a",
, :

if ((fp = fopen(FILENAME, "a")) == NULL)


{
printf(" %s\n", FILENAME);
exit();
}
, .
1 20.
, , getslot() :

getslot()
{
int index, flag, pause;
do
{
flag = 0;
printf(" : ");
scanf("%d", &disc.number);
for (index = 0; index < count; index++)
{
if (slots[index] == disc.number)
{
printf(" ,

www.books-shop.com

, \n");
flag = 1;
}
}
}
while (disc.number < 1 || disc.number >
MAX || flag == 1);
count++;
slots[count] = disc.number;
return;
}
getslot() .
- getslots(), .

: delcd()
, , . , , .
"w", . "a"
.
. delcd() ,
"r", "w":

if (fp = fopen(FILENAME, "r")) == NULL)


{
printf(" %s\n", FILENAME);
exit();
}
if ((tp = fopen(tempfile, "w")) == NULL)
{
printf(" %s\n", tempfile);
fclose(fp);
exit();
}
, :

printf(" : ");
gets(delname);
while () :

while (fread(&disc, sizeof(disc), 1, fp) == 1)


{
, , delcd() :

if (strcmp(disc.name, delname) != 0)
fwrite(&disc, sizeof(disc), 1, tp);
strcmp() ,
. strcmp() K&R- , ANSI ++.
, delcd() , , , :

www.books-shop.com

else
fflag = 'y';
}
while , ,
:

fclose(fp);
fclose(tp);
delcd() , ,
, :

puts("\n ");
pause = getchar();
, . , , . , , delcd() , .
, , .
++ ,
, .
, , "w", ,
"r". openwr(), delcd() :

if (fp = fopen(FILENAME, "w")) == NULL)


{
printf(" %s\n", FILENAME);
exit();
}
if ((tp = fopen(TEMPFILE, "r")) == NULL)
{
printf(" %s\n", tempfile);
fclose(fp);
exit();
}
, delcd()
. , ,
, :

while (fread(&disc, sizeof(disc), 1, tp) == 1)


fwrite(&disc, sizeof(disc), 1, fp);
fclose(fp);
fclose(tp);
delcd() getslots(), .
, , .
,
. .
- , , ,
. -
, "w", :

tp = fopen(tempfile, "w");
fclose(tp);

www.books-shop.com

, ,
.
, . ,
.



.
, . :

openwr(): ,
;

openrw(): ,
;

nofind(): , .

,
, . , .

: chcd()
, :
, . , , , , , .
, chcd() -, , while, :

openrw();
puts(" : ");
gets(chname);
while (fread(&disc, sizeof(disc), 1, fp) == 1)
{
, , :

if (strcmp(disc.name, chname) != 0)
fwrite(&disc, sizeof(disc), 1, tp);
, , :

else
{
fflag = 'y';
puts(" \n");
showdisc();
puts(" \n");
printf(" : ");
gets(disc.name);
printf(" : ");
gets(disc.description);
printf(" : ");

www.books-shop.com

gets(disc.category);
printf(" : ");
scanf("%f", &disc.cost);
if (count >= MAX)
{
puts(" \n");
pause = getchar();
}
else
{
getslot();
}
if (count >= MAX) , . getslot() .
chcd() ,
, . , , Enter , . , :

printf(" : ");
gets(disc.name);
if (strlen(name) > 0)
strcpy(disc.name, name);
, disc.name, . Enter, , disc.name
.
,
, showdisc(),
:

showdisc()
{
printf("

%s\n", disc.name);

printf("

%s\n", disc.description);

printf("

%s\n", disc.category);

printf("
printf(" /

%6.2f\n", disc.cost);
%d\n", disc.number);

puts("\n\n");
return;
}
chcd() :

fwrite(&disc, sizeof(disc), 1, tp);


fp chcd() .
(, ), :

fclose(fp);
fclose(tp);

www.books-shop.com

if (fflag == 'n')
nofind();
, chcd() , :

else
{
openwr();
while (fread(&disc, sizeof(disc), 1, tp) == 1)
fwrite(&disc, sizeof(disc), 1, fp);
fclose(fp);
fclose(tp);
}
getslots();
return;
}

: chloc()
, -, ,
, :

puts(" \n");
showdisc();
puts("\n \n");
getslot();
if, , .

: locate()
, ,
, :

while (fread(&disc, sizeof(disc), 1, fp) == 1)


{
if (strcmp(disc.name, name) == 0)
{
locate() showdisc(), , , :

fflag = 'y';
showdisc();
printf("
Enter");
pause = getchar();
putchar('\n');
}
}
showdisc() ,
.

: plist()

www.books-shop.com

-,
: ,



. , .
, if
, :

if (select = '3')
{
puts(" \n");
printf(" : ");
gets(disc.name);
printf(" : ");
gets(disc.description);
printf(" : ");
gets(disc.category);
printf(" : ");
scanf("%f", &disc.cost);
}
getslot();
3,
, , . if getslot() . 4, , .
, MS-DOS "prn",
:

if ((fp = fopen(filename, "r")) == NULL)


{
printf(" %s\n", filename);
exit();
}
if (printer = fopen("prn", "w")) == NULL)
{
printf("
\n", filename);
fclose(fp);
exit();
}
plist() fprintf():

while (fread(&disc, sizeof(disc), 1, fp) == 1)


{
fprintf(printer, "

%s\n",

disc.name);
%RRNVVKRS

piracy@books-shop.com

fprintf(printer, "

%s\n",

disc.description);
fprintf(printer, "

%s\n",

disc.category);
fprintf(printer, "

%6.2f\n",

fprintf(printer, " /

%d\n",

disc.cost);
disc.number);
fprintf(printer, "\n\n");
}
main():

fclose(printer);
fclose(fp);
return;
}

: sort()
, ,
, -. ,
, ,
. , , ,
.
,
, , .
, ,
. , ,
, .
.
, . , ,
, .
sort() , ,
. :

sort()
{
struct CD temp[MAX];
int index, loop1, loop2, endloop;
loop1 = 0;
loop2 = 0;
endloop = 0;
index = 0;
sort() , :

while (fread(&disc, sizeof(disc), 1, fp) == 1)


{
temp[index] = disc;
index++;
}
fclose(fp);

www.books-shop.com

index .
, sort() , , ,
for:

if ((fp = fopen(filename, "w")) == NULL)


{
printf(" %s\n", filename);
exit();
}
for (loop1 = 1; loop1 < MAX+1; loop1++)
{
for (loop2 = 0; loop2 < count; loop2++)
for loop1 1
. , for , 1. ,
:

if (temp[loop2].number == loop1)
{
fwrite(&temp[loop2], sizeof(temp[loop2]), 1, fp);
endloop++;
}
. ,
, 2. , .
.
, . endloop ,
, . , sort()
endloop . endloop - , ,
:

if (endloop == count)
break;
sort() .
. ,
.

www.books-shop.com

I

1
1.
.

.
, .
1 .


1 .

1 , 1.5.
.

.
2. , ,
( 65 ).

65.

65,
,
.
65,
,
.

2
1. , :

.
.
main
{
puts(" .\n");
puts(" .\n");
}
2. , , .

main

www.books-shop.com

{
puts(" . \n");
puts(" 111 \n");
puts(" , 09987\n");
puts(" 123-555-1234\n");
}
3., :

main()
(
puts(" ");
}
main()
.

3
1., , , ,
( 40 ).

float payrate, reg_hours, o_hours, total;


,
,
reg_hours o_hours float,
, int.
2., , , .

:
float number_1, number_2, number_3, number_4, sum, average;
:
int number_1, number_2, number_3, number_4, sum;
float average;
3., :

char client[3]="";
main()
float tax_due;
char name(10);
int count(5);
, client,
4 ,
, ,
2 ( \0 !).
name

www.books-shop.com

, .
count
int, char, ,
.

4
1. puts().

main
{
puts(". ");
puts("111 ");
puts(", 09987");
}
2. printf().

main
{
printf(". \n");
printf("111 \n");
printf(", 09987\n");
}
3. puts(), "" . 80
.

puts("

\n") /* 34 */

4. printf(), "" .

printf("%80s","");
5. printf(), :

float length, width, height, volume;


float length, width, height, volume;
printf("%f %f %f %f", length, width, height, volume);
6. . printf(),
:

char name[12];
int age;
char name[12];
int age;
printf("%s is %d years old", name, age);
7. :

har item[] = "";


float cost = 3.55;
float markup = 0.75;
printf(), :

:
1 :


3.55

www.books-shop.com

0.75

printf(" : %17s\n", item);


printf(" 1 :

%17.2f\n",cost);

printf(":

%17.2f\n",markup);

8. :

int count = 30;


count , , :

! ! ! !
.
30 , .
putchar('\007');
printf("! ! ! !\n");
printf(" .\n");
printf(" %d , \
.", count);

5
1. , , .

main()
{
char name[25];
char telephone[12];
printf(", : ");
gets(name);
printf(" : ");
gets(telephone);
printf(": %s : %s", name, \
telephone);
}
2. , , ,
.

main()
{
int number;
printf(", : ");
scanf("%d", &number);
putchar('\n');
printf(" %d %d", \
number, &number);
}
3. , , , , .

www.books-shop.com

main()
{
int num_1, num_2, num_3;
printf(" : ");
scanf("%d", &num_1);
printf(" : ");
scanf("%d", &num_2);
printf(" : ");
scanf("%d", &num_3);
printf("3: %d 2: %d 1: %d", num_3, num_2, num_1);
}
4. , getchar(), gets() scanf().

main()
{
char first[10], last[15];
char initial;
int age;
printf(" : ");
gets(first);
printf(" : ");
initial = getchar();
putchar('\n');
printf(" : ");
gets(last);
printf(" : ");
scanf("%d", &age);
printf(": %s %c %s\n", first, initial, last);
printf(": %d", age);
}
5., :

main()
{
char initial;
initial = gets();
puts(initial);
}
initial .

getchar()
putchar() printf().

6
1. , , 2000 .

main()

www.books-shop.com

{
int year, age, togo;
printf(" : ");
scanf("%d", &year);
printf(" : ");
scanf("%d", &age);
togo = 2000 - year + age;
printf(" 2000 %d ", togo);
}
2. , .

main()
{
int number, square, cube;
printf(" : ");
scanf("%d", &number);
square = number * number;
cube = number * number * number;
printf(" %d\n", number);
printf(" %d\n", square);
printf(" %d\n", cube);
}
3. (F) (C).
C=(5.0/9.0)(F32).

main()
{
int temp;
float celsius;
printf(" \
: ");
scanf("%d", &temp);
celsius = (5.0/9.0)*(temp-32);
printf(" : %d \
: %f", temp, celsius);
}
4. 3 , , .

main()
{
int temp, ffreeze;
float celsius;
printf(" \
: ");
scanf("%d", &temp);
celsius = (5.0/9.0)*(temp-32);

www.books-shop.com

ffreeze = temp - 32;


printf(" : %d \
: %d\n", temp, ffreeze);
printf(" : %f : \
%f", celsius, celsius);
}
5., :

#define TAX_RATE 0.06


main()
{
float cost, total;
printf(" : ");
scanf("%f", &cost);
printf(" : ");
scanf("%f", &shipping)'
total = cost + cost * tax_rate + shipping;
printf(" %f", total);

shipping,
.
scanf() ,
.

7
1. , ;
.

char pause;
main()
{
quest1();
quest2();
quest3();
quest4();
}
quest1()
{
puts(" ?\n");
puts("
Enter\n");
pause=getchar();
puts(" \n");
return;
}
quest2()

www.books-shop.com

{
puts(" ?\n");
puts("
Enter\n");
pause=getchar();
puts(" \n");
return;
}
quest3()
{
puts(" .\n");
puts("
Enter\n");
pause=getchar();
puts(" A, B, AB O\n");
return;
}
quest4()
{
puts(" - ?\n");
puts("
Enter\n");
pause=getchar();
puts("- la mer\n");
return;
}
2. , , .

main()
{
int number;
printf(" : ");
scanf("%d", &number);
tothefourth(number);
}
tothefourth(value)
int value;
{
int power;
power=value*value*value*value;
printf(" %d %d",
value, power);
return;
%RRNVVKRS

piracy@books-shop.com

}
3. 2 ,
, main() .

main()
{
int number, fourth;
printf(" : ");
scanf("%d", &number);
fourth = tothefourth(number);
printf(" %d %d",
number, fourth);
}
int tothefourth(value)
int value;
{
return(value*value*value*value);
}
4., :

dothis()
{
puts(" ");
main();
return(0);
}
main()
{
puts(" ");
return();
}

main(), .
main().

:
main()
{
puts(" ");
dothis();
return(0);
}
dothis()
{
puts(" ");

www.books-shop.com

return(0);
}

8
1. , , ,
.

main()
{
int number, remain;
printf(" : ");
scanf("%d", &number);
remain=number % 2;
if(remain==0)
puts(" ");
else
puts(" ");
}
2. , ,
1 100.

main()
{
int number;
printf(" : ");
scanf("%d", &number);
if(number > 0 && number <= 100)
puts("
1 100");
}
3. , , ,
: 0, 0 50, 51 100, 101 150, 150.

main()
{
int number;
printf(" : ");
scanf("%d", &number);
if(number < 0)
puts(" ");
else
if(number > 0 && number < 51)
puts("
0 50");
else
if(number > 50 && number <= 101)
puts("
51 100");

www.books-shop.com

else
if(number > 101 && number < 151)
puts("
101 150");
else
puts("
150");
}
4. , lownum
highnum. lownum highnum.
, , lownum,
highnum. .

main()
{
int lownum, highnum, temp;
puts(" . \n");
puts(" , .\n");
printf(" : ");
scanf("%d", &lownum);
printf(" : ");
scanf("%d", &highnum);
putchar('\n');
if(lownum < highnum)
puts("
.\n");
else
{
temp = highnum;
highnum = lownum;
lownum = temp;
puts(" .\n");
puts(" .\n");
}
printf(" lownum
%d\n", lownum);
printf(" highnum
%d\n", highnum);
}
5., :

main()
{
int age;
printf( );
scanf("%f", &age);

www.books-shop.com

if age < 18 then


puts(" ");
else
if age > 18 then
puts(" ");
printf()
. age,
int,
scanf() float.
if
. then .

,
18 .

9
1. 8.10 ( 8) ,
, .

main()
{
float rate, hours, total, regular, extra,
d_time, overtime;
int moredata;
do
{
printf(" : ");
scanf("%f", &rate);
printf("
: ");
scanf("%f", &hours);
d_time=rate * 2;
if (hours <= 40)
{
regular = hours * rate;
extra = 0.0;
overtime = 0.0;
total = regular;
}
else
{
regular = 40 * rate;
extra = hours - 40;
overtime = extra * d_time;

www.books-shop.com

total = regular + overtime;


}
putchar('\n');
printf("
%.2f\n", regular);
printf(" %.2f
\n", extra);
printf(" 1
$%.2f\n", d_time);
printf("
%.2f\n", overtime);
printf(",
%.2f\n", total);
printf(" ?
Y or N: ");
moredata = getchar();
putchar('\n');
}
while(moredata=='y' || moredata=='Y');
}
2. , 6- ,
1 50 ,

$.06

$1.06

$.12

$2.12

main()
{
int cost;
float shipping, total;
puts("\t\t\n");
for(cost=1;cost<51;cost++)
{
shipping = cost * 0.06;
total = cost + shipping;
printf("$%d\t$%.2f\t\t$%6.2f\n",cost,
shipping, total);
}
}
3. , 0 25.

main()
{
int count, number;

www.books-shop.com

for(count=1;count<11;count++)
{
printf(" N%d", count);
putchar('\n');
do
{
printf("
0 25: ");
scanf("%d",&number);
}
while(number<0 || number > 25);
putchar('\n');
}
}
4. , :

*****
****
***
**
*
**
***
****
*****
main()
{
int outer,inner;
for(outer=5;outer>0;outer--)
{
for(inner=1;inner<=outer;inner++)
printf("*");
putchar('\n');
}
for(outer=2;outer<6;outer++)
{
for(inner=1;inner<=outer;inner++)
printf("*");
putchar('\n');
}
}
5., :

main()

www.books-shop.com

{
float row, column;
puts("\t\t \n\n");
for (row = 1; row <= 10; row++
{
for (column = 1; column <= 10; column+
printf("%d", row * column);
}
putchar('\n');
}
row column
float,
, int.
putchar('\n')
,
. , ,
.
,
.

10
1. , , 20 .

main()
{
char names[20][20], street[20][20],
city[20][20];
char state[20][3], zip[20][6],
phone[20][13], lookfor[20];
int count;
for(count=0;count<20;count++)
{
puts(" ");
gets(names[count]);
puts(" ");
gets(street[count]);
puts(" ");
gets(city[count]);
puts(" ");
gets(state[count]);
puts(" ");
gets(zip[count]);
puts(" ");

www.books-shop.com

gets(phone[count]);
}
}
2. 1 , .

main()
{
char names[20][20], street[20][20],
city[20][20];
char state[20][3], zip[20][6],
phone[20][13], lookfor[20];
int count;
for(count=0;count<20;count++)
{
puts(" : ");
gets(names[count]);
puts(" : ");
gets(street[count]);
puts(" : ");
gets(city[count]);
puts(" : ");
gets(state[count]);
puts(" : ");
gets(zip[count]);
puts(" : ");
gets(phone[count]);
}
puts(" : ");
gets(lookfor);
for(count=0; count<20;count++)
{
if(strcmp(names[count],lookfor)==0)
printf("%s %s\n", names[count],
phone[count]);
}
}
3., :

main()
{
int temps(31);
int index, total;
for (index = 0; index < 31; index++)
{
printf("

www.books-shop.com

#%d: ", index);


scanf("%d", &temps(index));
}
high = temps(0);
low = temps(0);

index = 1;

while (index < 31)


{
if (temps(index) > high)
high = temps(index);
else
low = temps(index);
index++;
}
printf("
%d\n", low);
printf("
%d\n", high);
}
temps
,
. high low
. total ,
.
if...else :

high,
,
low.

11
1. , . , , , .

struct product
{
char name[20];
float cost;
int quantity;
char vendor[20];
} item;
main()
{
puts(" \n\n");
printf(" :");

www.books-shop.com

gets(item.name);
printf(" :");
scanf("%f", &item.cost);
printf(" :");
scanf("%d", &item.quantity);
printf(" :");
gets(item.vendor);
}
2. 1 , , 20 .

struct product
{
char name[20];
float cost;
int quantity;
char vendor[20];
} item[20];
main()
{
int count;
for(count=0;count<20;count++)
{
puts("\n \n\n");
printf(" :");
gets(item[count].name);
printf(" :");
scanf("%f", &item[count].cost);
printf(" :");
scanf("%d", &item[count].quantity);
printf(" :");
gets(item[count].vendor);
}
}
3. 2 , .

struct product
{
char name[20];
float cost;
int quantity;
char vendor[20];
} item[20];
main()
{
%RRNVVKRS

piracy@books-shop.com

float total;
int count;
total=0;
for(count=0;count<20;count++)
{
puts("\n \n\n");
printf(" : ");
gets(item[count].name);
printf(" : ");
scanf("%f", &item[count].cost);
printf(" : ");
scanf("%d", &item[count].quantity);
printf(" : ");
gets(item[count].vendor);
total = total + (item[count].cost *
item[count].quantity);
}
printf(" \
%8.2f", total);
}
4. , float main() , , .

main()
{
float num1, num2;
puts(" ");
scanf("%f",&num1);
puts(" ");
scanf("%f",&num2);
doubleit(&num1, &num2);
}
doubleit(dcount1, dcount2)
float *dcount1, *dcount2;
{
float sq1, sq2;
sq1 = *dcount1 * *dcount1;
sq2 = *dcount2 * *dcount2;
printf(" %f %f\n",*dcount1, sq1);
printf(" %f %f\n",*dcount2, sq2);
}
, :

main()
{
float num1, num2;

www.books-shop.com

puts(" ");
scanf("%f",&num1);
puts(" ");
scanf("%f",&num2);
doubleit(&num1, &num2);
printf(" %f\n", num1);
printf(" %f", num2);
}
doubleit(dcount1, dcount2)
float *dcount1, *dcount2;
{
float sq1, sq2;
*dcount1 = *dcount1 * *dcount1;
*dcount2 = *dcount2 * *dcount2;
}
5., :

main()
{
struct CD
{
char description[40];
char category[12];
char name[20];
float cost;
int number;
} disc;
puts(" ");
printf(" : ");
gets(name);
printf(" : ");
gets(description);
printf(" : ");
gets(category);
printf(" : ");
scanf("%f", &cost);
printf(" : ");
scanf("%d", &number);
puts(" : ");
printf(":

%s\n", name);

printf(":

%s\n", description);

printf(":

%s\n", category);

printf(":
printf(" /:

%6.2f\n", cost);
%d\n", number);

www.books-shop.com

}
,
,
.
disc.name,
disc.description .

12
1. , fputs() , .

#include "stdio.h"
main()
{
FILE *fp;
char flag;
char title[20];
if((fp = fopen("MOVIES","w"))==NULL)
{
puts(" ");
exit();
}
flag = 'y';
while(flag!='n')
{
puts(" : ");
gets(title);
fputs(title, fp);
fputs("\n",fp);
printf(" ?");
flag=getchar();
putchar('\n');
}
fclose(fp);
}
2. , (1) .

#include "stdio.h"
main()
{
FILE *fp;
int index;
char titles[80][12];
index = 0;
if((fp = fopen("MOVIES","r"))==NULL)

www.books-shop.com

{
puts(" ");
exit();
}
while(fgets(titles[index],12,fp)!= NULL)
{
puts(titles[index]);
index++;
if(index>80)
{
puts(" ,
80 .");
break;
}
}
fclose(fp);
}
3. , fprintf() ,
, , .

#include "stdio.h"
main()
{
FILE *fp;
struct product
{
char name[20];
float cost;
int quant;
} item;
if((fp = fopen("MYFILE","w"))==NULL)
{
puts("Cannot open the file");
exit();
}
puts("\n \n\n");
printf(" : ");
gets(item.name);
while(strlen(item.name)>0)
{
printf(" : ");
canf("%f", &item.cost);
printf(" : ");
scanf("%d", &item.quant);

www.books-shop.com

fprintf(fp, "%s %f %d\n", item.name,


item.cost, item.quant);
printf(" : ");
gets(item.name);
}
fclose(fp);
}
4. , , 3.

#include "stdio.h"
main()
{
FILE *fp;
struct product
{
char name[20];
float cost;
int quant;
} item;
if((fp = fopen("MYFILE","r"))==NULL)
{
puts(" ");
exit();
}
while(fscanf(fp,"%s %f %d", item.name,
&item.cost, &item.quant) != EOF)
{
printf(": %s\n", item.name);
printf(" %f\n", item.cost);
printf(": %d\n", item.quant);
}
fclose(fp);
}
5. 3 4 , .

#include "stdio.h"
main()
{
FILE *fp;
struct product
{
char name[20];
loat cost;
int quant;
} item;

www.books-shop.com

if((fp = fopen("MYFILE","w"))==NULL)
{
puts(" ");
exit();
}
puts("\n \n\n");
printf(" : ");
gets(item.name);
while(strlen(item.name)>0)
{
printf(" : ");
scanf("%f", &item.cost);
printf(" : ");
scanf("%d", &item.quant);
fwrite(&item, sizeof(item), 1, fp);
printf(" : ");
gets(item.name);
}
fclose(fp);
}
#include "stdio.h"
main()
{
FILE *fp;
struct product
{
char name[20];
float cost;
int quant;
} item;
if((fp = fopen("MYFILE","r"))==NULL)
{
puts(" ");
exit();
}
while(fread(&item, sizeof(item), 1, fp)==1)
{
printf(": %s\n", item.name);
printf(": %f\n", item.cost);
printf(": %d\n", item.quant);
}
fclose(fp);

www.books-shop.com

}
6., :

#include "stdio.c"
main()
{
FILE fp;
char letter;
if ((fp = fopen("MYFILE", "w")) == NULL)
{
puts(" ");
exit();
}
do
{
letter = getchar();
fputc(letter, fp);
}
while(letter != '\n');
fclose(fp);
}
STDIO.H, STDIO.C.

: *fp. while
\r, \n.

www.books-shop.com

II

13. -.

/*CDAPP.C*/
#include "stdio.h"
#define FILENAME "CDfile"
#define TEMPFILE "Temp"
#define MAX 20
FILE *fp, *tp, *printer;
struct CD
{
char name[20];
char description[40];
char category[12];
float cost;
int number;
} disc;
int slots[MAX];
int count;
main()
{
char select;
getslots();
do
{
puts(" -\n");
puts("1 \n");
puts("2 \n");
puts("3
\n");
puts("4 \n");
puts("5 \n");
puts("6 \n");
puts("7 \n");
puts("8 \n");
printf(", : ");
select = getchar();
putchar('\n');
switch(select)
{
case '1' :
addcd();

www.books-shop.com

break;
case '2':
delcd();
break;
case '3':
chcd();
break;
case '4':
chloc();
break;
case '5':
sort();
break;
case '6':
locate();
break;
case '7':
plist();
break;
case '8':
break;
default:
puts(",
\n\n");
}
}
while(select!='8');
return(0);
}
addcd()
{
int pause;
if(count>>=MAX)
{
puts("
\n");
pause=getchar();
return;
}
if((fp = fopen(FILENAME,"a")) == NULL)
{
printf("
%s\n",FILENAME);

www.books-shop.com

exit();
}
puts("\n");
printf(" : ");
gets(disc.name);
printf(" :");
gets(disc.description);
printf(" :");
gets(disc.category);
printf(" :");
scanf("%f", &disc.cost);
getslot();
fwrite(&disc, sizeof(disc), 1, fp);
fclose(fp);
getslots();
return;
}
delcd()
{
char delname[20];
char fflag;
fflag='n';
openrw();
puts(" \n");
printf(" : ");
gets(delname);
while(fread(&disc, sizeof(disc), 1, fp)==1)
{
if(strcmp(disc.name, delname) != 0)
fwrite(&disc, sizeof(disc), 1, tp);
else
fflag='y';
}
fclose(fp);
fclose(tp);
if(fflag=='n')
nofind();
else
{
openwr();
while(fread(&disc, sizeof(disc), 1, tp)==1)
fwrite(&disc, sizeof(disc), 1, fp);
fclose(fp);
%RRNVVKRS

piracy@books-shop.com

fclose(tp);
}
getslots();
return;
}
chcd()
{
char chname[20];
char fflag;
int pause;
fflag='n';
openrw();
puts(" \n");
printf(" : ");
gets(chname);
while(fread(&disc, sizeof(disc), 1, fp)==1)
{
if(strcmp(disc.name,chname)!=0)
fwrite(&disc, sizeof(disc), 1, tp);
else
{
fflag='y';
puts(" \n");
showdisc();
puts(" \n");
printf(" : ");
gets(disc.name);
printf(" :");
gets(disc.description);
printf(" :");
gets(disc.category);
printf(" :");
scanf("%f", &disc.cost);
if(count>>=MAX)
{
puts("
\n");
pause=getchar();
}
else
{
getslot();
}

www.books-shop.com

fwrite(&disc, sizeof(disc), 1, tp);


}
}
fclose(fp);
fclose(tp);
if(fflag=='n')
nofind();
else
{
openwr();
while(fread(&disc, sizeof(disc), 1, tp)==1)
fwrite(&disc, sizeof(disc), 1, fp);
fclose(fp);
fclose(tp);
}
getslots();
return;
}
chloc()
{
char chname[20];
char fflag;
int pause;
fflag='n';
if(count>>=MAX)
{
puts(" \n");
pause=getchar();
return;
}
openrw();
puts(" \n");
printf(" : ");
gets(chname);
while(fread(&disc, sizeof(disc), 1, fp)==1)
{
if(strcmp(disc.name,chname)!=0)
fwrite(&disc, sizeof(disc), 1, tp);
else
{
fflag='y';
puts(" \n");
showdisc();

www.books-shop.com

puts("\n \n");
getslot();
fwrite(&disc, sizeof(disc), 1, tp);
}
}
fclose(fp);
fclose(tp);
if(fflag=='n')
nofind();
else
{
openwr();
while(fread(&disc, sizeof(disc), 1, tp)==1)
fwrite(&disc, sizeof(disc), 1, fp);
fclose(fp);
fclose(tp);
}
getslots();
return;
}
locate()
{
char name[20];
char fflag, pause;
fflag='n';
if((fp = fopen(FILENAME,"r")) == NULL)
{
printf("
%s\n",FILENAME);
exit();
}
puts(" \n");
printf(" : ");
gets(name);
while(fread(&disc, sizeof(disc), 1, fp)==1)
{
if(strcmp(disc.name,name)==0)
{
fflag='y';
showdisc();
printf("
Enter");
pause=getchar();

www.books-shop.com

putchar('\n');
}
}
fclose(fp);
fclose(tp);
if(fflag=='n')
nofind();
return;
}
plist()
{
if((fp = fopen(FILENAME,"r")) == NULL)
{
printf("
%s\n",FILENAME);
exit();
}
if((printer = fopen("prn","w")) == NULL)
{
printf("
\n");
fclose(fp);
exit();
}
while(fread(&disc, sizeof(disc), 1, fp)==1)
{
fprintf(printer,":
%s\n",disc.name);
fprintf(printer,":
%s\n",disc.description);
fprintf(printer,":
%s\n",disc.category);
fprintf(printer,":
%6.2f\n",disc.cost);
fprintf(printer," /:
%d\n",disc.number);
fprintf(printer,"\n\n");
}
fclose(printer);
fclose(fp);
return;
}
sort()

www.books-shop.com

{
struct CD temp[MAX];
int index, loop1, loop2, endloop;
loop1=0;
loop2=0;
endloop=0;
index = 0;
if((fp = fopen(FILENAME,"r")) == NULL)
{
printf("
%s\n",FILENAME);
exit();
}
while(fread(&disc, sizeof(disc), 1, fp)==1)
{
temp[index]=disc;
index++;
}
fclose(fp);
if((fp = fopen(FILENAME,"w")) == NULL)
{
printf("
%s\n",FILENAME);
exit();
}
for(loop1=1;loop1<<< || disc.number>>MAX || flag==1);
count++;
slots[count]=disc.number;
return;
}
getslots()
{
int index;
index=0;
count=0;
if((fp = fopen(FILENAME,"r")) != NULL)
{
while(fread(&disc, sizeof(disc), 1, fp)==1)
{
slots[index]=disc.number;
index++;
count++;
}

www.books-shop.com

fclose(fp);
}
}

www.books-shop.com