第 1 章 見
21 1-1 見えないエラー 見 List 1-1 "max2x1.h" a, b max2 List 1-1 chap01/max2x1.h max2 "max2x1.h" #define max2(a, b) ((a) > (b)? (a) : (b)) max2 List 1-2 List 1-2 chap01/max2x1test.c max2 #include <stdio.h> #include "max2x1.h" 実行結果 int main(void) int x, y; printf("x"); printf("y"); scanf("%d", &x); scanf("%d", &y); printf("max2(x, y) = %d\n", max2(x, y)); return 0; "max2x1.h" EOF "max2x1.h" Fig.1-1 a
3a #define max2(a, b) ((a) > (b)? (a) : (b)) EOF b #define max2(a, b) ((a) > (b)? (a) : (b)) Ÿ EOF EOF Ÿ 1-1 Fig.1-1 #define #define #include preprocessing directive b a 4 4 4 4 4 4 4 4 4
41 見 "max2x1.h" List 1-2 '0x81' '0x40' JIS Fig.1-2 x, y int int x, y; Fig.1-2 white-space character # List 1-3 fopen fclose 8
5 List 1-3 detab #include <stdio.h> #include <stdlib.h> --- srcdst --- void detab(file *src, FILE *dst, int width) int ch; int pos = 1; chap01/detab.c 1-1 while ((ch = fgetc(src))!= EOF) int num; switch (ch) case '\t': num = width - (pos - 1) % width; for ( ; num > 0; num--) fputc(' ', dst); pos++; break; case '\n': fputc(ch, dst); pos=1; break; default: fputc(ch, dst); pos++; break; int main(int argc, char *argv[]) int width = 8; 8 FILE *fp; 実行方法 detab "test.c"4 > detab -t4 test.c Ÿ 8 > detab test.c xyz.c Ÿ > detab -t4 test.c -t8 xyz.c Ÿ if (argc < 2) detab(stdin, stdout, width); else while (--argc > 0) if (**(++argv) == '-') if (*++(*argv) == 't') width = atoi(++*argv); else fputs("\n", stderr); return 1; else if ((fp = fopen(*argv, "r")) == NULL) fprintf(stderr, "%s\n", *argv); return 1; else detab(fp, stdout, width); fp fclose(fp); return 0;
61 見落 List 1-4 List 1-1 List 1-4 chap01/max2x2.h max2 "max2x2.h" #define max2 (a, b) ((a) > (b)? (a) : (b)) "chap01/max2x2test.c" max2 object-like macro TRUE 1 4 4 #define TRUE 1 TRUE function-like macro 4 4 ( ) ( List 1-4 max2 ( Fig.1-3 a max2 max2 (a, b) ((a) > (b)? (a) : (b)) 4 4 4 4 b a max2 #define max2(a, b) ((a) > (b)? (a) : (b)) max2(x, y) (a, b) ((a) > (b)? (a) : (b))(x, y) b max2 #define max2(a, b) ((a) > (b)? (a) : (b)) max2(x, y) ((x) > (y)? (x) : (y)) Fig.1-3
7 ( 4 4 4 ( z = max2 (x, y); max2( ( ) z = (max2)(x, y); max2 max2 1-1 ( ) List 1-5 List 1-5 chap01/max2.c #include <stdio.h> --- --- #define max2(a, b) ((a) > (b)? (a) : (b)) --- --- int (max2)(int a, int b) puts("max2"); return a > b? a : b; 実行例 x15 Ÿ y7 Ÿ max2(x, y) = 15 max2 (max2)(x, y) = 15 int main(void) int x, y; printf("x"); printf("y"); scanf("%d", &x); scanf("%d", &y); printf("max2(x, y) = %d\n\n", max2(x, y)); printf("(max2)(x, y) = %d\n", (max2)(x, y)); return 0;
81 前処理指令内 空白 #include <stdio.h> # # # # include #if defined( DOHC ) #include <double.h> #else Fig.1-4 #include <single.h> #endif Fig.1-4 #if 指令 注釈 #if Fig.1-5 a = x; a = x; ax Fig.1-5 Fig.1-6 #if #if 0 0 a = x; #endif ax Fig.1-6 #if
9#if 0 #if0 if 0 #if Fig.1-7 # # if # if if Fig.1-7 #if List 1-6 1-1 List 1-6 #if #include <stdio.h> #define DEBUG 0 int main(void) int a = 5; int x = 1; #if DEBUG == 1 a = x; ax #endif printf("a%d\n", a); 1 chap01/debug.c 実行結果㆒ a5 実行結果㆓ a1 return 0; DEBUG 0 DEBUG 1 #if Column 1-1p.12 NDEBUG
101 ㆒ #define para 10 ㆒ #define para 10 ㆓ ㆓ #include "max2.h" #include "max2.h" "abcd.h" max2 "max2.h" #include "max2.h" "max2.h" #include "abcd.h" "max2.h""abcd.h" "max2.h" "def.h" int a; #include "def.h" #include "def.h" List 1-7 List 1-7 "max2.h" #ifndef MAX2 #define MAX2 chap01/max2.h #define max2(a, b) ((a) > (b)? (a) : (b)) #endif MAX2 MAX2 max2 2 MAX2 #ifndef MAX2 inlcude guard 7
11関数形式 実行効率 max2 a, b, c, d x = max2(max2(a, b), max2(c, d)); Fig.1-8 1-1 x = max2(max2(a, b), max2(c, d)); x = ((((a) > (b)? (a) : (b))) > (((c) > (d)? (c) : (d)))? (((a) > (b)? (a) : (b))) : (((c) > (d)? (c) : (d)))); Fig.1-8 max2 x = max2(max2(max2(a, b), c), d); Fig.1-9 x = max2(max2(max2(a, b), c), d); x = ((((((a) > (b)? (a) : (b))) > (c)? (((a) > (b)? (a) : (b))) : (c))) > (d)? (((((a) > (b)? (a) : (b))) > (c)? (((a) > (b)? (a) : (b))) : (c))) > (d)); Fig.1-9 max2 > if x = a; if (b > x) x = b; if (c > x) x = c; if (d > x) x = d;
121 関数形式 副作用 z = max2(x++, y); x y z x z = ((x++) > (y)? (x++) : y); x Column 2-2 side effect Column 1-1 NDEBUG assert List 1-6p.9 NDEBUG assert NDEBUG #define NDEBUG NDEBUG Visual Studio NDEBUG assert NDEBUG <assert.h> NDEBUG assert #define assert(ignore) ((void)0) assert
13NDEBUG assert void assert(int expression); expression 0 abort FILE LINE Assertion failed :, file, line assert List 1C-1 1-1 List 1C-1 assert chap01/assert_div.c #include <stdio.h> #include <assert.h> --- ab --- void div(int a, int b) assert(b!= 0); printf("%d%d%d%d\n", a, b, a / b, a % b); int main(void) int a, b; printf("a = "); scanf("%d", &a); printf("b = "); scanf("%d", &b); div(a, b); 実行例 a = 7 Ÿ b = 2 Ÿ 7231 実行結果一例 a = 7 Ÿ b = 0 Ÿ Assertion failed: b!= 0, file \C\chap01\assert_div.c, line 11 return 0; div a b b 0 b!= 0 b 0 assert(b!= 0) b 0 assert
141 C++ max2 実現 max2 関数 List 1-8 inlineinline function "chap01/max2_inline_test.cpp" List 1-8 max2++ //--- ---// inline int max2(int a, int b) return a > b? a : b; chap01/max2_inline.h max > max2 int 多重定義 function overloading List 1-9 "max2_overload.h" List 1-9 chap01/max2_overload.h max2++ #ifdef cplusplus ++ inline int max2(int a, int b) return a > b? a : b; inline long max2(long a, long b) return a > b? a : b; inline double max2(double a, double b) return a > b? a : b; #else #define max2(a, b) ((a) > (b)? (a) : (b)) #endif cplusplus "chap01/max2_overload_test.cpp" int long double
15関数 function template max2 List 1-10 "max2_template.h" List 1-10 C++ chap01/max2_template.h max2++ #ifdef cplusplus ++ template <typename Type> Type max2(const Type& a, const Type& b) return a > b? a : b; #else #define max2(a, b) ((a) > (b)? (a) : (b)) #endif 1-1 List 1-11 List 1-11 max2++ #include <iostream> #include "max2_template.h" using namespace std; int main(void) int a, b; double x, y; cout << "a"; cin >> a; cout << "b"; cin >> b; cout << "x"; cin >> x; cout << "y"; cin >> y; cout << "max2(a, b) = " << max2(a, b) << '\n'; cout << "max2(x, y) = " << max2(x, y) << '\n'; return 0; chap01/max2_template_test.cpp 実行例 a15 Ÿ b7 Ÿ x1.25 Ÿ y3.14 Ÿ max2(a, b) = 15 max2(x, y) = 3.14 int double