. Unix © Copyright . 1992-95 Email: abs@decart.msu.su mailto:abs@decart.msu.su Txt version is located at http://decart.msu.su:100/~abs/
. , 1992-95 - 1 - UNIX 0. . . , , , . , , , - "C" () UNIX. " " . " " . "   ". IBM PC - UNIX, SPARCstation 20 Solaris 2 ( UNIX svr4), - ( ) MS DOS|=, UNIX. . , . , , - , - "".  "- " , . - . - , - , , ... C++, . Objective-C. - ,   , - UNIX . () " ": man2 ( - ), man3 ( ). - . . , , , . , "" , . , ( FORTRAN-e) . - { }, `;' . "" - - ! , . - FORTRAN- !!! "" , , cb. cb < .c > /tmp/$$ mv /tmp/$$ .c . ("") ( ). - , , . - , , . -, , , ( , strcmp, strcpy, - qsort, malloc, gets) ( - ). ____________________ |= MS DOS - Microsoft Corporation. ( ""); DOS - . . , 1992-95 - 2 - UNIX . - , . , - ( ). UNIX , man  (man - manual, ""). : ! - ( , , ). ( ). - - . . : -8 UNIX - . , : c & 0177 - . - ( UNIX-sites RelCom). ISO 8859/5 . . . Microsoft 1251 , Microsoft Windows. , UNIX ( 1994 ). MS DOS , MS DOS. Macintosh "" . , , - - . Relax and enjoy. - - . , , ! , - . . , 1992-95 - 3 - UNIX 1. . , . 1.1. printf. "Hello, world !" (",  !"). 1.2. #include <stdio.h> main(){ printf("Hello, world\n"); } : , main (int). main - return. : #include <stdio.h> main(){ printf("Hello, world\n"); return 0; } #include <stdio.h> void main(){ printf("Hello, world\n"); exit(0); } - : #include <stdio.h> int main(int argc, char *argv[]){ printf("Hello, world\n"); return 0; } 1.3. #include studio.h main { int i i := 43 print (' i ') } 1.4. , : int n; n = 2; printf ("%d + %d = %d\n", n, n, n + n); 1.5. ? . , 1992-95 - 4 - UNIX if( x > 2 ) then x = 2; if x < 1 x = 1; : then, if, while ()-. 1.6. , , . - printf, - puts. 1.7. - : a = b = 5 a + b a++ + b ++a + b --a + b a-- + b . 1.8. for ________________________________________________________________________________ for(INIT; CONDITION; INCR) BODY ________________________________________________________________________________ INIT; repeat: if(CONDITION){ BODY; cont: INCR; goto repeat; } out: ; while ________________________________________________________________________________ while(COND) BODY ________________________________________________________________________________ cont: repeat: if(CONDITION){ BODY; goto repeat; } out: ; . , 1992-95 - 5 - UNIX do ________________________________________________________________________________ do BODY while(CONDITION) ________________________________________________________________________________ cont: repeat: BODY; if(CONDITION) goto repeat; out: ;  BODY break continue; : #define break goto out #define continue goto cont 1.9. * ** *** **** ***** for. , - . 1.10. , WIDTH, x0 '-', w '*', - '-'. : int x; for(x=0; x < x0; ++x) putchar('-'); for( ; x < x0 + w; x++) putchar('*'); for( ; x < WIDTH ; ++x) putchar('-'); putchar('\n'); for(x=0; x < WIDTH; x++) putchar( x < x0 ? '-' : x < x0 + w ? '*' : '-' ); putchar('\n'); 1.11. , : * *** ***** ******* ********* . , 1992-95 - 6 - UNIX : /* */ #include <stdio.h> /* n c */ printn(c, n){ while( --n >= 0 ) putchar(c); } int lines = 10; /* */ void main(argc, argv) char *argv[]; { register int nline; /* */ register int naster; /* */ register int i; if( argc > 1 ) lines = atoi( argv[1] ); for( nline=0; nline < lines ; nline++ ){ naster = 1 + 2 * nline; /* */ printn(' ', lines-1 - nline); /* */ printn('*', naster); /* */ putchar( '\n' ); } exit(0); /* */ } 1.12. ? main(){ /* 10 */ int i; while(i < 10){ printf("%d- \n", i+1); i++; } } : i 0, - . 10,  ( 0 ). , ! int i = 0; i , 0. for, - : for(i=0; i < 10; i++) printf(...); . , 1992-95 - 7 - UNIX 1.13. , ( - , ) , i, j. , : main(){ int _ ; for( _ = 0; _ < 10; _++) printf("%d\n", _ ); } , - . 1.14. 2 : main(){ int x = 12; printf( "x=%d\n" ); int y; y = 2 * x; printf( "y=%d\n", y ); } : - ( , ). .   ( - ). 1.15. : int n; n = 12; main(){ int y; y = n+2; printf( "%d\n", y ); } : n=12 - . main() y, main() int n = 12; n 12 , . - ( static, ); - 0 ('\0', NULL, ""). - , "" . 1.16. : TYPE x = ; () TYPE x; /* */ x = ; /* */ . , 1992-95 - 8 - UNIX : #include <stdio.h> extern double sqrt(); /* */ double x = 1.17; double s12 = sqrt(12.0); /* #1 */ double y = x * 2.0; /* #2 */ FILE *fp = fopen("out.out", "w"); /* #3 */ main(){ double ss = sqrt(25.0) + x; /* #4 */ ... } #1, #2 #3 . ? : ( s12, y fp , - ) , . , ( ). #4 ss, ..  . , , , ..., . 1.17. , - . EOF. UNIX EOF CTRL D (CTRL ), CTRL/D; MS DOS - CTRL/Z. getchar() putchar() . 1.18. , . : #include <stdio.h> main(){ double cnt = 0.0; while (getchar() != EOF) ++cnt; printf("%.0f\n", cnt ); } : , double. -  ; - double  , int long ( ), . , long cnt; ( "%ld"). 1.19. : a -> b b -> c c -> d ... z -> a -> * . 1.20. : . , 1992-95 - 9 - UNIX B -> A C -> B ... Z -> Y -> * . 1.21. , . ,  ENTER, , - . , ENTER, - '\n' -  ( !). 1.22. , '0' ( ) '\0' ( ). printf( "%d %d %c\n", '\0', '0', '0' ); : ? main(){ int c = 060; /* '0' */ printf( "%c %d %o\n", c, c, c); } 0 48 60? int c = 060; char c = '0'; 1.23. ? #include <stdio.h> void main(){ printf("ab\0cd\nxyz"); putchar('\n'); } , '\0' , '\n' - . "abcd\n" : 'a','b','c','d','\n','\0' "ab\0cd\nxyz" - 'a','b','\0','c','d','\n','x','y',z','\0' "abcd\0" - , ( , ?). printf , . ab . : sizeof("ab\0cd\nxyz")? : 10. 1.24. , 0 100. 1.25. , . . , 1992-95 - 10 - UNIX 1.26. , n . 1.27. , , , . 1.28. , - . 1.29. , (- , C C+1 ~C ). 1.30. , - , . , 'b' "break". 1.31. , 1 200, "=" (), "<" () ">" (). - . 1.32. , 1, 2, 4, 8, ... , , n, - - . 1.33. (): x(0) = a 1 a x(n+1) = - * ( ---- + x(n)) 2 x(n) , | x(n+1) - x(n) | < 0.001 ! . - x . 1.34. , 1000. 1, 2, 3, 5, 7, 11, 13, 17, ... . , 1992-95 - 11 - UNIX /*#!/bin/cc primes.c -o primes -lm * . */ #include <stdio.h> #include <math.h> int debug = 0; /* */ #define eps 0.0001 double sqrt (x) double x; { double sq, sqold, EPS; if (x < 0.0) return -1.0; if (x == 0.0) return 0.0; /* 0 */ EPS = x * eps; sq = x; sqold = x + 30.0; /* != sq */ while (fabs (sq * sq - x) >= EPS) { /* fabs( sq - sqold )>= EPS */ sqold = sq; sq = 0.5 * (sq + x / sq); } return sq; } /* o */ int is_prime (t) register int t; { register int i, up; int not_div; if (t == 2 || t == 3 || t == 5 || t == 7) return 1; /* prime */ if (t % 2 == 0 || t == 1) return 0; /* composite */ up = ceil (sqrt ((double) t)) + 1; i = 3; not_div = 1; while (i <= up && not_div) { if (t % i == 0) { if (debug) fprintf (stderr, "%d %d\n", t, i); not_div = 0; break; } i += 2; /* * , * 2*n, * 2, * . */ } return not_div; } . , 1992-95 - 12 - UNIX #define COL 6 int n; main (argc, argv) char **argv; { int i, j; int n; if( argc < 2 ){ fprintf( stderr, ": %s [-]\n", argv[0] ); exit(1); } i = atoi (argv[1]); /* -> , */ if( argc > 2 ) debug = 1; printf ("\t*** 2 %d ***\n", i); n = 0; for (j = 1; j <= i; j++) if (is_prime (j)){ /* COL */ printf ("%3d%s", j, n == COL-1 ? "\n" : "\t"); if( n == COL-1 ) n = 0; else n++; } printf( "\n---\n" ); exit (0); } 1.35. A + B * I ( ) . scanf printf. , scanf, : - ? int x; scanf( "%d", x ); : " x", scanf( "%d", &x ); 1.36. f(x)=0 . - ( , , - ): /* unsigned long */ #define MAXINT (~0L) /* - unsigned long */ typedef unsigned long ulong; /* , : */ #define FUNC(x, arg) ((x) * (x) - (arg)) /* x*x - arg = 0 x*x = arg, * x = _(arg) */ /* . * FUNC */ #define LEFT_X(arg) 0 #define RIGHT_X(arg) (arg > MAXINT)? MAXINT : (arg/2)+1; /* , . * : * FUNC(x, arg) = 0; x = ? . , 1992-95 - 13 - UNIX */ ulong i_sqrt( ulong arg ) { register ulong mid, /* */ rgt, /* */ lft; /* */ lft = LEFT_X(arg); rgt = RIGHT_X(arg); do{ mid = (lft + rgt + 1 )/2; /* +1 */ if( FUNC(mid, arg) > 0 ){ if( rgt == mid ) mid--; rgt = mid ; /* */ } else lft = mid ; /* */ } while( lft < rgt ); return mid; } void main(){ ulong i; for(i=0; i <= 100; i++) printf("%ld -> %lu\n", i, i_sqrt(i)); } register , , , ( ). register ; register ; /* int */ : & . 1.37. , . C(0,n) = C(n,n) = 1 n = 0... C(k,n+1) = C(k-1,n) + C(k,n) k = 1..n n - , . 1.38. -. . : [A..B], int x = A + rand() % (B+1-A);  ( "") srand( ); /* */   , : srand(NBEG); x=rand(); ... ; x=rand(); /* */ srand(NBEG); x=rand(); ... ; x=rand(); : . , 1992-95 - 14 - UNIX static unsigned long int next = 1L; int rand(){ next = next * 1103515245 + 12345; return ((unsigned int)(next/65536) % 32768); } void srand(seed) unsigned int seed; { next = seed; } : char t[sizeof(long)]; time(t); srand(t[0] + t[1] + t[2] + t[3] + getpid()); 1.39. . /*#!/bin/cc $* -lm * */ #include <math.h> extern double integral(), sin(), fabs(); #define PI 3.141593 double myf(x) double x; { return sin(x / 2.0); } int niter; /* */ void main(){ double integral(); printf("%g\n", integral(0.0, PI, myf, 0.000000001)); /* , myf, myf(). * 2.0 */ printf("%d \n", niter ); } . , 1992-95 - 15 - UNIX double integral(a, b, f, eps) double a, b; /* */ double eps; /* */ double (*f)(); /* */ { register long i; double fab = (*f)(a) + (*f)(b); /* */ double h, h2; /* */ long n, n2; /* */ double Sodd, Seven; /* f */ double S, Sprev;/* */ double x; /* */ niter = 0; n = 10L; /* */ n2 = n * 2; h = fabs(b - a) / n2; h2 = h * 2.0; /* */ /* : */ for( Sodd = 0.0, x = a+h, i = 0; i < n; i++, x += h2 ) Sodd += (*f)(x); /* : */ for( Seven = 0.0, x = a+h2, i = 0; i < n-1; i++, x += h2 ) Seven += f(x); /* : */ S = h / 3.0 * (fab + 4.0 * Sodd + 2.0 * Seven ); do{ niter++; Sprev = S; /* */ h2 = h; h /= 2.0; if( h == 0.0 ) break; /* */ n = n2; n2 *= 2; Seven = Seven + Sodd; /* : */ for( Sodd = 0.0, x = a+h, i = 0; i < n; i++, x += h2 ) Sodd += (*f)(x); /* */ S = h / 3.0 * (fab + 4.0 * Sodd + 2.0 * Seven ); } while( niter < 31 && fabs(S - Sprev) / 15.0 >= eps ); /* */ return ( 16.0 * S - Sprev ) / 15.0 ; /* */ } . , 1992-95 - 16 - UNIX 1.40. ? struct time_now{ int hour, min, sec; } X = { 13, 08, 00 }; /* 13 08 00 .*/ : 08 - ( )! 8 9 . 1.41. : int i = -2; i <<= 2; printf("%d\n", i); /* i : -8 */ i >>= 2; printf("%d\n", i); /* -2 */ ( ): int i = -2; i <<= 2; /* printf("%d\n", i); /* i : -8 */ i >>= 2; */ printf("%d\n", i); /* -2 */ ? : ? : . : #ifdef COMMENT ... ... #endif /*COMMENT*/ /**/ printf("here");/* */ /* printf("here");/* */ /* (); /**/ (); /**/ ( ) - , : /*printf("here"); */ 1.42. i2 ? . , 1992-95 - 17 - UNIX int main(int argc, char *argv[]){ int i1, i2; i1 = 1; /* i1 / i2 = 2; /* i2 */ printf("Numbers %d %d\n", i1, i2); return(0); } : - ! : int main(int argc, char *argv[]){ int i1, i2; i1 = 1; /* i1 */ i2 = 2; /* i2 */ printf("Numbers %d %d\n", i1, i2); return(0); } 1.43. "" . void main(){ int n = 10; int *ptr = &n; int x, y = 40; x = y/*ptr /* 4 */ + 1; printf( "%d\n", x ); /* */ exit(0); } /* - Relcom */ ... cost = nRecords/*pFactor /* divided by Factor, and */ + fixMargin; /* plus the precalculated */ ... . , y/*ptr ! . x = y / *ptr /* 4 */ + 1; 1.44. |- ( ). ____________________ |- - /lib/cpp . , 1992-95 - 18 - UNIX | | #include <stdio.h> |#include < sys/types.h > |# define inc (x) ((x) + 1) |#define N 12; |#define X -2 | |... printf( "n=%d\n", N ); |... p = 4-X; : #. . <> , - ! "" . - inc (x) , :    inc(x) ,  inc (x)((x)+1). , # . - ; . - printf() printf( "n=%d\n", 12; ); ; . , p=4-X; p=4--2; . - , p = 4 - X; /* */ ( ) : #define X (-2) 1.45. max(x, y), . . min(x, y) abs(x) (abs - ). : #define abs(x) ((x) < 0 ? -(x) : (x)) #define min(x,y) (((x) < (y)) ? (x) : (y)) x (x)? , #define abs(x) (x < 0 ? -x : x ) abs(-z) abs(a|b) (-z < 0 ? --z : -z ) (a|b < 0 ? -a|b : a|b ) "" --z; a|b<0 a|(b<0), - ! . ! ,  : #define div(x, y) (x)/(y) . , 1992-95 - 19 - UNIX z = sizeof div(1, 2); z = sizeof(1) / (2); sizeof(int)/2, sizeof(int). #define div(x, y) ((x) / (y)) . 1.46. , , : int sqr(int x){ return x * x; } #define SQR(x) ((x) * (x)) main(){ int y=2, z; z = sqr(y++); printf("y=%d z=%d\n", y, z); y = 2; z = SQR(y++); printf("y=%d z=%d\n", y, z); } sqr "y=3 z=4", . SQR z = ((y++) * (y++)); "y=4 z=6", z 2. 1.47. ANSI |- ## - " ": #define VAR(a, b) a ## b #define CV(x) command_ ## x main(){ int VAR(x, 31) = 1; /* int x31 = 1; */ int CV(a) = 2; /* int command_a = 2; */ ... } , - : #define VAR(a, b) a/**/b , , . , - . 1.48. , , . , max min ! ____________________ |- ANSI -