American National Standards Institute,
.
. , 1992-95 - 20 - UNIX
#include <stdio.h>
main(){
int max, min, x, n;
for( n=0; scanf("%d", &x) != EOF; n++)
if( n == 0 ) min = max = x;
else{
if( x > max ) max = x;
if( x < min ) min = x;
}
printf( " %d : min=%d max=%d\n",
n, min, max);
}
-
, min=max=array[0];
1.49. ,
() . , -
.
/*
* .
* obj.
* v------.-------.------.-------.------0
* ! ! ! !
* * * * *
* obj
* .
*/
#include <stdio.h>
#include <string.h>
#include <locale.h>
#define obj char
static shsort (v,n,compare)
int n; /* */
obj *v[]; /* */
int (*compare)(); /* */
{
int g, /* , */
i,j; /* */
obj *temp;
for( g = n/2 ; g > 0 ; g /= 2 )
for( i = g ; i < n ; i++ )
for( j = i-g ; j >= 0 ; j -= g )
{
if((*compare)(v[j],v[j+g]) <= 0)
break; /* */
/* */
temp = v[j]; v[j] = v[j+g]; v[j+g] = temp;
/*
* curses- ,
* :
* ,
* */
}
}
. , 1992-95 - 21 - UNIX
/* */
ssort(v) obj **v;
{
extern less(); /* */
int len;
/* */
len=0;
while(v[len]) len++;
shsort(v,len,less);
}
/* .
* , a < b
* , a == b
* , a > b
*/
less(a,b) obj *a,*b;
{
return strcoll(a,b);
/* strcoll - strcmp,
* .
*/
}
char *strings[] = {
"", "", "",
"", "", "",
" ", "",
NULL
};
int main(){
char **next;
setlocale(LC_ALL, "");
ssort( strings );
/* */
for( next = strings ; *next ; next++ )
printf( "%s\n", *next );
return 0;
}
1.50. .
. , 1992-95 - 22 - UNIX
/* . ""
* (animate-) curses.
* cc -o qsort qsort.c -lcurses -ltermcap
*/
#include "curses.h"
#define N 10 /* */
/* , */
int target [N] = {
7, 6, 10, 4, 2,
9, 3, 8, 5, 1
};
int maxim; /* */
/* quick sort */
qsort (a, from, to)
int a[]; /* */
int from; /* */
int to; /* */
{
register i, j, x, tmp;
if( from >= to ) return;
/* <= 1 */
i = from; j = to;
x = a[ (i+j) / 2 ]; /* */
do{
/* */
while( a[i] < x ) i++ ;
/* */
while( x < a[j] ) j--;
if( i <= j ){ /* */
tmp = a[i]; a[i] = a[j] ; a[j] = tmp;
i++; j--;
demochanges(); /* */
}
} while( i <= j );
/* .
* = j - from + 1
* = to - i + 1
* .
* .
* (
* ). :
*/
if( (j - from) < (to - i) ){
qsort( a, from, j );
qsort( a, i, to );
} else {
qsort( a, i, to );
qsort( a, from, j );
}
}
. , 1992-95 - 23 - UNIX
int main (){
register i;
initscr(); /* curses- */
/* */
for( maxim = target[0], i = 1 ; i < N ; i++ )
if( target[i] > maxim )
maxim = target[i];
demochanges();
qsort( target, 0, N-1 );
demochanges();
mvcur( -1, -1, LINES-1, 0);
/* */
endwin(); /* curses- */
return 0;
}
#define GAPY 2
#define GAPX 20
/* */
demochanges(){
register i, j;
int h = LINES - 3 * GAPY - N;
int height;
erase(); /* */
attron( A_REVERSE );
/* */
for( i=0 ; i < N ; i++ )
for( j = 0; j < N ; j++ ){
move( GAPY + i , GAPX + j * 2 );
addch( target[i] >= target[j] ? '*' : '.' );
addch( ' ' );
/* '*'
* .
* target[i] > target[j]
*/
}
attroff( A_REVERSE );
/* */
for( i = 0 ; i < N ; i++ ){
move( GAPY + i , 5 );
printw( "%4d", target[i] );
height = (long) h * target[i] / maxim ;
for( j = 2 * GAPY + N + (h - height) ;
j < LINES - GAPY; j++ ){
move( j, GAPX + i * 2 );
addch( '|' );
}
}
refresh(); /* */
sleep(1);
}
. , 1992-95 - 24 - UNIX
1.51. goto
.
if ( i > 10 ) goto M1;
goto M2;
M1: j = j + i; flag = 2; goto M3;
M2: j = j - i; flag = 1;
M3: ;
, ( );
{ ..... Label: } { ..... Label: ; }
1.52. goto?
: , .. break
( ).
1.53. if- else?
if(...) ... if(...) ... else ...
: ( , else).
( ):
if(...){ ... if(...) ... else ... }
if(...){ ... if(...) ... } else ...
1.54. ,
{...} (), -
if else-:
#define MACRO { x=1; y=2; }
if(z) MACRO;
else .......;
if(z) { x=1; y=2; } /* if- */ ;
else .......; /* else */
,
if(...) _;
else .....
if(...){ ; ...; ; }
else .....
-- } . , {...}
do{...}while(0)
#define MACRO do{ x=1; y=2; }while(0)
"" ,
:
. , 1992-95 - 25 - UNIX
if(z) do{ x=1; y=2; }while(0);
else .......;
1.55. ( "")?
int x = 12;
if( x < 20 and x > 10 ) printf( "O'K\n");
else if( x > 100 or x < 0 ) printf( "Bad x\n");
else printf( "x=%d\n", x);
#define and &&
#define or ||
1.56. ? -
:
int i = 0;
char *s = " 3 spaces";
while(*s == ' ' || *s++ == '\t')
printf( " %d\n", ++i);
: || && ; -
|| ( && ) -
. *s==' ' , s++
! :
while(*s == ' ' || *s == '\t'){
printf( " %d\n", ++i); s++;
}
, || && , :
if( x != 0.0 && y/x < 1.0 ) ... ;
0, 0.
x==0 . :
int a[5], i;
for(i=0; i < 5 && a[i] != 0; ++i) ...;
i , a[i] ,
.. .
&& ,
if((cond) && f());
if( cond ) f();
if(C1 && C2 && C3) DO;
if(C1) if(C2) if(C3) DO;
""
. , 1992-95 - 26 - UNIX
if(C1 || C2 || C3) DO;
if(C1) goto ok;
else if(C2) goto ok;
else if(C3){ ok: DO; }
, ||
#include <stdio.h>
main(argc, argv) int argc; char *argv[];
{ FILE *fp;
if(argc < 2 || (fp=fopen(argv[1], "r")) == NULL){
fprintf(stderr, " \n");
exit(1); /* */
}
...
}
argc==1, argv[1] ,
argv[1] !
, -
"" , ..
. MSG ( LANG):
:
1) "MSG=engl"
2) MSG
3) "MSG=rus"
getenv() " UNIX", strchr()
- " ".
#include <stdio.h>
int _ediag = 0; /* : 1- */
extern char *getenv(), *strchr();
#define ediag(e,r) (_ediag?(r):(e))
main(){ char *s;
_ediag = ((s=getenv("MSG")) != NULL &&
strchr("rR", *s) != NULL);
printf(ediag("%d:english\n", "%d:\n"), _ediag);
}
MSG , s==NULL strchr(s,...)
( NULL-). -
*s=='r'; s NULL, *s (-
NULL , ,
).
1.57. , -
:
a && b = ! ( !a || !b )
a || b = ! ( !a && !b )
,
! !a = a
! (a == b) = (a != b)
:
. , 1992-95 - 27 - UNIX
if( c != 'a' && c != 'b' && c != 'c' )...;
if( !(c == 'a' || c == 'b' || c == 'c')) ...;
1.58. , .
, .
&& || (
, ).
: .
#include <stdio.h>
extern int errno; /* */
FILE *fp;
int openFile(){
errno = 0;
fp = fopen("/etc/inittab", "r");
printf("fp=%x\n", fp);
return(fp == NULL ? 0 : 1);
}
int closeFile(){
printf("closeFile\n");
if(fp) fclose(fp);
return 0;
}
int die(int code){
printf("exit(%d)\n", code);
exit(code);
return 0;
}
void main(){
char buf[2048];
if( !openFile()) die(errno); closeFile();
openFile() || die(errno); closeFile();
/* , die() */
openFile() ? 0 : die(errno); closeFile();
if(openFile()) closeFile();
openFile() && closeFile();
/* closeFile() openFile() */
openFile() && (printf("%s", fgets(buf, sizeof buf, fp)), closeFile());
}
"": (a,b,c) -
c.
1.59. , .
1.60. , .
1.61. ?
for ( i = 36; i > 0; i /= 2 )
printf ( "%d%s", i,
i==1 ? ".\n":", ");
. , 1992-95 - 28 - UNIX
: 36, 18, 9, 4, 2, 1.
1.62. :
main {
int i, j, k(10);
for ( i = 0, i <= 10, i++ ){
k[i] = 2 * i + 3;
for ( j = 0, j <= i, j++ )
printf ("%i\n", k[j]);
}
}
%i, ? ,
int?
1.63. , . -
, 5 .
1.64.
, , , .
1.65. : return -
()-. , return - , .
return ;
return ();
(, exit) - -
: exit(1); exit 1;
1.66. ,
, :
int func (int x) {
if( x > 10 ) return x*2;
if( x == 10 ) return (10);
/* - return; */
}
x < 10 !
.
1.67. , "" .
. getchar() printf().
:
#include <stdio.h> /* standard input/output */
main(){
char buffer[81]; int i;
printf( " :" );
while((i = getstr( buffer, sizeof buffer )) != EOF){
printf( ", %s\n", buffer );
printf( " :" );
}
}
getstr( s, maxlen )
char *s; /* */
int maxlen; /* :
. , 1992-95 - 29 - UNIX
. = maxlen-1 */
{ int c; /* char! ( ?) */
register int i = 0;
maxlen--; /* '\0' */
while(i < maxlen && (c = getchar()) != '\n'
&& c != EOF )
s[i++] = c;
/* , '\n'
* */
s[i] = '\0'; /* */
return (i == 0 && c == EOF) ? EOF : i;
/* */
}
:
fgetstr(buffer,sizeof(buffer),stdin);
fgets ( @1 @2
).
char *fgetstr(char *s, int maxlen, register FILE *fp){
register c; register char *cs = s;
while(--maxlen > 0 && (c = getc(fp)) != EOF){
if(c == '\n') break; /* @1 */
*cs++ = c; /* @2 */
}
if(c == EOF && cs == s) return NULL;
/* , EOF s ! */
*cs = '\0'; return s;
}
, ( max-
len). : "" getstr()
gets(buffer).
1.68. , d %X F,
? 32- .
main(){
unsigned short u = 65535; /* 16 : 0xFFFF */
short d = u; /* 15 + */
printf( "%X %d\n", d, d); /* FFFFFFFF -1 */
}
: ( ). -
?
1.69. 128 ?
main()
{
/*signed*/ char c = 128; /* : 10000000 */
unsigned char uc = 128;
int d = c; /* 32- int */
printf( "%d %d %x\n", c, d, d );
/* -128 -128 ffffff80 */
d = uc;
printf( "%d %d %x\n", uc, d, d );
/* 128 128 80 */
}
. , 1992-95 - 30 - UNIX
: char int (7-),
. int- 1,
. c 128..255 (-
0200). unsigned char int .
:
printf( "%d\n", c & 0377 );
c int (
char int), &0377
( 1), .
1.70.
printf("%d\n", '\377' == 0377 );
printf("%d\n", '\xFF' == 0xFF );
0 ()? : ,
printf("%d %d\n", '\377', 0377);
-1 255, : char '\377'
( 0377 - ).
1.71.
#include <stdio.h>
int main(int ac, char **av){
int c;
while((c = getchar()) != EOF)
switch(c){
case '': printf(" \n"); break;
case '': printf(" \n"); break;
default: printf(" %d\n", c); break;
}
return 0;
}
:
% a.out
202
198
217
215
10
^D
%
default, case '' case ''?
: () 1. case -
int .
. :
void main(void){
int c = '';
printf("%d\n", c);
}
-54
. , 1992-95 - 31 - UNIX
:
#include <stdio.h>
/* */
#define U(c) ((c) & 0xFF)
#define UC(c) ((unsigned char) (c))
int main(int ac, char **av){
int c;
while((c = getchar()) != EOF)
switch(c){
case U(''): printf(" \n"); break;
case UC(''): printf(" \n"); break;
default: printf(" %d\n", c); break;
}
return 0;
}
:
% a.out
198
215
10
^D
%
:
case 0312:
. -
if:
int c;
...
if(c == '') ...
if(c == UC('')) ...
- signed int, signed int. -
, - unsigned.
1.72. , 0 255.
:
int main(int ac, char *av[]){
unsigned char ch;
for(ch=0; ch < 256; ch++)
printf("%d\n", ch);
return 0;
}
, , ch==255,
256. ch++, ch 0, char
. , 1992-95 - 32 - UNIX
256 (2 8 ). 255+1=0
: - unsigned char int. - -
.
int main(int ac, char *av[]){
unsigned char ch;
for(ch=0; ; ch++){
printf("%d\n", ch);
if(ch == 255) break;
}
return 0;
}
1.73. ,
unsigned a, b, c;
a < b + c a - b < c
( - ). ( 32- ):
a = 1; b = 3; c = 2;
printf( "%u\n", a - b ); /* 4294967294,
1 - 3 = -2 */
printf( "%d\n", a < b + c ); /* 1 */
printf( "%d\n", a - b < c ); /* 0 */
unsigned ?
1.74. :
short x = 40000;
printf("%d\n", x);
-25536. . : -
(16 )? x? ( - -
).
1.75.
double x = 5 / 2;
printf( "%g\n", x );
x 2 2.5 ?
: , 2
double. 2.5,
:
double x = 5.0 / 2;
x = 5 / 2.0;
x = (double) 5 / 2;
x = 5 / (double) 2;
x = 5.0 / 2.0;
double.
, :
. , 1992-95 - 33 - UNIX
double g = 9.0;
int t = 3;
double dist = g * t * t / 2; /* 40.5 */
dist = g * (t * t / 2); /* 36.0 */
dist = g * (t * t / 2.0); /* 40.5 */
, - ? ?
1.76. int 16 :
long n = 1024 * 1024;
long nn = 512 * 512;
printf( "%ld %ld\n", n, nn );
0 0 1048576 262144?
: (2**20 2**18) - ;
16 , . 0.
long (32 ) -
0.
, =
long 32 .
long:
long n = (long) 1024 * 1024;
long nn = 512 * 512L;
1.77. :
x - = 4; /* x 4 */
: `-' `=' .
x @= expr;
x = x @ expr;
( @ - + - * / % ^ >> << & |), x -
(.. , ).
a=a+n a+=n; ,
a. a+=n ; a=a+n .
. , 1992-95 - 34 - UNIX
#include <stdio.h>
static int x = 0;
int *iaddr(char *msg){
printf("iaddr(%s) for x=%d evaluated\n", msg, x);
return &x;
}
int main(){
static int a[4];
int *p, i;
printf( "1: "); x = 0; (*iaddr("a"))++;
printf( "2: "); x = 0; *iaddr("b") += 1;
printf( "3: "); x = 0; *iaddr("c") = *iaddr("d") + 1;
for(i=0, p = a; i < sizeof(a)/sizeof(*a); i++) a[i] = 0;
*p++ += 1;
for(i=0; i < sizeof(a)/sizeof(*a); i++)
printf("a[%d]=%d ", i, a[i]);
printf("offset=%d\n", p - a);
for(i=0, p = a; i < sizeof(a)/sizeof(*a); i++) a[i] = 0;
*p++ = *p++ + 1;
for(i=0; i < sizeof(a)/sizeof(*a); i++)
printf("a[%d]=%d ", i, a[i]);
printf("offset=%d\n", p - a);
return 0;
}
:
1: iaddr(a) for x=0 evaluated
2: iaddr(b) for x=0 evaluated
3: iaddr(d) for x=0 evaluated
iaddr(c) for x=0 evaluated
a[0]=1 a[1]=0 a[2]=0 a[3]=0 offset=1
a[0]=1 a[1]=0 a[2]=0 a[3]=0 offset=2
,
a[i++] += z;
a[i] = a[i] + z; i++;
a[i++] = a[i++] + z;
1.78. y = ++x;
y = (x = x+1, x);
y = x++;
y = (tmp = x, x = x+1, tmp);
y = (x += 1) - 1;
tmp - , x. `,'
. , 1992-95 - 35 - UNIX
( . ).
x=1. x y
y = ++x + ++x + ++x;
1.79. i=4. x i
x = --i + --i + --i;
1.80. x=1. x y
y = x++ + x++ + x++;
1.81. i=4. i y
y = i-- + i-- + i--;
1.82.
char *p = "Jabberwocky"; char s[] = "0123456789?";
int i = 0;
s[i] = p[i++]; *p = *++p;
s[i] = i++;
*p++ = f( *p );
: , ,
: . , ,
! i s[i]: 0 1 (++ ),
int i = 0; s[i] = i++;
s[0] = 0; s[1] = 0; ?
p *p: ?
s[i++] = p[i++];
,
int i=0, j=0;
s[i++] = p[j++];
, -
.
if( a[i++] < b[i] )...;
, , :
b[i] a[i++] ( b[i+1] ).
, :
if( a[i] < b[i+1] )...; *p = *(p+1);
i++; ++p;
. , 1992-95 - 36 - UNIX
, i++ ++i i i+1 ,
i. ,
i+1, i=i+1.
if( a[i] < a[i+1] ) ... ; /* */
if( a[i] < a[++i] ) ... ; /* */
1.83. (
- ?).
int f(x,s) int x; char *s;
{ printf( "%s:%d ", s, x ); return x; }
main(){
int x = 1;
int y = f(x++, "f1") + f(x+=2, "f2");
printf("%d\n", y);
}
f1:1 f2:4 5
f2:3 f1:3 6
( f() -
: ?). :
int y = 2;
int x = ((y = 4) * y );
printf( "%d\n", x );
16, 8 , ..
.
y = 4; x = y * y;
1.84.
f(x++, x++); f(x, x++);
: , .
f( c = getchar(), c );
c = getchar(); f(c, c);
( ). :
...
case '+':
push(pop()+pop()); break;
case '-':
push(pop()-pop()); break;
...
. , 1992-95 - 37 - UNIX
...
case '+':
push(pop()+pop()); break;
case '-':
{ int x = pop(); int y = pop();
push(y - x); break;
}
...
:
int x = 0;
printf( "%d %d\n", x = 2, x ); /* 2 0 2 2 */
struct pnt{ int x; int y; }arr[20]; int i=0;
...
scanf( "%d%d", & arr[i].x, & arr[i++].y );
i++ , x. :
main(){
int i = 3;
printf( "%d %d %d\n", i += 7, i++, i++ );
}
, IBM PC |- PDP-11 |=
( 12 4 3). ,
( ).
1.85. x=1 x=0 - -
:
#include <stdio.h>
void main(){
int c = 1;
int x = c - c++;
printf( "x=%d c=%d\n", x, c );
exit(0);
}
?
left = c; right = c++; x = left - right;
right = c++; left = c; x = left - right;
left right -
.
____________________
|- IBM ("--") - International Buisiness Machines Corporation.
IBM PC Intel.
|= PDP-11 - (Programmed Data Processor) - DEC (Digital Equipment
Corporation), -1420. VAX.
. , 1992-95 - 38 - UNIX
:
x = c-- - --c; /* c-----c */
1.86. , 1 3 0 6.
. :
int x = 0xF0;
x |= (1 << 3);
x &= ~(1 << 6);
( -
). :
#define A 0x08 /* */
#define B 0x40 /* */
: x |= A|B;
: x &= ~(A|B);
A : if( x & A ) ...;
, : if((x & (A|B)) == (A|B))...;
, : if((x & (A|B)) == 0 )...;
, : if( x & (A|B))...;
, A : if((x & (A|B)) == A)...;
,
x y : diff = x ^ y;
1.87. "": -
, -
. .
:
#define SET(n,a) (a[(n)/BITS] |= (1L <<((n)%BITS)))
#define CLR(n,a) (a[(n)/BITS] &= ~(1L <<((n)%BITS)))
#define ISSET(n,a) (a[(n)/BITS] & (1L <<((n)%BITS)))
#define BITS 8 /* bits per char ( ) */
/* */
enum fruit { APPLE, PEAR, ORANGE=113,
GRAPES, RAPE=125, CHERRY};
/* : n 0..(25*BITS)-1 */
static char fr[25];
main(){
SET(GRAPES, fr); /* */
if(ISSET(GRAPES, fr)) printf("here\n");
CLR(GRAPES, fr); /* */
}
1.88. , N
. , :
i- . (
) N-1. N,
i- ( ) N-1.
i .
- i-
( ).
, , .
:
. , 1992-95 - 39 - UNIX
/* n m */
extern void *calloc(unsigned nelem, unsigned elsize);
/* , .
* .
* - free();
*/
extern void free(char *ptr);
static int N, M, number;
static char *scale; /* */
int *res; /* */
/* ... SET, CLR, ISSET, BITS ... */
static void choose(int ind){
if(ind == M){ /* */
register i;
printf(" #%04d", ++number);
for(i=0; i < M; i++) printf(" %2d", res[i]);
putchar('\n'); return;
} else
/* ind-
* .
*/
for(res[ind] = 0; res[ind] < N; ++res[ind])
if( !ISSET(res[ind], scale)) {
/* */
SET(res[ind], scale); /* */
choose(ind+1);
CLR(res[ind], scale); /* */
}
}
void arrange(int n, int m){
res = (int *) calloc(m, sizeof(int));
scale = (char *) calloc((n+BITS-1)/BITS, 1);
M = m; N = n; number = 0;
if( N >= M ) choose(0);
free((char *) res); free((char *) scale);
}
void main(int ac, char **av){
if(ac != 3){ printf("Arg count\n"); exit(1); }
arrange(atoi(av[1]), atoi(av[2]));
}
n!/(n-m)! , x! = 1*2*...*x - "-
". 0! = 1. , -
:
res = init_iterator(n, m);
/* ,