├── .gitignore ├── CMakeLists.txt ├── README.md ├── assets └── codes │ ├── Ch01 │ ├── inform.c │ ├── listing1.c │ └── listing2.c │ ├── Ch02 │ ├── fathm_ft.c │ ├── first.c │ ├── nogood.c │ ├── stillbad.c │ └── two_func.c │ ├── Ch03 │ ├── altnames.c │ ├── badcount.c │ ├── bases.c │ ├── charcode.c │ ├── escape.c │ ├── floaterr.c │ ├── platinum.c │ ├── print1.c │ ├── print2.c │ ├── showf_pt.c │ ├── toobig.c │ └── typesize.c │ ├── Ch04 │ ├── defines.c │ ├── flags.c │ ├── floatcnv.c │ ├── floats.c │ ├── input.c │ ├── intconv.c │ ├── longstrg.c │ ├── pizza.c │ ├── praise1.c │ ├── praise2.c │ ├── printout.c │ ├── prntval.c │ ├── skiptwo.c │ ├── stringf.c │ ├── talkback.c │ ├── varwid.c │ └── width.c │ ├── Ch05 │ ├── add_one.c │ ├── addemup.c │ ├── bottles.c │ ├── convert.c │ ├── divide.c │ ├── golf.c │ ├── min_sec.c │ ├── post_pre.c │ ├── pound.c │ ├── rules.c │ ├── running.c │ ├── shoes1.c │ ├── shoes2.c │ ├── sizeof.c │ ├── squares.c │ └── wheat.c │ ├── Ch06 │ ├── boolean.c │ ├── cmpflt.c │ ├── do_while.c │ ├── entry.c │ ├── for_13s.c │ ├── for_char.c │ ├── for_cube.c │ ├── for_down.c │ ├── for_geo.c │ ├── for_none.c │ ├── for_show.c │ ├── for_wild.c │ ├── postage.c │ ├── power.c │ ├── rows1.c │ ├── rows2.c │ ├── scores_in.c │ ├── summing.c │ ├── sweetie1.c │ ├── sweetie2.c │ ├── t_and_f.c │ ├── trouble.c │ ├── truth.c │ ├── when.c │ ├── while1.c │ ├── while2.c │ └── zeno.c │ ├── Ch07 │ ├── animals.c │ ├── break.c │ ├── chcount.c │ ├── colddays.c │ ├── cypher1.c │ ├── cypher2.c │ ├── divisors.c │ ├── electric.c │ ├── paint.c │ ├── skippart.c │ ├── vowels.c │ └── wordcnt.c │ ├── Ch08 │ ├── checking.c │ ├── echo.c │ ├── echo_eof.c │ ├── file_eof.c │ ├── guess.c │ ├── menuette.c │ ├── newguess.c │ ├── showchar1.c │ └── showchar2.c │ ├── Ch09 │ ├── binary.c │ ├── factor.c │ ├── hotel.c │ ├── hotel.h │ ├── lesser.c │ ├── lethead1.c │ ├── lethead2.c │ ├── loccheck.c │ ├── misuse.c │ ├── proto.c │ ├── recur.c │ ├── swap1.c │ ├── swap2.c │ └── usehotel.c │ ├── Ch10 │ ├── arf.c │ ├── array2d.c │ ├── bounds.c │ ├── day_mon1.c │ ├── day_mon2.c │ ├── day_mon3.c │ ├── designate.c │ ├── flc.c │ ├── no_data.c │ ├── order.c │ ├── pnt_add.c │ ├── ptr_ops.c │ ├── rain.c │ ├── some_data.c │ ├── sum_arr1.c │ ├── sum_arr2.c │ ├── vararr2d.c │ ├── zippo1.c │ └── zippo2.c │ ├── Ch11 │ ├── addresses.c │ ├── arrchar.c │ ├── compare.c │ ├── compback.c │ ├── copy1.c │ ├── copy2.c │ ├── copy3.c │ ├── fgets1.c │ ├── fgets2.c │ ├── fgets3.c │ ├── format.c │ ├── getsputs.c │ ├── join_chk.c │ ├── mod_str.c │ ├── name1.c │ ├── nogo.c │ ├── nono.c │ ├── p_and_s.c │ ├── put_out.c │ ├── put_put.c │ ├── quit_chk.c │ ├── repeat.c │ ├── scan_str.c │ ├── sort_str.c │ ├── starsrch.c │ ├── str_cat.c │ ├── strcnvt.c │ ├── strings1.c │ ├── strptr.c │ └── test_fit.c │ ├── Ch12 │ ├── diceroll.c │ ├── diceroll.h │ ├── dyn_arr.c │ ├── forc99.c │ ├── global.c │ ├── hiding.c │ ├── loc_stat.c │ ├── manydice.c │ ├── parta.c │ ├── partb.c │ ├── r_drive0.c │ ├── r_drive1.c │ ├── rand0.c │ ├── s_and_r.c │ └── where.c │ ├── Ch13 │ ├── addaword.c │ ├── append.c │ ├── count.c │ ├── eddy │ ├── randbin.c │ ├── reducto.c │ ├── reverse.c │ └── wordy │ ├── Ch14 │ ├── book.c │ ├── booksave.c │ ├── complit.c │ ├── enum.c │ ├── flexmemb.c │ ├── friend.c │ ├── friends.c │ ├── funct_ptr.c │ ├── funds1.c │ ├── funds2.c │ ├── funds3.c │ ├── funds4.c │ ├── manybook.c │ ├── names1.c │ ├── names2.c │ └── names3.c │ ├── Ch15 │ ├── align.c │ ├── binbit.c │ ├── dualview.c │ ├── fields.c │ └── invert4.c │ ├── Ch16 │ ├── assert.c │ ├── byebye.c │ ├── doubleincl.c │ ├── eatline.h │ ├── el1.c │ ├── el2.c │ ├── file1.c │ ├── file2.c │ ├── file3.c │ ├── generic.c │ ├── glue.c │ ├── ifdef.c │ ├── mac_arg.c │ ├── mems.c │ ├── mytype.c │ ├── names.h │ ├── names_st.c │ ├── names_st.h │ ├── predef.c │ ├── preproc.c │ ├── qsorter.c │ ├── rect_pol.c │ ├── statasrt.c │ ├── subst.c │ ├── useheader.c │ ├── varargs.c │ └── variadic.c │ ├── Ch17 │ ├── films1.c │ ├── films2.c │ ├── films3.c │ ├── list.c │ ├── list.h │ ├── mall.c │ ├── petclub.c │ ├── queue.c │ ├── queue.h │ ├── tree.c │ ├── tree.h │ └── use_q.c │ └── XB │ ├── complex.c │ ├── complex0.c │ ├── def.c │ ├── offset.c │ ├── strrtok.c │ └── wider.c ├── docs ├── .nojekyll ├── README.md ├── _sidebar.md ├── ch02 │ ├── ex01.md │ ├── ex03.md │ ├── ex04.md │ ├── ex05.md │ ├── ex06.md │ ├── ex07.md │ └── ex08.md ├── ch03 │ ├── ex02.md │ ├── ex03.md │ ├── ex04.md │ ├── ex05.md │ ├── ex06.md │ ├── ex07.md │ └── ex08.md ├── ch04 │ ├── ex01.md │ ├── ex02.md │ ├── ex03.md │ ├── ex04.md │ ├── ex05.md │ ├── ex06.md │ ├── ex07.md │ └── ex08.md ├── ch05 │ ├── ex01.md │ ├── ex02.md │ ├── ex03.md │ ├── ex04.md │ ├── ex05.md │ ├── ex06.md │ ├── ex07.md │ ├── ex08.md │ └── ex09.md ├── ch06 │ ├── ex01.md │ ├── ex02.md │ ├── ex03.md │ ├── ex04.md │ ├── ex05.md │ ├── ex06.md │ ├── ex07.md │ ├── ex08.md │ ├── ex09.md │ ├── ex10.md │ ├── ex11.md │ ├── ex12.md │ ├── ex13.md │ ├── ex14.md │ ├── ex15.md │ ├── ex16.md │ ├── ex17.md │ └── ex18.md ├── ch07 │ ├── ex01.md │ ├── ex02.md │ ├── ex03.md │ ├── ex04.md │ ├── ex05.md │ ├── ex06.md │ ├── ex07.md │ ├── ex08.md │ ├── ex09.md │ ├── ex10.md │ └── ex11.md ├── ch08 │ ├── ex01.md │ ├── ex02.md │ ├── ex03.md │ ├── ex04.md │ ├── ex05.md │ ├── ex06.md │ ├── ex07.md │ ├── ex08.md │ └── images │ │ ├── ex01_redirect_input_config.png │ │ └── ex02_redirect_input_config.png ├── ch09 │ ├── ex01.md │ ├── ex02.md │ ├── ex03.md │ ├── ex04.md │ ├── ex05.md │ ├── ex06.md │ ├── ex07.md │ ├── ex08.md │ ├── ex09.md │ ├── ex10.md │ └── ex11.md ├── ch10 │ ├── ex01.md │ ├── ex02.md │ ├── ex03.md │ ├── ex04.md │ ├── ex05.md │ ├── ex06.md │ ├── ex07.md │ ├── ex08.md │ ├── ex09.md │ ├── ex10.md │ ├── ex11.md │ ├── ex12.md │ ├── ex13.md │ └── ex14.md ├── ch11 │ ├── ex01.md │ ├── ex02.md │ ├── ex03.md │ ├── ex04.md │ ├── ex05.md │ ├── ex06.md │ ├── ex07.md │ ├── ex08.md │ ├── ex09.md │ ├── ex10.md │ ├── ex11.md │ ├── ex12.md │ ├── ex13.md │ ├── ex14.md │ ├── ex15.md │ ├── ex16.md │ └── images │ │ ├── ex13_program_arguments_config.png │ │ ├── ex14_program_arguments_config.png │ │ ├── ex16_program_arguments_config_l.png │ │ ├── ex16_program_arguments_config_p.png │ │ └── ex16_program_arguments_config_u.png ├── ch12 │ ├── ex01.md │ ├── ex02.md │ ├── ex03.md │ ├── ex04.md │ ├── ex05.md │ ├── ex06.md │ ├── ex07.md │ ├── ex08.md │ └── ex09.md ├── ch13 │ ├── ex01.md │ ├── ex02.md │ ├── ex03.md │ ├── ex04.md │ ├── ex05.md │ ├── ex06.md │ ├── ex07.md │ ├── ex08.md │ ├── ex09.md │ ├── ex10.md │ ├── ex11.md │ ├── ex12.md │ ├── ex13.md │ ├── ex14.md │ └── images │ │ ├── ex01_working_directory_config.png │ │ ├── ex02_program_arguments_and_working_directory_config.png │ │ ├── ex03_working_directory_config.png │ │ ├── ex04_program_arguments_and_working_directory_config.png │ │ ├── ex05_program_arguments_and_working_directory_config.png │ │ ├── ex06_working_directory_config.png │ │ ├── ex07_a_program_arguments_and_working_directory_config.png │ │ ├── ex07_b_program_arguments_and_working_directory_config.png │ │ ├── ex08_program_arguments_and_working_directory_config.png │ │ ├── ex09_working_directory_config.png │ │ ├── ex10_working_directory_config.png │ │ ├── ex11_program_arguments_and_working_directory_config.png │ │ ├── ex12_working_directory_config.png │ │ ├── ex13_working_directory_config.png │ │ └── ex14_working_directory_config.png ├── ch14 │ ├── ex01.md │ ├── ex02.md │ ├── ex03.md │ ├── ex04.md │ ├── ex05.md │ ├── ex06.md │ ├── ex07.md │ ├── ex08.md │ ├── ex09.md │ ├── ex10.md │ ├── ex11.md │ └── images │ │ ├── ex06_working_directory_config.png │ │ └── ex07_working_directory_config.png ├── ch15 │ ├── ex01.md │ ├── ex02.md │ ├── ex03.md │ ├── ex04.md │ ├── ex05.md │ ├── ex06.md │ └── ex07.md ├── ch16 │ ├── ex02.md │ ├── ex03.md │ ├── ex04.md │ ├── ex05.md │ ├── ex06.md │ ├── ex07.md │ └── images │ │ └── pic_6_13.png ├── ch17 │ ├── ex01.md │ ├── ex02.md │ ├── ex03.md │ ├── ex04.md │ ├── ex05.md │ ├── ex06.md │ ├── ex07.md │ ├── ex08.md │ └── images │ │ └── ex07_working_directory_config.png ├── index.html ├── resources │ ├── CMake_config.png │ └── Toolchains_config.png └── template.md ├── exercises ├── ch02 │ ├── ex01.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ └── ex08.c ├── ch03 │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ └── ex08.c ├── ch04 │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ └── ex08.c ├── ch05 │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ ├── ex08.c │ └── ex09.c ├── ch06 │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ ├── ex08.c │ ├── ex09.c │ ├── ex10.c │ ├── ex11.c │ ├── ex12.c │ ├── ex13.c │ ├── ex14.c │ ├── ex15.c │ ├── ex16.c │ ├── ex17.c │ └── ex18.c ├── ch07 │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ ├── ex08.c │ ├── ex09.c │ ├── ex10.c │ └── ex11.c ├── ch08 │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ ├── ex08.c │ └── files │ │ └── file ├── ch09 │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ ├── ex08.c │ ├── ex09.c │ ├── ex10.c │ └── ex11.c ├── ch10 │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ ├── ex08.c │ ├── ex09.c │ ├── ex10.c │ ├── ex11.c │ ├── ex12.c │ ├── ex13.c │ └── ex14.c ├── ch11 │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ ├── ex08.c │ ├── ex09.c │ ├── ex10.c │ ├── ex11.c │ ├── ex12.c │ ├── ex13.c │ ├── ex14.c │ ├── ex15.c │ ├── ex16.c │ └── files │ │ └── file ├── ch12 │ ├── ex01.c │ ├── ex02 │ │ ├── pe12-2a.c │ │ ├── pe12-2a.h │ │ └── pe12-2b.c │ ├── ex03 │ │ ├── pe12-3a.c │ │ ├── pe12-3a.h │ │ └── pe12-3b.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06 │ │ ├── ex06.c │ │ ├── rand.h │ │ └── s_and_r.c │ ├── ex07.c │ ├── ex08.c │ └── ex09.c ├── ch13 │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07-a.c │ ├── ex07-b.c │ ├── ex08.c │ ├── ex09.c │ ├── ex10.c │ ├── ex11.c │ ├── ex12.c │ ├── ex13.c │ ├── ex14.c │ └── files │ │ ├── data_file │ │ ├── eddy │ │ └── wordy ├── ch14 │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04-a.c │ ├── ex04-b.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ ├── ex08.c │ ├── ex09.c │ ├── ex10.c │ ├── ex11.c │ └── files │ │ ├── baseball_team.dat │ │ └── create-book-data.c ├── ch15 │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ └── ex07.c ├── ch16 │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ └── ex07.c └── ch17 │ ├── ex01-a.c │ ├── ex01-b.c │ ├── ex02 │ ├── films3.c │ ├── list.c │ └── list.h │ ├── ex03 │ ├── films3.c │ ├── list.c │ └── list.h │ ├── ex04 │ ├── mall.c │ ├── queue.c │ └── queue.h │ ├── ex05 │ ├── ex05.c │ ├── stack.c │ └── stack.h │ ├── ex06.c │ ├── ex07 │ ├── ex07.c │ ├── tree.c │ ├── tree.h │ └── wordy │ └── ex08 │ ├── ex08.c │ ├── tree.c │ └── tree.h └── resources ├── CMake_config.png └── Toolchains_config.png /assets/codes/Ch01/inform.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void) 3 | { 4 | printf("A .c is used to end a C program filename.\n"); 5 | 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /assets/codes/Ch01/listing1.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void) 3 | { 4 | int dogs; 5 | 6 | printf("How many dogs do you have?\n"); 7 | scanf("%d", &dogs); 8 | printf("So you have %d dog(s)!\n", dogs); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /assets/codes/Ch01/listing2.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void) 3 | { 4 | printf("Concrete contains gravel and cement.\n"); 5 | 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /assets/codes/Ch02/fathm_ft.c: -------------------------------------------------------------------------------- 1 | // fathm_ft.c -- converts 2 fathoms to feet 2 | 3 | #include 4 | int main(void) 5 | { 6 | int feet, fathoms; 7 | 8 | fathoms = 2; 9 | feet = 6 * fathoms; 10 | printf("There are %d feet in %d fathoms!\n", feet, fathoms); 11 | printf("Yes, I said %d feet!\n", 6 * fathoms); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /assets/codes/Ch02/first.c: -------------------------------------------------------------------------------- 1 | // first.c 2 | #include 3 | int main(void) /* a simple program */ 4 | { 5 | int num; /* define a variable called num */ 6 | num = 1; /* assign a value to num */ 7 | 8 | printf("I am a simple "); /* use the printf() function */ 9 | printf("computer.\n"); 10 | printf("My favorite number is %d because it is first.\n",num); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /assets/codes/Ch02/nogood.c: -------------------------------------------------------------------------------- 1 | /* nogood.c -- a program with errors */ 2 | #include 3 | int main(void) 4 | ( 5 | int n, int n2, int n3; 6 | 7 | /* this program has several errors 8 | n = 5; 9 | n2 = n * n; 10 | n3 = n2 * n2; 11 | printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3) 12 | 13 | return 0; 14 | ) 15 | -------------------------------------------------------------------------------- /assets/codes/Ch02/stillbad.c: -------------------------------------------------------------------------------- 1 | /* stillbad.c -- a program with its syntax errors fixed */ 2 | #include 3 | int main(void) 4 | { 5 | int n, n2, n3; 6 | 7 | /* this program has a semantic error */ 8 | n = 5; 9 | n2 = n * n; 10 | n3 = n2 * n2; 11 | printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /assets/codes/Ch02/two_func.c: -------------------------------------------------------------------------------- 1 | //* two_func.c -- a program using two functions in one file */ 2 | #include 3 | void butler(void); /* ANSI/ISO C function prototyping */ 4 | int main(void) 5 | { 6 | printf("I will summon the butler function.\n"); 7 | butler(); 8 | printf("Yes. Bring me some tea and writeable DVDs.\n"); 9 | 10 | return 0; 11 | } 12 | 13 | void butler(void) /* start of function definition */ 14 | { 15 | printf("You rang, sir?\n"); 16 | } 17 | -------------------------------------------------------------------------------- /assets/codes/Ch03/altnames.c: -------------------------------------------------------------------------------- 1 | /* altnames.c -- portable names for integer types */ 2 | #include 3 | #include // supports portable types 4 | int main(void) 5 | { 6 | int32_t me32; // me32 a 32-bit signed variable 7 | 8 | me32 = 45933945; 9 | printf("First, assume int32_t is int: "); 10 | printf("me32 = %d\n", me32); 11 | printf("Next, let's not make any assumptions.\n"); 12 | printf("Instead, use a \"macro\" from inttypes.h: "); 13 | printf("me32 = %" PRId32 "\n", me32); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /assets/codes/Ch03/badcount.c: -------------------------------------------------------------------------------- 1 | /* badcount.c -- incorrect argument counts */ 2 | #include 3 | int main(void) 4 | { 5 | int n = 4; 6 | int m = 5; 7 | float f = 7.0f; 8 | float g = 8.0f; 9 | 10 | printf("%d\n", n, m); /* too many arguments */ 11 | printf("%d %d %d\n", n); /* too few arguments */ 12 | printf("%d %d\n", f, g); /* wrong kind of values */ 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /assets/codes/Ch03/bases.c: -------------------------------------------------------------------------------- 1 | /* bases.c--prints 100 in decimal, octal, and hex */ 2 | #include 3 | int main(void) 4 | { 5 | int x = 100; 6 | 7 | printf("dec = %d; octal = %o; hex = %x\n", x, x, x); 8 | printf("dec = %d; octal = %#o; hex = %#x\n", x, x, x); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /assets/codes/Ch03/charcode.c: -------------------------------------------------------------------------------- 1 | /* charcode.c-displays code number for a character */ 2 | #include 3 | int main(void) 4 | { 5 | char ch; 6 | 7 | printf("Please enter a character.\n"); 8 | scanf("%c", &ch); /* user inputs character */ 9 | printf("The code for %c is %d.\n", ch, ch); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /assets/codes/Ch03/escape.c: -------------------------------------------------------------------------------- 1 | /* escape.c -- uses escape characters */ 2 | #include 3 | int main(void) 4 | { 5 | float salary; 6 | 7 | printf("\aEnter your desired monthly salary:");/* 1 */ 8 | printf(" $_______\b\b\b\b\b\b\b"); /* 2 */ 9 | scanf("%f", &salary); 10 | printf("\n\t$%.2f a month is $%.2f a year.", salary, 11 | salary * 12.0); /* 3 */ 12 | printf("\rGee!\n"); /* 4 */ 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /assets/codes/Ch03/floaterr.c: -------------------------------------------------------------------------------- 1 | /* floaterr.c--demonstrates round-off error */ 2 | #include 3 | int main(void) 4 | { 5 | float a,b; 6 | 7 | b = 2.0e20 + 1.0; 8 | a = b - 2.0e20; 9 | printf("%f \n", a); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /assets/codes/Ch03/platinum.c: -------------------------------------------------------------------------------- 1 | /* platinum.c -- your weight in platinum */ 2 | #include 3 | int main(void) 4 | { 5 | float weight; /* user weight */ 6 | float value; /* platinum equivalent */ 7 | 8 | printf("Are you worth your weight in platinum?\n"); 9 | printf("Let's check it out.\n"); 10 | printf("Please enter your weight in pounds: "); 11 | 12 | /* get input from the user */ 13 | scanf("%f", &weight); 14 | /* assume platinum is $1700 per ounce */ 15 | /* 14.5833 converts pounds avd. to ounces troy */ 16 | value = 1700.0 * weight * 14.5833; 17 | printf("Your weight in platinum is worth $%.2f.\n", value); 18 | printf("You are easily worth that! If platinum prices drop,\n"); 19 | printf("eat more to maintain your value.\n"); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /assets/codes/Ch03/print1.c: -------------------------------------------------------------------------------- 1 | /* print1.c-displays some properties of printf() */ 2 | #include 3 | int main(void) 4 | { 5 | int ten = 10; 6 | int two = 2; 7 | 8 | printf("Doing it right: "); 9 | printf("%d minus %d is %d\n", ten, 2, ten - two ); 10 | printf("Doing it wrong: "); 11 | printf("%d minus %d is %d\n", ten ); // forgot 2 arguments 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /assets/codes/Ch03/print2.c: -------------------------------------------------------------------------------- 1 | /* print2.c-more printf() properties */ 2 | #include 3 | int main(void) 4 | { 5 | unsigned int un = 3000000000; /* system with 32-bit int */ 6 | short end = 200; /* and 16-bit short */ 7 | long big = 65537; 8 | long long verybig = 12345678908642; 9 | 10 | printf("un = %u and not %d\n", un, un); 11 | printf("end = %hd and %d\n", end, end); 12 | printf("big = %ld and not %hd\n", big, big); 13 | printf("verybig= %lld and not %ld\n", verybig, verybig); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /assets/codes/Ch03/showf_pt.c: -------------------------------------------------------------------------------- 1 | /* showf_pt.c -- displays float value in two ways */ 2 | #include 3 | int main(void) 4 | { 5 | float aboat = 32000.0; 6 | double abet = 2.14e9; 7 | long double dip = 5.32e-5; 8 | 9 | printf("%f can be written %e\n", aboat, aboat); 10 | // next line requires C99 or later compliance 11 | printf("And it's %a in hexadecimal, powers of 2 notation\n", aboat); 12 | printf("%f can be written %e\n", abet, abet); 13 | printf("%Lf can be written %Le\n", dip, dip); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /assets/codes/Ch03/toobig.c: -------------------------------------------------------------------------------- 1 | /* toobig.c-exceeds maximum int size on our system */ 2 | #include 3 | int main(void) 4 | { 5 | int i = 2147483647; 6 | unsigned int j = 4294967295; 7 | 8 | printf("%d %d %d\n", i, i+1, i+2); 9 | printf("%u %u %u\n", j, j+1, j+2); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /assets/codes/Ch03/typesize.c: -------------------------------------------------------------------------------- 1 | //* typesize.c -- prints out type sizes */ 2 | #include 3 | int main(void) 4 | { 5 | /* c99 provides a %zd specifier for sizes */ 6 | printf("Type int has a size of %zd bytes.\n", sizeof(int)); 7 | printf("Type char has a size of %zd bytes.\n", sizeof(char)); 8 | printf("Type long has a size of %zd bytes.\n", sizeof(long)); 9 | printf("Type long long has a size of %zd bytes.\n", 10 | sizeof(long long)); 11 | printf("Type double has a size of %zd bytes.\n", 12 | sizeof(double)); 13 | printf("Type long double has a size of %zd bytes.\n", 14 | sizeof(long double)); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /assets/codes/Ch04/defines.c: -------------------------------------------------------------------------------- 1 | // defines.c -- uses defined constants from limit.h and float. 2 | #include 3 | #include // integer limits 4 | #include // floating-point limits 5 | int main(void) 6 | { 7 | printf("Some number limits for this system:\n"); 8 | printf("Biggest int: %d\n", INT_MAX); 9 | printf("Smallest long long: %lld\n", LLONG_MIN); 10 | printf("One byte = %d bits on this system.\n", CHAR_BIT); 11 | printf("Largest double: %e\n", DBL_MAX); 12 | printf("Smallest normal float: %e\n", FLT_MIN); 13 | printf("float precision = %d digits\n", FLT_DIG); 14 | printf("float epsilon = %e\n", FLT_EPSILON); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /assets/codes/Ch04/flags.c: -------------------------------------------------------------------------------- 1 | /* flags.c -- illustrates some formatting flags */ 2 | #include 3 | int main(void) 4 | { 5 | printf("%x %X %#x\n", 31, 31, 31); 6 | printf("**%d**% d**% d**\n", 42, 42, -42); 7 | printf("**%5d**%5.3d**%05d**%05.3d**\n", 6, 6, 6, 6); 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /assets/codes/Ch04/floatcnv.c: -------------------------------------------------------------------------------- 1 | /* floatcnv.c -- mismatched floating-point conversions */ 2 | #include 3 | int main(void) 4 | { 5 | float n1 = 3.0; 6 | double n2 = 3.0; 7 | long n3 = 2000000000; 8 | long n4 = 1234567890; 9 | 10 | printf("%.1e %.1e %.1e %.1e\n", n1, n2, n3, n4); 11 | printf("%ld %ld\n", n3, n4); 12 | printf("%ld %ld %ld %ld\n", n1, n2, n3, n4); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /assets/codes/Ch04/floats.c: -------------------------------------------------------------------------------- 1 | // floats.c -- some floating-point combinations 2 | #include 3 | 4 | int main(void) 5 | { 6 | const double RENT = 3852.99; // const-style constant 7 | 8 | printf("*%f*\n", RENT); 9 | printf("*%e*\n", RENT); 10 | printf("*%4.2f*\n", RENT); 11 | printf("*%3.1f*\n", RENT); 12 | printf("*%10.3f*\n", RENT); 13 | printf("*%10.3E*\n", RENT); 14 | printf("*%+4.2f*\n", RENT); 15 | printf("*%010.2f*\n", RENT); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /assets/codes/Ch04/input.c: -------------------------------------------------------------------------------- 1 | // input.c -- when to use & 2 | #include 3 | int main(void) 4 | { 5 | int age; // variable 6 | float assets; // variable 7 | char pet[30]; // string 8 | 9 | printf("Enter your age, assets, and favorite pet.\n"); 10 | scanf("%d %f", &age, &assets); // use the & here 11 | scanf("%s", pet); // no & for char array 12 | printf("%d $%.2f %s\n", age, assets, pet); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /assets/codes/Ch04/intconv.c: -------------------------------------------------------------------------------- 1 | /* intconv.c -- some mismatched integer conversions */ 2 | #include 3 | #define PAGES 336 4 | #define WORDS 65618 5 | int main(void) 6 | { 7 | short num = PAGES; 8 | short mnum = -PAGES; 9 | 10 | printf("num as short and unsigned short: %hd %hu\n", num, 11 | num); 12 | printf("-num as short and unsigned short: %hd %hu\n", mnum, 13 | mnum); 14 | printf("num as int and char: %d %c\n", num, num); 15 | printf("WORDS as int, short, and char: %d %hd %c\n", 16 | WORDS, WORDS, WORDS); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /assets/codes/Ch04/longstrg.c: -------------------------------------------------------------------------------- 1 | /* longstrg.c –– printing long strings */ 2 | #include 3 | int main(void) 4 | { 5 | printf("Here's one way to print a "); 6 | printf("long string.\n"); 7 | printf("Here's another way to print a \ 8 | long string.\n"); 9 | printf("Here's the newest way to print a " 10 | "long string.\n"); /* ANSI C */ 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /assets/codes/Ch04/pizza.c: -------------------------------------------------------------------------------- 1 | /* pizza.c -- uses defined constants in a pizza context */ 2 | #include 3 | #define PI 3.14159 4 | int main(void) 5 | { 6 | float area, circum, radius; 7 | 8 | printf("What is the radius of your pizza?\n"); 9 | scanf("%f", &radius); 10 | area = PI * radius * radius; 11 | circum = 2.0 * PI *radius; 12 | printf("Your basic pizza parameters are as follows:\n"); 13 | printf("circumference = %1.2f, area = %1.2f\n", circum, 14 | area); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /assets/codes/Ch04/praise1.c: -------------------------------------------------------------------------------- 1 | /* praise1.c -- uses an assortment of strings */ 2 | #include 3 | #define PRAISE "You are an extraordinary being." 4 | int main(void) 5 | { 6 | char name[40]; 7 | 8 | printf("What's your name? "); 9 | scanf("%s", name); 10 | printf("Hello, %s. %s\n", name, PRAISE); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /assets/codes/Ch04/praise2.c: -------------------------------------------------------------------------------- 1 | /* praise2.c */ 2 | // try the %u or %lu specifiers if your implementation 3 | // does not recognize the %zd specifier 4 | #include 5 | #include /* provides strlen() prototype */ 6 | #define PRAISE "You are an extraordinary being." 7 | int main(void) 8 | { 9 | char name[40]; 10 | 11 | printf("What's your name? "); 12 | scanf("%s", name); 13 | printf("Hello, %s. %s\n", name, PRAISE); 14 | printf("Your name of %zd letters occupies %zd memory cells.\n", 15 | strlen(name), sizeof name); 16 | printf("The phrase of praise has %zd letters ", 17 | strlen(PRAISE)); 18 | printf("and occupies %zd memory cells.\n", sizeof PRAISE); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /assets/codes/Ch04/printout.c: -------------------------------------------------------------------------------- 1 | /* printout.c -- uses conversion specifiers */ 2 | #include 3 | #define PI 3.141593 4 | int main(void) 5 | { 6 | int number = 7; 7 | float pies = 12.75; 8 | int cost = 7800; 9 | 10 | 11 | printf("The %d contestants ate %f berry pies.\n", number, 12 | pies); 13 | printf("The value of pi is %f.\n", PI); 14 | printf("Farewell! thou art too dear for my possessing,\n"); 15 | printf("%c%d\n", '$', 2 * cost); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /assets/codes/Ch04/prntval.c: -------------------------------------------------------------------------------- 1 | /* prntval.c -- finding printf()'s return value */ 2 | #include 3 | int main(void) 4 | { 5 | int bph2o = 212; 6 | int rv; 7 | 8 | rv = printf("%d F is water's boiling point.\n", bph2o); 9 | printf("The printf() function printed %d characters.\n", 10 | rv); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /assets/codes/Ch04/skiptwo.c: -------------------------------------------------------------------------------- 1 | /* skiptwo.c -- skips over first two integers of input */ 2 | #include 3 | int main(void) 4 | { 5 | int n; 6 | 7 | printf("Please enter three integers:\n"); 8 | scanf("%*d %*d %d", &n); 9 | printf("The last integer was %d\n", n); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /assets/codes/Ch04/stringf.c: -------------------------------------------------------------------------------- 1 | /* stringf.c -- string formatting */ 2 | #include 3 | #define BLURB "Authentic imitation!" 4 | int main(void) 5 | { 6 | printf("[%2s]\n", BLURB); 7 | printf("[%24s]\n", BLURB); 8 | printf("[%24.5s]\n", BLURB); 9 | printf("[%-24.5s]\n", BLURB); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /assets/codes/Ch04/talkback.c: -------------------------------------------------------------------------------- 1 | // talkback.c -- nosy, informative program 2 | #include 3 | #include // for strlen() prototype 4 | #define DENSITY 62.4 // human density in lbs per cu ft 5 | int main() 6 | { 7 | float weight, volume; 8 | int size, letters; 9 | char name[40]; // name is an array of 40 chars 10 | 11 | printf("Hi! What's your first name?\n"); 12 | scanf("%s", name); 13 | printf("%s, what's your weight in pounds?\n", name); 14 | scanf("%f", &weight); 15 | size = sizeof name; 16 | letters = strlen(name); 17 | volume = weight / DENSITY; 18 | printf("Well, %s, your volume is %2.2f cubic feet.\n", 19 | name, volume); 20 | printf("Also, your first name has %d letters,\n", 21 | letters); 22 | printf("and we have %d bytes to store it.\n", size); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /assets/codes/Ch04/varwid.c: -------------------------------------------------------------------------------- 1 | /* varwid.c -- uses variable-width output field */ 2 | #include 3 | int main(void) 4 | { 5 | unsigned width, precision; 6 | int number = 256; 7 | double weight = 242.5; 8 | 9 | printf("Enter a field width:\n"); 10 | scanf("%d", &width); 11 | printf("The number is :%*d:\n", width, number); 12 | printf("Now enter a width and a precision:\n"); 13 | scanf("%d %d", &width, &precision); 14 | printf("Weight = %*.*f\n", width, precision, weight); 15 | printf("Done!\n"); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /assets/codes/Ch04/width.c: -------------------------------------------------------------------------------- 1 | /* width.c -- field widths */ 2 | #include 3 | #define PAGES 959 4 | int main(void) 5 | { 6 | printf("*%d*\n", PAGES); 7 | printf("*%2d*\n", PAGES); 8 | printf("*%10d*\n", PAGES); 9 | printf("*%-10d*\n", PAGES); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /assets/codes/Ch05/add_one.c: -------------------------------------------------------------------------------- 1 | /* add_one.c -- incrementing: prefix and postfix */ 2 | #include 3 | int main(void) 4 | { 5 | int ultra = 0, super = 0; 6 | 7 | while (super < 5) 8 | { 9 | super++; 10 | ++ultra; 11 | printf("super = %d, ultra = %d \n", super, ultra); 12 | } 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /assets/codes/Ch05/addemup.c: -------------------------------------------------------------------------------- 1 | /* addemup.c -- five kinds of statements */ 2 | #include 3 | int main(void) /* finds sum of first 20 integers */ 4 | { 5 | int count, sum; /* declaration statement */ 6 | 7 | count = 0; /* assignment statement */ 8 | sum = 0; /* ditto */ 9 | while (count++ < 20) /* while */ 10 | sum = sum + count; /* statement */ 11 | printf("sum = %d\n", sum);/* function statement */ 12 | 13 | return 0; /* return statement */ 14 | } 15 | -------------------------------------------------------------------------------- /assets/codes/Ch05/bottles.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 100 3 | int main(void) 4 | { 5 | int count = MAX + 1; 6 | 7 | while (--count > 0) { 8 | printf("%d bottles of spring water on the wall, " 9 | "%d bottles of spring water!\n", count, count); 10 | printf("Take one down and pass it around,\n"); 11 | printf("%d bottles of spring water!\n\n", count - 1); 12 | } 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /assets/codes/Ch05/convert.c: -------------------------------------------------------------------------------- 1 | /* convert.c -- automatic type conversions */ 2 | #include 3 | int main(void) 4 | { 5 | char ch; 6 | int i; 7 | float fl; 8 | 9 | fl = i = ch = 'C'; /* line 9 */ 10 | printf("ch = %c, i = %d, fl = %2.2f\n", ch, i, fl); /* line 10 */ 11 | ch = ch + 1; /* line 11 */ 12 | i = fl + 2 * ch; /* line 12 */ 13 | fl = 2.0 * ch + i; /* line 13 */ 14 | printf("ch = %c, i = %d, fl = %2.2f\n", ch, i, fl); /* line 14 */ 15 | ch = 1107; /* line 15 */ 16 | printf("Now ch = %c\n", ch); /* line 16 */ 17 | ch = 80.89; /* line 17 */ 18 | printf("Now ch = %c\n", ch); /* line 18 */ 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /assets/codes/Ch05/divide.c: -------------------------------------------------------------------------------- 1 | /* divide.c -- divisions we have known */ 2 | #include 3 | int main(void) 4 | { 5 | printf("integer division: 5/4 is %d \n", 5/4); 6 | printf("integer division: 6/3 is %d \n", 6/3); 7 | printf("integer division: 7/4 is %d \n", 7/4); 8 | printf("floating division: 7./4. is %1.2f \n", 7./4.); 9 | printf("mixed division: 7./4 is %1.2f \n", 7./4); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /assets/codes/Ch05/golf.c: -------------------------------------------------------------------------------- 1 | /* golf.c -- golf tournament scorecard */ 2 | #include 3 | int main(void) 4 | { 5 | int jane, tarzan, cheeta; 6 | 7 | cheeta = tarzan = jane = 68; 8 | printf(" cheeta tarzan jane\n"); 9 | printf("First round score %4d %8d %8d\n",cheeta,tarzan,jane); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /assets/codes/Ch05/min_sec.c: -------------------------------------------------------------------------------- 1 | // min_sec.c -- converts seconds to minutes and seconds 2 | #include 3 | #define SEC_PER_MIN 60 // seconds in a minute 4 | int main(void) 5 | { 6 | int sec, min, left; 7 | 8 | printf("Convert seconds to minutes and seconds!\n"); 9 | printf("Enter the number of seconds (<=0 to quit):\n"); 10 | scanf("%d", &sec); // read number of seconds 11 | while (sec > 0) 12 | { 13 | min = sec / SEC_PER_MIN; // truncated number of minutes 14 | left = sec % SEC_PER_MIN; // number of seconds left over 15 | printf("%d seconds is %d minutes, %d seconds.\n", sec, 16 | min, left); 17 | printf("Enter next value (<=0 to quit):\n"); 18 | scanf("%d", &sec); 19 | } 20 | printf("Done!\n"); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /assets/codes/Ch05/post_pre.c: -------------------------------------------------------------------------------- 1 | /* post_pre.c -- postfix vs prefix */ 2 | #include 3 | int main(void) 4 | { 5 | int a = 1, b = 1; 6 | int a_post, pre_b; 7 | 8 | a_post = a++; // value of a++ during assignment phase 9 | pre_b = ++b; // value of ++b during assignment phase 10 | printf("a a_post b pre_b \n"); 11 | printf("%1d %5d %5d %5d\n", a, a_post, b, pre_b); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /assets/codes/Ch05/pound.c: -------------------------------------------------------------------------------- 1 | /* pound.c -- defines a function with an argument */ 2 | #include 3 | void pound(int n); // ANSI function prototype declaration 4 | int main(void) 5 | { 6 | int times = 5; 7 | char ch = '!'; // ASCII code is 33 8 | float f = 6.0f; 9 | 10 | pound(times); // int argument 11 | pound(ch); // same as pound((int)ch); 12 | pound(f); // same as pound((int)f); 13 | 14 | return 0; 15 | } 16 | 17 | void pound(int n) // ANSI-style function header 18 | { // says takes one int argument 19 | while (n-- > 0) 20 | printf("#"); 21 | printf("\n"); 22 | } 23 | -------------------------------------------------------------------------------- /assets/codes/Ch05/rules.c: -------------------------------------------------------------------------------- 1 | /* rules.c -- precedence test */ 2 | #include 3 | int main(void) 4 | { 5 | int top, score; 6 | 7 | top = score = -(2 + 5) * 6 + (4 + 3 * (2 + 3)); 8 | printf("top = %d, score = %d\n", top, score); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /assets/codes/Ch05/shoes1.c: -------------------------------------------------------------------------------- 1 | /* shoes1.c -- converts a shoe size to inches */ 2 | #include 3 | #define ADJUST 7.31 // one kind of symbolic constant 4 | int main(void) 5 | { 6 | const double SCALE = 0.333; // another kind of symbolic constant 7 | double shoe, foot; 8 | 9 | shoe = 9.0; 10 | foot = SCALE * shoe + ADJUST; 11 | printf("Shoe size (men's) foot length\n"); 12 | printf("%10.1f %15.2f inches\n", shoe, foot); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /assets/codes/Ch05/shoes2.c: -------------------------------------------------------------------------------- 1 | /* shoes2.c -- calculates foot lengths for several sizes */ 2 | #include 3 | #define ADJUST 7.31 // one kind of symbolic constant 4 | int main(void) 5 | { 6 | const double SCALE = 0.333; // another kind of symbolic constant 7 | double shoe, foot; 8 | 9 | printf("Shoe size (men's) foot length\n"); 10 | shoe = 3.0; 11 | while (shoe < 18.5) /* starting the while loop */ 12 | { /* start of block */ 13 | foot = SCALE * shoe + ADJUST; 14 | printf("%10.1f %15.2f inches\n", shoe, foot); 15 | shoe = shoe + 1.0; 16 | } /* end of block */ 17 | printf("If the shoe fits, wear it.\n"); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /assets/codes/Ch05/sizeof.c: -------------------------------------------------------------------------------- 1 | // sizeof.c -- uses sizeof operator 2 | // uses C99 %z modifier -- try %u or %lu if you lack %zd 3 | #include 4 | int main(void) 5 | { 6 | int n = 0; 7 | size_t intsize; 8 | 9 | intsize = sizeof (int); 10 | printf("n = %d, n has %zd bytes; all ints have %zd bytes.\n", 11 | n, sizeof n, intsize ); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /assets/codes/Ch05/squares.c: -------------------------------------------------------------------------------- 1 | /* squares.c -- produces a table of first 20 squares */ 2 | #include 3 | int main(void) 4 | { 5 | int num = 1; 6 | 7 | while (num < 21) 8 | { 9 | printf("%4d %6d\n", num, num * num); 10 | num = num + 1; 11 | } 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /assets/codes/Ch05/wheat.c: -------------------------------------------------------------------------------- 1 | /* wheat.c -- exponential growth */ 2 | #include 3 | #define SQUARES 64 // squares on a checkerboard 4 | int main(void) 5 | { 6 | const double CROP = 2E16; // world wheat production in wheat grains 7 | double current, total; 8 | int count = 1; 9 | 10 | printf("square grains total "); 11 | printf("fraction of \n"); 12 | printf(" added grains "); 13 | printf("world total\n"); 14 | total = current = 1.0; /* start with one grain */ 15 | printf("%4d %13.2e %12.2e %12.2e\n", count, current, 16 | total, total/CROP); 17 | while (count < SQUARES) 18 | { 19 | count = count + 1; 20 | current = 2.0 * current; 21 | /* double grains on next square */ 22 | total = total + current; /* update total */ 23 | printf("%4d %13.2e %12.2e %12.2e\n", count, current, 24 | total, total/CROP); 25 | } 26 | printf("That's all.\n"); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /assets/codes/Ch06/boolean.c: -------------------------------------------------------------------------------- 1 | // boolean.c -- using a _Bool variable 2 | #include 3 | int main(void) 4 | { 5 | long num; 6 | long sum = 0L; 7 | _Bool input_is_good; 8 | 9 | printf("Please enter an integer to be summed "); 10 | printf("(q to quit): "); 11 | input_is_good = (scanf("%ld", &num) == 1); 12 | while (input_is_good) 13 | { 14 | sum = sum + num; 15 | printf("Please enter next integer (q to quit): "); 16 | input_is_good = (scanf("%ld", &num) == 1); 17 | } 18 | printf("Those integers sum to %ld.\n", sum); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /assets/codes/Ch06/cmpflt.c: -------------------------------------------------------------------------------- 1 | // cmpflt.c -- floating-point comparisons 2 | #include 3 | #include 4 | int main(void) 5 | { 6 | const double ANSWER = 3.14159; 7 | double response; 8 | 9 | printf("What is the value of pi?\n"); 10 | scanf("%lf", &response); 11 | while (fabs(response - ANSWER) > 0.0001) 12 | { 13 | printf("Try again!\n"); 14 | scanf("%lf", &response); 15 | } 16 | printf("Close enough!\n"); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /assets/codes/Ch06/do_while.c: -------------------------------------------------------------------------------- 1 | /* do_while.c -- exit condition loop */ 2 | #include 3 | int main(void) 4 | { 5 | const int secret_code = 13; 6 | int code_entered; 7 | 8 | do 9 | { 10 | printf("To enter the triskaidekaphobia therapy club,\n"); 11 | printf("please enter the secret code number: "); 12 | scanf("%d", &code_entered); 13 | } while (code_entered != secret_code); 14 | printf("Congratulations! You are cured!\n"); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /assets/codes/Ch06/entry.c: -------------------------------------------------------------------------------- 1 | /* entry.c -- entry condition loop */ 2 | #include 3 | int main(void) 4 | { 5 | const int secret_code = 13; 6 | int code_entered; 7 | 8 | printf("To enter the triskaidekaphobia therapy club,\n"); 9 | printf("please enter the secret code number: "); 10 | scanf("%d", &code_entered); 11 | while (code_entered != secret_code) 12 | { 13 | printf("To enter the triskaidekaphobia therapy club,\n"); 14 | printf("please enter the secret code number: "); 15 | scanf("%d", &code_entered); 16 | } 17 | printf("Congratulations! You are cured!\n"); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /assets/codes/Ch06/for_13s.c: -------------------------------------------------------------------------------- 1 | /* for_13s.c */ 2 | #include 3 | int main(void) 4 | { 5 | int n; // count by 13s from 2 6 | 7 | for (n = 2; n < 60; n = n + 13) 8 | printf("%d \n", n); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /assets/codes/Ch06/for_char.c: -------------------------------------------------------------------------------- 1 | /* for_char.c */ 2 | #include 3 | int main(void) 4 | { 5 | char ch; 6 | 7 | for (ch = 'a'; ch <= 'z'; ch++) 8 | printf("The ASCII value for %c is %d.\n", ch, ch); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /assets/codes/Ch06/for_cube.c: -------------------------------------------------------------------------------- 1 | /* for_cube.c -- using a for loop to make a table of cubes */ 2 | #include 3 | int main(void) 4 | { 5 | int num; 6 | 7 | printf(" n n cubed\n"); 8 | for (num = 1; num <= 6; num++) 9 | printf("%5d %5d\n", num, num*num*num); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /assets/codes/Ch06/for_down.c: -------------------------------------------------------------------------------- 1 | /* for_down.c */ 2 | #include 3 | int main(void) 4 | { 5 | int secs; 6 | 7 | for (secs = 5; secs > 0; secs--) 8 | printf("%d seconds!\n", secs); 9 | printf("We have ignition!\n"); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /assets/codes/Ch06/for_geo.c: -------------------------------------------------------------------------------- 1 | /* for_geo.c */ 2 | #include 3 | int main(void) 4 | { 5 | double debt; 6 | 7 | for (debt = 100.0; debt < 150.0; debt = debt * 1.1) 8 | printf("Your debt is now $%.2f.\n", debt); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /assets/codes/Ch06/for_none.c: -------------------------------------------------------------------------------- 1 | /* for_none.c */ 2 | #include 3 | int main(void) 4 | { 5 | int ans, n; 6 | 7 | ans = 2; 8 | for (n = 3; ans <= 25; ) 9 | ans = ans * n; 10 | printf("n = %d; ans = %d.\n", n, ans); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /assets/codes/Ch06/for_show.c: -------------------------------------------------------------------------------- 1 | /* for_show.c */ 2 | #include 3 | int main(void) 4 | { 5 | int num = 0; 6 | 7 | for (printf("Keep entering numbers!\n"); num != 6; ) 8 | scanf("%d", &num); 9 | printf("That's the one I want!\n"); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /assets/codes/Ch06/for_wild.c: -------------------------------------------------------------------------------- 1 | /* for_wild.c */ 2 | #include 3 | int main(void) 4 | { 5 | int x; 6 | int y = 55; 7 | 8 | for (x = 1; y <= 75; y = (++x * 5) + 50) 9 | printf("%10d %10d\n", x, y); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /assets/codes/Ch06/postage.c: -------------------------------------------------------------------------------- 1 | // postage.c -- first-class postage rates 2 | #include 3 | int main(void) 4 | { 5 | const int FIRST_OZ = 46; // 2013 rate 6 | const int NEXT_OZ = 20; // 2013 rate 7 | int ounces, cost; 8 | 9 | printf(" ounces cost\n"); 10 | for (ounces=1, cost=FIRST_OZ; ounces <= 16; ounces++, 11 | cost += NEXT_OZ) 12 | printf("%5d $%4.2f\n", ounces, cost/100.0); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /assets/codes/Ch06/power.c: -------------------------------------------------------------------------------- 1 | // power.c -- raises numbers to integer powers 2 | #include 3 | double power(double n, int p); // ANSI prototype 4 | int main(void) 5 | { 6 | double x, xpow; 7 | int exp; 8 | 9 | printf("Enter a number and the positive integer power"); 10 | printf(" to which\nthe number will be raised. Enter q"); 11 | printf(" to quit.\n"); 12 | while (scanf("%lf%d", &x, &exp) == 2) 13 | { 14 | xpow = power(x,exp); // function call 15 | printf("%.3g to the power %d is %.5g\n", x, exp, xpow); 16 | printf("Enter next pair of numbers or q to quit.\n"); 17 | } 18 | printf("Hope you enjoyed this power trip -- bye!\n"); 19 | 20 | return 0; 21 | } 22 | 23 | double power(double n, int p) // function definition 24 | { 25 | double pow = 1; 26 | int i; 27 | 28 | for (i = 1; i <= p; i++) 29 | pow *= n; 30 | 31 | return pow; // return the value of pow 32 | } 33 | -------------------------------------------------------------------------------- /assets/codes/Ch06/rows1.c: -------------------------------------------------------------------------------- 1 | /* rows1.c -- uses nested loops */ 2 | #include 3 | #define ROWS 6 4 | #define CHARS 10 5 | int main(void) 6 | { 7 | int row; 8 | char ch; 9 | 10 | for (row = 0; row < ROWS; row++) /* line 10 */ 11 | { 12 | for (ch = 'A'; ch < ('A' + CHARS); ch++) /* line 12 */ 13 | printf("%c", ch); 14 | printf("\n"); 15 | } 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /assets/codes/Ch06/rows2.c: -------------------------------------------------------------------------------- 1 | // rows2.c -- using dependent nested loops 2 | #include 3 | int main(void) 4 | { 5 | const int ROWS = 6; 6 | const int CHARS = 6; 7 | int row; 8 | char ch; 9 | 10 | for (row = 0; row < ROWS; row++) 11 | { 12 | for (ch = ('A' + row); ch < ('A' + CHARS); ch++) 13 | printf("%c", ch); 14 | printf("\n"); 15 | } 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /assets/codes/Ch06/scores_in.c: -------------------------------------------------------------------------------- 1 | // scores_in.c -- uses loops for array processing 2 | #include 3 | #define SIZE 10 4 | #define PAR 72 5 | int main(void) 6 | { 7 | int index, score[SIZE]; 8 | int sum = 0; 9 | float average; 10 | 11 | printf("Enter %d golf scores:\n", SIZE); 12 | for (index = 0; index < SIZE; index++) 13 | scanf("%d", &score[index]); // read in the ten scores 14 | printf("The scores read in are as follows:\n"); 15 | for (index = 0; index < SIZE; index++) 16 | printf("%5d", score[index]); // verify input 17 | printf("\n"); 18 | for (index = 0; index < SIZE; index++) 19 | sum += score[index]; // add them up 20 | average = (float) sum / SIZE; // time-honored method 21 | printf("Sum of scores = %d, average = %.2f\n", sum, average); 22 | printf("That's a handicap of %.0f.\n", average - PAR); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /assets/codes/Ch06/summing.c: -------------------------------------------------------------------------------- 1 | /* summing.c -- sums integers entered interactively */ 2 | #include 3 | int main(void) 4 | { 5 | long num; 6 | long sum = 0L; /* initialize sum to zero */ 7 | int status; 8 | 9 | printf("Please enter an integer to be summed "); 10 | printf("(q to quit): "); 11 | status = scanf("%ld", &num); 12 | while (status == 1) /* == means "is equal to" */ 13 | { 14 | sum = sum + num; 15 | printf("Please enter next integer (q to quit): "); 16 | status = scanf("%ld", &num); 17 | } 18 | printf("Those integers sum to %ld.\n", sum); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /assets/codes/Ch06/sweetie1.c: -------------------------------------------------------------------------------- 1 | // sweetie1.c -- a counting loop 2 | #include 3 | int main(void) 4 | { 5 | const int NUMBER = 22; 6 | int count = 1; // initialization 7 | 8 | while (count <= NUMBER) // test 9 | { 10 | printf("Be my Valentine!\n"); // action 11 | count++; // update count 12 | } 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /assets/codes/Ch06/sweetie2.c: -------------------------------------------------------------------------------- 1 | // sweetie2.c -- a counting loop using for 2 | #include 3 | int main(void) 4 | { 5 | const int NUMBER = 22; 6 | int count; 7 | 8 | for (count = 1; count <= NUMBER; count++) 9 | printf("Be my Valentine!\n"); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /assets/codes/Ch06/t_and_f.c: -------------------------------------------------------------------------------- 1 | /* t_and_f.c -- true and false values in C */ 2 | #include 3 | int main(void) 4 | { 5 | int true_val, false_val; 6 | 7 | true_val = (10 > 2); // value of a true relationship 8 | false_val = (10 == 2); // value of a false relationship 9 | printf("true = %d; false = %d \n", true_val, false_val); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /assets/codes/Ch06/trouble.c: -------------------------------------------------------------------------------- 1 | // trouble.c -- misuse of = 2 | // will cause infinite loop 3 | #include 4 | int main(void) 5 | { 6 | long num; 7 | long sum = 0L; 8 | int status; 9 | 10 | printf("Please enter an integer to be summed "); 11 | printf("(q to quit): "); 12 | status = scanf("%ld", &num); 13 | while (status = 1) 14 | { 15 | sum = sum + num; 16 | printf("Please enter next integer (q to quit): "); 17 | status = scanf("%ld", &num); 18 | } 19 | printf("Those integers sum to %ld.\n", sum); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /assets/codes/Ch06/truth.c: -------------------------------------------------------------------------------- 1 | // truth.c -- what values are true? 2 | #include 3 | int main(void) 4 | { 5 | int n = 3; 6 | 7 | while (n) 8 | printf("%2d is true\n", n--); 9 | printf("%2d is false\n", n); 10 | 11 | n = -3; 12 | while (n) 13 | printf("%2d is true\n", n++); 14 | printf("%2d is false\n", n); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /assets/codes/Ch06/when.c: -------------------------------------------------------------------------------- 1 | // when.c -- when a loop quits 2 | #include 3 | int main(void) 4 | { 5 | int n = 5; 6 | 7 | while (n < 7) // line 7 8 | { 9 | printf("n = %d\n", n); 10 | n++; // line 10 11 | printf("Now n = %d\n", n); // line 11 12 | } 13 | printf("The loop has finished.\n"); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /assets/codes/Ch06/while1.c: -------------------------------------------------------------------------------- 1 | /* while1.c -- watch your braces */ 2 | /* bad coding creates an infinite loop */ 3 | #include 4 | int main(void) 5 | { 6 | int n = 0; 7 | 8 | while (n < 3) 9 | printf("n is %d\n", n); 10 | n++; 11 | printf("That's all this program does\n"); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /assets/codes/Ch06/while2.c: -------------------------------------------------------------------------------- 1 | /* while2.c -- watch your semicolons */ 2 | #include 3 | int main(void) 4 | { 5 | int n = 0; 6 | 7 | while (n++ < 3); /* line 7 */ 8 | printf("n is %d\n", n); /* line 8 */ 9 | printf("That's all this program does.\n"); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /assets/codes/Ch06/zeno.c: -------------------------------------------------------------------------------- 1 | /* zeno.c -- series sum */ 2 | #include 3 | 4 | int main(void) 5 | { 6 | int t_ct; // term count 7 | double time, power_of_2; 8 | int limit; 9 | 10 | printf("Enter the number of terms you want: "); 11 | scanf("%d", &limit); 12 | for (time=0, power_of_2=1, t_ct=1; t_ct <= limit; 13 | t_ct++, power_of_2 *= 2.0) 14 | { 15 | time += 1.0/power_of_2; 16 | printf("time = %f when terms = %d.\n", time, t_ct); 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /assets/codes/Ch07/break.c: -------------------------------------------------------------------------------- 1 | /* break.c -- uses break to exit a loop */ 2 | #include 3 | int main(void) 4 | { 5 | float length, width; 6 | 7 | printf("Enter the length of the rectangle:\n"); 8 | while (scanf("%f", &length) == 1) 9 | { 10 | printf("Length = %0.2f:\n", length); 11 | printf("Enter its width:\n"); 12 | if (scanf("%f", &width) != 1) 13 | break; 14 | printf("Width = %0.2f:\n", width); 15 | printf("Area = %0.2f:\n", length * width); 16 | printf("Enter the length of the rectangle:\n"); 17 | } 18 | printf("Done.\n"); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /assets/codes/Ch07/chcount.c: -------------------------------------------------------------------------------- 1 | // chcount.c -- use the logical AND operator 2 | #include 3 | #define PERIOD '.' 4 | int main(void) 5 | { 6 | char ch; 7 | int charcount = 0; 8 | 9 | while ((ch = getchar()) != PERIOD) 10 | { 11 | if (ch != '"' && ch != '\'') 12 | charcount++; 13 | } 14 | printf("There are %d non-quote characters.\n", charcount); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /assets/codes/Ch07/colddays.c: -------------------------------------------------------------------------------- 1 | // colddays.c -- finds percentage of days below freezing 2 | #include 3 | int main(void) 4 | { 5 | const int FREEZING = 0; 6 | float temperature; 7 | int cold_days = 0; 8 | int all_days = 0; 9 | 10 | printf("Enter the list of daily low temperatures.\n"); 11 | printf("Use Celsius, and enter q to quit.\n"); 12 | while (scanf("%f", &temperature) == 1) 13 | { 14 | all_days++; 15 | if (temperature < FREEZING) 16 | cold_days++; 17 | } 18 | if (all_days != 0) 19 | printf("%d days total: %.1f%% were below freezing.\n", 20 | all_days, 100.0 * (float) cold_days / all_days); 21 | if (all_days == 0) 22 | printf("No data entered!\n"); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /assets/codes/Ch07/cypher1.c: -------------------------------------------------------------------------------- 1 | // cypher1.c -- alters input, preserving spaces 2 | #include 3 | #define SPACE ' ' // that's quote-space-quote 4 | int main(void) 5 | { 6 | char ch; 7 | 8 | ch = getchar(); // read a character 9 | while (ch != '\n') // while not end of line 10 | { 11 | if (ch == SPACE) // leave the space 12 | putchar(ch); // character unchanged 13 | else 14 | putchar(ch + 1); // change other characters 15 | ch = getchar(); // get next character 16 | } 17 | putchar(ch); // print the newline 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /assets/codes/Ch07/cypher2.c: -------------------------------------------------------------------------------- 1 | // cypher2.c -- alters input, preserving non-letters 2 | #include 3 | #include // for isalpha() 4 | int main(void) 5 | { 6 | char ch; 7 | 8 | while ((ch = getchar()) != '\n') 9 | { 10 | if (isalpha(ch)) // if a letter, 11 | putchar(ch + 1); // display next letter 12 | else // otherwise, 13 | putchar(ch); // display as is 14 | } 15 | putchar(ch); // display the newline 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /assets/codes/Ch07/paint.c: -------------------------------------------------------------------------------- 1 | /* paint.c -- uses conditional operator */ 2 | #include 3 | #define COVERAGE 350 // square feet per paint can 4 | int main(void) 5 | { 6 | int sq_feet; 7 | int cans; 8 | 9 | printf("Enter number of square feet to be painted:\n"); 10 | while (scanf("%d", &sq_feet) == 1) 11 | { 12 | cans = sq_feet / COVERAGE; 13 | cans += ((sq_feet % COVERAGE == 0)) ? 0 : 1; 14 | printf("You need %d %s of paint.\n", cans, 15 | cans == 1 ? "can" : "cans"); 16 | printf("Enter next value (q to quit):\n"); 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /assets/codes/Ch08/echo.c: -------------------------------------------------------------------------------- 1 | /* echo.c -- repeats input */ 2 | #include 3 | int main(void) 4 | { 5 | char ch; 6 | 7 | while ((ch = getchar()) != '#') 8 | putchar(ch); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /assets/codes/Ch08/echo_eof.c: -------------------------------------------------------------------------------- 1 | /* echo_eof.c -- repeats input to end of file */ 2 | #include 3 | int main(void) 4 | { 5 | int ch; 6 | 7 | while ((ch = getchar()) != EOF) 8 | putchar(ch); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /assets/codes/Ch08/file_eof.c: -------------------------------------------------------------------------------- 1 | // file_eof.c --open a file and display it 2 | #include 3 | #include // for exit() 4 | int main() 5 | { 6 | int ch; 7 | FILE * fp; 8 | char fname[50]; // to hold the file name 9 | 10 | printf("Enter the name of the file: "); 11 | scanf("%s", fname); 12 | fp = fopen(fname, "r"); // open file for reading 13 | if (fp == NULL) // attempt failed 14 | { 15 | printf("Failed to open file. Bye\n"); 16 | exit(1); // quit program 17 | } 18 | // getc(fp) gets a character from the open file 19 | while ((ch = getc(fp)) != EOF) 20 | putchar(ch); 21 | fclose(fp); // close the file 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /assets/codes/Ch08/guess.c: -------------------------------------------------------------------------------- 1 | /* guess.c -- an inefficient and faulty number-guesser */ 2 | #include 3 | int main(void) 4 | { 5 | int guess = 1; 6 | 7 | printf("Pick an integer from 1 to 100. I will try to guess "); 8 | printf("it.\nRespond with a y if my guess is right and with"); 9 | printf("\nan n if it is wrong.\n"); 10 | printf("Uh...is your number %d?\n", guess); 11 | while (getchar() != 'y') /* get response, compare to y */ 12 | printf("Well, then, is it %d?\n", ++guess); 13 | printf("I knew I could do it!\n"); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /assets/codes/Ch08/newguess.c: -------------------------------------------------------------------------------- 1 | /* newguess.c -- an inefficient but improved number-guesser */ 2 | #include 3 | int main(void) 4 | { 5 | int guess = 1; 6 | char response; 7 | 8 | printf("Pick an integer from 1 to 100. I will try to guess "); 9 | printf("it.\nRespond with a y if my guess is right and with"); 10 | printf("\nan n if it is wrong.\n"); 11 | printf("Uh...is your number %d?\n", guess); 12 | while ((response = getchar()) != 'y') /* get response */ 13 | { 14 | if (response == 'n') 15 | printf("Well, then, is it %d?\n", ++guess); 16 | else 17 | printf("Sorry, I understand only y or n.\n"); 18 | while (getchar() != '\n') 19 | continue; /* skip rest of input line */ 20 | } 21 | printf("I knew I could do it!\n"); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /assets/codes/Ch08/showchar1.c: -------------------------------------------------------------------------------- 1 | /* showchar1.c -- program with a BIG I/O problem */ 2 | #include 3 | void display(char cr, int lines, int width); 4 | int main(void) 5 | { 6 | int ch; /* character to be printed */ 7 | int rows, cols; /* number of rows and columns */ 8 | printf("Enter a character and two integers:\n"); 9 | while ((ch = getchar()) != '\n') 10 | { 11 | scanf("%d %d", &rows, &cols); 12 | display(ch, rows, cols); 13 | printf("Enter another character and two integers;\n"); 14 | printf("Enter a newline to quit.\n"); 15 | } 16 | printf("Bye.\n"); 17 | 18 | return 0; 19 | } 20 | 21 | void display(char cr, int lines, int width) 22 | { 23 | int row, col; 24 | 25 | for (row = 1; row <= lines; row++) 26 | { 27 | for (col = 1; col <= width; col++) 28 | putchar(cr); 29 | putchar('\n'); /* end line and start a new one */ 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /assets/codes/Ch09/binary.c: -------------------------------------------------------------------------------- 1 | /* binary.c -- prints integer in binary form */ 2 | #include 3 | void to_binary(unsigned long n); 4 | 5 | int main(void) 6 | { 7 | unsigned long number; 8 | printf("Enter an integer (q to quit):\n"); 9 | while (scanf("%lu", &number) == 1) 10 | { 11 | printf("Binary equivalent: "); 12 | to_binary(number); 13 | putchar('\n'); 14 | printf("Enter an integer (q to quit):\n"); 15 | } 16 | printf("Done.\n"); 17 | 18 | return 0; 19 | } 20 | 21 | void to_binary(unsigned long n) /* recursive function */ 22 | { 23 | int r; 24 | 25 | r = n % 2; 26 | if (n >= 2) 27 | to_binary(n / 2); 28 | putchar(r == 0 ? '0' : '1'); 29 | 30 | return; 31 | } 32 | -------------------------------------------------------------------------------- /assets/codes/Ch09/hotel.h: -------------------------------------------------------------------------------- 1 | /* hotel.h -- constants and declarations for hotel.c */ 2 | #define QUIT 5 3 | #define HOTEL1 180.00 4 | #define HOTEL2 225.00 5 | #define HOTEL3 255.00 6 | #define HOTEL4 355.00 7 | #define DISCOUNT 0.95 8 | #define STARS "**********************************" 9 | 10 | // shows list of choices 11 | int menu(void); 12 | 13 | // returns number of nights desired 14 | int getnights(void); 15 | 16 | // calculates price from rate, nights 17 | // and displays result 18 | void showprice(double rate, int nights); 19 | -------------------------------------------------------------------------------- /assets/codes/Ch09/lesser.c: -------------------------------------------------------------------------------- 1 | /* lesser.c -- finds the lesser of two evils */ 2 | #include 3 | int imin(int, int); 4 | 5 | int main(void) 6 | { 7 | int evil1, evil2; 8 | 9 | printf("Enter a pair of integers (q to quit):\n"); 10 | while (scanf("%d %d", &evil1, &evil2) == 2) 11 | { 12 | printf("The lesser of %d and %d is %d.\n", 13 | evil1, evil2, imin(evil1,evil2)); 14 | printf("Enter a pair of integers (q to quit):\n"); 15 | } 16 | printf("Bye.\n"); 17 | 18 | return 0; 19 | } 20 | 21 | int imin(int n,int m) 22 | { 23 | int min; 24 | 25 | if (n < m) 26 | min = n; 27 | else 28 | min = m; 29 | 30 | return min; 31 | } 32 | -------------------------------------------------------------------------------- /assets/codes/Ch09/lethead1.c: -------------------------------------------------------------------------------- 1 | /* lethead1.c */ 2 | #include 3 | #define NAME "GIGATHINK, INC." 4 | #define ADDRESS "101 Megabuck Plaza" 5 | #define PLACE "Megapolis, CA 94904" 6 | #define WIDTH 40 7 | 8 | void starbar(void); /* prototype the function */ 9 | 10 | int main(void) 11 | { 12 | starbar(); 13 | printf("%s\n", NAME); 14 | printf("%s\n", ADDRESS); 15 | printf("%s\n", PLACE); 16 | starbar(); /* use the function */ 17 | 18 | return 0; 19 | } 20 | 21 | void starbar(void) /* define the function */ 22 | { 23 | int count; 24 | 25 | for (count = 1; count <= WIDTH; count++) 26 | putchar('*'); 27 | putchar('\n'); 28 | } 29 | -------------------------------------------------------------------------------- /assets/codes/Ch09/loccheck.c: -------------------------------------------------------------------------------- 1 | /* loccheck.c -- checks to see where variables are stored */ 2 | #include 3 | void mikado(int); /* declare function */ 4 | int main(void) 5 | { 6 | int pooh = 2, bah = 5; /* local to main() */ 7 | 8 | printf("In main(), pooh = %d and &pooh = %p\n", 9 | pooh, &pooh); 10 | printf("In main(), bah = %d and &bah = %p\n", 11 | bah, &bah); 12 | mikado(pooh); 13 | 14 | return 0; 15 | } 16 | 17 | void mikado(int bah) /* define function */ 18 | { 19 | int pooh = 10; /* local to mikado() */ 20 | 21 | printf("In mikado(), pooh = %d and &pooh = %p\n", 22 | pooh, &pooh); 23 | printf("In mikado(), bah = %d and &bah = %p\n", 24 | bah, &bah); 25 | } 26 | -------------------------------------------------------------------------------- /assets/codes/Ch09/misuse.c: -------------------------------------------------------------------------------- 1 | /* misuse.c -- uses a function incorrectly */ 2 | #include 3 | int imax(); /* old-style declaration */ 4 | 5 | int main(void) 6 | { 7 | printf("The maximum of %d and %d is %d.\n", 8 | 3, 5, imax(3)); 9 | printf("The maximum of %d and %d is %d.\n", 10 | 3, 5, imax(3.0, 5.0)); 11 | return 0; 12 | } 13 | 14 | int imax(n, m) 15 | int n, m; 16 | { 17 | return (n > m ? n : m); 18 | } 19 | -------------------------------------------------------------------------------- /assets/codes/Ch09/proto.c: -------------------------------------------------------------------------------- 1 | /* proto.c -- uses a function prototype */ 2 | #include 3 | int imax(int, int); /* prototype */ 4 | int main(void) 5 | { 6 | printf("The maximum of %d and %d is %d.\n", 7 | 3, 5, imax(3)); 8 | printf("The maximum of %d and %d is %d.\n", 9 | 3, 5, imax(3.0, 5.0)); 10 | return 0; 11 | } 12 | 13 | int imax(int n, int m) 14 | { 15 | return (n > m ? n : m); 16 | } 17 | -------------------------------------------------------------------------------- /assets/codes/Ch09/recur.c: -------------------------------------------------------------------------------- 1 | /* recur.c -- recursion illustration */ 2 | #include 3 | void up_and_down(int); 4 | 5 | int main(void) 6 | { 7 | up_and_down(1); 8 | return 0; 9 | } 10 | 11 | void up_and_down(int n) 12 | { 13 | printf("Level %d: n location %p\n", n, &n); // 1 14 | if (n < 4) 15 | up_and_down(n+1); 16 | printf("LEVEL %d: n location %p\n", n, &n); // 2 17 | 18 | } 19 | -------------------------------------------------------------------------------- /assets/codes/Ch09/swap1.c: -------------------------------------------------------------------------------- 1 | /* swap1.c -- first attempt at a swapping function */ 2 | #include 3 | void interchange(int u, int v); /* declare function */ 4 | 5 | int main(void) 6 | { 7 | int x = 5, y = 10; 8 | 9 | printf("Originally x = %d and y = %d.\n", x , y); 10 | interchange(x, y); 11 | printf("Now x = %d and y = %d.\n", x, y); 12 | 13 | return 0; 14 | } 15 | 16 | void interchange(int u, int v) /* define function */ 17 | { 18 | int temp; 19 | 20 | temp = u; 21 | u = v; 22 | v = temp; 23 | } 24 | -------------------------------------------------------------------------------- /assets/codes/Ch09/swap2.c: -------------------------------------------------------------------------------- 1 | /* swap2.c -- researching swap1.c */ 2 | #include 3 | void interchange(int u, int v); 4 | 5 | int main(void) 6 | { 7 | int x = 5, y = 10; 8 | 9 | printf("Originally x = %d and y = %d.\n", x , y); 10 | interchange(x, y); 11 | printf("Now x = %d and y = %d.\n", x, y); 12 | 13 | return 0; 14 | } 15 | 16 | void interchange(int u, int v) 17 | { 18 | int temp; 19 | 20 | printf("Originally u = %d and v = %d.\n", u , v); 21 | temp = u; 22 | u = v; 23 | v = temp; 24 | printf("Now u = %d and v = %d.\n", u, v); 25 | } 26 | -------------------------------------------------------------------------------- /assets/codes/Ch09/usehotel.c: -------------------------------------------------------------------------------- 1 | /* usehotel.c -- room rate program */ 2 | /* compile with Listing 9.10 */ 3 | #include 4 | #include "hotel.h" /* defines constants, declares functions */ 5 | 6 | int main(void) 7 | { 8 | int nights; 9 | double hotel_rate; 10 | int code; 11 | 12 | while ((code = menu()) != QUIT) 13 | { 14 | switch(code) 15 | { 16 | case 1 : hotel_rate = HOTEL1; 17 | break; 18 | case 2 : hotel_rate = HOTEL2; 19 | break; 20 | case 3 : hotel_rate = HOTEL3; 21 | break; 22 | case 4 : hotel_rate = HOTEL4; 23 | break; 24 | default: hotel_rate = 0.0; 25 | printf("Oops!\n"); 26 | break; 27 | } 28 | nights = getnights(); 29 | showprice(hotel_rate, nights); 30 | } 31 | printf("Thank you and goodbye.\n"); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /assets/codes/Ch10/arf.c: -------------------------------------------------------------------------------- 1 | /* arf.c -- array functions */ 2 | #include 3 | #define SIZE 5 4 | void show_array(const double ar[], int n); 5 | void mult_array(double ar[], int n, double mult); 6 | int main(void) 7 | { 8 | double dip[SIZE] = {20.0, 17.66, 8.2, 15.3, 22.22}; 9 | 10 | printf("The original dip array:\n"); 11 | show_array(dip, SIZE); 12 | mult_array(dip, SIZE, 2.5); 13 | printf("The dip array after calling mult_array():\n"); 14 | show_array(dip, SIZE); 15 | 16 | return 0; 17 | } 18 | 19 | /* displays array contents */ 20 | void show_array(const double ar[], int n) 21 | { 22 | int i; 23 | 24 | for (i = 0; i < n; i++) 25 | printf("%8.3f ", ar[i]); 26 | putchar('\n'); 27 | } 28 | 29 | /* multiplies each array member by the same multiplier */ 30 | void mult_array(double ar[], int n, double mult) 31 | { 32 | int i; 33 | 34 | for (i = 0; i < n; i++) 35 | ar[i] *= mult; 36 | } 37 | -------------------------------------------------------------------------------- /assets/codes/Ch10/bounds.c: -------------------------------------------------------------------------------- 1 | // bounds.c -- exceed the bounds of an array 2 | #include 3 | #define SIZE 4 4 | int main(void) 5 | { 6 | int value1 = 44; 7 | int arr[SIZE]; 8 | int value2 = 88; 9 | int i; 10 | 11 | printf("value1 = %d, value2 = %d\n", value1, value2); 12 | for (i = -1; i <= SIZE; i++) 13 | arr[i] = 2 * i + 1; 14 | 15 | for (i = -1; i < 7; i++) 16 | printf("%2d %d\n", i , arr[i]); 17 | printf("value1 = %d, value2 = %d\n", value1, value2); 18 | 19 | printf("address of arr[-1]: %p\n", &arr[-1]); 20 | printf("address of arr[4]: %p\n", &arr[4]); 21 | printf("address of value1: %p\n", &value1); 22 | printf("address of value2: %p\n", &value2); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /assets/codes/Ch10/day_mon1.c: -------------------------------------------------------------------------------- 1 | /* day_mon1.c -- prints the days for each month */ 2 | #include 3 | #define MONTHS 12 4 | 5 | int main(void) 6 | { 7 | int days[MONTHS] = {31,28,31,30,31,30,31,31,30,31,30,31}; 8 | int index; 9 | 10 | for (index = 0; index < MONTHS; index++) 11 | printf("Month %d has %2d days.\n", index +1, 12 | days[index]); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /assets/codes/Ch10/day_mon2.c: -------------------------------------------------------------------------------- 1 | /* day_mon2.c -- letting the compiler count elements */ 2 | #include 3 | int main(void) 4 | { 5 | const int days[] = {31,28,31,30,31,30,31,31,30,31}; 6 | int index; 7 | 8 | for (index = 0; index < sizeof days / sizeof days[0]; index++) 9 | printf("Month %2d has %d days.\n", index +1, 10 | days[index]); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /assets/codes/Ch10/day_mon3.c: -------------------------------------------------------------------------------- 1 | /* day_mon3.c -- uses pointer notation */ 2 | #include 3 | #define MONTHS 12 4 | 5 | int main(void) 6 | { 7 | int days[MONTHS] = {31,28,31,30,31,30,31,31,30,31,30,31}; 8 | int index; 9 | 10 | for (index = 0; index < MONTHS; index++) 11 | printf("Month %2d has %d days.\n", index +1, 12 | *(days + index)); // same as days[index] 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /assets/codes/Ch10/designate.c: -------------------------------------------------------------------------------- 1 | // designate.c -- use designated initializers 2 | #include 3 | #define MONTHS 12 4 | int main(void) 5 | { 6 | int days[MONTHS] = {31,28, [4] = 31,30,31, [1] = 29}; 7 | int i; 8 | 9 | for (i = 0; i < MONTHS; i++) 10 | printf("%2d %d\n", i + 1, days[i]); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /assets/codes/Ch10/no_data.c: -------------------------------------------------------------------------------- 1 | /* no_data.c -- uninitialized array */ 2 | #include 3 | #define SIZE 4 4 | int main(void) 5 | { 6 | int no_data[SIZE]; /* uninitialized array */ 7 | int i; 8 | 9 | printf("%2s%14s\n", 10 | "i", "no_data[i]"); 11 | for (i = 0; i < SIZE; i++) 12 | printf("%2d%14d\n", i, no_data[i]); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /assets/codes/Ch10/order.c: -------------------------------------------------------------------------------- 1 | /* order.c -- precedence in pointer operations */ 2 | #include 3 | int data[2] = {100, 200}; 4 | int moredata[2] = {300, 400}; 5 | int main(void) 6 | { 7 | int * p1, * p2, * p3; 8 | 9 | p1 = p2 = data; 10 | p3 = moredata; 11 | printf(" *p1 = %d, *p2 = %d, *p3 = %d\n", 12 | *p1 , *p2 , *p3); 13 | printf("*p1++ = %d, *++p2 = %d, (*p3)++ = %d\n", 14 | *p1++ , *++p2 , (*p3)++); 15 | printf(" *p1 = %d, *p2 = %d, *p3 = %d\n", 16 | *p1 , *p2 , *p3); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /assets/codes/Ch10/pnt_add.c: -------------------------------------------------------------------------------- 1 | // pnt_add.c -- pointer addition 2 | #include 3 | #define SIZE 4 4 | int main(void) 5 | { 6 | short dates [SIZE]; 7 | short * pti; 8 | short index; 9 | double bills[SIZE]; 10 | double * ptf; 11 | 12 | pti = dates; // assign address of array to pointer 13 | ptf = bills; 14 | printf("%23s %15s\n", "short", "double"); 15 | for (index = 0; index < SIZE; index ++) 16 | printf("pointers + %d: %10p %10p\n", 17 | index, pti + index, ptf + index); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /assets/codes/Ch10/some_data.c: -------------------------------------------------------------------------------- 1 | /* some_data.c -- partially initialized array */ 2 | #include 3 | #define SIZE 4 4 | int main(void) 5 | { 6 | int some_data[SIZE] = {1492, 1066}; 7 | int i; 8 | 9 | printf("%2s%14s\n", 10 | "i", "some_data[i]"); 11 | for (i = 0; i < SIZE; i++) 12 | printf("%2d%14d\n", i, some_data[i]); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /assets/codes/Ch10/sum_arr1.c: -------------------------------------------------------------------------------- 1 | // sum_arr1.c -- sums the elements of an array 2 | // use %u or %lu if %zd doesn't work 3 | #include 4 | #define SIZE 10 5 | int sum(int ar[], int n); 6 | int main(void) 7 | { 8 | int marbles[SIZE] = {20,10,5,39,4,16,19,26,31,20}; 9 | long answer; 10 | 11 | answer = sum(marbles, SIZE); 12 | printf("The total number of marbles is %ld.\n", answer); 13 | printf("The size of marbles is %zd bytes.\n", 14 | sizeof marbles); 15 | 16 | return 0; 17 | } 18 | 19 | int sum(int ar[], int n) // how big an array? 20 | { 21 | int i; 22 | int total = 0; 23 | 24 | for( i = 0; i < n; i++) 25 | total += ar[i]; 26 | printf("The size of ar is %zd bytes.\n", sizeof ar); 27 | 28 | return total; 29 | } 30 | -------------------------------------------------------------------------------- /assets/codes/Ch10/sum_arr2.c: -------------------------------------------------------------------------------- 1 | /* sum_arr2.c -- sums the elements of an array */ 2 | #include 3 | #define SIZE 10 4 | int sump(int * start, int * end); 5 | int main(void) 6 | { 7 | int marbles[SIZE] = {20,10,5,39,4,16,19,26,31,20}; 8 | long answer; 9 | 10 | answer = sump(marbles, marbles + SIZE); 11 | printf("The total number of marbles is %ld.\n", answer); 12 | 13 | return 0; 14 | } 15 | 16 | /* use pointer arithmetic */ 17 | int sump(int * start, int * end) 18 | { 19 | int total = 0; 20 | 21 | while (start < end) 22 | { 23 | total += *start; // add value to total 24 | start++; // advance pointer to next element 25 | } 26 | 27 | return total; 28 | } 29 | -------------------------------------------------------------------------------- /assets/codes/Ch10/zippo1.c: -------------------------------------------------------------------------------- 1 | /* zippo1.c -- zippo info */ 2 | #include 3 | int main(void) 4 | { 5 | int zippo[4][2] = { {2,4}, {6,8}, {1,3}, {5, 7} }; 6 | 7 | printf(" zippo = %p, zippo + 1 = %p\n", 8 | zippo, zippo + 1); 9 | printf("zippo[0] = %p, zippo[0] + 1 = %p\n", 10 | zippo[0], zippo[0] + 1); 11 | printf(" *zippo = %p, *zippo + 1 = %p\n", 12 | *zippo, *zippo + 1); 13 | printf("zippo[0][0] = %d\n", zippo[0][0]); 14 | printf(" *zippo[0] = %d\n", *zippo[0]); 15 | printf(" **zippo = %d\n", **zippo); 16 | printf(" zippo[2][1] = %d\n", zippo[2][1]); 17 | printf("*(*(zippo+2) + 1) = %d\n", *(*(zippo+2) + 1)); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /assets/codes/Ch10/zippo2.c: -------------------------------------------------------------------------------- 1 | /* zippo2.c -- zippo info via a pointer variable */ 2 | #include 3 | int main(void) 4 | { 5 | int zippo[4][2] = { {2,4}, {6,8}, {1,3}, {5, 7} }; 6 | int (*pz)[2]; 7 | pz = zippo; 8 | 9 | printf(" pz = %p, pz + 1 = %p\n", 10 | pz, pz + 1); 11 | printf("pz[0] = %p, pz[0] + 1 = %p\n", 12 | pz[0], pz[0] + 1); 13 | printf(" *pz = %p, *pz + 1 = %p\n", 14 | *pz, *pz + 1); 15 | printf("pz[0][0] = %d\n", pz[0][0]); 16 | printf(" *pz[0] = %d\n", *pz[0]); 17 | printf(" **pz = %d\n", **pz); 18 | printf(" pz[2][1] = %d\n", pz[2][1]); 19 | printf("*(*(pz+2) + 1) = %d\n", *(*(pz+2) + 1)); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /assets/codes/Ch11/addresses.c: -------------------------------------------------------------------------------- 1 | // addresses.c -- addresses of strings 2 | #define MSG "I'm special." 3 | 4 | #include 5 | int main() 6 | { 7 | char ar[] = MSG; 8 | const char *pt = MSG; 9 | printf("address of \"I'm special\": %p \n", "I'm special"); 10 | printf(" address ar: %p\n", ar); 11 | printf(" address pt: %p\n", pt); 12 | printf(" address of MSG: %p\n", MSG); 13 | printf("address of \"I'm special\": %p \n", "I'm special"); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /assets/codes/Ch11/arrchar.c: -------------------------------------------------------------------------------- 1 | // arrchar.c -- array of pointers, array of strings 2 | #include 3 | #define SLEN 40 4 | #define LIM 5 5 | int main(void) 6 | { 7 | const char *mytalents[LIM] = { 8 | "Adding numbers swiftly", 9 | "Multiplying accurately", "Stashing data", 10 | "Following instructions to the letter", 11 | "Understanding the C language" 12 | }; 13 | char yourtalents[LIM][SLEN] = { 14 | "Walking in a straight line", 15 | "Sleeping", "Watching television", 16 | "Mailing letters", "Reading email" 17 | }; 18 | int i; 19 | 20 | puts("Let's compare talents."); 21 | printf ("%-36s %-25s\n", "My Talents", "Your Talents"); 22 | for (i = 0; i < LIM; i++) 23 | printf("%-36s %-25s\n", mytalents[i], yourtalents[i]); 24 | printf("\nsizeof mytalents: %zd, sizeof yourtalents: %zd\n", 25 | sizeof(mytalents), sizeof(yourtalents)); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /assets/codes/Ch11/compare.c: -------------------------------------------------------------------------------- 1 | /* compare.c -- this will work */ 2 | #include 3 | #include // declares strcmp() 4 | 5 | #define ANSWER "Grant" 6 | #define SIZE 40 7 | char * s_gets(char * st, int n); 8 | 9 | int main(void) 10 | { 11 | char try[SIZE]; 12 | 13 | puts("Who is buried in Grant's tomb?"); 14 | s_gets(try, SIZE); 15 | while (strcmp(try,ANSWER) != 0) 16 | { 17 | puts("No, that's wrong. Try again."); 18 | s_gets(try, SIZE); 19 | } 20 | puts("That's right!"); 21 | 22 | return 0; 23 | } 24 | 25 | 26 | char * s_gets(char * st, int n) 27 | { 28 | char * ret_val; 29 | int i = 0; 30 | 31 | ret_val = fgets(st, n, stdin); 32 | if (ret_val) 33 | { 34 | while (st[i] != '\n' && st[i] != '\0') 35 | i++; 36 | if (st[i] == '\n') 37 | st[i] = '\0'; 38 | else // must have words[i] == '\0' 39 | while (getchar() != '\n') 40 | continue; 41 | } 42 | return ret_val; 43 | } 44 | -------------------------------------------------------------------------------- /assets/codes/Ch11/compback.c: -------------------------------------------------------------------------------- 1 | /* compback.c -- strcmp returns */ 2 | #include 3 | #include 4 | int main(void) 5 | { 6 | 7 | printf("strcmp(\"A\", \"A\") is "); 8 | printf("%d\n", strcmp("A", "A")); 9 | 10 | printf("strcmp(\"A\", \"B\") is "); 11 | printf("%d\n", strcmp("A", "B")); 12 | 13 | printf("strcmp(\"B\", \"A\") is "); 14 | printf("%d\n", strcmp("B", "A")); 15 | 16 | printf("strcmp(\"C\", \"A\") is "); 17 | printf("%d\n", strcmp("C", "A")); 18 | 19 | printf("strcmp(\"Z\", \"a\") is "); 20 | printf("%d\n", strcmp("Z", "a")); 21 | 22 | printf("strcmp(\"apples\", \"apple\") is "); 23 | printf("%d\n", strcmp("apples", "apple")); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /assets/codes/Ch11/copy2.c: -------------------------------------------------------------------------------- 1 | /* copy2.c -- strcpy() demo */ 2 | #include 3 | #include // declares strcpy() 4 | #define WORDS "beast" 5 | #define SIZE 40 6 | 7 | int main(void) 8 | { 9 | const char * orig = WORDS; 10 | char copy[SIZE] = "Be the best that you can be."; 11 | char * ps; 12 | 13 | puts(orig); 14 | puts(copy); 15 | ps = strcpy(copy + 7, orig); 16 | puts(copy); 17 | puts(ps); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /assets/codes/Ch11/fgets1.c: -------------------------------------------------------------------------------- 1 | /* fgets1.c -- using fgets() and fputs() */ 2 | #include 3 | #define STLEN 14 4 | int main(void) 5 | { 6 | char words[STLEN]; 7 | 8 | puts("Enter a string, please."); 9 | fgets(words, STLEN, stdin); 10 | printf("Your string twice (puts(), then fputs()):\n"); 11 | puts(words); 12 | fputs(words, stdout); 13 | puts("Enter another string, please."); 14 | fgets(words, STLEN, stdin); 15 | printf("Your string twice (puts(), then fputs()):\n"); 16 | puts(words); 17 | fputs(words, stdout); 18 | puts("Done."); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /assets/codes/Ch11/fgets2.c: -------------------------------------------------------------------------------- 1 | /* fgets2.c -- using fgets() and fputs() */ 2 | #include 3 | #define STLEN 10 4 | int main(void) 5 | { 6 | char words[STLEN]; 7 | 8 | puts("Enter strings (empty line to quit):"); 9 | while (fgets(words, STLEN, stdin) != NULL && words[0] != '\n') 10 | fputs(words, stdout); 11 | puts("Done."); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /assets/codes/Ch11/fgets3.c: -------------------------------------------------------------------------------- 1 | /* fgets3.c -- using fgets() */ 2 | #include 3 | #define STLEN 10 4 | int main(void) 5 | { 6 | char words[STLEN]; 7 | int i; 8 | 9 | puts("Enter strings (empty line to quit):"); 10 | while (fgets(words, STLEN, stdin) != NULL 11 | && words[0] != '\n') 12 | { 13 | i = 0; 14 | while (words[i] != '\n' && words[i] != '\0') 15 | i++; 16 | if (words[i] == '\n') 17 | words[i] = '\0'; 18 | else // must have words[i] == '\0' 19 | while (getchar() != '\n') 20 | continue; 21 | puts(words); 22 | } 23 | puts("done"); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /assets/codes/Ch11/getsputs.c: -------------------------------------------------------------------------------- 1 | /* getsputs.c -- using gets() and puts() */ 2 | #include 3 | #define STLEN 81 4 | int main(void) 5 | { 6 | char words[STLEN]; 7 | 8 | puts("Enter a string, please."); 9 | gets(words); // typical use 10 | printf("Your string twice:\n"); 11 | printf("%s\n", words); 12 | puts(words); 13 | puts("Done."); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /assets/codes/Ch11/name1.c: -------------------------------------------------------------------------------- 1 | /* name1.c -- reads a name */ 2 | #include 3 | #define MAX 81 4 | int main(void) 5 | { 6 | char name[MAX]; /* allot space */ 7 | 8 | printf("Hi, what's your name?\n"); 9 | gets(name); 10 | // gets_s(name, MAX); /* place string into name array */ 11 | printf("Nice name, %s.\n", name); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /assets/codes/Ch11/nogo.c: -------------------------------------------------------------------------------- 1 | /* nogo.c -- will this work? */ 2 | #include 3 | #define ANSWER "Grant" 4 | #define SIZE 40 5 | char * s_gets(char * st, int n); 6 | 7 | int main(void) 8 | { 9 | char try[SIZE]; 10 | 11 | puts("Who is buried in Grant's tomb?"); 12 | s_gets(try, SIZE); 13 | while (try != ANSWER) 14 | { 15 | puts("No, that's wrong. Try again."); 16 | s_gets(try, SIZE); 17 | } 18 | puts("That's right!"); 19 | 20 | return 0; 21 | } 22 | 23 | char * s_gets(char * st, int n) 24 | { 25 | char * ret_val; 26 | int i = 0; 27 | 28 | ret_val = fgets(st, n, stdin); 29 | if (ret_val) 30 | { 31 | while (st[i] != '\n' && st[i] != '\0') 32 | i++; 33 | if (st[i] == '\n') 34 | st[i] = '\0'; 35 | else // must have words[i] == '\0' 36 | while (getchar() != '\n') 37 | continue; 38 | } 39 | return ret_val; 40 | } 41 | -------------------------------------------------------------------------------- /assets/codes/Ch11/nono.c: -------------------------------------------------------------------------------- 1 | /* nono.c -- no! */ 2 | #include 3 | int main(void) 4 | { 5 | char side_a[] = "Side A"; 6 | char dont[] = {'W', 'O', 'W', '!' }; 7 | char side_b[] = "Side B"; 8 | 9 | puts(dont); /* dont is not a string */ 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /assets/codes/Ch11/p_and_s.c: -------------------------------------------------------------------------------- 1 | /* p_and_s.c -- pointers and strings */ 2 | #include 3 | int main(void) 4 | { 5 | const char * mesg = "Don't be a fool!"; 6 | const char * copy; 7 | 8 | copy = mesg; 9 | printf("%s\n", copy); 10 | printf("mesg = %s; &mesg = %p; value = %p\n", 11 | mesg, &mesg, mesg); 12 | printf("copy = %s; © = %p; value = %p\n", 13 | copy, ©, copy); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /assets/codes/Ch11/put_out.c: -------------------------------------------------------------------------------- 1 | /* put_out.c -- using puts() */ 2 | #include 3 | #define DEF "I am a #defined string." 4 | int main(void) 5 | { 6 | char str1[80] = "An array was initialized to me."; 7 | const char * str2 = "A pointer was initialized to me."; 8 | 9 | puts("I'm an argument to puts()."); 10 | puts(DEF); 11 | puts(str1); 12 | puts(str2); 13 | puts(&str1[5]); 14 | puts(str2+4); 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /assets/codes/Ch11/put_put.c: -------------------------------------------------------------------------------- 1 | //put_put.c -- user-defined output functions 2 | #include 3 | void put1(const char *); 4 | int put2(const char *); 5 | 6 | int main(void) 7 | { 8 | put1("If I'd as much money"); 9 | put1(" as I could spend,\n"); 10 | printf("I count %d characters.\n", 11 | put2("I never would cry old chairs to mend.")); 12 | 13 | return 0; 14 | } 15 | 16 | void put1(const char * string) 17 | { 18 | while (*string) /* same as *string != '\0' */ 19 | putchar(*string++); 20 | } 21 | 22 | int put2(const char * string) 23 | { 24 | int count = 0; 25 | while (*string) 26 | { 27 | putchar(*string++); 28 | count++; 29 | } 30 | putchar('\n'); 31 | 32 | return(count); 33 | } 34 | -------------------------------------------------------------------------------- /assets/codes/Ch11/repeat.c: -------------------------------------------------------------------------------- 1 | /* repeat.c -- main() with arguments */ 2 | #include 3 | int main(int argc, char *argv[]) 4 | { 5 | int count; 6 | 7 | printf("The command line has %d arguments:\n", argc - 1); 8 | for (count = 1; count < argc; count++) 9 | printf("%d: %s\n", count, argv[count]); 10 | printf("\n"); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /assets/codes/Ch11/scan_str.c: -------------------------------------------------------------------------------- 1 | /* scan_str.c -- using scanf() */ 2 | #include 3 | int main(void) 4 | { 5 | char name1[11], name2[11]; 6 | int count; 7 | 8 | printf("Please enter 2 names.\n"); 9 | count = scanf("%5s %10s",name1, name2); 10 | printf("I read the %d names %s and %s.\n", 11 | count, name1, name2); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /assets/codes/Ch11/starsrch.c: -------------------------------------------------------------------------------- 1 | /* starsrch.c -- use strncmp() */ 2 | #include 3 | #include 4 | #define LISTSIZE 6 5 | int main() 6 | { 7 | const char * list[LISTSIZE] = 8 | { 9 | "astronomy", "astounding", 10 | "astrophysics", "ostracize", 11 | "asterism", "astrophobia" 12 | }; 13 | int count = 0; 14 | int i; 15 | 16 | for (i = 0; i < LISTSIZE; i++) 17 | if (strncmp(list[i],"astro", 5) == 0) 18 | { 19 | printf("Found: %s\n", list[i]); 20 | count++; 21 | } 22 | printf("The list contained %d words beginning" 23 | " with astro.\n", count); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /assets/codes/Ch11/strings1.c: -------------------------------------------------------------------------------- 1 | 2 | // strings1.c 3 | #include 4 | #define MSG "I am a symbolic string constant." 5 | #define MAXLENGTH 81 6 | int main(void) 7 | { 8 | char words[MAXLENGTH] = "I am a string in an array."; 9 | const char * pt1 = "Something is pointing at me."; 10 | puts("Here are some strings:"); 11 | puts(MSG); 12 | puts(words); 13 | puts(pt1); 14 | words[8] = 'p'; 15 | puts(words); 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /assets/codes/Ch11/strptr.c: -------------------------------------------------------------------------------- 1 | /* strptr.c -- strings as pointers */ 2 | #include 3 | int main(void) 4 | { 5 | printf("%s, %p, %c\n", "We", "are", *"space farers"); 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /assets/codes/Ch11/test_fit.c: -------------------------------------------------------------------------------- 1 | /* test_fit.c -- try the string-shrinking function */ 2 | #include 3 | #include /* contains string function prototypes */ 4 | void fit(char *, unsigned int); 5 | 6 | int main(void) 7 | { 8 | char mesg[] = "Things should be as simple as possible," 9 | " but not simpler."; 10 | 11 | puts(mesg); 12 | fit(mesg,38); 13 | puts(mesg); 14 | puts("Let's look at some more of the string."); 15 | puts(mesg + 39); 16 | 17 | return 0; 18 | } 19 | 20 | void fit(char *string, unsigned int size) 21 | { 22 | if (strlen(string) > size) 23 | string[size] = '\0'; 24 | } 25 | -------------------------------------------------------------------------------- /assets/codes/Ch12/diceroll.c: -------------------------------------------------------------------------------- 1 | /* diceroll.c -- dice role simulation */ 2 | /* compile with mandydice.c */ 3 | #include "diceroll.h" 4 | #include 5 | #include /* for library rand() */ 6 | 7 | int roll_count = 0; /* external linkage */ 8 | 9 | static int rollem(int sides) /* private to this file */ 10 | { 11 | int roll; 12 | 13 | roll = rand() % sides + 1; 14 | ++roll_count; /* count function calls */ 15 | 16 | return roll; 17 | } 18 | 19 | int roll_n_dice(int dice, int sides) 20 | { 21 | int d; 22 | int total = 0; 23 | if (sides < 2) 24 | { 25 | printf("Need at least 2 sides.\n"); 26 | return -2; 27 | } 28 | if (dice < 1) 29 | { 30 | printf("Need at least 1 die.\n"); 31 | return -1; 32 | } 33 | 34 | for (d = 0; d < dice; d++) 35 | total += rollem(sides); 36 | 37 | return total; 38 | } 39 | -------------------------------------------------------------------------------- /assets/codes/Ch12/diceroll.h: -------------------------------------------------------------------------------- 1 | //diceroll.h 2 | extern int roll_count; 3 | 4 | int roll_n_dice(int dice, int sides); 5 | -------------------------------------------------------------------------------- /assets/codes/Ch12/forc99.c: -------------------------------------------------------------------------------- 1 | // forc99.c -- new C99 block rules 2 | #include 3 | int main() 4 | { 5 | int n = 8; 6 | 7 | printf(" Initially, n = %d at %p\n", n, &n); 8 | for (int n = 1; n < 3; n++) 9 | printf(" loop 1: n = %d at %p\n", n, &n); 10 | printf("After loop 1, n = %d at %p\n", n, &n); 11 | for (int n = 1; n < 3; n++) 12 | { 13 | printf(" loop 2 index n = %d at %p\n", n, &n); 14 | int n = 6; 15 | printf(" loop 2: n = %d at %p\n", n, &n); 16 | n++; 17 | } 18 | printf("After loop 2, n = %d at %p\n", n, &n); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /assets/codes/Ch12/global.c: -------------------------------------------------------------------------------- 1 | /* global.c -- uses an external variable */ 2 | #include 3 | int units = 0; /* an external variable */ 4 | void critic(void); 5 | int main(void) 6 | { 7 | extern int units; /* an optional redeclaration */ 8 | 9 | printf("How many pounds to a firkin of butter?\n"); 10 | scanf("%d", &units); 11 | while ( units != 56) 12 | critic(); 13 | printf("You must have looked it up!\n"); 14 | 15 | return 0; 16 | } 17 | 18 | void critic(void) 19 | { 20 | /* optional redeclaration omitted */ 21 | printf("No luck, my friend. Try again.\n"); 22 | scanf("%d", &units); 23 | } 24 | -------------------------------------------------------------------------------- /assets/codes/Ch12/hiding.c: -------------------------------------------------------------------------------- 1 | // hiding.c -- variables in blocks 2 | #include 3 | int main() 4 | { 5 | int x = 30; // original x 6 | 7 | printf("x in outer block: %d at %p\n", x, &x); 8 | { 9 | int x = 77; // new x, hides first x 10 | printf("x in inner block: %d at %p\n", x, &x); 11 | } 12 | printf("x in outer block: %d at %p\n", x, &x); 13 | while (x++ < 33) // original x 14 | { 15 | int x = 100; // new x, hides first x 16 | x++; 17 | printf("x in while loop: %d at %p\n", x, &x); 18 | } 19 | printf("x in outer block: %d at %p\n", x, &x); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /assets/codes/Ch12/loc_stat.c: -------------------------------------------------------------------------------- 1 | /* loc_stat.c -- using a local static variable */ 2 | #include 3 | void trystat(void); 4 | 5 | int main(void) 6 | { 7 | int count; 8 | 9 | for (count = 1; count <= 3; count++) 10 | { 11 | printf("Here comes iteration %d:\n", count); 12 | trystat(); 13 | } 14 | 15 | return 0; 16 | } 17 | 18 | void trystat(void) 19 | { 20 | int fade = 1; 21 | static int stay = 1; 22 | 23 | printf("fade = %d and stay = %d\n", fade++, stay++); 24 | } 25 | -------------------------------------------------------------------------------- /assets/codes/Ch12/parta.c: -------------------------------------------------------------------------------- 1 | // parta.c --- various storage classes 2 | // compile with partb.c 3 | #include 4 | void report_count(); 5 | void accumulate(int k); 6 | int count = 0; // file scope, external linkage 7 | 8 | int main(void) 9 | { 10 | int value; // automatic variable 11 | register int i; // register variable 12 | 13 | printf("Enter a positive integer (0 to quit): "); 14 | while (scanf("%d", &value) == 1 && value > 0) 15 | { 16 | ++count; // use file scope variable 17 | for (i = value; i >= 0; i--) 18 | accumulate(i); 19 | printf("Enter a positive integer (0 to quit): "); 20 | } 21 | report_count(); 22 | 23 | return 0; 24 | } 25 | 26 | void report_count() 27 | { 28 | printf("Loop executed %d times\n", count); 29 | } 30 | -------------------------------------------------------------------------------- /assets/codes/Ch12/partb.c: -------------------------------------------------------------------------------- 1 | // partb.c -- rest of the program 2 | // compile with parta.c 3 | #include 4 | 5 | extern int count; // reference declaration, external linkage 6 | 7 | static int total = 0; // static definition, internal linkage 8 | void accumulate(int k); // prototype 9 | 10 | void accumulate(int k) // k has block scope, no linkage 11 | { 12 | static int subtotal = 0; // static, no linkage 13 | 14 | if (k <= 0) 15 | { 16 | printf("loop cycle: %d\n", count); 17 | printf("subtotal: %d; total: %d\n", subtotal, total); 18 | subtotal = 0; 19 | } 20 | else 21 | { 22 | subtotal += k; 23 | total += k; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /assets/codes/Ch12/r_drive0.c: -------------------------------------------------------------------------------- 1 | /* r_drive0.c -- test the rand0() function */ 2 | /* compile with rand0.c */ 3 | #include 4 | extern unsigned int rand0(void); 5 | 6 | int main(void) 7 | { 8 | int count; 9 | 10 | for (count = 0; count < 5; count++) 11 | printf("%d\n", rand0()); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /assets/codes/Ch12/r_drive1.c: -------------------------------------------------------------------------------- 1 | /* r_drive1.c -- test rand1() and srand1() */ 2 | /* compile with s_and_r.c */ 3 | #include 4 | #include 5 | extern void srand1(unsigned int x); 6 | extern int rand1(void); 7 | 8 | int main(void) 9 | { 10 | int count; 11 | unsigned seed; 12 | 13 | printf("Please enter your choice for seed.\n"); 14 | while (scanf("%u", &seed) == 1) 15 | { 16 | srand1(seed); /* reset seed */ 17 | for (count = 0; count < 5; count++) 18 | printf("%d\n", rand1()); 19 | printf("Please enter next seed (q to quit):\n"); 20 | } 21 | printf("Done\n"); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /assets/codes/Ch12/rand0.c: -------------------------------------------------------------------------------- 1 | /* rand0.c –– produces random numbers */ 2 | /* uses ANSI C portable algorithm */ 3 | static unsigned long int next = 1; /* the seed */ 4 | 5 | unsigned int rand0(void) 6 | { 7 | /* magic formula to generate pseudorandom number */ 8 | next = next * 1103515245 + 12345; 9 | return (unsigned int) (next/65536) % 32768; 10 | } 11 | -------------------------------------------------------------------------------- /assets/codes/Ch12/s_and_r.c: -------------------------------------------------------------------------------- 1 | /* s_and_r.c -- file for rand1() and srand1() */ 2 | /* uses ANSI C portable algorithm */ 3 | static unsigned long int next = 1; /* the seed */ 4 | 5 | int rand1(void) 6 | { 7 | /* magic formula to generate pseudorandom number */ 8 | next = next * 1103515245 + 12345; 9 | return (unsigned int) (next/65536) % 32768; 10 | } 11 | 12 | void srand1(unsigned int seed) 13 | { 14 | next = seed; 15 | } 16 | -------------------------------------------------------------------------------- /assets/codes/Ch12/where.c: -------------------------------------------------------------------------------- 1 | // where.c -- where's the memory? 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | int static_store = 30; 8 | const char * pcg = "String Literal"; 9 | int main() 10 | { 11 | int auto_store = 40; 12 | char auto_string[] = "Auto char Array"; 13 | int * pi; 14 | char * pcl; 15 | 16 | pi = (int *) malloc(sizeof(int)); 17 | *pi = 35; 18 | pcl = (char *) malloc(strlen("Dynamic String") + 1); 19 | strcpy(pcl, "Dynamic String"); 20 | 21 | printf("static_store: %d at %p\n", static_store, &static_store); 22 | printf(" auto_store: %d at %p\n", auto_store, &auto_store); 23 | printf(" *pi: %d at %p\n", *pi, pi); 24 | printf(" %s at %p\n", pcg, pcg); 25 | printf(" %s at %p\n", auto_string, auto_string); 26 | printf(" %s at %p\n", pcl, pcl); 27 | printf(" %s at %p\n", "Quoted String", "Quoted String"); 28 | free(pi); 29 | free(pcl); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /assets/codes/Ch13/addaword.c: -------------------------------------------------------------------------------- 1 | /* addaword.c -- uses fprintf(), fscanf(), and rewind() */ 2 | #include 3 | #include 4 | #include 5 | #define MAX 41 6 | 7 | int main(void) 8 | { 9 | FILE *fp; 10 | char words[MAX]; 11 | 12 | if ((fp = fopen("wordy", "a+")) == NULL) 13 | { 14 | fprintf(stdout,"Can't open \"wordy\" file.\n"); 15 | exit(EXIT_FAILURE); 16 | } 17 | 18 | puts("Enter words to add to the file; press the #"); 19 | puts("key at the beginning of a line to terminate."); 20 | while ((fscanf(stdin,"%40s", words) == 1) && (words[0] != '#')) 21 | fprintf(fp, "%s\n", words); 22 | 23 | puts("File contents:"); 24 | rewind(fp); /* go back to beginning of file */ 25 | while (fscanf(fp,"%s",words) == 1) 26 | puts(words); 27 | puts("Done!"); 28 | if (fclose(fp) != 0) 29 | fprintf(stderr,"Error closing file\n"); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /assets/codes/Ch13/count.c: -------------------------------------------------------------------------------- 1 | /* count.c -- using standard I/O */ 2 | #include 3 | #include // exit() prototype 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | int ch; // place to store each character as read 8 | FILE *fp; // "file pointer" 9 | unsigned long count = 0; 10 | if (argc != 2) 11 | { 12 | printf("Usage: %s filename\n", argv[0]); 13 | exit(EXIT_FAILURE); 14 | } 15 | if ((fp = fopen(argv[1], "r")) == NULL) 16 | { 17 | printf("Can't open %s\n", argv[1]); 18 | exit(EXIT_FAILURE); 19 | } 20 | while ((ch = getc(fp)) != EOF) 21 | { 22 | putc(ch,stdout); // same as putchar(ch); 23 | count++; 24 | } 25 | fclose(fp); 26 | printf("File %s has %lu characters\n", argv[1], count); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /assets/codes/Ch13/eddy: -------------------------------------------------------------------------------- 1 | So even Eddy came oven ready. 2 | 3 | -------------------------------------------------------------------------------- /assets/codes/Ch13/reverse.c: -------------------------------------------------------------------------------- 1 | /* reverse.c -- displays a file in reverse order */ 2 | #include 3 | #include 4 | #define CNTL_Z '\032' /* eof marker in DOS text files */ 5 | #define SLEN 81 6 | int main(void) 7 | { 8 | char file[SLEN]; 9 | char ch; 10 | FILE *fp; 11 | long count, last; 12 | 13 | puts("Enter the name of the file to be processed:"); 14 | scanf("%80s", file); 15 | if ((fp = fopen(file,"rb")) == NULL) 16 | { /* read-only mode */ 17 | printf("reverse can't open %s\n", file); 18 | exit(EXIT_FAILURE); 19 | } 20 | 21 | fseek(fp, 0L, SEEK_END); /* go to end of file */ 22 | last = ftell(fp); 23 | for (count = 1L; count <= last; count++) 24 | { 25 | fseek(fp, -count, SEEK_END); /* go backward */ 26 | ch = getc(fp); 27 | if (ch != CNTL_Z && ch != '\r') /* MS-DOS files */ 28 | putchar(ch); 29 | } 30 | putchar('\n'); 31 | fclose(fp); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /assets/codes/Ch13/wordy: -------------------------------------------------------------------------------- 1 | The 2 | fabulous 3 | programmer 4 | enchanted 5 | the 6 | large 7 | -------------------------------------------------------------------------------- /assets/codes/Ch14/complit.c: -------------------------------------------------------------------------------- 1 | /* complit.c -- compound literals */ 2 | #include 3 | #define MAXTITL 41 4 | #define MAXAUTL 31 5 | 6 | struct book { // structure template: tag is book 7 | char title[MAXTITL]; 8 | char author[MAXAUTL]; 9 | float value; 10 | }; 11 | 12 | int main(void) 13 | { 14 | struct book readfirst; 15 | int score; 16 | 17 | printf("Enter test score: "); 18 | scanf("%d",&score); 19 | 20 | if(score >= 84) 21 | readfirst = (struct book) {"Crime and Punishment", 22 | "Fyodor Dostoyevsky", 23 | 11.25}; 24 | else 25 | readfirst = (struct book) {"Mr. Bouncy's Nice Hat", 26 | "Fred Winsome", 27 | 5.99}; 28 | printf("Your assigned reading:\n"); 29 | printf("%s by %s: $%.2f\n",readfirst.title, 30 | readfirst.author, readfirst.value); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /assets/codes/Ch14/funds1.c: -------------------------------------------------------------------------------- 1 | /* funds1.c -- passing structure members as arguments */ 2 | #include 3 | #define FUNDLEN 50 4 | 5 | struct funds { 6 | char bank[FUNDLEN]; 7 | double bankfund; 8 | char save[FUNDLEN]; 9 | double savefund; 10 | }; 11 | 12 | double sum(double, double); 13 | 14 | int main(void) 15 | { 16 | struct funds stan = { 17 | "Garlic-Melon Bank", 18 | 4032.27, 19 | "Lucky's Savings and Loan", 20 | 8543.94 21 | }; 22 | 23 | printf("Stan has a total of $%.2f.\n", 24 | sum(stan.bankfund, stan.savefund) ); 25 | return 0; 26 | } 27 | 28 | /* adds two double numbers */ 29 | double sum(double x, double y) 30 | { 31 | return(x + y); 32 | } 33 | -------------------------------------------------------------------------------- /assets/codes/Ch14/funds2.c: -------------------------------------------------------------------------------- 1 | /* funds2.c -- passing a pointer to a structure */ 2 | #include 3 | #define FUNDLEN 50 4 | 5 | struct funds { 6 | char bank[FUNDLEN]; 7 | double bankfund; 8 | char save[FUNDLEN]; 9 | double savefund; 10 | }; 11 | 12 | double sum(const struct funds *); /* argument is a pointer */ 13 | 14 | int main(void) 15 | { 16 | struct funds stan = { 17 | "Garlic-Melon Bank", 18 | 4032.27, 19 | "Lucky's Savings and Loan", 20 | 8543.94 21 | }; 22 | 23 | printf("Stan has a total of $%.2f.\n", sum(&stan)); 24 | 25 | return 0; 26 | } 27 | 28 | double sum(const struct funds * money) 29 | { 30 | return(money->bankfund + money->savefund); 31 | } 32 | -------------------------------------------------------------------------------- /assets/codes/Ch14/funds3.c: -------------------------------------------------------------------------------- 1 | /* funds3.c -- passing a structure */ 2 | #include 3 | #define FUNDLEN 50 4 | 5 | struct funds { 6 | char bank[FUNDLEN]; 7 | double bankfund; 8 | char save[FUNDLEN]; 9 | double savefund; 10 | }; 11 | 12 | double sum(struct funds moolah); /* argument is a structure */ 13 | 14 | int main(void) 15 | { 16 | struct funds stan = { 17 | "Garlic-Melon Bank", 18 | 4032.27, 19 | "Lucky's Savings and Loan", 20 | 8543.94 21 | }; 22 | 23 | printf("Stan has a total of $%.2f.\n", sum(stan)); 24 | 25 | return 0; 26 | } 27 | 28 | double sum(struct funds moolah) 29 | { 30 | return(moolah.bankfund + moolah.savefund); 31 | } 32 | -------------------------------------------------------------------------------- /assets/codes/Ch15/align.c: -------------------------------------------------------------------------------- 1 | // align.c -- using _Alignof and _Alignas (C11) 2 | 3 | #include 4 | int main(void) 5 | { 6 | double dx; 7 | char ca; 8 | char cx; 9 | double dz; 10 | char cb; 11 | char _Alignas(double) cz; 12 | 13 | printf("char alignment: %zd\n", _Alignof(char)); 14 | printf("double alignment: %zd\n", _Alignof(double)); 15 | printf("&dx: %p\n", &dx); 16 | printf("&ca: %p\n", &ca); 17 | printf("&cx: %p\n", &cx); 18 | printf("&dz: %p\n", &dz); 19 | printf("&cb: %p\n", &cb); 20 | printf("&cz: %p\n", &cz); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /assets/codes/Ch16/assert.c: -------------------------------------------------------------------------------- 1 | /* assert.c -- use assert() */ 2 | #include 3 | #include 4 | #include 5 | int main() 6 | { 7 | double x, y, z; 8 | 9 | puts("Enter a pair of numbers (0 0 to quit): "); 10 | while (scanf("%lf%lf", &x, &y) == 2 11 | && (x != 0 || y != 0)) 12 | { 13 | z = x * x - y * y; /* should be + */ 14 | assert(z >= 0); 15 | printf("answer is %f\n", sqrt(z)); 16 | puts("Next pair of numbers: "); 17 | } 18 | puts("Done"); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /assets/codes/Ch16/byebye.c: -------------------------------------------------------------------------------- 1 | /* byebye.c -- atexit() example */ 2 | #include 3 | #include 4 | void sign_off(void); 5 | void too_bad(void); 6 | 7 | int main(void) 8 | { 9 | int n; 10 | 11 | atexit(sign_off); /* register the sign_off() function */ 12 | puts("Enter an integer:"); 13 | if (scanf("%d",&n) != 1) 14 | { 15 | puts("That's no integer!"); 16 | atexit(too_bad); /* register the too_bad() function */ 17 | exit(EXIT_FAILURE); 18 | } 19 | printf("%d is %s.\n", n, (n % 2 == 0)? "even" : "odd"); 20 | 21 | return 0; 22 | } 23 | 24 | void sign_off(void) 25 | { 26 | puts("Thus terminates another magnificent program from"); 27 | puts("SeeSaw Software!"); 28 | } 29 | 30 | void too_bad(void) 31 | { 32 | puts("SeeSaw Software extends its heartfelt condolences"); 33 | puts("to you upon the failure of your program."); 34 | } 35 | -------------------------------------------------------------------------------- /assets/codes/Ch16/doubleincl.c: -------------------------------------------------------------------------------- 1 | // doubincl.c -- include header twice 2 | #include 3 | #include "names.h" 4 | #include "names.h" // accidental second inclusion 5 | 6 | int main() 7 | { 8 | names winner = {"Less", "Ismoor"}; 9 | printf("The winner is %s %s.\n", winner.first, 10 | winner.last); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /assets/codes/Ch16/eatline.h: -------------------------------------------------------------------------------- 1 | // eatline.h 2 | 3 | #ifndef EATLINE_H_ 4 | #define EATLINE_H_ 5 | inline static void eatline(void) 6 | { 7 | while (getchar() != '\n') 8 | continue; 9 | } 10 | 11 | #endif -------------------------------------------------------------------------------- /assets/codes/Ch16/el1.c: -------------------------------------------------------------------------------- 1 | // el1.c 2 | #include 3 | #include "eatline.h" 4 | void dmb(void); 5 | 6 | int main(void) 7 | { 8 | int n, m; 9 | 10 | scanf("%d", &n); 11 | eatline(); 12 | scanf("%d", &m); 13 | printf("%d %d\n", n, m); 14 | eatline(); 15 | dmb(); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /assets/codes/Ch16/el2.c: -------------------------------------------------------------------------------- 1 | 2 | // el2.c 3 | 4 | #include 5 | #include "eatline.h" 6 | 7 | void dmb(void) 8 | { 9 | double m, b; 10 | 11 | scanf("%lf", &m); 12 | eatline(); 13 | scanf("%lf", &b); 14 | printf("%f %f\n", m, b); 15 | return; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /assets/codes/Ch16/file1.c: -------------------------------------------------------------------------------- 1 | // 2 | // file1.c 3 | 4 | #include 5 | 6 | inline static double square(double x) { return x*x; } 7 | void spam(double); 8 | void masp(double); 9 | int main() 10 | { 11 | double q = square(1.3); 12 | 13 | printf("%.2f\n", q); 14 | spam(12.6); 15 | masp(1.6); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /assets/codes/Ch16/file2.c: -------------------------------------------------------------------------------- 1 | // 2 | // file2.c 3 | #include 4 | double square(double x) { return (int) (x*x); } 5 | void spam(double v) 6 | { 7 | double kv = square(v); 8 | printf("%.2f\n", kv); 9 | return; 10 | } 11 | -------------------------------------------------------------------------------- /assets/codes/Ch16/file3.c: -------------------------------------------------------------------------------- 1 | // 2 | // file3.c 3 | #include 4 | inline double square(double x) { return (int) (x * x + 0.5); } 5 | void masp(double w) 6 | { 7 | double kw = square(w); 8 | printf("%.2f\n", kw); 9 | return; 10 | } 11 | -------------------------------------------------------------------------------- /assets/codes/Ch16/glue.c: -------------------------------------------------------------------------------- 1 | // glue.c -- use the ## operator 2 | #include 3 | #define XNAME(n) x ## n 4 | #define PRINT_XN(n) printf("x" #n " = %d\n", x ## n); 5 | 6 | int main(void) 7 | { 8 | int XNAME(1) = 14; // becomes int x1 = 14; 9 | int XNAME(2) = 20; // becomes int x2 = 20; 10 | int x3 = 30; 11 | PRINT_XN(1); // becomes printf("x1 = %d\n", x1); 12 | PRINT_XN(2); // becomes printf("x2 = %d\n", x2); 13 | PRINT_XN(3); // becomes printf("x3 = %d\n", x3); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /assets/codes/Ch16/ifdef.c: -------------------------------------------------------------------------------- 1 | /* ifdef.c -- uses conditional compilation */ 2 | #include 3 | #define JUST_CHECKING 4 | #define LIMIT 4 5 | 6 | int main(void) 7 | { 8 | int i; 9 | int total = 0; 10 | 11 | for (i = 1; i <= LIMIT; i++) 12 | { 13 | total += 2*i*i + 1; 14 | #ifdef JUST_CHECKING 15 | printf("i=%d, running total = %d\n", i, total); 16 | #endif 17 | } 18 | printf("Grand total = %d\n", total); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /assets/codes/Ch16/mac_arg.c: -------------------------------------------------------------------------------- 1 | /* mac_arg.c -- macros with arguments */ 2 | #include 3 | #define SQUARE(X) X*X 4 | #define PR(X) printf("The result is %d.\n", X) 5 | int main(void) 6 | { 7 | int x = 5; 8 | int z; 9 | 10 | printf("x = %d\n", x); 11 | z = SQUARE(x); 12 | printf("Evaluating SQUARE(x): "); 13 | PR(z); 14 | z = SQUARE(2); 15 | printf("Evaluating SQUARE(2): "); 16 | PR(z); 17 | printf("Evaluating SQUARE(x+2): "); 18 | PR(SQUARE(x+2)); 19 | printf("Evaluating 100/SQUARE(2): "); 20 | PR(100/SQUARE(2)); 21 | printf("x is %d.\n", x); 22 | printf("Evaluating SQUARE(++x): "); 23 | PR(SQUARE(++x)); 24 | printf("After incrementing, x is %x.\n", x); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /assets/codes/Ch16/mytype.c: -------------------------------------------------------------------------------- 1 | // mytype.c 2 | 3 | #include 4 | 5 | #define MYTYPE(X) _Generic((X),\ 6 | int: "int",\ 7 | float : "float",\ 8 | double: "double",\ 9 | default: "other"\ 10 | ) 11 | 12 | int main(void) 13 | { 14 | int d = 5; 15 | 16 | printf("%s\n", MYTYPE(d)); // d is type int 17 | printf("%s\n", MYTYPE(2.0*d)); // 2.0* d is type double 18 | printf("%s\n", MYTYPE(3L)); // 3L is type long 19 | printf("%s\n", MYTYPE(&d)); // &d is type int * 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /assets/codes/Ch16/names.h: -------------------------------------------------------------------------------- 1 | // names.h --revised with include protection 2 | 3 | #ifndef NAMES_H_ 4 | #define NAMES_H_ 5 | 6 | // constants 7 | #define SLEN 32 8 | 9 | // structure declarations 10 | struct names_st 11 | { 12 | char first[SLEN]; 13 | char last[SLEN]; 14 | }; 15 | 16 | // typedefs 17 | typedef struct names_st names; 18 | 19 | // function prototypes 20 | void get_names(names *); 21 | void show_names(const names *); 22 | char * s_gets(char * st, int n); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /assets/codes/Ch16/names_st.c: -------------------------------------------------------------------------------- 1 | // names_st.c -- define names_st functions 2 | #include 3 | #include "names_st.h" // include the header file 4 | 5 | // function definitions 6 | void get_names(names * pn) 7 | { 8 | printf("Please enter your first name: "); 9 | s_gets(pn->first, SLEN); 10 | 11 | printf("Please enter your last name: "); 12 | s_gets(pn->last, SLEN); 13 | } 14 | 15 | void show_names(const names * pn) 16 | { 17 | printf("%s %s", pn->first, pn->last); 18 | } 19 | 20 | char * s_gets(char * st, int n) 21 | { 22 | char * ret_val; 23 | char * find; 24 | 25 | ret_val = fgets(st, n, stdin); 26 | if (ret_val) 27 | { 28 | find = strchr(st, '\n'); // look for newline 29 | if (find) // if the address is not NULL, 30 | *find = '\0'; // place a null character there 31 | else 32 | while (getchar() != '\n') 33 | continue; // dispose of rest of line 34 | } 35 | return ret_val; 36 | } 37 | -------------------------------------------------------------------------------- /assets/codes/Ch16/names_st.h: -------------------------------------------------------------------------------- 1 | // names_st.h -- names_st structure header file 2 | // constants 3 | #include 4 | #define SLEN 32 5 | 6 | // structure declarations 7 | struct names_st 8 | { 9 | char first[SLEN]; 10 | char last[SLEN]; 11 | }; 12 | 13 | // typedefs 14 | typedef struct names_st names; 15 | 16 | // function prototypes 17 | void get_names(names *); 18 | void show_names(const names *); 19 | char * s_gets(char * st, int n); 20 | 21 | -------------------------------------------------------------------------------- /assets/codes/Ch16/predef.c: -------------------------------------------------------------------------------- 1 | // predef.c -- predefined identifiers 2 | #include 3 | void why_me(); 4 | 5 | int main() 6 | { 7 | printf("The file is %s.\n", __FILE__); 8 | printf("The date is %s.\n", __DATE__); 9 | printf("The time is %s.\n", __TIME__); 10 | printf("The version is %ld.\n", __STDC_VERSION__); 11 | printf("This is line %d.\n", __LINE__); 12 | printf("This function is %s\n", __func__); 13 | why_me(); 14 | 15 | return 0; 16 | } 17 | 18 | void why_me() 19 | { 20 | printf("This function is %s\n", __func__); 21 | printf("This is line %d.\n", __LINE__); 22 | } 23 | -------------------------------------------------------------------------------- /assets/codes/Ch16/preproc.c: -------------------------------------------------------------------------------- 1 | /* preproc.c -- simple preprocessor examples */ 2 | #include 3 | #define TWO 2 /* you can use comments if you like */ 4 | #define OW "Consistency is the last refuge of the unimagina\ 5 | tive. - Oscar Wilde" /* a backslash continues a definition */ 6 | /* to the next line */ 7 | #define FOUR TWO*TWO 8 | #define PX printf("X is %d.\n", x) 9 | #define FMT "X is %d.\n" 10 | 11 | int main(void) 12 | { 13 | int x = TWO; 14 | 15 | PX; 16 | x = FOUR; 17 | printf(FMT, x); 18 | printf("%s\n", OW); 19 | printf("TWO: OW\n"); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /assets/codes/Ch16/statasrt.c: -------------------------------------------------------------------------------- 1 | // statasrt.c 2 | #include 3 | #include 4 | _Static_assert(CHAR_BIT == 16, "16-bit char falsely assumed"); 5 | int main(void) 6 | { 7 | puts("char is 16 bits."); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /assets/codes/Ch16/subst.c: -------------------------------------------------------------------------------- 1 | /* subst.c -- substitute in string */ 2 | #include 3 | #define PSQR(x) printf("The square of " #x " is %d.\n",((x)*(x))) 4 | 5 | int main(void) 6 | { 7 | int y = 5; 8 | 9 | PSQR(y); 10 | PSQR(2 + 4); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /assets/codes/Ch16/useheader.c: -------------------------------------------------------------------------------- 1 | // useheader.c -- use the names_st structure 2 | #include 3 | #include "names_st.h" 4 | // remember to link with names_st.c 5 | 6 | int main(void) 7 | { 8 | names candidate; 9 | 10 | get_names(&candidate); 11 | printf("Let's welcome "); 12 | show_names(&candidate); 13 | printf(" to this program!\n"); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /assets/codes/Ch16/varargs.c: -------------------------------------------------------------------------------- 1 | //varargs.c -- use variable number of arguments 2 | #include 3 | #include 4 | double sum(int, ...); 5 | 6 | int main(void) 7 | { 8 | double s,t; 9 | 10 | s = sum(3, 1.1, 2.5, 13.3); 11 | t = sum(6, 1.1, 2.1, 13.1, 4.1, 5.1, 6.1); 12 | printf("return value for " 13 | "sum(3, 1.1, 2.5, 13.3): %g\n", s); 14 | printf("return value for " 15 | "sum(6, 1.1, 2.1, 13.1, 4.1, 5.1, 6.1): %g\n", t); 16 | 17 | return 0; 18 | } 19 | 20 | double sum(int lim,...) 21 | { 22 | va_list ap; // declare object to hold arguments 23 | double tot = 0; 24 | int i; 25 | 26 | va_start(ap, lim); // initialize ap to argument list 27 | for (i = 0; i < lim; i++) 28 | tot += va_arg(ap, double); // access each item in argument list 29 | va_end(ap); // clean up 30 | 31 | return tot; 32 | } 33 | -------------------------------------------------------------------------------- /assets/codes/Ch16/variadic.c: -------------------------------------------------------------------------------- 1 | // variadic.c -- variadic macros 2 | #include 3 | #include 4 | #define PR(X, ...) printf("Message " #X ": " __VA_ARGS__) 5 | 6 | int main(void) 7 | { 8 | double x = 48; 9 | double y; 10 | 11 | y = sqrt(x); 12 | PR(1, "x = %g\n", x); 13 | PR(2, "x = %.2f, y = %.4f\n", x, y); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /assets/codes/XB/complex.c: -------------------------------------------------------------------------------- 1 | // complex.c 2 | #include 3 | #include 4 | void show_cmlx(complex double cv); 5 | int main(void) 6 | { 7 | complex double v1 = 4.0 + 3.0*I; 8 | double re, im; 9 | complex double v2; 10 | complex double sum, conj1; 11 | 12 | printf("Enter the real part of a complex number: "); 13 | scanf("%lf", &re); 14 | printf("Enter the imaginary part of a complex number: "); 15 | scanf("%lf", &im); 16 | v2 = CMPLX(re, im); 17 | // v2 = re + im * I; 18 | show_cmlx(v1); 19 | putchar('\n'); 20 | show_cmlx(v2); 21 | putchar('\n'); 22 | 23 | sum = CMPLX(creal(v1)+creal(v2), (cimag(v1) + cimag(v2))); 24 | // sum = creal(v1)+creal(v2) +(cimag(v1)+ cimag(v2)) * I; 25 | conj1 =conj(v1); 26 | show_cmlx(sum); 27 | putchar('\n'); 28 | show_cmlx(conj1); 29 | putchar('\n'); 30 | 31 | return 0; 32 | 33 | } 34 | 35 | void show_cmlx(complex double cv) 36 | { 37 | printf("(%f, %fi)", creal(cv), cimag(cv)); 38 | return; 39 | } 40 | -------------------------------------------------------------------------------- /assets/codes/XB/def.c: -------------------------------------------------------------------------------- 1 | // 2 | // def.c 3 | #include 4 | #include 5 | 6 | struct car 7 | { 8 | char brand[30]; 9 | char model[30]; 10 | double hp; 11 | double price; 12 | }; 13 | int main(void) 14 | { 15 | size_t into = offsetof(struct car, hp); /* offset of hp member */ 16 | printf("%zd\n", into); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /assets/codes/XB/offset.c: -------------------------------------------------------------------------------- 1 | // 2 | // offset.c 3 | #include 4 | #include 5 | 6 | struct car 7 | { 8 | char brand[30]; 9 | char model[30]; 10 | double hp; 11 | double price; 12 | }; 13 | int main(void) 14 | { 15 | size_t into = offsetof(struct car, hp); /* offset of hp member */ 16 | printf("%zd\n", into); 17 | } 18 | -------------------------------------------------------------------------------- /assets/codes/XB/strrtok.c: -------------------------------------------------------------------------------- 1 | // 2 | // strrtok.c 3 | 4 | #include 5 | #include 6 | 7 | int main(void) 8 | { 9 | char data[] = " C is\t too#much\nfun!"; 10 | const char tokseps[] = " \t\n#"; /* separators */ 11 | char * pt; 12 | 13 | puts(data); 14 | pt = strtok(data,tokseps); /* intial call */ 15 | while (pt) /* quit on NULL */ 16 | { 17 | puts (pt); /* show token */ 18 | pt = strtok(NULL, tokseps); /* next token */ 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /assets/codes/XB/wider.c: -------------------------------------------------------------------------------- 1 | // 2 | // wider.c 3 | #include 4 | #include 5 | int main(void) 6 | { 7 | wchar_t value = L'\u00f6'; 8 | wchar_t wch = L'I'; 9 | wchar_t w_arr[20] = L"am wide!"; 10 | printf("%lc %ls %lc\n", wch, w_arr, value); 11 | puts("Enter your grade:"); 12 | scanf("%lc", &wch); 13 | puts("Enter your first name:"); 14 | scanf("%ls",w_arr); 15 | printf("%lc %ls\n", wch, w_arr); 16 | wchar_t * pw = L"Points to a wide-character string"; 17 | int dozen = 12; 18 | wprintf(L"Item %d: %ls\n", dozen, pw); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /docs/.nojekyll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/.nojekyll -------------------------------------------------------------------------------- /docs/ch02/ex01.md: -------------------------------------------------------------------------------- 1 | # 习题2.1 2 | 3 |   编写一个程序,调用一次`printf()`函数,把你的名和姓打印在一行。再调用一次`printf()`函数,把你的名和姓分别打印在两行。然后,再调用两次`printf()`函数,把你的名和姓打印在一行。输出应如下所示(当然要把示例的内容换成你的名字): 4 | ``` 5 | Gustav Mahler <-第1次打印的内容 6 | Gustav <-第2次打印的内容 7 | Mahler <-仍是第2次打印的内容 8 | Gustav Mahler <-第3次和第4次打印的内容 9 | ``` 10 | 11 | 12 | 13 | **解答:** 14 | 代码位置:`exercises/ch02/ex01.c` 15 | ```c 16 | #include 17 | int main(void){ 18 | // 打印第1次 19 | printf("Gustav Mahler\n"); 20 | // 打印两行 21 | printf("Gustav\nMahler\n"); 22 | // 两次打印仅输出一行 23 | printf("Gustav "); 24 | printf("Mahler\n"); 25 | return 0; 26 | } 27 | ``` 28 | 29 | **执行结果:** 30 | 31 | ``` 32 | CPrimerPlus\cmake-build-debug-mingw\ex02-01.exe 33 | Gustav Mahler 34 | Gustav 35 | Mahler 36 | Gustav Mahler 37 | 38 | Process finished with exit code 0 39 | ``` 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /docs/ch02/ex03.md: -------------------------------------------------------------------------------- 1 | # 习题2.3 2 | 3 |   编写一个程序把你的年龄转换成天数,并显示这两个值。这里不用考虑闰年的问题。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch02/ex03.c` 7 | ```c 8 | #include 9 | 10 | int main(void){ 11 | int age_years, age_days; 12 | // 设置年龄 13 | age_years = 32; 14 | // 计算天数 15 | age_days = 365 * age_years; 16 | // 打印并显示年龄和天数 17 | printf("The age of %d years is %d days.\n", age_years, age_days); 18 | return 0; 19 | } 20 | ``` 21 | 22 | **执行结果:** 23 | 24 | ``` 25 | CPrimerPlus\cmake-build-debug-mingw\ex02-03.exe 26 | The age of 32 years is 11680 days. 27 | 28 | Process finished with exit code 0 29 | ``` 30 | 31 | -------------------------------------------------------------------------------- /docs/ch02/ex04.md: -------------------------------------------------------------------------------- 1 | # 习题2.4 2 | 3 |   编写一个程序,生成以下输出: 4 | ``` 5 | For he's a jolly good fellow! 6 | For he's a jolly good fellow! 7 | For he's a jolly good fellow! 8 | Whick nobody can deny! 9 | ``` 10 |   除了`main()`函数以外,该程序还要调用两个自定义函数:一个名为`jolly()`,用于打印前3条信息,调用一次打印一条;另一个函数名为`deny()`,打印最后一条信息。 11 | 12 | **解答:** 13 | 代码位置:`exercises/ch02/ex04.c` 14 | ```c 15 | #include 16 | 17 | void jolly(void); 18 | void deny(void); 19 | 20 | int main(void){ 21 | jolly(); 22 | jolly(); 23 | jolly(); 24 | deny(); 25 | return 0; 26 | } 27 | 28 | /** 29 | * 用于打印前3条信息 30 | */ 31 | void jolly(void){ 32 | printf("For he's a jolly good fellow!\n"); 33 | } 34 | 35 | /** 36 | * 用于打印最后一条信息 37 | */ 38 | void deny(void){ 39 | printf("Which nobody can deny!"); 40 | } 41 | ``` 42 | 43 | **执行结果:** 44 | ``` 45 | CPrimerPlus\cmake-build-debug-mingw\ex02-04.exe 46 | For he's a jolly good fellow! 47 | For he's a jolly good fellow! 48 | For he's a jolly good fellow! 49 | Which nobody can deny! 50 | Process finished with exit code 0 51 | ``` -------------------------------------------------------------------------------- /docs/ch02/ex05.md: -------------------------------------------------------------------------------- 1 | # 习题2.5 2 | 3 |   编写一个程序,生成以下输出: 4 | ``` 5 | Brazil, Russia, India, China 6 | India, China 7 | Brazil, Russia 8 | ``` 9 |   除了`main()`以外,该程序还要调用两个自定义函数:一个名为`br()`,调用一次打印一次“Brazil, Russia”,另一个名为`ic()`,调用一次打印一次“India, China”。其他内容在`main()`函数中完成。 10 | 11 | **解答:** 12 | 代码位置:`exercises/ch02/ex05.c` 13 | ```c 14 | #include 15 | 16 | void br(void); 17 | void ic(void); 18 | 19 | int main(void){ 20 | br(); 21 | printf(", "); 22 | ic(); 23 | printf("\n"); 24 | ic(); 25 | printf("\n"); 26 | br(); 27 | return 0; 28 | } 29 | 30 | /** 31 | * 用户打印"Brazil, Russia" 32 | */ 33 | void br(void){ 34 | printf("Brazil, Russia"); 35 | } 36 | 37 | /** 38 | * 用于打印“India, China” 39 | */ 40 | void ic(void){ 41 | printf("India, China"); 42 | } 43 | ``` 44 | 45 | **执行结果:** 46 | ``` 47 | CPrimerPlus\cmake-build-debug-mingw\ex02-05.exe 48 | Brazil, Russia, India, China 49 | India, China 50 | Brazil, Russia 51 | Process finished with exit code 0 52 | ``` 53 | 54 | -------------------------------------------------------------------------------- /docs/ch02/ex06.md: -------------------------------------------------------------------------------- 1 | # 习题2.6 2 | 3 |   编写一个程序,创建一个整型变量`toes`,并将`toes`设置为10。程序中还要计算`toes`的两倍和`toes`的平方。该程序应打印3个值,并分别描述以示区分。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch02/ex06.c` 7 | ```c 8 | #include 9 | int main(void){ 10 | // 定义整型变量toes 11 | int toes; 12 | // 将toes设置为10 13 | toes = 10; 14 | printf("toes = %d\n", toes); 15 | // 计算toes的两倍 16 | printf("Twice toes = %d\n", 2 * toes); 17 | // 计算toes的平方 18 | printf("toes squared = %d\n", toes * toes); 19 | return 0; 20 | } 21 | ``` 22 | 23 | **执行结果:** 24 | ``` 25 | CPrimerPlus\cmake-build-debug-mingw\ex02-06.exe 26 | toes = 10 27 | Twice toes = 20 28 | toes squared = 100 29 | 30 | Process finished with exit code 0 31 | ``` -------------------------------------------------------------------------------- /docs/ch02/ex07.md: -------------------------------------------------------------------------------- 1 | # 习题2.7 2 | 3 |   许多研究表明,微笑益处多多。编写一个程序,生成以下格式的输出: 4 | ``` 5 | Smile!Smile!Smile! 6 | Smile!Smile! 7 | Smile! 8 | ``` 9 |   该程序要定义一个函数,该函数被调用一次打印一次“Smile!”,根据程序的需要使用该函数。 10 | 11 | **解答:** 12 | 代码位置:`exercises/ch02/ex07.c` 13 | ```c 14 | #include 15 | 16 | void smile(void); 17 | 18 | int main(void){ 19 | // 打印三次Smile! 20 | smile(); 21 | smile(); 22 | smile(); 23 | printf("\n"); 24 | // 打印两次Smile! 25 | smile(); 26 | smile(); 27 | printf("\n"); 28 | // 打印一次Smile! 29 | smile(); 30 | printf("\n"); 31 | return 0; 32 | } 33 | 34 | /** 35 | * 用于打印Smile! 36 | */ 37 | void smile(void){ 38 | printf("Smile!"); 39 | } 40 | ``` 41 | 42 | **执行结果:** 43 | ``` 44 | CPrimerPlus\cmake-build-debug-mingw\ex02-07.exe 45 | Smile!Smile!Smile! 46 | Smile!Smile! 47 | Smile! 48 | 49 | Process finished with exit code 0 50 | ``` -------------------------------------------------------------------------------- /docs/ch02/ex08.md: -------------------------------------------------------------------------------- 1 | # 习题2.8 2 | 3 |   在C语言中,函数可以调用另一个函数。编写一个程序,调用一个名为`one_three()`的函数。该函数在一行打印单词“one”,再调用第2个函数`two()`,然后在另一行打印单词“three”。`two()`函数在一行显示单词“two”。`main()`函数在调用`one_three()`函数前要打印短语“starting now:”,并在调用完毕后显示短语“done!”。因此,该程序的输出应如下所示: 4 | ``` 5 | starting now: 6 | one 7 | two 8 | three 9 | done! 10 | ``` 11 | 12 | **解答:** 13 | 代码位置:`exercises/ch02/ex08.c` 14 | ```c 15 | #include 16 | 17 | void one_three(void); 18 | void two(void); 19 | 20 | int main(void){ 21 | printf("starting now;\n"); 22 | one_three(); 23 | printf("done!\n"); 24 | return 0; 25 | } 26 | 27 | void one_three(void){ 28 | // 打印one 29 | printf("one\n"); 30 | // 调用函数tow() 31 | two(); 32 | // 打印three 33 | printf("three\n"); 34 | } 35 | 36 | void two(void){ 37 | // 打印tow 38 | printf("two\n"); 39 | } 40 | ``` 41 | 42 | **执行结果:** 43 | ``` 44 | CPrimerPlus\cmake-build-debug-mingw\ex02-08.exe 45 | starting now; 46 | one 47 | two 48 | three 49 | done! 50 | 51 | Process finished with exit code 0 52 | ``` 53 | 54 | -------------------------------------------------------------------------------- /docs/ch03/ex02.md: -------------------------------------------------------------------------------- 1 | # 习题3.2 2 | 3 |   编写一个程序,要求提示输入一个ASCII码值(如,66),然后打印输入的字符。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch03/ex02.c` 7 | ```c 8 | #include 9 | int main(void){ 10 | int ascii; 11 | 12 | // 提示输入一个ASCII码值 13 | printf("Enter an ASCII code:"); 14 | scanf("%d", &ascii); 15 | // 打印输入的字符 16 | printf("%d is the ASCII code for %c.\n", ascii, ascii); 17 | return 0; 18 | } 19 | ``` 20 | 21 | **执行结果:** 22 | ``` 23 | CPrimerPlus\cmake-build-debug-mingw\ex03-02.exe 24 | Enter an ASCII code:66 25 | 66 is the ASCII code for B. 26 | 27 | Process finished with exit code 0 28 | ``` -------------------------------------------------------------------------------- /docs/ch03/ex03.md: -------------------------------------------------------------------------------- 1 | # 习题3.3 2 | 3 |   编写一个程序,发出一声警报,然后打印下面的文本: 4 | ``` 5 | Startled by the sudden sound, Sally shouted, 6 | "By the Great Pumpkin, what was that!" 7 | ``` 8 | 9 | **解答:** 10 | 代码位置:`exercises/ch03/ex03.c` 11 | ```c 12 | #include 13 | int main(void){ 14 | // 发出一声警报,并打印文本 15 | printf("\aStartled by the sudden sound, Sally shouted,\n"); 16 | printf("\"By the Great Pumpkin, what was that!\""); 17 | 18 | return 0; 19 | } 20 | ``` 21 | 22 | **执行结果:** 23 | ``` 24 | CPrimerPlus\cmake-build-debug-mingw\ex03-03.exe 25 | Startled by the sudden sound, Sally shouted, 26 | "By the Great Pumpkin, what was that!" 27 | Process finished with exit code 0 28 | ``` -------------------------------------------------------------------------------- /docs/ch03/ex04.md: -------------------------------------------------------------------------------- 1 | # 习题3.4 2 | 3 |   编写一个程序,读取一个浮点数,先打印成小数点形式,再打印成指数形式。然后,如果系统支持,再打印成p记数法(即十六进制记数法)。按以下格式输出(实际显示的指数位数因系统而异): 4 | ``` 5 | Enter a floating-point value: 64.25 6 | fixed-point notation: 64.250000 7 | exponential notation: 6.425000e+01 8 | p notation: 0x1.01p+6 9 | ``` 10 | 11 | **解答:** 12 | 代码位置:`exercises/ch03/ex04.c` 13 | ```c 14 | #include 15 | int main(void){ 16 | float num; 17 | // 输入一个浮点数 18 | printf("Enter a floating-point value:"); 19 | scanf("%f", &num); 20 | // 打印小数点形式 21 | printf("fixed-point notation: %f\n", num); 22 | // 打印指数形式 23 | printf("exponential notation:%e\n", num); 24 | // 打印p记数法形式 25 | printf("p notation:%a\n", num); 26 | return 0; 27 | } 28 | ``` 29 | 30 | **执行结果:** 31 | ``` 32 | CPrimerPlus\cmake-build-debug-mingw\ex03-04.exe 33 | Enter a floating-point value:64.25 34 | fixed-point notation: 64.250000 35 | exponential notation:6.425000e+001 36 | p notation:0x1.010000p+6 37 | 38 | Process finished with exit code 0 39 | ``` -------------------------------------------------------------------------------- /docs/ch03/ex05.md: -------------------------------------------------------------------------------- 1 | # 习题3.5 2 | 3 |   一年大约有$3.156 \times 10^7$秒。编写一个程序,提示用户输入年龄,然后显示该年龄对应的秒数。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch03/ex05.c` 7 | ```c 8 | #include 9 | int main(void){ 10 | int age_years; 11 | double age_seconds; 12 | // 提示输入年龄 13 | printf("Enter your age:"); 14 | scanf("%d", &age_years); 15 | // 计算该年龄对应的秒数 16 | age_seconds = 3.156e7 * age_years; 17 | printf("The age of %d years is %e seconds.\n", age_years, age_seconds); 18 | return 0; 19 | } 20 | ``` 21 | 22 | **执行结果:** 23 | ``` 24 | CPrimerPlus\cmake-build-debug-mingw\ex03-05.exe 25 | Enter your age:33 26 | The age of 33 years is 1.041480e+009 seconds. 27 | 28 | Process finished with exit code 0 29 | ``` -------------------------------------------------------------------------------- /docs/ch03/ex06.md: -------------------------------------------------------------------------------- 1 | # 习题3.6 2 | 3 |   1个水分子的质量约为$3.0 \times 10^{-23}$克。1夸脱水大约是950克。编写一个程序,提示用户输入水的夸脱数,并显示水分子的数量。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch03/ex06.c` 7 | ```c 8 | #include 9 | int main(void){ 10 | float mass_mol = 3.0e-23; 11 | float mass_qt = 950; 12 | float quarts, molecules; 13 | 14 | // 提示用户输入水的夸脱数 15 | printf("Enter the number of quarts of water:"); 16 | scanf("%f", &quarts); 17 | // 计算水分子的数量 18 | molecules = quarts * mass_qt / mass_mol; 19 | printf("%g quarts of water contain %e molecules.\n", quarts, molecules); 20 | return 0; 21 | } 22 | ``` 23 | 24 | **执行结果:** 25 | ``` 26 | CPrimerPlus\cmake-build-debug-mingw\ex03-06.exe 27 | Enter the number of quarts of water:2 28 | 2 quarts of water contain 6.333333e+025 molecules. 29 | 30 | Process finished with exit code 0 31 | ``` -------------------------------------------------------------------------------- /docs/ch03/ex07.md: -------------------------------------------------------------------------------- 1 | # 习题3.7 2 | 3 |   1英寸相当于2.54厘米。编写一个程序,提示用户输入身高(/英寸),然后以厘米为单位显示身高。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch03/ex07.c` 7 | ```c 8 | #include 9 | int main(void){ 10 | float inch = 2.54; 11 | float height; 12 | // 提示用户输入身高(单位:英寸) 13 | printf("Enter the num of height (in):"); 14 | scanf("%f", &height); 15 | // 计算并打印身高(单位:厘米) 16 | printf("%.2f(in) height is %.2f(cm).\n", height, height * inch); 17 | 18 | return 0; 19 | } 20 | ``` 21 | 22 | **执行结果:** 23 | ``` 24 | CPrimerPlus\cmake-build-debug-mingw\ex03-07.exe 25 | Enter the num of height (in):67 26 | 67.00(in) height is 170.18(cm). 27 | 28 | Process finished with exit code 0 29 | ``` 30 | 31 | -------------------------------------------------------------------------------- /docs/ch04/ex01.md: -------------------------------------------------------------------------------- 1 | # 习题4.1 2 | 3 |   编写一个程序,提示用于输入名和姓,然后以“名.姓”的格式打印出来。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch04/ex01.c` 7 | 8 | ```c 9 | #include 10 | int main(void){ 11 | char first_name[40]; 12 | char last_name[40]; 13 | 14 | // 提示用户输入名 15 | printf("Enter your first name:"); 16 | scanf("%s", first_name); 17 | // 提示用户输入姓 18 | printf("Enter your last name:"); 19 | scanf("%s", last_name); 20 | // 打印“名,姓” 21 | printf("%s, %s\n", first_name, last_name); 22 | return 0; 23 | } 24 | ``` 25 | 26 | **执行结果:** 27 | ``` 28 | CPrimerPlus\cmake-build-debug-mingw\ex04-01.exe 29 | Enter your first name:Relph 30 | Enter your last name:Hu 31 | Relph, Hu 32 | 33 | Process finished with exit code 0 34 | ``` -------------------------------------------------------------------------------- /docs/ch04/ex02.md: -------------------------------------------------------------------------------- 1 | # 习题4.2 2 | 3 |   编写一个程序,提示用户输入名字,并执行以下操作: 4 | a. 打印名字,包括双引号; 5 | b. 在宽度为20的字段右端打印名字,包括双引号; 6 | c. 在宽度为20的字段左端打印名字,包括双引号; 7 | d. 在比姓名宽度宽3的字段中打印名字。 8 | 9 | **解答:** 10 | 代码位置:`exercises/ch04/ex02.c` 11 | ```c 12 | #include 13 | #include 14 | 15 | int main(void) { 16 | char name[40]; 17 | // 提示用户输入名字 18 | printf("Enter your name:"); 19 | scanf("%s", name); 20 | // a. 打印名字,包括双引号 21 | printf("\"%s\"\n", name); 22 | // b. 在宽度为20的字段右端打印名字,包括双引号 23 | printf("\"%*s\"\n", 20, name); 24 | // c. 在宽度为20的字段左端打印名字,包括双引号 25 | printf("\"%-*s\"\n", 20, name); 26 | // d. 在比姓名宽度宽3的字段中打印名字 27 | printf("%*s\n", strlen(name) + 3, name); 28 | 29 | return 0; 30 | } 31 | ``` 32 | 33 | **执行结果:** 34 | ``` 35 | CPrimerPlus\cmake-build-debug-mingw\ex04-02.exe 36 | Enter your name:Relph 37 | "Relph" 38 | " Relph" 39 | "Relph " 40 | Relph 41 | 42 | Process finished with exit code 0 43 | ``` 44 | 45 | -------------------------------------------------------------------------------- /docs/ch04/ex03.md: -------------------------------------------------------------------------------- 1 | # 习题4.3 2 | 3 |   编写一个程序,读取一个浮点数,首先以小数点记数法打印,然后以指数记数法打印。用下面的格式进行输出(系统不同,指数记数法显示的位数可能不同): 4 | ``` 5 | a. The input is 21.3 or 2.1e+001. 6 | b. The input is +21.290 or 2.129E+001 7 | ``` 8 | 9 | **解答:** 10 | 代码位置:`exercises/ch04/ex03.c` 11 | ```c 12 | #include 13 | int main(void){ 14 | float val; 15 | // 读取一个浮点数 16 | printf("Enter a floating-point value:"); 17 | scanf("%f", &val); 18 | // 以小数点记数法打印 19 | printf("The input is %.1f or %.1e.\n", val, val); 20 | // 以指数记数法打印 21 | printf("The input is %+.3f or %.3E.\n", val, val); 22 | return 0; 23 | } 24 | ``` 25 | 26 | **执行结果:** 27 | ``` 28 | CPrimerPlus\cmake-build-debug-mingw\ex04-03.exe 29 | Enter a floating-point value:21.29 30 | The input is 21.3 or 2.1e+001. 31 | The input is +21.290 or 2.129E+001. 32 | 33 | Process finished with exit code 0 34 | ``` -------------------------------------------------------------------------------- /docs/ch04/ex04.md: -------------------------------------------------------------------------------- 1 | # 习题4.4 2 | 3 |   编写一个程序,提示用户输入身高(单位:英寸)和姓名,然后以下面的格式显示用户刚输入的信息: 4 | ``` 5 | Dabney, you are 6.208 feet tall 6 | ``` 7 | 使用float类型,并用`/`作为除号。如果你愿意,可以要求用户以厘米为单位输入身高,并以米为单位显示出来。 8 | 9 | **解答:** 10 | 代码位置:`exercises/ch04/ex04.c` 11 | ```c 12 | #include 13 | 14 | int main(void) { 15 | float height; 16 | char name[40]; 17 | // 提示用户输入身高 18 | printf("Enter your height(in):"); 19 | scanf("%f", &height); 20 | // 提示用户输入姓名 21 | printf("Enter your name:"); 22 | scanf("%s", name); 23 | // 显示用户输入的信息 24 | printf("%s, you are %.3f feet tall.\n", name, height / 12.0); 25 | 26 | return 0; 27 | } 28 | ``` 29 | 30 | **执行结果:** 31 | ``` 32 | CPrimerPlus\cmake-build-debug-mingw\ex04-04.exe 33 | Enter your height(in):74.5 34 | Enter your name:Dabney 35 | Dabney, you are 6.208 feet tall. 36 | 37 | Process finished with exit code 0 38 | ``` -------------------------------------------------------------------------------- /docs/ch05/ex02.md: -------------------------------------------------------------------------------- 1 | # 习题5.2 2 | 3 |   编写一个程序,提示用户输入一个整数,然后打印从该数到比该数大10的所有整数(例如,用户输入5,则打印5~15的所有整数,包括5和15)。要求打印的各值之间用一个空格、制表符或换行符分开。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch05/ex02.c` 7 | ```c 8 | #include 9 | 10 | int main(void) { 11 | int num; 12 | 13 | // 提示用户输入一个整数 14 | printf("Enter a number:"); 15 | scanf("%d", &num); 16 | // 得到上限值 17 | int limits = num + 10; 18 | // 使用while循环,打印范围内的所有整数 19 | printf("The number from %d to %d:\n", num, limits); 20 | while (num <= limits) { 21 | printf("%d ", num); 22 | num++; 23 | } 24 | return 0; 25 | } 26 | ``` 27 | 28 | **执行结果:** 29 | 30 | ``` 31 | CPrimerPlus\cmake-build-debug-mingw\ex05-02.exe 32 | Enter a number:5 33 | The number from 5 to 15: 34 | 5 6 7 8 9 10 11 12 13 14 15 35 | Process finished with exit code 0 36 | ``` -------------------------------------------------------------------------------- /docs/ch05/ex05.md: -------------------------------------------------------------------------------- 1 | # 习题5.5 2 | 3 |   修改程序`addemup.c`(程序清单5.13),你可以认为`addemup.c`是计算20天里赚多少钱的程序(假设第1天赚\$1、第2天赚\$2、第3天赚\$3,以此类推)。修改程序,使其可以与用户交互,根据用户输入的数进行计算(即用读入的一个变量来代替20)。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch05/ex05.c` 7 | ```c 8 | #include 9 | 10 | int main(void) { 11 | int count, sum; 12 | int n; 13 | 14 | // 提示用户输入一个求和上限值 15 | printf("Enter the upper limit:"); 16 | scanf("%d", &n); 17 | 18 | count = 0; 19 | sum = 0; 20 | // 进行迭代求和 21 | while (count++ < n) 22 | sum += count; 23 | // 打印求和结果 24 | printf("sum = %d\n", sum); 25 | return 0; 26 | } 27 | ``` 28 | 29 | **执行结果:** 30 | ``` 31 | CPrimerPlus\cmake-build-debug-mingw\ex05-05.exe 32 | Enter the upper limit:20 33 | sum = 210 34 | 35 | Process finished with exit code 0 36 | ``` -------------------------------------------------------------------------------- /docs/ch05/ex06.md: -------------------------------------------------------------------------------- 1 | # 习题5.6 2 | 3 |   修改编程练习5(习题5.5)的程序,使其能计算整数的平方和(可以认为第1天赚\$1、第2天赚\$4、第3天赚\$9,以此类推,这看起来很不错)。C没有平方函数,但是可以用`n * n`来表示n的平方。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch05/ex06.c` 7 | ```c 8 | #include 9 | 10 | int main(void) { 11 | int count, sum; 12 | int n; 13 | 14 | // 提示用户输入一个上限值 15 | printf("Enter the upper limit:"); 16 | scanf("%d", &n); 17 | 18 | count = 0; 19 | sum = 0; 20 | // 进行迭代求和 21 | while (count++ < n) 22 | // 平方求和 23 | sum += count * count; 24 | // 打印求和结果 25 | printf("sum = %d\n", sum); 26 | return 0; 27 | } 28 | ``` 29 | 30 | **执行结果:** 31 | 32 | ``` 33 | CPrimerPlus\cmake-build-debug-mingw\ex05-06.exe 34 | Enter the upper limit:5 35 | sum = 55 36 | 37 | Process finished with exit code 0 38 | ``` -------------------------------------------------------------------------------- /docs/ch05/ex07.md: -------------------------------------------------------------------------------- 1 | # 习题5.7 2 | 3 |   编写一个程序,提示用户输入一个`double`类型的数,并打印该数的立方值。自己设计一个函数计算并打印立方值。`main()`函数要把用户输入的值传递给函数。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch05/ex07.c` 7 | 8 | ```c 9 | #include 10 | 11 | void print_cube(double x); 12 | 13 | int main(void) { 14 | double num; 15 | 16 | // 提示用户输入一个double类型的数 17 | printf("Enter a double value:"); 18 | scanf("%lf", &num); 19 | // 调用函数,打印该数的立方值 20 | print_cube(num); 21 | 22 | return 0; 23 | } 24 | 25 | void print_cube(double x) { 26 | // 计算并打印x的立方值 27 | printf("The cube of %g is %g.\n", x, x * x * x); 28 | } 29 | ``` 30 | 31 | **执行结果:** 32 | 33 | ``` 34 | CPrimerPlus\cmake-build-debug-mingw\ex05-07.exe 35 | Enter a double value:1.2 36 | The cube of 1.2 is 1.728. 37 | 38 | Process finished with exit code 0 39 | 40 | ``` -------------------------------------------------------------------------------- /docs/ch06/ex01.md: -------------------------------------------------------------------------------- 1 | # 习题6.1 2 | 3 |   编写一个程序,创建一个包含26个元素的数组,并在其中存储26个小写字母。然后打印数组的所有内容。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch06/ex01.c` 7 | ```c 8 | #include 9 | 10 | // 定义元素个数 11 | #define SIZE 26 12 | 13 | int main(void) { 14 | // 创建一个包含26个元素的数组 15 | int letters[SIZE]; 16 | int i; 17 | 18 | // 使用for循环存储26个小写字母 19 | for (i = 0; i < SIZE; i++) 20 | letters[i] = 'a' + i; 21 | 22 | // 使用for循环打印数组的所有内容 23 | for (i = 0; i < SIZE; i++) 24 | printf("%c", letters[i]); 25 | 26 | printf("\n"); 27 | return 0; 28 | } 29 | ``` 30 | 31 | **执行结果:** 32 | ``` 33 | CPrimerPlus\cmake-build-debug-mingw\ex06-01.exe 34 | abcdefghijklmnopqrstuvwxyz 35 | 36 | Process finished with exit code 0 37 | ``` -------------------------------------------------------------------------------- /docs/ch06/ex02.md: -------------------------------------------------------------------------------- 1 | # 习题6.2 2 | 3 |   使用嵌套循环,按下面的格式打印字符: 4 | ``` 5 | $ 6 | $$ 7 | $$$ 8 | $$$$ 9 | $$$$$ 10 | ``` 11 | 12 | **解答:** 13 | 代码位置:`exercises/ch06/ex02.c` 14 | 15 | ```c 16 | #include 17 | // 设置需要打印的数量 18 | #define SIZE 5 19 | 20 | int main(void) { 21 | // 循环打印每一行 22 | for (int i = 0; i < SIZE; i++) { 23 | // 循环打印该行的字符 24 | for (int j = 0; j <= i; j++) 25 | printf("$"); 26 | printf("\n"); 27 | } 28 | 29 | return 0; 30 | } 31 | ``` 32 | 33 | **执行结果:** 34 | ``` 35 | CPrimerPlus\cmake-build-debug-mingw\ex06-02.exe 36 | $ 37 | $$ 38 | $$$ 39 | $$$$ 40 | $$$$$ 41 | 42 | Process finished with exit code 0 43 | ``` -------------------------------------------------------------------------------- /docs/ch06/ex04.md: -------------------------------------------------------------------------------- 1 | # 习题6.4 2 | 3 |   使用嵌套循环,按下面的格式打印字母: 4 | ``` 5 | A 6 | BC 7 | DEF 8 | GHIJ 9 | KLMNO 10 | PQRSTU 11 | ``` 12 | 如果你的系统不使用以数字顺序编码的代码,请参照练习3的方案解决。 13 | 14 | **解答:** 15 | **解答思路:** 16 | 1. 第1层`for`循环控制打印行的数量,同时记录每行需要打印的第一个字母,设置变量为`temp` 17 | 2. 第2层`for`循环控制每行的打印,通过观察,可知道每一行打印的字母个数跟行号有关,可对变量`temp`进行自增打印 18 | 19 | 代码位置:`exercises/ch06/ex04.c` 20 | ```c 21 | #include 22 | 23 | #define SIZE 6 24 | 25 | int main(void) { 26 | 27 | int count; 28 | // 打印的字母初始化为A 29 | char letter = 'A'; 30 | for (count = 0; count < SIZE; count++) { 31 | // 每行需要打印的第一个字母 32 | letter += count; 33 | // 用于字母自增打印 34 | char temp = letter; 35 | for (int j = 0; j <= count; temp++, j++) { 36 | printf("%c", temp); 37 | } 38 | printf("\n"); 39 | } 40 | 41 | return 0; 42 | } 43 | ``` 44 | 45 | **执行结果:** 46 | ``` 47 | CPrimerPlus\cmake-build-debug-mingw\ex06-04.exe 48 | A 49 | BC 50 | DEF 51 | GHIJ 52 | KLMNO 53 | PQRSTU 54 | 55 | Process finished with exit code 0 56 | ``` -------------------------------------------------------------------------------- /docs/ch06/ex07.md: -------------------------------------------------------------------------------- 1 | # 习题6.7 2 | 3 |   编写一个程序把一个单词读入一个字符数组中,然后倒序打印这个单词。提示:`strlen()`函数(第4章介绍过)可用于计算数组最后一个字符的下标。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch06/ex07.c` 7 | ```c 8 | #include 9 | #include 10 | 11 | #define LEN 40 12 | 13 | int main(void) { 14 | char word[LEN]; 15 | int i, length; 16 | 17 | // 提示用户输入一个单词 18 | printf("Enter a word:"); 19 | scanf("%s", word); 20 | 21 | // 得到单词的长度 22 | length = strlen(word) - 1; 23 | printf("The reverse of %s is ", word); 24 | // 逆序打印这个单词 25 | for (i = length; i >= 0; i--) { 26 | printf("%c", word[i]); 27 | } 28 | 29 | return 0; 30 | } 31 | ``` 32 | 33 | **执行结果:** 34 | ``` 35 | CPrimerPlus\cmake-build-debug-mingw\ex06-07.exe 36 | Enter a word:static 37 | The reverse of static is citats 38 | Process finished with exit code 0 39 | ``` -------------------------------------------------------------------------------- /docs/ch06/ex08.md: -------------------------------------------------------------------------------- 1 | # 习题6.8 2 | 3 |   编写一个程序,要求用户输入两个浮点数,并打印两数之差除以两数乘积的结果。在用户输入非数字之前,程序应循环处理用户输入的每对值。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch06/ex08.c` 7 | ```c 8 | #include 9 | 10 | int main(void) { 11 | double n, m; 12 | double res; 13 | 14 | // 提示用户输入两个浮点数 15 | printf("Enter a pair of floating-point numbers:"); 16 | 17 | while (scanf("%lf %lf", &n, &m) == 2) { 18 | // 计算两数之差除以两数乘积的结果 19 | res = (n - m) / (n * m); 20 | // 打印结果 21 | printf("(%.3g - %.3g) / (%.3g * %.3g) = %.5g\n", n, m, n, m, res); 22 | // 提示用户再次输入两个浮点数 23 | printf("Enter next pair (non-numeric to quit):"); 24 | } 25 | 26 | return 0; 27 | } 28 | ``` 29 | 30 | **执行结果:** 31 | ``` 32 | CPrimerPlus\cmake-build-debug-mingw\ex06-08.exe 33 | Enter a pair of floating-point numbers:3.4 5.6 34 | (3.4 - 5.6) / (3.4 * 5.6) = -0.11555 35 | Enter next pair (non-numeric to quit):4.3 6.5 36 | (4.3 - 6.5) / (4.3 * 6.5) = -0.078712 37 | Enter next pair (non-numeric to quit):q 38 | 39 | Process finished with exit code 0 40 | ``` -------------------------------------------------------------------------------- /docs/ch06/ex11.md: -------------------------------------------------------------------------------- 1 | # 习题6.11 2 | 3 |   编写一个程序,在数组中读入8个整数,然后按倒序打印这8个整数。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch06/ex11.c` 7 | ```c 8 | #include 9 | 10 | #define SIZE 8 11 | 12 | int main(void) { 13 | // 定义整数数组 14 | int input[SIZE]; 15 | int i; 16 | 17 | // 提示用户输入8个整数 18 | printf("Please enter %d integers.\n", SIZE); 19 | for (i = 0; i < SIZE; i++) { 20 | scanf("%d", &input[i]); 21 | } 22 | 23 | // 逆序打印8个整数 24 | printf("Here are the values in reverse order you entered:\n"); 25 | for (i = SIZE - 1; i >= 0; i--) { 26 | printf("%d ", input[i]); 27 | } 28 | printf("\n"); 29 | 30 | return 0; 31 | } 32 | ``` 33 | 34 | **执行结果:** 35 | ``` 36 | CPrimerPlus\cmake-build-debug-mingw\ex06-11.exe 37 | Please enter 8 integers. 38 | 1 2 3 4 5 6 7 8 39 | Here are the values in reverse order you entered: 40 | 8 7 6 5 4 3 2 1 41 | 42 | Process finished with exit code 0 43 | ``` -------------------------------------------------------------------------------- /docs/ch06/ex13.md: -------------------------------------------------------------------------------- 1 | # 习题6.13 2 | 3 |   编写一个程序,创建一个包含8个元素的`int`类型数组,分别把数组元素设置为2的前8次幂。使用`for`循环设置数组元素的值,使用`do while`循环显示数组元素的值。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch06/ex13.c` 7 | ```c 8 | #include 9 | 10 | #define SIZE 8 11 | 12 | int main(void) { 13 | // 定义一个包含8个元素的int类型数组 14 | int arr[SIZE]; 15 | int i; 16 | int value = 1; 17 | 18 | // 设置数组元素的值 19 | for (i = 0; i < SIZE; i++) { 20 | // 根据i,设置为2的i次幂 21 | arr[i] = value; 22 | value *= 2; 23 | } 24 | 25 | i = 0; 26 | // 使用do while循环显示数组元素的值 27 | printf("Here are the list values:\n"); 28 | do { 29 | printf("%d ", arr[i++]); 30 | } while (i < SIZE); 31 | printf("\n"); 32 | 33 | return 0; 34 | } 35 | ``` 36 | 37 | **执行结果:** 38 | ``` 39 | CPrimerPlus\cmake-build-debug-mingw\ex06-13.exe 40 | Here are the list values: 41 | 1 2 4 8 16 32 64 128 42 | 43 | Process finished with exit code 0 44 | ``` -------------------------------------------------------------------------------- /docs/ch08/ex01.md: -------------------------------------------------------------------------------- 1 | # 习题8.1 2 | 3 |   设计一个程序,统计在读到文件结尾之前读取的字符数。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch08/ex01.c` 7 | ```c 8 | #include 9 | 10 | int main(void) { 11 | int ch; 12 | // 字符数 13 | int count = 0; 14 | 15 | // 读取文件输入流,配置Redirect input from:exercises\ch08\files\file 16 | while ((ch = getchar()) != '&') { 17 | count++; 18 | } 19 | // 打印字符总数 20 | printf("%d characters read.\n", count); 21 | return 0; 22 | } 23 | ``` 24 | 25 | 该程序需要配置重定向文件,具体配置信息见下图: 26 | ![配置重定向文件](images/ex01_redirect_input_config.png) 27 | 28 | 29 | 文件`exercises/ch08/files/file`的内容: 30 | ``` 31 | Ishphat the robot 32 | slid open the hatch 33 | and shouted his challenge.& 34 | ``` 35 | 36 | **执行结果:** 37 | ``` 38 | CPrimerPlus\cmake-build-debug-mingw\ex08-01.exe 39 | 64 characters read. 40 | 41 | Process finished with exit code 0 42 | ``` 43 | 44 | -------------------------------------------------------------------------------- /docs/ch08/ex06.md: -------------------------------------------------------------------------------- 1 | # 习题8.6 2 | 3 |   修改程序清单8.8中的`get_first()`函数,让该函数返回读取的第1个非空白字符,并在一个简单的程序中测试。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch08/ex06.c` 7 | ```c 8 | #include 9 | #include 10 | 11 | int get_first(void); 12 | 13 | int main(void) { 14 | int ch; 15 | 16 | // 提示用户输入一个单词 17 | printf("Please enter a word:"); 18 | ch = get_first(); 19 | printf("The first character is %c", ch); 20 | 21 | return 0; 22 | } 23 | 24 | int get_first(void) { 25 | int ch; 26 | 27 | // 获取第一个字符 28 | ch = getchar(); 29 | // 如果是空白或标点字符,就取下一个字符 30 | while (isspace(ch) || ispunct(ch)) 31 | ch = getchar(); 32 | 33 | // 将所有的输入流都清空 34 | while (getchar() != '\n') 35 | continue; 36 | 37 | // 返回不是空白或标点的字符,即已取到的字符 38 | return ch; 39 | } 40 | ``` 41 | 42 | **执行结果:** 43 | ``` 44 | CPrimerPlus\cmake-build-debug-mingw\ex08-06.exe 45 | Please enter a word: abcdefg 46 | The first character is a 47 | Process finished with exit code 0 48 | ``` -------------------------------------------------------------------------------- /docs/ch08/images/ex01_redirect_input_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch08/images/ex01_redirect_input_config.png -------------------------------------------------------------------------------- /docs/ch08/images/ex02_redirect_input_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch08/images/ex02_redirect_input_config.png -------------------------------------------------------------------------------- /docs/ch11/ex14.md: -------------------------------------------------------------------------------- 1 | # 习题11.14 2 | 3 |   编写一个通过命令行运行的程序计算幂。第1个命令行参数是`double`类型的数,作为幂的底数,第2个参数是整数,作为幂的指数。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch11/ex14.c` 7 | ```c 8 | #include 9 | #include 10 | #include 11 | 12 | int main(int argc, char *argv[]) { 13 | double num, exp; 14 | 15 | if (argc != 3) { 16 | // 提示命令用法 17 | printf("Usage: %s number exponent\n", argv[0]); 18 | } else { 19 | // 使用atof函数将命令行参数转为double类型的数 20 | num = atof(argv[1]); 21 | exp = atof(argv[2]); 22 | // 打印幂的结果 23 | printf("%g to the %g power = %g\n", num, exp, pow(num, exp)); 24 | } 25 | 26 | return 0; 27 | } 28 | ``` 29 | 30 | 该程序需要配置命令行参数,具体配置信息见下图: 31 | ![配置命令行参数](images/ex14_program_arguments_config.png) 32 | **执行结果:** 33 | ``` 34 | CPrimerPlus\cmake-build-debug-mingw\ex11-14.exe 2.3 4.0 35 | 2.3 to the 4 power = 27.9841 36 | 37 | Process finished with exit code 0 38 | ``` -------------------------------------------------------------------------------- /docs/ch11/images/ex13_program_arguments_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch11/images/ex13_program_arguments_config.png -------------------------------------------------------------------------------- /docs/ch11/images/ex14_program_arguments_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch11/images/ex14_program_arguments_config.png -------------------------------------------------------------------------------- /docs/ch11/images/ex16_program_arguments_config_l.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch11/images/ex16_program_arguments_config_l.png -------------------------------------------------------------------------------- /docs/ch11/images/ex16_program_arguments_config_p.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch11/images/ex16_program_arguments_config_p.png -------------------------------------------------------------------------------- /docs/ch11/images/ex16_program_arguments_config_u.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch11/images/ex16_program_arguments_config_u.png -------------------------------------------------------------------------------- /docs/ch12/ex01.md: -------------------------------------------------------------------------------- 1 | # 习题12.1 2 | 3 |   不使用全局变量,重写程序清单12.4。 4 | 5 | **解答:** 6 | 代码位置:`exercises/ch12/ex01.c` 7 | ```c 8 | #include 9 | void critic(int *u); 10 | 11 | int main(void) { 12 | int units; 13 | 14 | // 提示用户输入磅数 15 | printf("How many pounds to a firkin of butter?\n"); 16 | scanf("%d", &units); 17 | while (units != 56) 18 | // 判断用户输入的值 19 | critic(&units); 20 | printf("You must have looked it up!\n"); 21 | 22 | return 0; 23 | } 24 | 25 | void critic(int *u) { 26 | printf("No luck, my friend. Try again.\n"); 27 | // 继续猜测磅数 28 | scanf("%d", u); 29 | } 30 | ``` 31 | 32 | **执行结果:** 33 | ``` 34 | CPrimerPlus\cmake-build-debug-mingw\ex12-01.exe 35 | How many pounds to a firkin of butter? 36 | 14 37 | No luck, my friend. Try again. 38 | 56 39 | You must have looked it up! 40 | 41 | Process finished with exit code 0 42 | ``` -------------------------------------------------------------------------------- /docs/ch13/images/ex01_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch13/images/ex01_working_directory_config.png -------------------------------------------------------------------------------- /docs/ch13/images/ex02_program_arguments_and_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch13/images/ex02_program_arguments_and_working_directory_config.png -------------------------------------------------------------------------------- /docs/ch13/images/ex03_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch13/images/ex03_working_directory_config.png -------------------------------------------------------------------------------- /docs/ch13/images/ex04_program_arguments_and_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch13/images/ex04_program_arguments_and_working_directory_config.png -------------------------------------------------------------------------------- /docs/ch13/images/ex05_program_arguments_and_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch13/images/ex05_program_arguments_and_working_directory_config.png -------------------------------------------------------------------------------- /docs/ch13/images/ex06_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch13/images/ex06_working_directory_config.png -------------------------------------------------------------------------------- /docs/ch13/images/ex07_a_program_arguments_and_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch13/images/ex07_a_program_arguments_and_working_directory_config.png -------------------------------------------------------------------------------- /docs/ch13/images/ex07_b_program_arguments_and_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch13/images/ex07_b_program_arguments_and_working_directory_config.png -------------------------------------------------------------------------------- /docs/ch13/images/ex08_program_arguments_and_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch13/images/ex08_program_arguments_and_working_directory_config.png -------------------------------------------------------------------------------- /docs/ch13/images/ex09_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch13/images/ex09_working_directory_config.png -------------------------------------------------------------------------------- /docs/ch13/images/ex10_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch13/images/ex10_working_directory_config.png -------------------------------------------------------------------------------- /docs/ch13/images/ex11_program_arguments_and_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch13/images/ex11_program_arguments_and_working_directory_config.png -------------------------------------------------------------------------------- /docs/ch13/images/ex12_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch13/images/ex12_working_directory_config.png -------------------------------------------------------------------------------- /docs/ch13/images/ex13_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch13/images/ex13_working_directory_config.png -------------------------------------------------------------------------------- /docs/ch13/images/ex14_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch13/images/ex14_working_directory_config.png -------------------------------------------------------------------------------- /docs/ch14/images/ex06_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch14/images/ex06_working_directory_config.png -------------------------------------------------------------------------------- /docs/ch14/images/ex07_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch14/images/ex07_working_directory_config.png -------------------------------------------------------------------------------- /docs/ch16/images/pic_6_13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch16/images/pic_6_13.png -------------------------------------------------------------------------------- /docs/ch17/images/ex07_working_directory_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/ch17/images/ex07_working_directory_config.png -------------------------------------------------------------------------------- /docs/resources/CMake_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/resources/CMake_config.png -------------------------------------------------------------------------------- /docs/resources/Toolchains_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/docs/resources/Toolchains_config.png -------------------------------------------------------------------------------- /exercises/ch02/ex01.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | // 打印第1次 4 | printf("Gustav Mahler\n"); 5 | // 打印两行 6 | printf("Gustav\nMahler\n"); 7 | // 两次打印仅输出一行 8 | printf("Gustav "); 9 | printf("Mahler\n"); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /exercises/ch02/ex03.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/26. 3 | // 4 | #include 5 | int main(void){ 6 | int age_years, age_days; 7 | // 设置年龄 8 | age_years = 32; 9 | // 计算天数 10 | age_days = 365 * age_years; 11 | // 打印并显示年龄和天数 12 | printf("The age of %d years is %d days.\n", age_years, age_days); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /exercises/ch02/ex04.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/26. 3 | // 4 | #include 5 | 6 | void jolly(void); 7 | void deny(void); 8 | 9 | int main(void){ 10 | jolly(); 11 | jolly(); 12 | jolly(); 13 | deny(); 14 | return 0; 15 | } 16 | 17 | /** 18 | * 用于打印前3条信息 19 | */ 20 | void jolly(void){ 21 | printf("For he's a jolly good fellow!\n"); 22 | } 23 | 24 | /** 25 | * 用于打印最后一条信息 26 | */ 27 | void deny(void){ 28 | printf("Which nobody can deny!"); 29 | } -------------------------------------------------------------------------------- /exercises/ch02/ex05.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/26. 3 | // 4 | #include 5 | 6 | void br(void); 7 | void ic(void); 8 | 9 | int main(void){ 10 | br(); 11 | printf(", "); 12 | ic(); 13 | printf("\n"); 14 | ic(); 15 | printf("\n"); 16 | br(); 17 | return 0; 18 | } 19 | 20 | /** 21 | * 用户打印"Brazil, Russia" 22 | */ 23 | void br(void){ 24 | printf("Brazil, Russia"); 25 | } 26 | 27 | /** 28 | * 用于打印“India, China” 29 | */ 30 | void ic(void){ 31 | printf("India, China"); 32 | } 33 | -------------------------------------------------------------------------------- /exercises/ch02/ex06.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/26. 3 | // 4 | #include 5 | int main(void){ 6 | // 定义整型变量toes 7 | int toes; 8 | // 将toes设置为10 9 | toes = 10; 10 | printf("toes = %d\n", toes); 11 | // 计算toes的两倍 12 | printf("Twice toes = %d\n", 2 * toes); 13 | // 计算toes的平方 14 | printf("toes squared = %d\n", toes * toes); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /exercises/ch02/ex07.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/26. 3 | // 4 | #include 5 | 6 | void smile(void); 7 | 8 | int main(void){ 9 | // 打印三次Smile! 10 | smile(); 11 | smile(); 12 | smile(); 13 | printf("\n"); 14 | // 打印两次Smile! 15 | smile(); 16 | smile(); 17 | printf("\n"); 18 | // 打印一次Smile! 19 | smile(); 20 | printf("\n"); 21 | return 0; 22 | } 23 | 24 | /** 25 | * 用于打印Smile! 26 | */ 27 | void smile(void){ 28 | printf("Smile!"); 29 | } 30 | -------------------------------------------------------------------------------- /exercises/ch02/ex08.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/26. 3 | // 4 | #include 5 | 6 | void one_three(void); 7 | void two(void); 8 | 9 | int main(void){ 10 | printf("starting now;\n"); 11 | one_three(); 12 | printf("done!\n"); 13 | return 0; 14 | } 15 | 16 | void one_three(void){ 17 | // 打印one 18 | printf("one\n"); 19 | // 调用函数tow() 20 | two(); 21 | // 打印three 22 | printf("three\n"); 23 | } 24 | 25 | void two(void){ 26 | // 打印tow 27 | printf("two\n"); 28 | } 29 | -------------------------------------------------------------------------------- /exercises/ch03/ex02.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/27. 3 | // 4 | #include 5 | int main(void){ 6 | int ascii; 7 | 8 | // 提示输入一个ASCII码值 9 | printf("Enter an ASCII code:"); 10 | scanf("%d", &ascii); 11 | // 打印输入的字符 12 | printf("%d is the ASCII code for %c.\n", ascii, ascii); 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /exercises/ch03/ex03.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/27. 3 | // 4 | #include 5 | int main(void){ 6 | // 发出一声警报,并打印文本 7 | printf("\aStartled by the sudden sound, Sally shouted,\n"); 8 | printf("\"By the Great Pumpkin, what was that!\""); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /exercises/ch03/ex04.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/27. 3 | // 4 | #include 5 | int main(void){ 6 | float num; 7 | // 输入一个浮点数 8 | printf("Enter a floating-point value:"); 9 | scanf("%f", &num); 10 | // 打印小数点形式 11 | printf("fixed-point notation: %f\n", num); 12 | // 打印指数形式 13 | printf("exponential notation:%e\n", num); 14 | // 打印p记数法形式 15 | printf("p notation:%a\n", num); 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /exercises/ch03/ex05.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/27. 3 | // 4 | #include 5 | int main(void){ 6 | int age_years; 7 | double age_seconds; 8 | // 提示输入年龄 9 | printf("Enter your age:"); 10 | scanf("%d", &age_years); 11 | // 计算该年龄对应的秒数 12 | age_seconds = 3.156e7 * age_years; 13 | printf("The age of %d years is %e seconds.\n", age_years, age_seconds); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /exercises/ch03/ex06.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/27. 3 | // 4 | #include 5 | int main(void){ 6 | float mass_mol = 3.0e-23; 7 | float mass_qt = 950; 8 | float quarts, molecules; 9 | 10 | // 提示用户输入水的夸脱数 11 | printf("Enter the number of quarts of water:"); 12 | scanf("%f", &quarts); 13 | // 计算水分子的数量 14 | molecules = quarts * mass_qt / mass_mol; 15 | printf("%g quarts of water contain %e molecules.\n", quarts, molecules); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /exercises/ch03/ex07.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/27. 3 | // 4 | #include 5 | int main(void){ 6 | float inch = 2.54; 7 | float height; 8 | // 提示用户输入身高(单位:英寸) 9 | printf("Enter the num of height (in):"); 10 | scanf("%f", &height); 11 | // 计算并打印身高(单位:厘米) 12 | printf("%.2f(in) height is %.2f(cm).\n", height, height * inch); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /exercises/ch03/ex08.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/27. 3 | // 4 | #include 5 | int main(void){ 6 | // 1品脱等于2杯 7 | float cup_per_pint = 2; 8 | // 1杯等于8盎司 9 | float oz_per_cup = 8; 10 | // 1盎司等于2大汤勺 11 | float tbs_per_oz = 2; 12 | // 1大汤勺等于3茶勺 13 | float tsp_per_tbs = 3; 14 | 15 | float num_cup; 16 | // 提示用户输入杯数 17 | printf("Enter the num of Cup:"); 18 | scanf("%f", &num_cup); 19 | // 以品脱显示容量 20 | printf("%g(cup) of pint is %g.\n", num_cup, num_cup / cup_per_pint); 21 | // 以盎司显示容量 22 | printf("%g(cup) of ounce is %g.\n", num_cup, num_cup * oz_per_cup); 23 | // 以汤勺显示容量 24 | printf("%g(cup) of tablespoon is %g.\n", num_cup, num_cup * oz_per_cup * tbs_per_oz); 25 | // 以茶勺显示容量 26 | printf("%g(cup) of teaspoon is %g.\n", num_cup, num_cup * oz_per_cup * tbs_per_oz * tsp_per_tbs); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /exercises/ch04/ex01.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/31. 3 | // 4 | #include 5 | int main(void){ 6 | char first_name[40]; 7 | char last_name[40]; 8 | 9 | // 提示用户输入名 10 | printf("Enter your first name:"); 11 | scanf("%s", first_name); 12 | // 提示用户输入姓 13 | printf("Enter your last name:"); 14 | scanf("%s", last_name); 15 | // 打印“名,姓” 16 | printf("%s, %s\n", first_name, last_name); 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /exercises/ch04/ex02.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/31. 3 | // 4 | #include 5 | #include 6 | 7 | int main(void) { 8 | char name[40]; 9 | // 提示用户输入名字 10 | printf("Enter your name:"); 11 | scanf("%s", name); 12 | // a. 打印名字,包括双引号 13 | printf("\"%s\"\n", name); 14 | // b. 在宽度为20的字段右端打印名字,包括双引号 15 | printf("\"%*s\"\n", 20, name); 16 | // c. 在宽度为20的字段左端打印名字,包括双引号 17 | printf("\"%-*s\"\n", 20, name); 18 | // d. 在比姓名宽度宽3的字段中打印名字 19 | printf("%*s\n", strlen(name) + 3, name); 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /exercises/ch04/ex03.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/31. 3 | // 4 | #include 5 | int main(void){ 6 | float val; 7 | // 读取一个浮点数 8 | printf("Enter a floating-point value:"); 9 | scanf("%f", &val); 10 | // 以小数点记数法打印 11 | printf("The input is %.1f or %.1e.\n", val, val); 12 | // 以指数记数法打印 13 | printf("The input is %+.3f or %.3E.\n", val, val); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /exercises/ch04/ex04.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/31. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | float height; 8 | char name[40]; 9 | // 提示用户输入身高 10 | printf("Enter your height(in):"); 11 | scanf("%f", &height); 12 | // 提示用户输入姓名 13 | printf("Enter your name:"); 14 | scanf("%s", name); 15 | // 显示用户输入的信息 16 | printf("%s, you are %.3f feet tall.\n", name, height / 12.0); 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /exercises/ch04/ex05.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/31. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | float download_speed, file_size; 8 | //输入下载速度 9 | printf("Enter a number of download speed(Mb/s):"); 10 | scanf("%f", &download_speed); 11 | // 输入文件大小 12 | printf("Enter a size of file(MB):"); 13 | scanf("%f", &file_size); 14 | // 打印信息 15 | printf("\nAt %.2f megabits per second, a file of %.2f megabytes\n", download_speed, file_size); 16 | // 计算并打印下载时间 17 | printf("downloads in %.2f second.\n", file_size * 8 / download_speed); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /exercises/ch04/ex06.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/31. 3 | // 4 | #include 5 | #include 6 | 7 | int main(void) { 8 | char first_name[40]; 9 | char last_name[40]; 10 | 11 | // 提示用户输入名 12 | printf("Enter your first name:"); 13 | scanf("%s", first_name); 14 | // 提示用户输入姓 15 | printf("Enter your last name:"); 16 | scanf("%s", last_name); 17 | // 打印用户输入的名和姓 18 | printf("%s %s\n", first_name, last_name); 19 | // 打印名和姓的字母数,并与相应的名和姓的结尾对齐 20 | printf("%*i %*i\n", strlen(first_name), strlen(first_name), strlen(last_name), strlen(last_name)); 21 | // 打印用户输入的名和姓 22 | printf("%s %s\n", first_name, last_name); 23 | // 打印名和姓的字母数,并与相应的名和姓的开头对齐 24 | printf("%-*i %-*i\n", strlen(first_name), strlen(first_name), strlen(last_name), strlen(last_name)); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /exercises/ch04/ex07.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/31. 3 | // 4 | #include 5 | #include 6 | 7 | int main(void) { 8 | // 设置double类型的变量 9 | double ot_d = 1.0 / 3.0; 10 | // 设置float类型的变量 11 | float ot_f = 1.0 / 3.0; 12 | 13 | // 按格式打印float类型的值 14 | printf(" float values: "); 15 | printf("%.6f %.12f %.16f\n", ot_f, ot_f, ot_f); 16 | // 按格式打印double类型的值 17 | printf("double values: "); 18 | printf("%.6f %.12f %.16f\n", ot_d, ot_d, ot_d); 19 | // 打印FLT_DIG的值 20 | printf("FLT_DIG: %d\n", FLT_DIG); 21 | // 打印DBL_DIG的值 22 | printf("DBL_DIG: %d\n", DBL_DIG); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /exercises/ch04/ex08.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/10/31. 3 | // 4 | #include 5 | // 1加仑大约3.785升 6 | #define LITER_PER_GALLONS 3.785 7 | // 1英里大约1.609千米 8 | #define KILOMETERS_PER_MILE 1.609 9 | 10 | int main(void) { 11 | float miles, oils; 12 | 13 | // 输入旅行的里程 14 | printf("Enter a mileage of journey(mile):"); 15 | scanf("%f", &miles); 16 | // 输入消耗的汽油量 17 | printf("Enter an amount of gasoline to cost(gallon):"); 18 | scanf("%f", &oils); 19 | // 计算并显示消耗每加仑汽油行驶的英里数(美国方案) 20 | printf("In UAS, your oil wear is %.1f M/G\n", miles / oils); 21 | // 计算并显示消耗每升汽油行驶的百公里数(欧洲方案) 22 | printf("In Europe, your oil wear is %.1f L/100KM\n", 23 | (oils * LITER_PER_GALLONS) / (miles * KILOMETERS_PER_MILE / 100)); 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /exercises/ch05/ex01.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/1. 3 | // 4 | #include 5 | // 每小时60分钟 6 | #define MIN_PER_HOUR 60 7 | int main(void){ 8 | int minutes, hours, mins; 9 | 10 | // 提示用户输入分钟数 11 | printf("Enter the number of minutes to convert:"); 12 | scanf("%d", &minutes); 13 | while (minutes > 0){ 14 | // 计算转换后的小时数 15 | hours = minutes / MIN_PER_HOUR; 16 | // 计算转换后的分钟数 17 | mins = minutes % MIN_PER_HOUR; 18 | // 打印转换后的用小时和分钟表示的时间 19 | printf("%d minutes = %d hours, %d minutes\n", minutes, hours, mins); 20 | // 提示用户再次输入 21 | printf("Enter next minutes value(0 to quit):"); 22 | scanf("%d", &minutes); 23 | } 24 | printf("Bye!\n"); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /exercises/ch05/ex02.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/1. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | int num; 8 | 9 | // 提示用户输入一个整数 10 | printf("Enter a number:"); 11 | scanf("%d", &num); 12 | // 得到上限值 13 | int limits = num + 10; 14 | // 使用while循环,打印范围内的所有整数 15 | printf("The number from %d to %d:\n", num, limits); 16 | while (num <= limits) { 17 | printf("%d ", num); 18 | num++; 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /exercises/ch05/ex03.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/1. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | // 每周7天 8 | const int days_per_week = 7; 9 | int days, weeks, day_rem; 10 | 11 | // 提示用户输入天数 12 | printf("Enter the number of days:"); 13 | scanf("%d", &days); 14 | while (days > 0) { 15 | // 计算周数 16 | weeks = days / days_per_week; 17 | // 计算转换后的天数 18 | day_rem = days % days_per_week; 19 | // 打印转换后的周数和天数 20 | printf("%d days are %d weeks, %d days.\n", days, weeks, day_rem); 21 | 22 | // 提示用户再次输入天数 23 | printf("Enter the number of days (0 or less to quit):"); 24 | scanf("%d", &days); 25 | } 26 | printf("Bye!\n"); 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /exercises/ch05/ex04.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/1. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | // 2.54厘米每英寸 8 | const float cm_per_inches = 2.54; 9 | // 12英寸每英尺 10 | const int inches_per_feet = 12; 11 | 12 | float height, inches; 13 | int feet; 14 | 15 | // 提示用户输入身高 16 | printf("Enter a height in centimeters:"); 17 | scanf("%f", &height); 18 | while (height > 0) { 19 | // 计算转换后的英寸 20 | inches = height / cm_per_inches; 21 | // 计算英尺 22 | feet = inches / inches_per_feet; 23 | // 计算英寸 24 | inches = inches - feet * inches_per_feet; 25 | // 打印信息 26 | printf("%.1f cm = %d feet, %.1f inches\n", height, feet, inches); 27 | 28 | // 让用户再次输入身高 29 | printf("Enter a height in centimeters (<=0 to quit):"); 30 | scanf("%f", &height); 31 | } 32 | printf("bye\n"); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /exercises/ch05/ex05.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/2. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | int count, sum; 8 | int n; 9 | 10 | // 提示用户输入一个求和上限值 11 | printf("Enter the upper limit:"); 12 | scanf("%d", &n); 13 | 14 | count = 0; 15 | sum = 0; 16 | // 进行迭代求和 17 | while (count++ < n) 18 | sum += count; 19 | // 打印求和结果 20 | printf("sum = %d\n", sum); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /exercises/ch05/ex06.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/2. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | int count, sum; 8 | int n; 9 | 10 | // 提示用户输入一个上限值 11 | printf("Enter the upper limit:"); 12 | scanf("%d", &n); 13 | 14 | count = 0; 15 | sum = 0; 16 | // 进行迭代求和 17 | while (count++ < n) 18 | // 平方求和 19 | sum += count * count; 20 | // 打印求和结果 21 | printf("sum = %d\n", sum); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /exercises/ch05/ex07.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/2. 3 | // 4 | #include 5 | 6 | void print_cube(double x); 7 | 8 | int main(void) { 9 | double num; 10 | 11 | // 提示用户输入一个double类型的数 12 | printf("Enter a double value:"); 13 | scanf("%lf", &num); 14 | // 调用函数,打印该数的立方值 15 | print_cube(num); 16 | 17 | return 0; 18 | } 19 | 20 | void print_cube(double x) { 21 | // 计算并打印x的立方值 22 | printf("The cube of %g is %g.\n", x, x * x * x); 23 | } -------------------------------------------------------------------------------- /exercises/ch05/ex08.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/2. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | int first_oper, second_oper; 8 | 9 | printf("This program computes moduli.\n"); 10 | // 提示用户输入求模运算的第2个运算对象 11 | printf("Enter an integer to serve as the second operand:"); 12 | scanf("%d", &second_oper); 13 | 14 | // 提示用户输入第1个运算对象 15 | printf("Now enter the first operand:"); 16 | scanf("%d", &first_oper); 17 | // 循环判断第1个运算对象是否大于0 18 | while (first_oper > 0) { 19 | // 计算并打印求模结果 20 | printf("%d %% %d is %d\n", first_oper, second_oper, first_oper % second_oper); 21 | // 提示用户再次输入第1个运算对象 22 | printf("Enter next number for first operand (<= 0 to quit):"); 23 | scanf("%d", &first_oper); 24 | } 25 | printf("Done\n"); 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /exercises/ch06/ex01.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/2. 3 | // 4 | #include 5 | 6 | // 定义元素个数 7 | #define SIZE 26 8 | 9 | int main(void) { 10 | // 创建一个包含26个元素的数组 11 | int letters[SIZE]; 12 | int i; 13 | 14 | // 使用for循环存储26个小写字母 15 | for (i = 0; i < SIZE; i++) 16 | letters[i] = 'a' + i; 17 | 18 | // 使用for循环打印数组的所有内容 19 | for (i = 0; i < SIZE; i++) 20 | printf("%c", letters[i]); 21 | 22 | printf("\n"); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /exercises/ch06/ex02.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/2. 3 | // 4 | #include 5 | // 设置需要打印的数量 6 | #define SIZE 5 7 | 8 | int main(void) { 9 | // 循环打印每一行 10 | for (int i = 0; i < SIZE; i++) { 11 | // 循环打印该行的字符 12 | for (int j = 0; j <= i; j++) 13 | printf("$"); 14 | printf("\n"); 15 | } 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /exercises/ch06/ex03.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/2. 3 | // 4 | #include 5 | 6 | #define LETTER 'F' 7 | 8 | int main(void) { 9 | char start; 10 | char end; 11 | 12 | // 由于打印结果是到A为止,可判断下界为字母'A' 13 | for (end = LETTER; end >= 'A'; end--) { 14 | // 打印一行,从字母F开始到end变量 15 | for (start = LETTER; start >= end; start--) 16 | printf("%c", start); 17 | // 打印之后换行 18 | printf("\n"); 19 | } 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /exercises/ch06/ex04.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/2. 3 | // 4 | #include 5 | 6 | #define SIZE 6 7 | 8 | int main(void) { 9 | 10 | int count; 11 | // 打印的字母初始化为A 12 | char letter = 'A'; 13 | for (count = 0; count < SIZE; count++) { 14 | // 每行需要打印的第一个字母 15 | letter += count; 16 | // 用于字母自增打印 17 | char temp = letter; 18 | for (int j = 0; j <= count; temp++, j++) { 19 | printf("%c", temp); 20 | } 21 | printf("\n"); 22 | } 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /exercises/ch06/ex06.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/2. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | int lower, upper, index; 8 | int square, cube; 9 | 10 | // 输入下限 11 | printf("Enter starting integer:"); 12 | scanf("%d", &lower); 13 | // 输入上限 14 | printf("Enter ending integer:"); 15 | scanf("%d", &upper); 16 | 17 | printf("%5s %10s %15s\n", "num", "square", "cube"); 18 | // 使用for循环打印整数、该数的平方、该数的立方 19 | for (index = lower; index <= upper; index++) { 20 | // 计算该数的平方 21 | square = index * index; 22 | // 计算该数的立方 23 | cube = index * square; 24 | printf("%5d %10d %15d\n", index, square, cube); 25 | } 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /exercises/ch06/ex07.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/2. 3 | // 4 | #include 5 | #include 6 | 7 | #define LEN 40 8 | 9 | int main(void) { 10 | char word[LEN]; 11 | int i, length; 12 | 13 | // 提示用户输入一个单词 14 | printf("Enter a word:"); 15 | scanf("%s", word); 16 | 17 | // 得到单词的长度 18 | length = strlen(word) - 1; 19 | printf("The reverse of %s is ", word); 20 | // 逆序打印这个单词 21 | for (i = length; i >= 0; i--) { 22 | printf("%c", word[i]); 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /exercises/ch06/ex08.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/2. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | double n, m; 8 | double res; 9 | 10 | // 提示用户输入两个浮点数 11 | printf("Enter a pair of floating-point numbers:"); 12 | 13 | while (scanf("%lf %lf", &n, &m) == 2) { 14 | // 计算两数之差除以两数乘积的结果 15 | res = (n - m) / (n * m); 16 | // 打印结果 17 | printf("(%.3g - %.3g) / (%.3g * %.3g) = %.5g\n", n, m, n, m, res); 18 | // 提示用户再次输入两个浮点数 19 | printf("Enter next pair (non-numeric to quit):"); 20 | } 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /exercises/ch06/ex09.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/2. 3 | // 4 | #include 5 | 6 | double calc(double n, double m); 7 | 8 | int main(void) { 9 | double n, m; 10 | double res; 11 | 12 | // 提示用户输入两个浮点数 13 | printf("Enter a pair of floating-point numbers:"); 14 | 15 | while (scanf("%lf %lf", &n, &m) == 2) { 16 | // 调用calc()函数,计算两数之差除以两数乘积的结果 17 | res = calc(n, m); 18 | // 打印结果 19 | printf("(%.3g - %.3g) / (%.3g * %.3g) = %.5g\n", n, m, n, m, res); 20 | // 提示用户再次输入两个浮点数 21 | printf("Enter next pair (non-numeric to quit):"); 22 | } 23 | 24 | return 0; 25 | } 26 | 27 | double calc(double n, double m) { 28 | return (n - m) / (n * m); 29 | } -------------------------------------------------------------------------------- /exercises/ch06/ex10.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/2. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | int lower, upper, index; 8 | int sum; 9 | 10 | // 提示用户输入一个上限整数和一个下限整数 11 | printf("Enter lower and upper integer limits:"); 12 | while (scanf("%d %d", &lower, &upper) == 2 && lower < upper) { 13 | sum = 0; 14 | // 求范围内所有整数的平方和 15 | for (index = lower; index <= upper; index++) { 16 | sum += index * index; 17 | } 18 | // 打印结果 19 | printf("The sums of the squares from %d to %d is %d\n", lower * lower, upper * upper, sum); 20 | // 提示用户再次输入 21 | printf("Enter next set of limits:"); 22 | } 23 | 24 | printf("Done"); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /exercises/ch06/ex11.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/2. 3 | // 4 | #include 5 | 6 | #define SIZE 8 7 | 8 | int main(void) { 9 | // 定义整数数组 10 | int input[SIZE]; 11 | int i; 12 | 13 | // 提示用户输入8个整数 14 | printf("Please enter %d integers.\n", SIZE); 15 | for (i = 0; i < SIZE; i++) { 16 | scanf("%d", &input[i]); 17 | } 18 | 19 | // 逆序打印8个整数 20 | printf("Here are the values in reverse order you entered:\n"); 21 | for (i = SIZE - 1; i >= 0; i--) { 22 | printf("%d ", input[i]); 23 | } 24 | printf("\n"); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /exercises/ch06/ex13.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/2. 3 | // 4 | #include 5 | 6 | #define SIZE 8 7 | 8 | int main(void) { 9 | // 定义一个包含8个元素的int类型数组 10 | int arr[SIZE]; 11 | int i; 12 | int value = 1; 13 | 14 | // 设置数组元素的值 15 | for (i = 0; i < SIZE; i++) { 16 | // 根据i,设置为2的i次幂 17 | arr[i] = value; 18 | value *= 2; 19 | } 20 | 21 | i = 0; 22 | // 使用do while循环显示数组元素的值 23 | printf("Here are the list values:\n"); 24 | do { 25 | printf("%d ", arr[i++]); 26 | } while (i < SIZE); 27 | printf("\n"); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /exercises/ch06/ex14.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/2. 3 | // 4 | #include 5 | 6 | #define SIZE 8 7 | 8 | int main(void) { 9 | double arr1[SIZE], arr2[SIZE]; 10 | int i; 11 | 12 | // 提示用户输入8个double类型的数,并赋值给第一个数组 13 | printf("Enter %d numbers to the FIRST array:\n", SIZE); 14 | for (i = 0; i < SIZE; i++) { 15 | scanf("%lf", &arr1[i]); 16 | } 17 | 18 | // 使用一个循环 19 | arr2[0] = arr1[0]; 20 | for (i = 1; i < SIZE; i++) { 21 | arr2[i] = arr2[i - 1] + arr1[i]; 22 | } 23 | 24 | // 显示两个数组的内容 25 | printf("All the data of two array:\n"); 26 | printf("First Array: "); 27 | for (int i = 0; i < 8; i++) { 28 | printf("%10lf. ", arr1[i]); 29 | } 30 | printf("\nSecond Array: "); 31 | for (int i = 0; i < 8; i++) { 32 | printf("%10lf. ", arr2[i]); 33 | } 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /exercises/ch06/ex15.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/2. 3 | // 4 | #include 5 | #include 6 | 7 | #define SIZE 255 8 | 9 | int main(void) { 10 | char data[SIZE]; 11 | int i = 0, length; 12 | 13 | // 提示用户输入一行字符 14 | printf("Enter the char in a line:\n"); 15 | do { 16 | // 循环读取用户输入的字符,保存到字符数组中,直到输入换行符 17 | scanf("%c", &data[i]); 18 | } while (data[i++] != '\n'); 19 | 20 | // 打印倒序的内容 21 | printf("The reverse char of the data:\n"); 22 | // 删除数组中的最后一个字符(即换行符) 23 | length = strlen(data) - 2; 24 | for (i = length; i >= 0; i--) { 25 | printf("%c", data[i]); 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /exercises/ch06/ex16.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/3. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | // 单利息 8 | const double RATE_SIMP = 0.10; 9 | // 复合利息 10 | const double RATE_COMP = 0.05; 11 | const double init_amt = 100.0; 12 | 13 | // 两个人都投资了100美元 14 | double daphne = init_amt; 15 | double deirdre = init_amt; 16 | 17 | int years = 0; 18 | 19 | // 判断Deirdre的投资额是否超过了Daphne 20 | while (deirdre <= daphne) { 21 | // 计算单利息方案的投资 22 | daphne += RATE_SIMP * init_amt; 23 | // 计算复合利息的投资 24 | deirdre += RATE_COMP * deirdre; 25 | years++; 26 | } 27 | 28 | // 显示两人的投资额 29 | printf("Investment values after %d years:\n", years); 30 | printf("Daphne: $%.2f\n", daphne); 31 | printf("Deirdre: $%.2f\n", deirdre); 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /exercises/ch06/ex17.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/3. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | // 年利率 8 | const double RATE_YEAR = 0.08; 9 | 10 | int year = 0; 11 | // 总金额 12 | double chuckie = 100; 13 | 14 | // 打印账户记录 15 | printf("Here is Chuckie's account recode:\n"); 16 | printf("%5s %6s\n", "Year", "Account"); 17 | do { 18 | // 年利率计算 19 | chuckie = chuckie + chuckie * RATE_YEAR; 20 | // 每年最后一天取10万美元 21 | chuckie -= 10; 22 | year++; 23 | printf("%4d %8.2f\n", year, chuckie > 0 ? chuckie : 0); 24 | } while (chuckie > 0); 25 | 26 | // 打印结果 27 | printf("%d years later, Chuckie's account is ZERO.\n", year); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /exercises/ch06/ex18.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/3. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | // 初始朋友数量 8 | int friends = 5; 9 | // 第1周 10 | int weeks = 1; 11 | 12 | printf("The number of Dr Rabnud's friends:\n"); 13 | printf("%5s %10s\n", "Week", "Friends"); 14 | 15 | while (friends <= 150) { 16 | // 第n周少了n个朋友,剩下的朋友数量翻倍 17 | friends = (friends - weeks) * 2; 18 | // 打印每周的朋友数量 19 | printf("%5d %7d\n", weeks, friends); 20 | weeks++; 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /exercises/ch07/ex01.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/3. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | int ch; 8 | int space_count = 0; 9 | int newlines_count = 0; 10 | int other = 0; 11 | 12 | printf("Please input chars (# to exit):\n"); 13 | while ((ch = getchar()) != '#') { 14 | if (ch == ' ') 15 | // 统计空格符号的数量 16 | space_count++; 17 | else if (ch == '\n') 18 | // 统计换行符的数量 19 | newlines_count++; 20 | else 21 | // 统计所有其他字符的数量 22 | other++; 23 | } 24 | 25 | // 打印统计结果 26 | printf("spaces: %d, newlines: %d, others: %d", space_count, newlines_count, other); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /exercises/ch07/ex02.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/3. 3 | // 4 | #include 5 | 6 | #define SIZE 8 7 | 8 | int main(void) { 9 | int ch; 10 | int count = 0; 11 | 12 | printf("Please input chars (# to quit):\n"); 13 | // 判断读到#字符停止 14 | while ((ch = getchar()) != '#') { 15 | if (count++ % SIZE == 0) { 16 | printf("\n"); 17 | } 18 | // 打印字符-ASCII码 19 | if (ch == '\n') { 20 | printf("\\n-%3d ", ch); 21 | } else if (ch == '\t') { 22 | printf("\\t-%3d ", ch); 23 | } else { 24 | printf("%c-%3d ", ch, ch); 25 | } 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /exercises/ch07/ex04.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/3. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | int ch; 8 | int count1 = 0; 9 | int count2 = 0; 10 | 11 | // 提示用户录入信息 12 | printf("Enter text to be analyzed (# to terminate):\n"); 13 | // 读到#停止 14 | while ((ch = getchar()) != '#') { 15 | // 用两个感叹号替换原来的感叹号 16 | if (ch == '!') { 17 | putchar('!'); 18 | putchar('!'); 19 | count1++; 20 | } else if (ch == '.') { 21 | // 用感叹号替换句号 22 | putchar('!'); 23 | count2++; 24 | } else { 25 | putchar(ch); 26 | } 27 | } 28 | 29 | // 报告替换次数 30 | printf("\n%d replacement(s) of . with !\n", count2); 31 | printf("%d replacement(s) of ! with !!\n", count1); 32 | printf("Total replace %d times\n", count1 + count2); 33 | printf("Done\n"); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /exercises/ch07/ex05.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/3. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | int ch; 8 | int count1 = 0; 9 | int count2 = 0; 10 | 11 | // 提示用户录入信息 12 | printf("Enter text to be analyzed (# to terminate):\n"); 13 | // 读到#停止 14 | while ((ch = getchar()) != '#') { 15 | switch (ch) { 16 | case '.': 17 | // 用感叹号替换句号 18 | putchar('!'); 19 | count1++; 20 | break; 21 | case '!': 22 | // 用两个感叹号替换原来的感叹号 23 | putchar('!'); 24 | putchar('!'); 25 | count2++; 26 | break; 27 | default: 28 | putchar(ch); 29 | } 30 | } 31 | 32 | // 报告替换次数 33 | printf("\n%d replacement(s) of . with !\n", count1); 34 | printf("%d replacement(s) of ! with !!\n", count2); 35 | printf("Total replace %d times\n", count1 + count2); 36 | printf("Done\n"); 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /exercises/ch07/ex06.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/3. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | // 当前字符 8 | int ch; 9 | // 前一个字符 10 | int prev = ' '; 11 | // ei出现的次数 12 | int count = 0; 13 | 14 | printf("Please input chars (# to quit):\n"); 15 | while ((ch = getchar()) != '#') { 16 | // 判断是否为ei 17 | if (prev == 'e' && ch == 'i') { 18 | count++; 19 | } else { 20 | // 记录前一个字符 21 | prev = ch; 22 | } 23 | } 24 | // 报告ei出现的次数 25 | printf("Total ei appear %d times in all chars.\n", count); 26 | printf("Done\n"); 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /exercises/ch08/ex01.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/4. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | int ch; 8 | // 字符数 9 | int count = 0; 10 | 11 | // 读取文件输入流,配置Redirect input from:exercises\ch08\files\file 12 | while ((ch = getchar()) != '&') { 13 | count++; 14 | } 15 | // 打印字符总数 16 | printf("%d characters read.\n", count); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /exercises/ch08/ex02.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/4. 3 | // 4 | #include 5 | 6 | int main(void) { 7 | int ch; 8 | // 字符数 9 | int count = 0; 10 | 11 | while ((ch = getchar()) != '&') { 12 | // 当满10个,则打印换行符 13 | if (count++ == 10) { 14 | printf("\n"); 15 | count = 1; 16 | } 17 | // 大于空格字符显示字符和相应的ASCII码 18 | if (ch >= '\040') { 19 | printf("%2c-%3d ", ch, ch); 20 | } else if (ch == '\t') { 21 | // 处理制表符 22 | printf("\\t-%3d", ch); 23 | printf(" "); 24 | } else if (ch == '\n') { 25 | // 处理换行符 26 | printf("\\n-%3d\n", ch); 27 | count = 0; 28 | } else { 29 | // 处理其他的非打印字符 30 | printf("^%c-%3d", ch, (ch + 64)); 31 | } 32 | } 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /exercises/ch08/ex03.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/4. 3 | // 4 | #include 5 | #include 6 | 7 | int main(void) { 8 | int ch; 9 | long upper_count = 0; 10 | long lower_count = 0; 11 | long other_count = 0; 12 | 13 | printf("Please enter chars (& to quit):\n"); 14 | // 当输入&字符停止 15 | while ((ch = getchar()) != '&'){ 16 | if (isupper(ch)) 17 | // 计算大写字母个数 18 | upper_count++; 19 | else if (islower(ch)) 20 | // 计算小写字母个数 21 | lower_count++; 22 | else 23 | other_count++; 24 | } 25 | 26 | // 打印大写字母个数 27 | printf("\n%ld uppercase characters read\n", upper_count); 28 | // 打印小写字母个数 29 | printf("%ld lowercase characters read\n", lower_count); 30 | // 打印其他字符个数 31 | printf("%ld other characters read\n", other_count); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /exercises/ch08/ex06.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/4. 3 | // 4 | #include 5 | #include 6 | 7 | int get_first(void); 8 | 9 | int main(void) { 10 | int ch; 11 | 12 | // 提示用户输入一个单词 13 | printf("Please enter a word:"); 14 | ch = get_first(); 15 | printf("The first character is %c", ch); 16 | 17 | return 0; 18 | } 19 | 20 | int get_first(void) { 21 | int ch; 22 | 23 | // 获取第一个字符 24 | ch = getchar(); 25 | // 如果是空白或标点字符,就取下一个字符 26 | while (isspace(ch) || ispunct(ch)) 27 | ch = getchar(); 28 | 29 | // 将所有的输入流都清空 30 | while (getchar() != '\n') 31 | continue; 32 | 33 | // 返回不是空白或标点的字符,即已取到的字符 34 | return ch; 35 | } -------------------------------------------------------------------------------- /exercises/ch08/files/file: -------------------------------------------------------------------------------- 1 | Ishphat the robot 2 | slid open the hatch 3 | and shouted his challenge.& -------------------------------------------------------------------------------- /exercises/ch09/ex01.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/5. 3 | // 4 | #include 5 | 6 | double min(double x, double y); 7 | 8 | int main(void) { 9 | double x, y; 10 | 11 | // 提示用户输入两个double类型的数字 12 | printf("Enter two numbers (q to quit):"); 13 | while (scanf("%lf %lf", &x, &y) == 2) { 14 | // 调用min()函数,返回最小值 15 | printf("The smaller number is %g.\n", min(x, y)); 16 | printf("Next two values (q to quit):"); 17 | } 18 | printf("Bye!\n"); 19 | return 0; 20 | } 21 | 22 | double min(double x, double y) { 23 | return x < y ? x : y; 24 | } 25 | -------------------------------------------------------------------------------- /exercises/ch09/ex02.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/5. 3 | // 4 | #include 5 | 6 | void chline(int ch, int i, int j); 7 | 8 | int main(void) { 9 | int ch; 10 | int i, j; 11 | // 提示用户输入一个字符 12 | printf("Enter a character:"); 13 | while ((ch = getchar()) != '\n') { 14 | // 提示用户输入两个整数 15 | printf("Enter two integers:"); 16 | if (scanf("%d %d", &i, &j) != 2) 17 | break; 18 | // 调用函数,打印j行i列 19 | chline(ch, i, j); 20 | while (getchar() != '\n') 21 | continue; 22 | printf("Enter another character (a newline to quit):"); 23 | } 24 | 25 | printf("Bye.\n"); 26 | return 0; 27 | } 28 | 29 | void chline(int ch, int i, int j) { 30 | int row, col; 31 | 32 | // 使用两个for循环,打印指定字符 33 | for (row = 0; row < j; row++) { 34 | for (col = 0; col < i; col++) 35 | putchar(ch); 36 | putchar('\n'); 37 | } 38 | } -------------------------------------------------------------------------------- /exercises/ch09/ex03.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/5. 3 | // 4 | #include 5 | 6 | void ch_line_row(int ch, int cols, int rows); 7 | 8 | int main(void) { 9 | int ch; 10 | int cols, rows; 11 | 12 | // 提示用户输入一个字符 13 | printf("Enter a character:"); 14 | while ((ch = getchar()) != '\n') { 15 | printf("Enter two integers:"); 16 | if (scanf("%d %d", &cols, &rows) != 2) 17 | break; 18 | // 调用函数打印 19 | ch_line_row(ch, cols, rows); 20 | while (getchar() != '\n') 21 | continue; 22 | printf("\nEnter next character (a newline to to quit):"); 23 | } 24 | 25 | printf("Bye!"); 26 | return 0; 27 | } 28 | 29 | void ch_line_row(int ch, int cols, int rows) { 30 | int i, j; 31 | 32 | // 使用两个for循环,打印指定字符 33 | for (i = 0; i < rows; i++) { 34 | for (j = 0; j < cols; j++) 35 | putchar(ch); 36 | putchar('\n'); 37 | } 38 | } -------------------------------------------------------------------------------- /exercises/ch09/ex04.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/5. 3 | // 4 | #include 5 | 6 | double harmean(double x, double y); 7 | 8 | int main(void) { 9 | double x, y; 10 | 11 | // 提示用户输入两个double类型的数 12 | printf("Enter two double-type number:"); 13 | while (scanf("%lf %lf", &x, &y) == 2) { 14 | // 检查是否为0 15 | if (x == 0 || y == 0) { 16 | printf("Enter two double-type number (other than 0):"); 17 | continue; 18 | } 19 | // 打印结果 20 | printf("The harmean of (%g, %g) is %g\n", x, y, harmean(x, y)); 21 | printf("Enter next double-type number (q to quit):"); 22 | } 23 | 24 | return 0; 25 | } 26 | 27 | double harmean(double x, double y) { 28 | // 计算调和平均数:两个倒数的平均值的倒数 29 | return 1 / ((1.0 / 2.0) * (1 / x + 1 / y)); 30 | } 31 | -------------------------------------------------------------------------------- /exercises/ch09/ex05.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/5. 3 | // 4 | #include 5 | 6 | void larger_of(double *p1, double *p2); 7 | 8 | int main(void) { 9 | double x, y; 10 | 11 | // 提示用户输入两个值 12 | printf("Enter tow number:"); 13 | while (scanf("%lf %lf", &x, &y) == 2) { 14 | larger_of(&x, &y); 15 | printf("The modified values are %g and %g.\n", x, y); 16 | printf("Next two value (q to quit):"); 17 | } 18 | 19 | printf("Bye!"); 20 | return 0; 21 | } 22 | 23 | void larger_of(double *p1, double *p2) { 24 | // 将两个数中较大的值进行重新赋值 25 | *p1 = *p2 = *p1 > *p2 ? *p1 : *p2; 26 | } 27 | -------------------------------------------------------------------------------- /exercises/ch09/ex06.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/5. 3 | // 4 | #include 5 | 6 | void handle(double *p1, double *p2, double *p3); 7 | void swap(double *x, double *y); 8 | 9 | int main(void) { 10 | double x, y, z; 11 | 12 | // 提示用户输入3个double类型的数 13 | printf("Enter three numbers:"); 14 | while (scanf("%lf %lf %lf", &x, &y, &z) == 3) { 15 | // 进行比较交换 16 | handle(&x, &y, &z); 17 | // 打印结果 18 | printf("The values are %g, %g and %g.\n", x, y, z); 19 | printf("Next three values (q to quit):"); 20 | } 21 | 22 | printf("Bye\n"); 23 | return 0; 24 | } 25 | 26 | // 处理三个变量的函数 27 | void handle(double *p1, double *p2, double *p3) { 28 | if (*p1 > *p2) { 29 | swap(p1, p2); 30 | } 31 | if (*p1 > *p3) { 32 | swap(p1, p3); 33 | } 34 | if (*p2 > *p3) { 35 | swap(p2, p3); 36 | } 37 | } 38 | 39 | // 交换位置的函数 40 | void swap(double *x, double *y) { 41 | double t; 42 | 43 | t = *x; 44 | *x = *y; 45 | *y = t; 46 | } -------------------------------------------------------------------------------- /exercises/ch09/ex07.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/5. 3 | // 4 | #include 5 | #include 6 | 7 | int locate(int ch); 8 | 9 | int main(void) { 10 | int ch; 11 | 12 | // 提示用户输入一个字符 13 | printf("Enter a character:"); 14 | while ((ch = getchar()) != '\n') { 15 | // 如果是字母,打印字母在字母表中的位置 16 | if (isalpha(ch)) { 17 | printf("The location of %c in alphabet is %d.\n", ch, locate(ch)); 18 | } 19 | while (getchar() != '\n') 20 | continue; 21 | printf("Enter next character (a newline to quit):"); 22 | } 23 | 24 | printf("Bye"); 25 | return 0; 26 | } 27 | 28 | int locate(int ch) { 29 | // 如果不是字母,返回-1 30 | int index = -1; 31 | 32 | // 如果是字母 33 | if (isalpha(ch)) { 34 | // 将字符转成小写字母,然后减去第一个字母并加上1 35 | index = tolower(ch) - 'a' + 1; 36 | } 37 | return index; 38 | } -------------------------------------------------------------------------------- /exercises/ch11/ex01.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/10. 3 | // 4 | #include 5 | 6 | #define LEN 10 7 | // 获取n个字符 8 | char *get_n_char(char *str, int n); 9 | 10 | int main(void) { 11 | char input[LEN]; 12 | char *ret_val; 13 | int n = LEN - 1; 14 | 15 | printf("Please enter chars:\n"); 16 | ret_val = get_n_char(input, n); 17 | if (ret_val == NULL) { 18 | puts("Input failed."); 19 | } else { 20 | printf("%d chars: ", n); 21 | puts(input); 22 | } 23 | puts("Done."); 24 | 25 | return 0; 26 | } 27 | 28 | char *get_n_char(char *str, int n) { 29 | int i; 30 | char ch; 31 | // for循环n次,取出n个字符,存储到str数组中 32 | for (i = 0; i < n; i++) { 33 | ch = getchar(); 34 | if (ch != EOF) 35 | str[i] = ch; 36 | else 37 | break; 38 | } 39 | 40 | if (ch == EOF) 41 | return NULL; 42 | else { 43 | // 赋给str数组的最后一个元素为空字符 44 | str[i] = '\0'; 45 | return str; 46 | } 47 | } -------------------------------------------------------------------------------- /exercises/ch11/ex13.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/11. 3 | // 4 | #include 5 | 6 | int main(int argc, char **argv) { 7 | int i; 8 | 9 | // 打印命令行参数 10 | printf("The argument values:\n"); 11 | for(i = 1; i < argc; i++) { 12 | printf("%s ", argv[i]); 13 | } 14 | 15 | printf("\nThe reserve arguments values:\n"); 16 | // 根据输入的参数个数,反向打印每个命令行参数 17 | for (i = argc - 1; i > 0; i--) { 18 | if (i == 0) { 19 | printf("\n"); 20 | } else { 21 | printf("%s ", argv[i]); 22 | } 23 | 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /exercises/ch11/ex14.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/11. 3 | // 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char *argv[]) { 9 | double num, exp; 10 | 11 | if (argc != 3) { 12 | // 提示命令用法 13 | printf("Usage: %s number exponent\n", argv[0]); 14 | } else { 15 | // 使用atof函数将命令行参数转为double类型的数 16 | num = atof(argv[1]); 17 | exp = atof(argv[2]); 18 | // 打印幂的结果 19 | printf("%g to the %g power = %g\n", num, exp, pow(num, exp)); 20 | } 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /exercises/ch11/files/file: -------------------------------------------------------------------------------- 1 | Ishphat the robot 2 | slid open the hatch 3 | and shouted his challenge.& -------------------------------------------------------------------------------- /exercises/ch12/ex01.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/14. 3 | // 4 | #include 5 | void critic(int *u); 6 | 7 | int main(void) { 8 | int units; 9 | 10 | // 提示用户输入磅数 11 | printf("How many pounds to a firkin of butter?\n"); 12 | scanf("%d", &units); 13 | while (units != 56) 14 | // 判断用户输入的值 15 | critic(&units); 16 | printf("You must have looked it up!\n"); 17 | 18 | return 0; 19 | } 20 | 21 | void critic(int *u) { 22 | printf("No luck, my friend. Try again.\n"); 23 | // 继续猜测磅数 24 | scanf("%d", u); 25 | } 26 | -------------------------------------------------------------------------------- /exercises/ch12/ex02/pe12-2a.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/14. 3 | // 4 | // 公制 5 | #define METRIC 0 6 | // 美制 7 | #define US 1 8 | 9 | // 设置模式 10 | void set_mode(int mode); 11 | // 根据用户输入的模式提示用户输入相应数据 12 | void get_info(void); 13 | // 根据设置的模式计算并显示油耗 14 | void show_info(void); -------------------------------------------------------------------------------- /exercises/ch12/ex02/pe12-2b.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/14. 3 | // 与pe12-2a.c一起编译 4 | // 5 | #include 6 | #include "pe12-2a.h" 7 | 8 | int main(void) { 9 | int mode; 10 | 11 | printf("Enter 0 for metric mode, 1 for US mode:"); 12 | scanf("%d", &mode); 13 | 14 | while (mode >= 0) { 15 | set_mode(mode); 16 | get_info(); 17 | show_info(); 18 | printf("Enter 0 for metric mode, 1 for US mode"); 19 | printf(" (-1 to quit):"); 20 | scanf("%d", &mode); 21 | } 22 | printf("Done.\n"); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /exercises/ch12/ex03/pe12-3a.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/14. 3 | // 4 | // 公制 5 | #define METRIC 0 6 | // 美制 7 | #define US 1 8 | 9 | // 设置模式 10 | void set_mode(int *mode); 11 | // 根据用户输入的模式提示用户输入相应数据 12 | void get_info(int mode, double *pd, double *pf); 13 | // 根据设置的模式计算并显示油耗 14 | void show_info(int mode, double pd, double pf); -------------------------------------------------------------------------------- /exercises/ch12/ex03/pe12-3b.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/14. 3 | // 4 | #include 5 | #include "pe12-3a.h" 6 | 7 | int main(void) { 8 | int mode; 9 | double distance, fuel; 10 | 11 | // 提示用户输入模式 12 | printf("Enter 0 for metric mode, 1 for US mode:"); 13 | scanf("%d", &mode); 14 | 15 | while (mode >= 0) { 16 | // 设置模式 17 | set_mode(&mode); 18 | // 根据用户输入的模式提示用户输入相应数据 19 | get_info(mode, &distance, &fuel); 20 | // 根据设置的模式计算并显示油耗 21 | show_info(mode, distance, fuel); 22 | // 再次提示用户输入模式 23 | printf("Enter 0 for metric mode, 1 for US mode"); 24 | printf(" (-1 to quit):"); 25 | scanf("%d", &mode); 26 | } 27 | printf("Done.\n"); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /exercises/ch12/ex04.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/14. 3 | // 4 | #include 5 | #define MAX_COUNT 10 6 | void foo(void); 7 | // 使用文件作用域,内部链接 8 | static int count = 0; 9 | 10 | int main(void) { 11 | int i; 12 | // 循环调用foo函数 13 | for (i = 0; i < MAX_COUNT; i++) { 14 | foo(); 15 | // 打印函数调用信息 16 | printf("The foo function is called %d times.\n", count); 17 | } 18 | 19 | return 0; 20 | } 21 | 22 | void foo(void) { 23 | printf("The function called.\n"); 24 | // 计算函数被调用次数 25 | count++; 26 | } -------------------------------------------------------------------------------- /exercises/ch12/ex06/rand.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/14. 3 | // 4 | int rand1(void); 5 | 6 | void srand1(unsigned int seed); 7 | -------------------------------------------------------------------------------- /exercises/ch12/ex06/s_and_r.c: -------------------------------------------------------------------------------- 1 | /* s_and_r.c -- file for rand1() and srand1() */ 2 | /* uses ANSI C portable algorithm */ 3 | #include "rand.h" 4 | 5 | static unsigned long int next = 1; /* the seed */ 6 | 7 | int rand1(void) 8 | { 9 | /* magic formula to generate pseudorandom number */ 10 | next = next * 1103515245 + 12345; 11 | return (unsigned int) (next/65536) % 32768; 12 | } 13 | 14 | void srand1(unsigned int seed) 15 | { 16 | next = seed; 17 | } 18 | -------------------------------------------------------------------------------- /exercises/ch13/files/eddy: -------------------------------------------------------------------------------- 1 | So even Eddy came oven ready. 2 | -------------------------------------------------------------------------------- /exercises/ch13/files/wordy: -------------------------------------------------------------------------------- 1 | The 2 | fabulous 3 | programmer 4 | enchanted 5 | the 6 | large 7 | 7: Hello 8 | 8: World 9 | 9: Relph# 10 | -------------------------------------------------------------------------------- /exercises/ch14/files/baseball_team.dat: -------------------------------------------------------------------------------- 1 | 1 Aubree Munro 7 3 2 1 2 | 2 Ali Aguilar 4 1 1 0 3 | 3 Ally Carda 3 1 0 0 4 | 4 Jessie Joybat 5 2 1 1 5 | 5 Kelsey Stewart 2 1 0 1 6 | 6 Haylie Cleney 3 0 0 1 7 | 7 Janie Reed 3 1 2 1 8 | 8 Monica Abbott 1 1 1 0 9 | 9 Michelle Moultrie 2 1 1 0 10 | 10 Valerie Arioto 3 1 0 1 11 | 11 Rachel Garcia 4 2 1 1 12 | 12 Dejah Mulipola 3 1 0 0 13 | 13 Cat Osterman 2 1 0 0 14 | 14 Bubba Nickles 2 1 0 1 15 | 15 Delaney Spaulding 2 0 1 1 16 | 1 Aubree Munro 8 3 2 1 17 | 2 Ali Aguilar 9 1 1 0 18 | 3 Ally Carda 3 1 0 0 19 | 4 Jessie Joybat 4 2 1 1 20 | 5 Kelsey Stewart 3 1 0 1 21 | 6 Haylie Cleney 5 1 0 1 22 | 7 Janie Reed 4 1 2 1 23 | 8 Monica Abbott 2 1 1 0 24 | 9 Michelle Moultrie 3 1 1 0 25 | 10 Valerie Arioto 2 1 0 1 26 | 7 Janie Reed 5 1 2 1 27 | 8 Monica Abbott 4 1 1 0 28 | 9 Michelle Moultrie 4 1 1 0 29 | 10 Valerie Arioto 6 1 0 1 30 | 11 Rachel Garcia 8 2 2 1 31 | 12 Dejah Mulipola 7 1 0 0 32 | 13 Cat Osterman 5 3 1 0 33 | 14 Bubba Nickles 6 1 3 1 -------------------------------------------------------------------------------- /exercises/ch16/ex02.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/22. 3 | // 4 | #include 5 | // 调和平均数的计算 6 | #define HMEAN(X, Y) (1 / ((1 / (X) + 1 / (Y)) / 2)) 7 | 8 | int main(void) { 9 | double f1, f2, ans; 10 | 11 | // 提示用户输入两个数 12 | printf("Enter a pair of numbers (q to quit):"); 13 | while (scanf("%lf %lf", &f1, &f2) == 2 && f1 != 0 && f2 != 0) { 14 | // 计算两个数的调和平均数 15 | ans = HMEAN(f1, f2); 16 | printf("The harmonic mean of %g %g is %g\n", f1, f2, ans); 17 | // 提示用户再次输入 18 | printf("Enter next pair of numbers (q to quit):"); 19 | } 20 | 21 | printf("Bye.\n"); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /exercises/ch16/ex04.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/22. 3 | // 4 | #include 5 | #include 6 | 7 | void delay(double second); 8 | 9 | int main(void) { 10 | double sec; 11 | 12 | // 提示用户输入延迟的时间(单位秒) 13 | printf("Enter some seconds to delay:"); 14 | while (scanf("%lf", &sec) == 1 && sec != 0) { 15 | // 执行延迟程序 16 | delay(sec); 17 | printf("Enter some seconds to delay (q to quit):"); 18 | } 19 | 20 | printf("Bye."); 21 | return 0; 22 | } 23 | 24 | void delay(double second) { 25 | // 获取系统开始时间 26 | clock_t start = clock(); 27 | printf("Test %lf second delay\n", second); 28 | // 获取系统当前时间,用于计算时间差 29 | clock_t now = clock(); 30 | 31 | while (((double) (now - start)) / CLOCKS_PER_SEC < second) { 32 | // 当每过1秒时,打印一次信息 33 | if ((now - start) % CLOCKS_PER_SEC == 0) { 34 | printf("The delay time is %lf second.\n", ((double) (now - start)) / CLOCKS_PER_SEC); 35 | } 36 | // 重新获取系统当前时间 37 | now = clock(); 38 | } 39 | } -------------------------------------------------------------------------------- /exercises/ch17/ex05/stack.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by HRF on 2021/11/23. 3 | // 4 | #ifndef STACK_H_ 5 | #define STACK_H_ 6 | #include 7 | 8 | typedef char Item; 9 | // 定义栈的上限 10 | #define MAXSTACK 100 11 | 12 | typedef struct stack { 13 | // 使用字符数组 14 | Item items[MAXSTACK]; 15 | // 栈顶的位置 16 | int top; 17 | } Stack; 18 | 19 | // 初始化栈 20 | void InitializeStack(Stack * ps); 21 | 22 | // 确定栈是否已满 23 | bool StackIsFull(const Stack * ps); 24 | 25 | // 确定栈是否为空 26 | bool StackIsEmpty(const Stack *ps); 27 | 28 | // 确定栈的长度 29 | int StackItemCount(const Stack * ps); 30 | 31 | // 把项压入栈 32 | bool Push(Item item, Stack * ps); 33 | 34 | // 把项弹出栈 35 | bool Pop(Item * pitem, Stack * ps); 36 | 37 | // 清空栈 38 | void EmptyTheStack(Stack * ps); 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /exercises/ch17/ex07/wordy: -------------------------------------------------------------------------------- 1 | From fairest creatures we desire increase, 2 | That thereby beauty's rose might never die, 3 | But as the riper should by time decease, 4 | His tender heir might bear his memory; 5 | But thou contracted to thine own bright eyes, 6 | Feeds thy light's flame with self-substantial fuel, 7 | Making a famine where abundance lies, 8 | Thyself thy foe, to thy sweet self too cruel. 9 | Thou that art now the world's fresh ornament, 10 | And only herald to the gaudy spring, 11 | Within thine own bud buriest thy content, 12 | And, tender churl, makes waste in niggarding. 13 | Pity the world, or else this glutton be, 14 | To eat the world's due, by the grave and thee. -------------------------------------------------------------------------------- /resources/CMake_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/resources/CMake_config.png -------------------------------------------------------------------------------- /resources/Toolchains_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Relph1119/c-primer-plus/f41710c461c85715f9890c901f4cbddc062816bd/resources/Toolchains_config.png --------------------------------------------------------------------------------