of(); double x = atof(str1);
extern long atol(); long y = atol(str2);
extern int atoi(); int i = atoi(str3);
sscanf(str1, "%f", &x);
sscanf(str2, "%ld", &y); sscanf(str3,"%d", &i);
, - -
sprintf(), printf(),
- , :
. , 1992-95 - 58 - UNIX
char represent[ 40 ];
int i = ... ;
sprintf( represent, "%d", i );
1.117. n- :
n n-1
Y = A * X + A * X + ... + A0
n n-1
():
Y = A0 + X * ( A1 + X * ( A2 + ... + X * An )))...)
:
poly( x, n, an, an-1, ... a0 );
, - UNIX man varargs. :
#include <varargs.h>
double poly(x, n, va_alist)
double x; int n; va_dcl
{
va_list args;
double sum = 0.0;
va_start(args); /* - */
while( n-- >= 0 ){
sum *= x;
sum += va_arg(args, double);
/* . double */
}
va_end(args); /* */
return sum;
}
main(){
/* y = 12*x*x + 3*x + 7 */
printf( "%g\n", poly(2.0, 2, 12.0, 3.0, 7.0));
}
:
double poly(double x, int n, ... );
va_. ,
, va_alist,
va_dcl . , --
va_dcl ! va_list args; "" ;
. va_start(args) -
, va_alist-. va_end(args)
( ,
;
). TYPE
TYPE x = va_arg(args, TYPE);
,
. , 1992-95 - 59 - UNIX
.
char, short, float:
char ch = va_arg(args, char);
int,
int, double . :
int ch = va_arg(args, int);
1.118. PDP-11 ( !):
unsigned x = 2;
printf( "%ld %ld",
- (long) x,
(long) -x
);
-2 65534.
long . sizeof
unsigned. , ?
static struct point{ int x, y ;}
p = { 33, 13 };
FILE *fp = fopen( "00", "w" );
/* */
fseek( fp, (long) sizeof( struct point ), 0 );
/* */
/*!*/ fseek( fp, (long) -sizeof( struct point ), 1 );
/* */
fwrite( &p, sizeof p, 1, fp );
/* */
fclose( fp );
fseek -
? ( - ,
PDP-11).
1.119. :
void g(x){ printf("%d: here\n", x); }
main(){
void (*f)() = g; /* g() */
(*f)(1); /* */
f (2); /* */
/* g(x); */
}
?
typedef void (*(*FUN))(); /*
typedef FUN (*FUN)(); */
FUN g(FUN f){ return f; }
void main(){
FUN y = g(g(g(g(g))));
if(y == g) printf("OK\n");
. , 1992-95 - 60 - UNIX
}
?
char *f(){
return "Hello, user!";
}
g(func)
char * (*func)();
{
puts((*func)());
}
main(){
g(f);
}
main(){
g(f());
}
( signal "
UNIX"):
#include <signal.h>
f(){ printf( "Good bye.\n" ); exit(0); }
main(){
signal ( SIGINT, f() );
...
}
, f() - f (..
return-; - ), f - f (
&f), (" ").
1.120. ? (
):
int f(n){ return n*2; }
int g(n){ return n+4; }
int h(n){ return n-1; }
int (*arr[3])() = { f, g, h };
main(){
int i;
for(i=0; i < 3; i++ )
printf( "%d\n", (*arr[i])(i+7) );
}
1.121. ?
extern double sin(), cos();
main(){ double x; /* cc -lm */
for(x=0.0; x < 1.0; x += 0.2)
printf("%6.4g %6.4g %6.4g\n",
(x > 0.5 ? sin : cos)(x), sin(x), cos(x));
}
. , 1992-95 - 61 - UNIX
extern double sin(), cos();
main(){ double x; double (*f)();
for(x=0.0; x < 1.0; x += 0.2){
f = (x > 0.5 ? sin : cos);
printf("%g\n", (*f)(x));
}
}
1.122. :
n! = 1 * 2 * ... * n
n! = n * (n-1)! 0! = 1
:
/* () */
int factorial1(n){ int res = 1;
while(n > 0){ res *= n--; }
return res;
}
/* */
int factorial2(n){
return (n==0 ? 1 : n * factorial2(n-1));
}
/* ,
* - return,
* " " (tail recursion)
* */
/* */
int fi(f, n) int (*f)(), n;
{ if(n == 0) return 1;
else return n * (*f)(f, n-1);
}
int factorial3(n){ return fi(fi, n); }
/* */
#include <setjmp.h>
jmp_buf checkpoint;
void fact(n, res) register int n, res;
{ if(n) fact(n - 1, res * n);
else longjmp(checkpoint, res+1);
}
int factorial4(n){ int res;
if(res = setjmp(checkpoint)) return (res - 1);
else fact(n, 1);
}
1.123. ,
base. :
. , 1992-95 - 62 - UNIX
printi( n, base ){
register int i;
if( n < 0 ){ putchar( '-' ); n = -n; }
if( i = n / base )
printi( i, base );
i = n % base ;
putchar( i >= 10 ? 'A' + i - 10 : '0' + i );
}
. -
, s -
printi: prints - , printi, putchar();
*res++ = ;
prints. :
static char *res;
... prints ...
char *itos( n, base, s )
char *s; /* char[] */
{
res = s; prints(n, base); *res = '\0';
return s;
}
main(){ char buf[20]; printf( "%s\n", itos(19,2,buf); }
1.124. . ,
8 * sizeof(int) . :
&. :
printb(n){
register i;
for(i = 8 * sizeof(int) - 1; i >= 0; --i)
putchar(n & (1 << i) ? '1':'0');
}
1.125. ,
. :
printf( "%d %s", n, grammar( n, "", "", "" ));
:
char *grammar( i, s1, s2, s3 )
char *s1, /* */
*s2, /* */
*s3; /* , , */
{
i = i % 100;
if( i > 10 && i <= 20 ) return s1;
i = i % 10;
if( i == 1 ) return s2;
if( i == 2 || i == 3 || i == 4 )
return s3;
return s1;
}
. , 1992-95 - 63 - UNIX
1.126. printf, 0..99
, 10 :
00 01 ... 09 10 11 ...
, .
:
printf ("%s%d", n < 10 ? "0" : "", n);
printf ("%02d", n );
printf ("%c%c", '0' + n/10, '0' + n%10 );
1.127. , .
putchar( "c" ); .
putchar( 'c' ); .
, putchar - , "c" -
. (, -
) (,
- ).
printf ( '\n' ); /* */
putchar( "\n" ); /* */
putchar( "ab" ); /* */
putchar( 'ab' ); /* */
char c; if((c = getchar()) == "q" ) ... ;
/* 'q' */
- ! ( -
).
1.128. " ",
. :
int m[20]; int i = 0;
while( scanf( "%d", & m[i++] ) != EOF );
printf( " %d \n", i );
i 1 , . .
: ,
scanf EOF, i++ scanf . -
while( scanf( "%d", & m[i] ) != EOF ) i++;
1.129. :
printf( "Hello \n" );
\n , .
( )
printf( "Hello\n" );
. , 1992-95 - 64 - UNIX
, - -
. .
printf( "Hello\n " );
.
1.130. printf - , .. . -
char s[20]; int i;
...
printf( "%c", s[i] ); printf( "\n" );
putchar( s[i] ); putchar( '\n' );
printf - ( )
putchar. !
1.131. , "" printf ,
. :
int x; ...
printf( x ? " x=%d\n" : "x \n\n", x);
- . x!=0, x -
%d. x==0, , %-.
x . , -
int x = ... ;
printf( x > 30000 ? "%f\n" : "%d\n", x);
( x 31000.000000) ,
%f .
x double:
printf("%f\n", (double) x);
?
printf( x > 30000 ? "%f\n" : "%d\n",
x > 30000 ? (double) x : x );
: . "" - double.
double %d.
if:
if( x > 30000 ) printf("%f\n", (double)x);
else printf("%d\n", x);
1.132. , :
- , - (
).
#define KBYTE 1024L /* */
#define THOUSAND 1024L /* . */
. , 1992-95 - 65 - UNIX
void tellsize(unsigned long sz){
if(sz < KBYTE) printf("%lu ", sz);
else{
unsigned long Kb = sz/KBYTE;
unsigned long Mb = Kb/THOUSAND;
unsigned long Dec = ((sz % KBYTE) * 10) / KBYTE;
if( Mb ){
Kb %= THOUSAND;
printf( Dec ? "%lu.%03lu.%01lu ." : "%lu.%lu .",
Mb, Kb, Dec );
} else
printf( Dec ? "%lu.%01lu .":"%lu .", Kb, Dec);
}
putchar('\n');
}
1.133.
printf("%s", string); /* A */
printf(string); /* B */
B, '%'
char string[] = "abc%defg";
%d . -, %d
; - - ,
- string?! - !
1.134.
char s[20];
scanf("%s", s); printf("%s\n", s);
..
""?
: , %s \n,
, , \n; %s .
, ( ) :
scanf("%[^\n]\n", s);
%[^\n] - , \n ( \n)
\n - \n
%[abcdef] - ,
.
%[^abcde] - ,
( ).
:
1856 1939
s , y - ,
- . ? " " %*:
scanf("%s%*s%d%*[^\n]\n",
s, &y );
. , 1992-95 - 66 - UNIX
%* , *,
, "" .
"%*[^\n]\n"
"" , .
" ", "\t", "\n" , ,
,
int c;
while((c = getc(stdin))== ' ' || c == '\t' || c == '\n' );
%*[ \t\n]
(%d, %o, %u, %ld, %x, %e, %f), %s,
.
scanf("%d%d", &x, &y);
scanf("%d %d", &x, &y);
( %d ).
%c %[... , "12 5 x"
main(){ int n, m; char c;
scanf("%d%d%c", &n, &m, &c);
printf("n=%d m=%d c='%c'\n", n, m, c);
}
"n=12 m=5 c=' '", c ( x),
x.
%s %s ,
. -
,
scanf("%[^\n]%*1[\n]", s);
1 \n,
, "\n". ( )
( \n).
: gets() fgets()!
1.135. scanf: scanf
( %-) EOF . ,
,
12 quack
int d1; double f; scanf("%d%lf", &d1, &f);
scanf 12 %d d1, quack -
%lf, scanf 1 ( -
). quack -
; f .
1.136. const, , -
, , .
const #define, ,
.
. , 1992-95 - 67 - UNIX
const int x = 22;
x = 33; /* : */
const :
-
const char *pc = "abc";
pc[1] = 'x'; /* */
pc = "123"; /* OK */
-
char *const cp = "abc";
cp[1] = 'x'; /* OK */
cp = "123"; /* */
-
const char *const cpc = "abc";
cpc[1] = 'x'; /* */
cpc = "123"; /* */
const TYPE*
int a = 1;
const int b = 2;
const int *pca = &a; /* OK, a */
const int *pcb = &b; /* OK */
int *pb = &b; /* , */
*pb = 3; /* b */
1.137. qsort ( quick sort)
: TYPE
TYPE arr[N];
qsort(arr,/* ? : arr+m */
N, /* ? */
/* : n < N */
sizeof(TYPE),/* sizeof arr[0] */
/* */
cmp);
int cmp(TYPE *a1, TYPE *a2);
*a1 *a2. - -
. cmp - ,
. cmp()
< 0, *a1 *a2 <
= 0, *a1 *a2 ==
> 0, *a1 *a2 >
(char *),
(char **). :
. , 1992-95 - 68 - UNIX
char *arr[N]; ...
cmps(s1, s2) char **s1, **s2;
{ return strcmp(*s1, *s2); }
( strcmp " "). ,
( TurboC++ |-)
int cmp (const void *a1, const void *a2);
:
cmps (const void *s1, const void *s2)
{ return strcmp(*(char **)s1, *(char **)s2); }
:
int cmps(char **s1, char **s2){
return strcmp(*s1, *s2);
}
typedef int (*CMPS)(const void *, const void *);
qsort((void *) array, ..., ..., (CMPS) cmps);
,
int cmps(const void *A, const void *B){
return strcmp(A, B);
}
:
int arr[N]; ...
cmpi(i1, i2) int *i1, *i2;
{ return *i1 - *i2; }
, key,
struct XXX{ int key; ... } arr[N];
cmpXXX(st1, st2) struct XXX *st1, *st2;
{ return( st1->key - st2->key ); }
long.
long arr[N]; ...
cmpl(L1, L2) long *L1, *L2;
{ return *L1 - *L2; }
: , . cmpl ,
long- long. int (
). ( long- )
! :
main(){
int n; long a = 1L; long b = 777777777L;
n = a - b; /* ... */
printf( "%ld %ld %d\n", a, b, n );
}
____________________
|- TurboC - MS DOS, Borland International.
. , 1992-95 - 69 - UNIX
1 777777777 3472. :
cmpl(L1, L2) long *L1, *L2; {
if( *L1 == *L2 ) return 0;
if( *L1 < *L2 ) return (-1);
return 1;
}
cmpl(L1, L2) long *L1, *L2; {
return( *L1 == *L2 ? 0 :
*L1 < *L2 ? -1 : 1 );
}
, .
, -
qsort():
int cmp(...){ ... } /* */
...
qsort(..... , cmp);
, ,
:
int cmp();
qsort(..... , cmp);
...
int cmp(...){ ... } /* */
1.138. ,
W:
a.c b.c
-------------------------- ------------------------------
#include <fcntl.h> #include <fcntl.h>
struct W{ int x,y; }a; struct W{ int x,y; }b;
main(){ int fd; main(){ int fd;
a.x = 12; a.y = 77; fd = open("f", O_RDONLY);
fd = creat("f", 0644); read(fd, &b, sizeof b);
write(fd, &a, sizeof a); close(fd);
close(fd); printf("%d %d\n", b.x, b.y);
} }
,
struct W { long x,y; };
struct W { char c; int x,y; };
a.c b.c? ?
, (
), ,
- (typedef);
- ;
- ( #define);
- ;
include- (header-), -
.
. , 1992-95 - 70 - UNIX
, .
include-,
, , -
!
W.h
-----------------------
struct W{ long x, y; };
a.c b.c
-------------------------- ------------------
#include <fcntl.h> #include <fcntl.h>
#include "W.h" #include "W.h"
struct W a; struct W b;
main(){ ... main(){ ...
printf("%ld...
, ( , -
, ...) - , -
,
- #include. , ,
! .h,
"header-file" (-).
include-
Makefile - make|-:
all: a b
echo a b
a ; b
a: a.c W.h
cc a.c -o a
b: b.c W.h
cc b.c -o b
make
: ____
,
____.
, ""
(), ( st_mtime stat UNIX).
1.139. . -
"", .
.
static:
- static
(.. )
.
- , , (
). static
- .
- , static, .
- () -
(
____________________
|- make UNIX.
. , 1992-95 - 71 - UNIX
) .
static:
f(x) static x; { x++; }
.
:
- ( ). -
static. "
" - ,
.
,
static.
- ( ).
-
. .
- ,
-, extern (""). -
extern int :
// A.c
int x, y, z; //
char ss[200]; // .
static int v, w; //
static char *s, p[20]; // .
int f(){ ... } // .
char *g(){ ... } // .
static int h(){ ... } // .
static char *sf(){ ... } // .
int fi(){ ... } // .
// B.c
extern int x, y;
extern z; // int
extern char ss[]; //
extern int f();
char *g(); // extern
extern fi(); // int
- -
:
extern int x, y; /* import from A.c */
char *tgetstr(); /* import from termlib */
A.c B.c |=
____________________
|= Makefile
CFLAGS = -O
AB: A.o B.o
cc A.o B.o -o AB
A.o: A.c
cc -c $(CFLAGS) A.c
B.o: B.c
cc -c $(CFLAGS) B.c
make.
. , 1992-95 - 72 - UNIX
cc A.c B.c -o AB
"x "?
A.c B.c
-----------------------------------------
int x=12; int x=25;
main(){ f(y) int *y;
f(&x); {
printf("%d\n", x); *y += x;
} }
: , x.
( ) x (
):
static int x=...;
"_f "?
A.c B.c
----------------------------------------------------
int x; extern int x;
main(){ f(5); g(77); } g(n){ f(x+n); }
f(n) { x=n; } f(m){ printf("%d\n", m); }
: B.c f : static f(m)...
main,
. - "_main
". main ! -
- , ( )
|=.
1.140. ?
A.c B.c
----------------------------------------------------
extern int x; extern int x;
main(){ x=2; f(){
f(); printf("%d\n", x);
} }
: x extern,
, .. x .
extern!
1.141. ?
A.c B.c
----------------------------------------------------
int x; extern double x;
... ...
. , ..
, , ""
____________________
|= "" , -
, ,
. , main()
- , -
.
. , 1992-95 - 73 - UNIX
,
. ,
! extern
include-:
proto.h
------------------
extern int x;
A.c B.c
------------------ ------------------
#include "proto.h" #include "proto.h"
int x; ...
, x A.c extern - ,
.. -
( extern - !).
1.142. ?
int a = 1; /* Bjarne Stroustrup- */
void f(){
int b = 1;
static int c = 1;
printf("a=%d b=%d c=%d\n", a++, b++, c++);
}
void main(){
while(a < 4) f();
}
:
a=1 b=1 c=1
a=2 b=1 c=2
a=3 b=1 c=3
1.143. , .
?
/* A.c */
int x=666; /*.*/
main(){
f(3);
printf(" ::x = %d\n", x);
g(2); g(5);
printf(" ::x = %d\n", x);
}
g(n){
static int x=17; /* g*/
printf("g::x = %2d g::n = %d\n", x++, n);
if(n) g(n-1); else x = 0;
}
/* B.c */
extern x; /**/
f(n){ /* */
x++; /**/
{ int x; /* */
x = n+1; /**/
. , 1992-95 - 74 - UNIX
n = 2*x; /**/
}
x = n-1; /**/
}
1.144. ,
- ,
( "");
- (
);
- , (
return);
( ) (pure).
( ) "" -
, -
" " .
, .. ""
.
- ,
( ) .
" ", (
, ""
, ).
:
FILE *fp; ... /* */
char delayedInput ()
{
static char prevchar; /* */
char c;
c = prevchar;
prevchar = getc (fp);
return c;
}
:
char delayedInput (char *prevchar, FILE *fp)
{
char c;
c = *prevchar;
*prevchar = getc (fp);
return c;
}
/* : */
FILE *fp1, *fp2; char prev1, prev2, c1, c2;
... x1 = delayedInput (&prev1, fp1);
x2 = delayedInput (&prev2, fp2); ...
, "" (.. prevchar)
.
( -
), ( -
,