本文共 11187 字,大约阅读时间需要 37 分钟。
gcc -vConfigured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1Apple LLVM version 10.0.0 (clang-1000.11.45.5)Target: x86_64-apple-darwin18.2.0Thread model: posixInstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/* rhodium.c -- 用金属铑衡量您的体重 */#includeint main(void){ float weight; // 用户的体重 float value; // 相等重量的铑的价值 printf("Are you worth your weight in rhodium?\n"); printf("Let`s check it out. \n"); printf("Please entr your weight in pounds: "); /* 从用户输入 */ scanf("%f", &weight); // 假设铑盎司770美元 // 14.5833 把常衡制的英镑转换为金衡制的盎司 value = 770 * weight * 14.5833; printf("Your weight in rhodium is worth $%0.2f.\n", value); printf("You are easily worth that! If rhodium prices drop. \n"); printf("eat more to maintain your value.\n"); return 0;}
Panda-MBP:CodeList3-1 panda8z$ ./rhodium.out Are you worth your weight in rhodium?Let`s check it out. Please entr your weight in pounds: 80Your weight in rhodium is worth $898331.25.You are easily worth that! If rhodium prices drop. eat more to maintain your value.Panda-MBP:CodeList3-1 panda8z$
/* print1.c -- 说明printf()的一些属性 */#includeint main(void){ int ten = 10; int two = 2; printf("Doing it right: "); printf("%d minus %d is %d\n", ten, 2, ten - two); printf("Doing it wrong: "); printf("%d minus %d is %d\n",ten);//缺少两个参数 return 0;}
也能编译链接成功,有一个警告⚠️。
Panda-MBP:CodeList3-2 panda8z$ gcc -o print1.out print1.c print1.c:10:23: warning: more '%' conversions than data arguments [-Wformat] printf("%d minus %d is %d\n",ten);//缺少两个参数 ~^1 warning generated.Panda-MBP:CodeList3-2 panda8z$ ./print1.out Doing it right: 10 minus 2 is 8Doing it wrong: 10 minus 0 is 1071055023Panda-MBP:CodeList3-2 panda8z$
/* bases.c -- 以十进制,八进制,十六进制形式输出100 */#includeint main(void){ int x = 100; printf("dec = %d; octal = %o; hex = %x\n", x, x, x); printf("dec = %d; octal = %o; hex = %#x\n", x, x, x); return 0;}
Panda-MBP:CodeList3-3 panda8z$ gcc -o bases.out bases.cPanda-MBP:CodeList3-3 panda8z$ ./bases.outdec = 100; octal = 144; hex = 64dec = 100; octal = 144; hex = 0x64Panda-MBP:CodeList3-3 panda8z$
/* print2.c -- printf()的更多属性 */#includeint main(void){ unsigned int un = 3000000000; //int为32位 short end = 200; //short为16位系统 long big = 6537; long long verybig = 12345678908642; //14位数 printf("un = %u and not %d\n", un, un); printf("end = %hd and %d\n", end, end); printf("big = %1d and not %hd\n", big, big); printf("verybig = %1ld and not %1d\n", verybig, verybig); return 0;}
Panda-MBP:CodeList3-4 panda8z$ gcc -o print2.out print2.c print2.c:12:39: warning: format specifies type 'int' but the argument has type 'long' [-Wformat] printf("big = %1d and not %hd\n", big, big); ~~~ ^~~ %1ldprint2.c:12:44: warning: format specifies type 'short' but the argument has type 'long' [-Wformat] printf("big = %1d and not %hd\n", big, big); ~~~ ^~~ %ldprint2.c:13:44: warning: format specifies type 'long' but the argument has type 'long long' [-Wformat] printf("verybig = %1ld and not %1d\n", verybig, verybig); ~~~~ ^~~~~~~ %1lldprint2.c:13:53: warning: format specifies type 'int' but the argument has type 'long long' [-Wformat] printf("verybig = %1ld and not %1d\n", verybig, verybig); ~~~ ^~~~~~~ %1lld4 warnings generated.Panda-MBP:CodeList3-4 panda8z$ ./print2.out un = 3000000000 and not -1294967296end = 200 and 200big = 6537 and not 6537verybig = 12345678908642 and not 1942899938Panda-MBP:CodeList3-4 panda8z$
/* charcode.c -- 显示一个字符的编辑值 */#includeint main(void){ char ch; printf("Please enter a charactr.\n"); scanf("%c", &ch); printf("The code for %c is %d.\n", ch, ch); return 0;}
Panda-MBP:CodeList3-5 panda8z$ gcc -o charcode.out charcode.c Panda-MBP:CodeList3-5 panda8z$ ./charcode.out Please enter a charactr.rriThe code for r is 114.Panda-MBP:CodeList3-5 panda8z$
/* altnames.c -- 可移植的整数类型名 */#include#include //支持可移植类型int main(void){ int16_t me16; // me16是一个16位有符号变量 me16 = 4593; printf("First, assume int16_t is short: "); printf("me16 = %hd\n",me16); printf("Next, let`s not make any assumptions. \n"); printf("instead, use a \"macro\" from inttypes.h: "); printf("me16 = %" PRId16 "\n",me16); return 0;}
Panda-MBP:CodeList3-6 panda8z$ gcc -o altnames.out altnames.c Panda-MBP:CodeList3-6 panda8z$ ./altnames.out First, assume int16_t is short: me16 = 4593Next, let`s not make any assumptions. instead, use a "macro" from inttypes.h: me16 = 4593Panda-MBP:CodeList3-6 panda8z$
书上写的源程序真正在我的mac os上编译的时候有问题,在log里可以看出
/* showf_pt.c -- 以两种方式显示浮点值 */#includeint main(void){ float aboat = 32000.0; double abet = 2.14e9; long double dip = 5.32e-5; printf("%f can be written %e\n", aboat, aboat); printf("%f can be written %e\n", abet, abet); printf("%f can be written %e\n", dip, dip); return 0; }
编译有问题的源程序。提示使用%Lf
和%Le
Panda-MBP:CodeList3-7 panda8z$ gcc -o showf_pt.out showf_pt.c showf_pt.c:10:38: warning: format specifies type 'double' but the argument has type 'long double' [-Wformat] printf("%f can be written %e\n", dip, dip); ~~ ^~~ %Lfshowf_pt.c:10:43: warning: format specifies type 'double' but the argument has type 'long double' [-Wformat] printf("%f can be written %e\n", dip, dip); ~~ ^~~ %Le2 warnings generated.Panda-MBP:CodeList3-7 panda8z$ ./showf_pt.out 32000.000000 can be written 3.200000e+042140000000.000000 can be written 2.140000e+092140000000.000000 can be written 2.140000e+09Panda-MBP:CodeList3-7 panda8z$
改成下面这样才能正常
/* showf_pt.c -- 以两种方式显示浮点值 */#includeint main(void){ float aboat = 32000.0; double abet = 2.14e9; long double dip = 5.32e-5; printf("%f can be written %e\n", aboat, aboat); printf("%f can be written %e\n", abet, abet); printf("%Lf can be written %Le\n", dip, dip); return 0; }
Panda-MBP:CodeList3-7 panda8z$ gcc -o showf_pt.out showf_pt.c Panda-MBP:CodeList3-7 panda8z$ ./showf_pt.out 32000.000000 can be written 3.200000e+042140000000.000000 can be written 2.140000e+090.000053 can be written 5.320000e-05Panda-MBP:CodeList3-7 panda8z$
/* typesize.c -- 输出类型的大小 */#includeint main(void){ /* c99为类型大小提供一个%zd说明符 */ printf("Type int has a size of %u bytes.\n",sizeof(int)); printf("Type char has a size of %u bytes.\n", sizeof(char)); printf("Type long has a size of %u bytes.\n", sizeof(long)); printf("Type double has a size of %u bytes.\n", sizeof(double)); return 0;}
按照书上代码写有问题,但是这里还是没影响到执行结果。
Panda-MBP:CodeList3-8 panda8z$ gcc -o typesize.out typesize.c typesize.c:6:49: warning: format specifies type 'unsigned int' but the argument has type 'unsigned long' [-Wformat] printf("Type int has a size of %u bytes.\n",sizeof(int)); ~~ ^~~~~~~~~~~ %lutypesize.c:7:51: warning: format specifies type 'unsigned int' but the argument has type 'unsigned long' [-Wformat] printf("Type char has a size of %u bytes.\n", sizeof(char)); ~~ ^~~~~~~~~~~~ %lutypesize.c:8:51: warning: format specifies type 'unsigned int' but the argument has type 'unsigned long' [-Wformat] printf("Type long has a size of %u bytes.\n", sizeof(long)); ~~ ^~~~~~~~~~~~ %lutypesize.c:9:53: warning: format specifies type 'unsigned int' but the argument has type 'unsigned long' [-Wformat] printf("Type double has a size of %u bytes.\n", sizeof(double)); ~~ ^~~~~~~~~~~~~~ %lu4 warnings generated.Panda-MBP:CodeList3-8 panda8z$ ./typesize.out Type int has a size of 4 bytes.Type char has a size of 1 bytes.Type long has a size of 8 bytes.Type double has a size of 8 bytes.
能看得出 在现在的这台MacOS上 long和double都是8个字节。也就是都是64个二进制位。
/* badcount.c -- 不正确的参数个数 */#includeint main(void){ int f = 4; int g = 5; float h = 5.0f; printf("%d\n", f, g); //参数太多 printf("%d %d\n", f); //参数太少 printf("%d %f\n", h, f); //参数类型不匹配 return 0;}
Panda-MBP:CodeList3-9 panda8z$ gcc -o badcount.out badcount.c badcount.c:9:23: warning: data argument not used by format string [-Wformat-extra-args] printf("%d\n", f, g); //参数太多 ~~~~~~ ^badcount.c:10:17: warning: more '%' conversions than data arguments [-Wformat] printf("%d %d\n", f); //参数太少 ~^badcount.c:11:23: warning: format specifies type 'int' but the argument has type 'float' [-Wformat] printf("%d %f\n", h, f); //参数类型不匹配 ~~ ^ %fbadcount.c:11:26: warning: format specifies type 'double' but the argument has type 'int' [-Wformat] printf("%d %f\n", h, f); //参数类型不匹配 ~~ ^ %d4 warnings generated.Panda-MBP:CodeList3-9 panda8z$ ./badcount.out 44 04 5.000000
/* escape.c -- 使用转义字符 */#includeint main(void){ float salary; printf("\aEnter your desired monthly salary: "); printf(" $_____\b\b\b\b\b\b\b"); scanf("%f", salary); printf("\n\t$.2f a month is $.2f a year.", salary, salary * 12.0); printf("\rGee!\n"); return 0;}
Panda-MBP:CodeList3-10 panda8z$ gcc -o escape.out escape.c escape.c:8:17: warning: format specifies type 'float *' but the argument has type 'double' [-Wformat] scanf("%f", salary); ~~ ^~~~~~escape.c:9:48: warning: data argument not used by format string [-Wformat-extra-args] printf("\n\t$.2f a month is $.2f a year.", salary, salary * 12.0); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^2 warnings generated.Panda-MBP:CodeList3-10 panda8z$ ./escape.out Enter your desired monthly salary:1200___Segmentation fault: 11Panda-MBP:CodeList3-10 panda8z$
正确的程序如下:
/* escape.c -- 使用转义字符 */#includeint main(void){ float salary; printf("\aEnter your desired monthly salary: "); printf(" $_______\b\b\b\b\b\b\b"); scanf("%f", &salary); printf("\n\t$%.2f a month is $%.2f a year.", salary, salary * 12.0); printf("\rGee!\n"); return 0;}
正确的输出如下:
Panda-MBP:CodeList3-10 panda8z$ gcc -o escape.out escape.c Panda-MBP:CodeList3-10 panda8z$ ./escape.out Enter your desired monthly salary: $1234567Gee! $1234567.00 a month is $14814804.00 a year.Panda-MBP:CodeList3-10 panda8z$
转载地址:http://atzi.baihongyu.com/