文档详情

CPrimerPlus(第五版)全书源代码

ali****an
实名认证
店铺
PDF
736.30KB
约70页
文档ID:118826037
CPrimerPlus(第五版)全书源代码_第1页
1/70

// chapter 01 /* #include int main(void) { int dogs; printf("How many dogs do you have?\n"); scanf("%d", printf("So you have %d dog(s)!\n", dogs); return 0; } */ ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// //chapter 02 // fathm_ft.c -- converts 2 fathoms to feet /* #include int main(void) { int feet, fathoms; fathoms = 2; feet = 6 * fathoms; printf("There are %d feet in %d fathoms!\n", feet, fathoms); printf("Yes, I said %d feet!\n", 6 * fathoms); return 0; } */ ////////////////////////////////////////////////////////////////// /* #include int main(void) // a simple program { int num; // define a variable called num num = 1; // assign a value to num printf("I am a simple "); // use the printf() function printf("computer.\n"); printf("My favorite number is %d because it is first.\n",num); return 0; }*/ ////////////////////////////////////////////////////////////////// // two_func.c -- a program using two functions in one file /* #include void butler(void); // ISO/ANSI C function prototyping int main(void) { printf("I will summon the butler function.\n"); butler(); printf("Yes. Bring me some tea and writeable CD-ROMS.\n"); return 0; } void butler(void) // start of function definition { printf("You rang, sir?\n"); } */ ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// //chapter 03 数据和 C /* // altnames.c -- portable names for integer types #include #include // supports portable types the system doesnt contain the header file int main(void) { int16_t me16; // me16 a 16-bit signed variable me16 = 4593; printf("First, assume int16_t is short: "); printf("me16 = %hd\n", me16); printf("Next, lets not make any assumptions.\n"); printf("Instead, use a \"macro\" from inttypes.h: "); printf("me16 = %" PRId16 "\n", me16); return 0; } */ ////////////////////////////////////////////////////////////////// /* // bases.c--prints 100 in decimal, octal, and hex #include int 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); //%# 十六进制前显示 Ox //八进制数前显示 o return 0; } */ ////////////////////////////////////////////////////////////// /* // charcode.c-displays code number for a character #include int main(void) { char ch; printf("Please enter a character.\n"); scanf("%c", printf("The code for %c is %d.\n", ch, ch); return 0; } */ ////////////////////////////////////////////////////////////// /* //print1.c-displays some properties of printf() #include int 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 ); // forgot 2 arguments return 0; } */ ////////////////////////////////////////////////////////////// /* print2.c-more printf() properties */ /* #include int main(void) { unsigned int un = 3000000000; // system with 32-bit int short end = 200; // and 16-bit short long big = 65537; long verybig = 12345678908642; //C 也可以使用前缀 h 来表示 short 类型。

//因此%hd 显示一个十进制的 short 整型ho 为八进制形式 printf("un = %u and not %d\n", un, un); printf("end = %hd and %d\n", end, end); printf("big = %ld and not %hd\n", big, big); printf("verybig= %lld and not %ld\n", verybig, verybig); return 0; } */ ////////////////////////////////////////////////////////////// /* showf_pt.c -- displays float value in two ways */ /* #include int 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; } */ ////////////////////////////////////////////////////////////// /* toobig.c-exceeds maximum int size on our system */ /* #include int main(void) { int i = 2147483647; unsigned int j = 4294967295; printf("%d %d %d\n", i, i+1, i+2); printf("%u %u %u\n", j, j+1, j+2); return 0; } */ ////////////////////////////////////////////////////////////// /* typesize.c -- prints out type sizes */ /* #include int main(void) { // c99 provides a %zd specifier for sizes printf("Type int has a size of %u bytes.\n", sizeof(int)); //4 bytes printf("Type char has a size of %u bytes.\n", sizeof(char));// 1 bytes printf("Type long has a size of %u bytes.\n", sizeof(long));//4 bytes printf("Type double has a size of %u bytes.\n", sizeof(double));//8 bytes return 0; } */ //////////////////////////////////////////////。

下载提示
相似文档
正为您匹配相似的精品文档