├── .gitignore ├── .nojekyll ├── LICENSE ├── Makefile ├── README.md ├── _quarto.yml ├── archive └── v1 │ ├── book.epub │ ├── book.pdf │ ├── book.tex │ ├── index.qmd │ └── src │ ├── ch02 │ ├── p01.c │ ├── p02.c │ ├── p03.c │ ├── p04.c │ ├── p05.c │ ├── p06.c │ ├── p07.c │ ├── p08.c │ ├── p09.c │ ├── p10.c │ ├── p11.c │ ├── p12.c │ ├── p13.c │ ├── p14.c │ ├── p15.c │ ├── p16.c │ ├── p17.c │ ├── p18.c │ ├── p19.c │ └── p20.c │ ├── ch03 │ ├── p01.c │ ├── p02.c │ ├── p03.c │ ├── p04.c │ ├── p05.c │ ├── p06.c │ ├── p07.c │ ├── p08.c │ ├── p09.c │ ├── p10.c │ ├── p11.c │ ├── p12.c │ ├── p13.c │ ├── p14.c │ ├── p15.c │ ├── p16.c │ ├── p17.c │ ├── p18.c │ ├── p19.c │ └── p20.c │ ├── ch04 │ ├── p01.c │ ├── p02.c │ ├── p03.c │ ├── p04.c │ ├── p05.c │ ├── p06.c │ ├── p07.c │ ├── p08.c │ ├── p09.c │ ├── p10.c │ ├── p11.c │ ├── p12.c │ ├── p13.c │ ├── p14.c │ ├── p15.c │ ├── p16.c │ ├── p17.c │ ├── p18.c │ ├── p19.c │ └── p20.c │ ├── ch05 │ ├── mathlib.c │ ├── mathlib.h │ ├── p01.c │ ├── p02.c │ ├── p03.c │ ├── p04.c │ ├── p05.c │ ├── p06.c │ ├── p07.c │ ├── p08.c │ ├── p09.c │ ├── p10.c │ ├── p11.c │ ├── p12.c │ ├── p13.c │ ├── p14.c │ ├── p15.c │ ├── p16.c │ ├── p17.c │ ├── p18.c │ ├── p19.c │ ├── p20.c │ ├── shapes.c │ └── shapes.h │ ├── ch06 │ ├── p01.c │ ├── p02.c │ ├── p03.c │ ├── p04.c │ ├── p05.c │ ├── p06.c │ ├── p07.c │ ├── p08.c │ ├── p09.c │ ├── p10.c │ ├── p11.c │ ├── p12.c │ ├── p13.c │ ├── p14.c │ ├── p15.c │ ├── p16.c │ ├── p17.c │ ├── p18.c │ ├── p19.c │ └── p20.c │ ├── ch07 │ ├── p01.c │ ├── p02.c │ ├── p03.c │ ├── p04.c │ ├── p05.c │ ├── p06.c │ ├── p07.c │ ├── p08.c │ ├── p09.c │ ├── p10.c │ ├── p11.c │ ├── p12.c │ ├── p13.c │ ├── p14.c │ ├── p15.c │ ├── p16.c │ ├── p17.c │ ├── p18.c │ ├── p19.c │ └── p20.c │ ├── ch08 │ ├── p01.c │ ├── p02.c │ ├── p03.c │ ├── p04.c │ ├── p05.c │ ├── p06.c │ ├── p07.c │ ├── p08.c │ ├── p09.c │ ├── p10.c │ ├── p11.c │ ├── p12.c │ ├── p13.c │ ├── p14.c │ ├── p15.c │ ├── p16.c │ ├── p17.c │ ├── p18.c │ ├── p19.c │ └── p20.c │ ├── ch09 │ ├── p01.c │ ├── p02.c │ ├── p03.c │ ├── p04.c │ ├── p05.c │ ├── p06.c │ ├── p07.c │ ├── p08.c │ ├── p09.c │ ├── p10.c │ ├── p11.c │ ├── p12.c │ ├── p13.c │ ├── p14.c │ ├── p15.c │ ├── p16.c │ ├── p17.c │ ├── p18.c │ ├── p19.c │ └── p20.c │ ├── ch10 │ ├── p01.c │ ├── p02.c │ ├── p03.c │ ├── p04.c │ ├── p05.c │ ├── p06.c │ ├── p07.c │ ├── p08.c │ ├── p09.c │ ├── p10.c │ ├── p11.c │ ├── p12.c │ ├── p13.c │ ├── p14.c │ ├── p15.c │ ├── p16.c │ ├── p17.c │ ├── p18.c │ ├── p19.c │ └── p20.c │ ├── ch11 │ ├── include │ │ └── p17_mathlib.h │ ├── p01_main.c │ ├── p01_mathlib.c │ ├── p01_mathlib.h │ ├── p06_main.c │ ├── p07_main.c │ ├── p08_main.c │ ├── p09_main.c │ ├── p09_shapes.c │ ├── p09_shapes.h │ ├── p12_config.c │ ├── p12_main.c │ ├── p13_file.h │ ├── p13_file1.c │ ├── p13_file2.c │ ├── p13_main.c │ ├── p15_Makefile │ ├── p16_perf.c │ ├── p18_main.c │ ├── p18_mathlib.h │ ├── p18_shapes.c │ ├── p18_shapes.h │ ├── p19_a.c │ ├── p19_b.c │ ├── p19_bad.c │ ├── p19_bad.h │ ├── p19_good_bad.h │ ├── p19_main.c │ ├── p20_calc.c │ ├── p20_calc.h │ ├── p20_main.c │ └── src │ │ ├── p17_main.c │ │ └── p17_mathlib.c │ └── ch12 │ ├── p01.c │ ├── p02.c │ ├── p03.c │ ├── p04.c │ ├── p05.c │ ├── p06.c │ ├── p07.c │ ├── p08.c │ ├── p09.c │ ├── p10.c │ ├── p11.c │ ├── p12.c │ ├── p13.c │ ├── p14.c │ ├── p15.c │ ├── p16.c │ ├── p17.c │ ├── p18.c │ ├── p19.c │ ├── p20.c │ ├── p21.c │ ├── p22.c │ ├── p23.c │ ├── p24.c │ └── p25.c ├── books └── en-US │ └── book.md ├── docs ├── .nojekyll ├── book-latex │ ├── .nojekyll │ └── book.tex ├── book.epub ├── book.pdf ├── books │ └── en-US │ │ └── book.html ├── index.html ├── search.json └── site_libs │ ├── bootstrap │ ├── bootstrap-27c261d06b905028a18691de25d09dde.min.css │ ├── bootstrap-icons.css │ ├── bootstrap-icons.woff │ └── bootstrap.min.js │ ├── clipboard │ └── clipboard.min.js │ ├── quarto-html │ ├── anchor.min.js │ ├── axe │ │ └── axe-check.js │ ├── popper.min.js │ ├── quarto-syntax-highlighting-1fe81d0376b2c50856e68e651e390326.css │ ├── quarto.js │ ├── tabsets │ │ └── tabsets.js │ ├── tippy.css │ └── tippy.umd.min.js │ ├── quarto-nav │ ├── headroom.min.js │ └── quarto-nav.js │ └── quarto-search │ ├── autocomplete.umd.js │ ├── fuse.min.js │ └── quarto-search.js ├── index.qmd └── releases ├── book.epub ├── book.pdf └── book.tex /.gitignore: -------------------------------------------------------------------------------- 1 | /.quarto/ 2 | **/*.quarto_ipynb 3 | 4 | book.aux 5 | book.toc 6 | texput.log 7 | 8 | .DS_Store 9 | 10 | # Ignore ONLY the top-level site_libs 11 | /site_libs/ 12 | 13 | # Keep the one inside docs (optional safeguards if you already ignore broadly) 14 | !docs/ 15 | !docs/site_libs/ 16 | !docs/site_libs/** -------------------------------------------------------------------------------- /.nojekyll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/little-book-of/c/0e8a33e9852266d417fb85ee48ce577c304e1671/.nojekyll -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Attribution-NonCommercial-ShareAlike 4.0 International 2 | 3 | ======================================================================= 4 | 5 | Creative Commons Corporation ("Creative Commons") is not a law firm and 6 | does not provide legal services or legal advice. Distribution of 7 | Creative Commons public licenses does not create a lawyer-client or 8 | other relationship. Creative Commons makes its licenses and related 9 | information available on an "as-is" basis. Creative Commons gives no 10 | warranties regarding its licenses, any material licensed under their 11 | terms and conditions, or any related information. Creative Commons 12 | disclaims all liability for damages resulting from their use to the 13 | fullest extent possible. 14 | 15 | Using Creative Commons Public Licenses 16 | 17 | Creative Commons public licenses provide a standard set of terms and 18 | conditions that creators and other rights holders may use to share 19 | original works of authorship and other material subject to copyright 20 | and certain other rights specified in the public license below. The 21 | following considerations are for informational purposes only, are not 22 | exhaustive, and do not form part of our licenses. 23 | 24 | Considerations for licensors: Our public licenses are 25 | intended for use by those authorized to give the public 26 | permission to use material in ways otherwise restricted by 27 | copyright and certain other rights. Our licenses are 28 | irrevocable. Licensors should read and understand the terms 29 | and conditions of the license they choose before applying it. 30 | Licensors should also secure all rights necessary before 31 | applying our licenses so that the public can reuse the 32 | material as expected. Licensors should clearly mark any 33 | material not subject to the license. This includes other CC- 34 | licensed material, or material used under an exception or 35 | limitation to copyright. More considerations for licensors: 36 | wiki.creativecommons.org/Considerations_for_licensors 37 | 38 | Considerations for the public: By using one of our public 39 | licenses, a licensor grants the public permission to use the 40 | licensed material under specified terms and conditions. If 41 | the licensor's permission is not necessary for any reason--for 42 | example, because of any applicable exception or limitation to 43 | copyright--then that use is not regulated by the license. Our 44 | licenses grant only permissions under copyright and certain 45 | other rights that a licensor has authority to grant. Use of 46 | the licensed material may still be restricted for other 47 | reasons, including because others have copyright or other 48 | rights in the material. A licensor may make special requests, 49 | such as asking that all changes be marked or described. 50 | Although not required by our licenses, you are encouraged to 51 | respect those requests where reasonable. More considerations 52 | for the public: 53 | wiki.creativecommons.org/Considerations_for_licensees 54 | 55 | ======================================================================= 56 | 57 | Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International 58 | Public License 59 | 60 | By exercising the Licensed Rights (defined below), You accept and agree 61 | to be bound by the terms and conditions of this Creative Commons 62 | Attribution-NonCommercial-ShareAlike 4.0 International Public License 63 | ("Public License"). To the extent this Public License may be 64 | interpreted as a contract, You are granted the Licensed Rights in 65 | consideration of Your acceptance of these terms and conditions, and the 66 | Licensor grants You such rights in consideration of benefits the 67 | Licensor receives from making the Licensed Material available under 68 | these terms and conditions. 69 | 70 | 71 | Section 1 -- Definitions. 72 | 73 | a. Adapted Material means material subject to Copyright and Similar 74 | Rights that is derived from or based upon the Licensed Material 75 | and in which the Licensed Material is translated, altered, 76 | arranged, transformed, or otherwise modified in a manner requiring 77 | permission under the Copyright and Similar Rights held by the 78 | Licensor. For purposes of this Public License, where the Licensed 79 | Material is a musical work, performance, or sound recording, 80 | Adapted Material is always produced where the Licensed Material is 81 | synched in timed relation with a moving image. 82 | 83 | b. Adapter's License means the license You apply to Your Copyright 84 | and Similar Rights in Your contributions to Adapted Material in 85 | accordance with the terms and conditions of this Public License. 86 | 87 | c. BY-NC-SA Compatible License means a license listed at 88 | creativecommons.org/compatiblelicenses, approved by Creative 89 | Commons as essentially the equivalent of this Public License. 90 | 91 | d. Copyright and Similar Rights means copyright and/or similar rights 92 | closely related to copyright including, without limitation, 93 | performance, broadcast, sound recording, and Sui Generis Database 94 | Rights, without regard to how the rights are labeled or 95 | categorized. For purposes of this Public License, the rights 96 | specified in Section 2(b)(1)-(2) are not Copyright and Similar 97 | Rights. 98 | 99 | e. Effective Technological Measures means those measures that, in the 100 | absence of proper authority, may not be circumvented under laws 101 | fulfilling obligations under Article 11 of the WIPO Copyright 102 | Treaty adopted on December 20, 1996, and/or similar international 103 | agreements. 104 | 105 | f. Exceptions and Limitations means fair use, fair dealing, and/or 106 | any other exception or limitation to Copyright and Similar Rights 107 | that applies to Your use of the Licensed Material. 108 | 109 | g. License Elements means the license attributes listed in the name 110 | of a Creative Commons Public License. The License Elements of this 111 | Public License are Attribution, NonCommercial, and ShareAlike. 112 | 113 | h. Licensed Material means the artistic or literary work, database, 114 | or other material to which the Licensor applied this Public 115 | License. 116 | 117 | i. Licensed Rights means the rights granted to You subject to the 118 | terms and conditions of this Public License, which are limited to 119 | all Copyright and Similar Rights that apply to Your use of the 120 | Licensed Material and that the Licensor has authority to license. 121 | 122 | j. Licensor means the individual(s) or entity(ies) granting rights 123 | under this Public License. 124 | 125 | k. NonCommercial means not primarily intended for or directed towards 126 | commercial advantage or monetary compensation. For purposes of 127 | this Public License, the exchange of the Licensed Material for 128 | other material subject to Copyright and Similar Rights by digital 129 | file-sharing or similar means is NonCommercial provided there is 130 | no payment of monetary compensation in connection with the 131 | exchange. 132 | 133 | l. Share means to provide material to the public by any means or 134 | process that requires permission under the Licensed Rights, such 135 | as reproduction, public display, public performance, distribution, 136 | dissemination, communication, or importation, and to make material 137 | available to the public including in ways that members of the 138 | public may access the material from a place and at a time 139 | individually chosen by them. 140 | 141 | m. Sui Generis Database Rights means rights other than copyright 142 | resulting from Directive 96/9/EC of the European Parliament and of 143 | the Council of 11 March 1996 on the legal protection of databases, 144 | as amended and/or succeeded, as well as other essentially 145 | equivalent rights anywhere in the world. 146 | 147 | n. You means the individual or entity exercising the Licensed Rights 148 | under this Public License. Your has a corresponding meaning. 149 | 150 | 151 | Section 2 -- Scope. 152 | 153 | a. License grant. 154 | 155 | 1. Subject to the terms and conditions of this Public License, 156 | the Licensor hereby grants You a worldwide, royalty-free, 157 | non-sublicensable, non-exclusive, irrevocable license to 158 | exercise the Licensed Rights in the Licensed Material to: 159 | 160 | a. reproduce and Share the Licensed Material, in whole or 161 | in part, for NonCommercial purposes only; and 162 | 163 | b. produce, reproduce, and Share Adapted Material for 164 | NonCommercial purposes only. 165 | 166 | 2. Exceptions and Limitations. For the avoidance of doubt, where 167 | Exceptions and Limitations apply to Your use, this Public 168 | License does not apply, and You do not need to comply with 169 | its terms and conditions. 170 | 171 | 3. Term. The term of this Public License is specified in Section 172 | 6(a). 173 | 174 | 4. Media and formats; technical modifications allowed. The 175 | Licensor authorizes You to exercise the Licensed Rights in 176 | all media and formats whether now known or hereafter created, 177 | and to make technical modifications necessary to do so. The 178 | Licensor waives and/or agrees not to assert any right or 179 | authority to forbid You from making technical modifications 180 | necessary to exercise the Licensed Rights, including 181 | technical modifications necessary to circumvent Effective 182 | Technological Measures. For purposes of this Public License, 183 | simply making modifications authorized by this Section 2(a) 184 | (4) never produces Adapted Material. 185 | 186 | 5. Downstream recipients. 187 | 188 | a. Offer from the Licensor -- Licensed Material. Every 189 | recipient of the Licensed Material automatically 190 | receives an offer from the Licensor to exercise the 191 | Licensed Rights under the terms and conditions of this 192 | Public License. 193 | 194 | b. Additional offer from the Licensor -- Adapted Material. 195 | Every recipient of Adapted Material from You 196 | automatically receives an offer from the Licensor to 197 | exercise the Licensed Rights in the Adapted Material 198 | under the conditions of the Adapter's License You apply. 199 | 200 | c. No downstream restrictions. You may not offer or impose 201 | any additional or different terms or conditions on, or 202 | apply any Effective Technological Measures to, the 203 | Licensed Material if doing so restricts exercise of the 204 | Licensed Rights by any recipient of the Licensed 205 | Material. 206 | 207 | 6. No endorsement. Nothing in this Public License constitutes or 208 | may be construed as permission to assert or imply that You 209 | are, or that Your use of the Licensed Material is, connected 210 | with, or sponsored, endorsed, or granted official status by, 211 | the Licensor or others designated to receive attribution as 212 | provided in Section 3(a)(1)(A)(i). 213 | 214 | b. Other rights. 215 | 216 | 1. Moral rights, such as the right of integrity, are not 217 | licensed under this Public License, nor are publicity, 218 | privacy, and/or other similar personality rights; however, to 219 | the extent possible, the Licensor waives and/or agrees not to 220 | assert any such rights held by the Licensor to the limited 221 | extent necessary to allow You to exercise the Licensed 222 | Rights, but not otherwise. 223 | 224 | 2. Patent and trademark rights are not licensed under this 225 | Public License. 226 | 227 | 3. To the extent possible, the Licensor waives any right to 228 | collect royalties from You for the exercise of the Licensed 229 | Rights, whether directly or through a collecting society 230 | under any voluntary or waivable statutory or compulsory 231 | licensing scheme. In all other cases the Licensor expressly 232 | reserves any right to collect such royalties, including when 233 | the Licensed Material is used other than for NonCommercial 234 | purposes. 235 | 236 | 237 | Section 3 -- License Conditions. 238 | 239 | Your exercise of the Licensed Rights is expressly made subject to the 240 | following conditions. 241 | 242 | a. Attribution. 243 | 244 | 1. If You Share the Licensed Material (including in modified 245 | form), You must: 246 | 247 | a. retain the following if it is supplied by the Licensor 248 | with the Licensed Material: 249 | 250 | i. identification of the creator(s) of the Licensed 251 | Material and any others designated to receive 252 | attribution, in any reasonable manner requested by 253 | the Licensor (including by pseudonym if 254 | designated); 255 | 256 | ii. a copyright notice; 257 | 258 | iii. a notice that refers to this Public License; 259 | 260 | iv. a notice that refers to the disclaimer of 261 | warranties; 262 | 263 | v. a URI or hyperlink to the Licensed Material to the 264 | extent reasonably practicable; 265 | 266 | b. indicate if You modified the Licensed Material and 267 | retain an indication of any previous modifications; and 268 | 269 | c. indicate the Licensed Material is licensed under this 270 | Public License, and include the text of, or the URI or 271 | hyperlink to, this Public License. 272 | 273 | 2. You may satisfy the conditions in Section 3(a)(1) in any 274 | reasonable manner based on the medium, means, and context in 275 | which You Share the Licensed Material. For example, it may be 276 | reasonable to satisfy the conditions by providing a URI or 277 | hyperlink to a resource that includes the required 278 | information. 279 | 3. If requested by the Licensor, You must remove any of the 280 | information required by Section 3(a)(1)(A) to the extent 281 | reasonably practicable. 282 | 283 | b. ShareAlike. 284 | 285 | In addition to the conditions in Section 3(a), if You Share 286 | Adapted Material You produce, the following conditions also apply. 287 | 288 | 1. The Adapter's License You apply must be a Creative Commons 289 | license with the same License Elements, this version or 290 | later, or a BY-NC-SA Compatible License. 291 | 292 | 2. You must include the text of, or the URI or hyperlink to, the 293 | Adapter's License You apply. You may satisfy this condition 294 | in any reasonable manner based on the medium, means, and 295 | context in which You Share Adapted Material. 296 | 297 | 3. You may not offer or impose any additional or different terms 298 | or conditions on, or apply any Effective Technological 299 | Measures to, Adapted Material that restrict exercise of the 300 | rights granted under the Adapter's License You apply. 301 | 302 | 303 | Section 4 -- Sui Generis Database Rights. 304 | 305 | Where the Licensed Rights include Sui Generis Database Rights that 306 | apply to Your use of the Licensed Material: 307 | 308 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right 309 | to extract, reuse, reproduce, and Share all or a substantial 310 | portion of the contents of the database for NonCommercial purposes 311 | only; 312 | 313 | b. if You include all or a substantial portion of the database 314 | contents in a database in which You have Sui Generis Database 315 | Rights, then the database in which You have Sui Generis Database 316 | Rights (but not its individual contents) is Adapted Material, 317 | including for purposes of Section 3(b); and 318 | 319 | c. You must comply with the conditions in Section 3(a) if You Share 320 | all or a substantial portion of the contents of the database. 321 | 322 | For the avoidance of doubt, this Section 4 supplements and does not 323 | replace Your obligations under this Public License where the Licensed 324 | Rights include other Copyright and Similar Rights. 325 | 326 | 327 | Section 5 -- Disclaimer of Warranties and Limitation of Liability. 328 | 329 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE 330 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS 331 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF 332 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, 333 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, 334 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR 335 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, 336 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT 337 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT 338 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. 339 | 340 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE 341 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, 342 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, 343 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, 344 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR 345 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN 346 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR 347 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR 348 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. 349 | 350 | c. The disclaimer of warranties and limitation of liability provided 351 | above shall be interpreted in a manner that, to the extent 352 | possible, most closely approximates an absolute disclaimer and 353 | waiver of all liability. 354 | 355 | 356 | Section 6 -- Term and Termination. 357 | 358 | a. This Public License applies for the term of the Copyright and 359 | Similar Rights licensed here. However, if You fail to comply with 360 | this Public License, then Your rights under this Public License 361 | terminate automatically. 362 | 363 | b. Where Your right to use the Licensed Material has terminated under 364 | Section 6(a), it reinstates: 365 | 366 | 1. automatically as of the date the violation is cured, provided 367 | it is cured within 30 days of Your discovery of the 368 | violation; or 369 | 370 | 2. upon express reinstatement by the Licensor. 371 | 372 | For the avoidance of doubt, this Section 6(b) does not affect any 373 | right the Licensor may have to seek remedies for Your violations 374 | of this Public License. 375 | 376 | c. For the avoidance of doubt, the Licensor may also offer the 377 | Licensed Material under separate terms or conditions or stop 378 | distributing the Licensed Material at any time; however, doing so 379 | will not terminate this Public License. 380 | 381 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public 382 | License. 383 | 384 | 385 | Section 7 -- Other Terms and Conditions. 386 | 387 | a. The Licensor shall not be bound by any additional or different 388 | terms or conditions communicated by You unless expressly agreed. 389 | 390 | b. Any arrangements, understandings, or agreements regarding the 391 | Licensed Material not stated herein are separate from and 392 | independent of the terms and conditions of this Public License. 393 | 394 | 395 | Section 8 -- Interpretation. 396 | 397 | a. For the avoidance of doubt, this Public License does not, and 398 | shall not be interpreted to, reduce, limit, restrict, or impose 399 | conditions on any use of the Licensed Material that could lawfully 400 | be made without permission under this Public License. 401 | 402 | b. To the extent possible, if any provision of this Public License is 403 | deemed unenforceable, it shall be automatically reformed to the 404 | minimum extent necessary to make it enforceable. If the provision 405 | cannot be reformed, it shall be severed from this Public License 406 | without affecting the enforceability of the remaining terms and 407 | conditions. 408 | 409 | c. No term or condition of this Public License will be waived and no 410 | failure to comply consented to unless expressly agreed to by the 411 | Licensor. 412 | 413 | d. Nothing in this Public License constitutes or may be interpreted 414 | as a limitation upon, or waiver of, any privileges and immunities 415 | that apply to the Licensor or You, including from the legal 416 | processes of any jurisdiction or authority. 417 | 418 | ======================================================================= 419 | 420 | Creative Commons is not a party to its public 421 | licenses. Notwithstanding, Creative Commons may elect to apply one of 422 | its public licenses to material it publishes and in those instances 423 | will be considered the “Licensor.” The text of the Creative Commons 424 | public licenses is dedicated to the public domain under the CC0 Public 425 | Domain Dedication. Except for the limited purpose of indicating that 426 | material is shared under a Creative Commons public license or as 427 | otherwise permitted by the Creative Commons policies published at 428 | creativecommons.org/policies, Creative Commons does not authorize the 429 | use of the trademark "Creative Commons" or any other trademark or logo 430 | of Creative Commons without its prior written consent including, 431 | without limitation, in connection with any unauthorized modifications 432 | to any of its public licenses or any other arrangements, 433 | understandings, or agreements concerning use of licensed material. For 434 | the avoidance of doubt, this paragraph does not form part of the 435 | public licenses. 436 | 437 | Creative Commons may be contacted at creativecommons.org. 438 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Quarto book builds 2 | # 3 | # Usage: 4 | # make help 5 | # make release 6 | # make preview 7 | # make clean 8 | 9 | QUARTO = quarto 10 | RELEASE_DIR = releases 11 | BUILD_DIR = docs 12 | 13 | .PHONY: all preview render release clean help 14 | 15 | # Default target 16 | all: release ## Default: build and copy distributables 17 | 18 | preview: ## Start Quarto live preview 19 | $(QUARTO) preview 20 | 21 | render: ## Render all formats with Quarto (HTML/PDF/EPUB/TeX as configured) 22 | $(QUARTO) render 23 | 24 | release: clean render ## Clean, render, and copy distributables to releases/ 25 | mkdir -p $(RELEASE_DIR) 26 | cp -f $(BUILD_DIR)/book-latex/book.tex $(RELEASE_DIR)/ 27 | cp -f $(BUILD_DIR)/book.epub $(RELEASE_DIR)/ 28 | cp -f $(BUILD_DIR)/book.pdf $(RELEASE_DIR)/ 29 | 30 | clean: ## Remove old build artifacts 31 | rm -rf $(BUILD_DIR)/* $(RELEASE_DIR)/* 32 | 33 | help: ## Show this help (auto-discovered from targets) 34 | @printf "Targets:\n" 35 | @awk 'BEGIN {FS":.*##"} \ 36 | /^[a-zA-Z0-9_.-]+:.*##/ {printf " \033[36m%-12s\033[0m %s\n", $$1, $$2}' \ 37 | $(MAKEFILE_LIST) | sort 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # The Little Book of C 2 | 3 | This is _The Little Book of C_ 4 | 5 | ## Formats 6 | 7 | - [Download PDF](releases/book.pdf) - print-ready 8 | - [Download EPUB](releases/book.epub) - e-reader friendly 9 | - [View LaTeX](releases/book.tex) - `.tex` source 10 | - [Read on GitHub Pages](https://little-book-of.github.io/c/) - online website 11 | 12 | ## Build it yourself (Quarto) 13 | 14 | We use [Quarto](https://quarto.org/docs/get-started/) to generate all outputs. 15 | 16 | Preview locally: 17 | 18 | ```bash 19 | quarto preview 20 | ``` 21 | 22 | Render outputs: 23 | 24 | ```bash 25 | # All configured formats 26 | quarto render 27 | 28 | # Individual formats 29 | quarto render --to html # site into docs/ 30 | quarto render --to pdf # docs/book.pdf 31 | quarto render --to epub # docs/book.epub 32 | quarto render --to latex # docs/book-latex/book.tex 33 | ``` 34 | 35 | ## Contributing 36 | 37 | We welcome contributions: fixing typos, clarifying sections, adding diagrams, or 38 | improving examples. 39 | 40 | ## Citation 41 | 42 | Nguyen, Duc-Tam (2025). _The Little Book of Maths_ 43 | 44 | ``` 45 | @book{Nguyen2025TLBoC, 46 | author = {Duc-Tam Nguyen}, 47 | title = {The Little Book of Maths}, 48 | year = {2025}, 49 | url = {https://github.com/little-book-of/c} 50 | } 51 | ``` 52 | 53 | ## License 54 | 55 | Licensed under **CC BY-NC-SA 4.0**. See [LICENSE](LICENSE) for details. 56 | -------------------------------------------------------------------------------- /_quarto.yml: -------------------------------------------------------------------------------- 1 | project: 2 | type: book 3 | output-dir: docs 4 | 5 | book: 6 | title: &book_title "The Little Book of C" 7 | subtitle: &book_subtitle "Version 0.3.2" 8 | author: &book_author "Duc-Tam Nguyen" 9 | date: &book_date "2025-10-05" 10 | output-file: book 11 | chapters: 12 | - index.qmd 13 | - books/en-US/book.md 14 | 15 | website: 16 | title: *book_title 17 | page-navigation: true 18 | navbar: 19 | search: true 20 | right: 21 | - text: *book_subtitle 22 | - icon: github 23 | href: https://github.com/little-book-of/c 24 | aria-label: GitHub 25 | 26 | format: 27 | html: 28 | sidebar: true 29 | toc: true 30 | toc-location: left 31 | toc-depth: 3 32 | number-sections: false 33 | theme: [cosmo, brand] 34 | from: markdown+autolink_bare_uris 35 | epub: 36 | title: *book_title 37 | subtitle: *book_subtitle 38 | author: *book_author 39 | date: *book_date 40 | toc: true 41 | toc-depth: 3 42 | number-sections: false 43 | pdf: 44 | title: *book_title 45 | subtitle: *book_subtitle 46 | author: *book_author 47 | date: *book_date 48 | toc: true 49 | toc-depth: 3 50 | number-sections: false 51 | colorlinks: true 52 | latex: 53 | number-sections: false 54 | -------------------------------------------------------------------------------- /archive/v1/book.epub: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/little-book-of/c/0e8a33e9852266d417fb85ee48ce577c304e1671/archive/v1/book.epub -------------------------------------------------------------------------------- /archive/v1/book.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/little-book-of/c/0e8a33e9852266d417fb85ee48ce577c304e1671/archive/v1/book.pdf -------------------------------------------------------------------------------- /archive/v1/src/ch02/p01.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | char name[50]; 5 | int age; 6 | char fav; 7 | 8 | printf("Enter your name: "); 9 | scanf("%49s", name); 10 | 11 | printf("Enter your age: "); 12 | scanf("%d", &age); 13 | 14 | printf("Enter your favorite character: "); 15 | scanf(" %c", &fav); 16 | 17 | printf("\nHello %s! You are %d years old and your favorite character is %c.\n", 18 | name, age, fav); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p02.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | float c; 5 | printf("Celsius: "); 6 | scanf("%f", &c); 7 | float f = c * 9.0f/5.0f + 32.0f; 8 | printf("Fahrenheit: %.2f\n", f); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p03.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int length, width; 5 | printf("Length and width: "); 6 | scanf("%d %d", &length, &width); 7 | int area = length * width; 8 | int peri = 2 * (length + width); 9 | printf("Area = %d, Perimeter = %d\n", area, peri); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p04.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | const float PI = 3.14159f; 5 | float r; 6 | printf("Radius: "); 7 | scanf("%f", &r); 8 | float area = PI * r * r; 9 | float circ = 2.0f * PI * r; 10 | printf("Area = %.4f, Circumference = %.4f\n", area, circ); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p05.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | #if __STDC_VERSION__ >= 202000L 5 | constexpr int MIN_PER_HOUR = 60; 6 | constexpr int HOURS_PER_DAY = 24; 7 | constexpr int DAYS_PER_WEEK = 7; 8 | constexpr int MINUTES_IN_WEEK = MIN_PER_HOUR * HOURS_PER_DAY * DAYS_PER_WEEK; 9 | #else 10 | /* Fallback for older compilers */ 11 | const int MIN_PER_HOUR = 60, HOURS_PER_DAY = 24, DAYS_PER_WEEK = 7; 12 | const int MINUTES_IN_WEEK = MIN_PER_HOUR * HOURS_PER_DAY * DAYS_PER_WEEK; 13 | #endif 14 | printf("Minutes in a week: %d\n", MINUTES_IN_WEEK); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p06.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a, b, t; 5 | printf("Enter two integers: "); 6 | scanf("%d %d", &a, &b); 7 | printf("Before: a=%d, b=%d\n", a, b); 8 | t = a; a = b; b = t; 9 | printf("After : a=%d, b=%d\n", a, b); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p07.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | char c; 5 | printf("Enter a character: "); 6 | scanf(" %c", &c); 7 | printf("You entered: %c\nASCII code: %d\n", c, (int)c); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p08.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | char name[50]; 5 | printf("Enter your name: "); 6 | scanf("%49s", name); 7 | printf("Hello, %s! Nice to meet you.\n", name); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p09.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | float h, w; 5 | printf("Height (m) and Weight (kg): "); 6 | scanf("%f %f", &h, &w); 7 | float bmi = w / (h * h); 8 | printf("BMI = %.2f\n", bmi); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p10.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a, b, c; 5 | printf("Enter three integers: "); 6 | scanf("%d %d %d", &a, &b, &c); 7 | int sum = a + b + c; 8 | float avg = sum / 3.0f; 9 | printf("Sum = %d, Average = %.2f\n", sum, avg); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p11.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int days; 5 | printf("Days: "); 6 | scanf("%d", &days); 7 | int hours = days * 24; 8 | int minutes = hours * 60; 9 | printf("%d days = %d hours = %d minutes\n", days, hours, minutes); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p12.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | char a, b, c; 5 | printf("Enter your initials (e.g., A B C): "); 6 | scanf(" %c %c %c", &a, &b, &c); 7 | printf("Your initials are: %c%c%c\n", a, b, c); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p13.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | const float PI = 3.14159f; 5 | float r1, r2; 6 | printf("Enter r1 and r2: "); 7 | scanf("%f %f", &r1, &r2); 8 | float a1 = PI * r1 * r1, a2 = PI * r2 * r2; 9 | if (a1 > a2) printf("Circle 1 larger (%.2f > %.2f)\n", a1, a2); 10 | else if (a2 > a1) printf("Circle 2 larger (%.2f > %.2f)\n", a2, a1); 11 | else printf("Areas are equal (%.2f)\n", a1); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p14.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int grade; 5 | printf("Grade (0-100): "); 6 | scanf("%d", &grade); 7 | printf("%s\n", grade >= 50 ? "Pass" : "Fail"); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p15.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int year = 2025; 5 | float height = 1.72f; 6 | char grade = 'A'; 7 | char name[] = "Alice"; 8 | 9 | printf("Name: %s\nYear: %d\nHeight: %.2f m\nGrade: %c\n", 10 | name, year, height, grade); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int years; 5 | printf("Age in years: "); 6 | scanf("%d", &years); 7 | long long days = (long long)years * 365; 8 | long long seconds = days * 24LL * 60LL * 60LL; 9 | printf("Approx seconds lived: %lld\n", seconds); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p17.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n; 5 | printf("Favorite integer: "); 6 | scanf("%d", &n); 7 | long long sq = (long long)n * n; 8 | long long cube = sq * n; 9 | printf("%d squared = %lld, cubed = %lld\n", n, sq, cube); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p18.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | char name[50]; 6 | printf("Enter your name: "); 7 | scanf("%49s", name); 8 | 9 | size_t w = strlen(name) + 4; // "* " + name + " *" 10 | for (size_t i = 0; i < w; ++i) putchar('*'); 11 | putchar('\n'); 12 | printf("* %s *\n", name); 13 | for (size_t i = 0; i < w; ++i) putchar('*'); 14 | putchar('\n'); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p19.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) { 6 | printf("int: %d .. %d\n", INT_MIN, INT_MAX); 7 | printf("uint: 0 .. %u\n", UINT_MAX); 8 | printf("float: %e .. %e\n", FLT_MIN, FLT_MAX); 9 | printf("double: %e .. %e\n", DBL_MIN, DBL_MAX); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch02/p20.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | const float PI = 3.14159f; 5 | 6 | int age; 7 | float height; 8 | char initial; 9 | char name[50]; 10 | 11 | printf("Age: "); 12 | scanf("%d", &age); 13 | 14 | printf("Height (m): "); 15 | scanf("%f", &height); 16 | 17 | printf("Initial: "); 18 | scanf(" %c", &initial); 19 | 20 | printf("Name: "); 21 | scanf("%49s", name); 22 | 23 | printf("\n--- Mini Profile ---\n"); 24 | printf("Name : %s\n", name); 25 | printf("Initial: %c\n", initial); 26 | printf("Age : %d\n", age); 27 | printf("Height : %.2f m\n", height); 28 | printf("(Pi const for fun) PI = %.5f\n", PI); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p01.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a, b; 5 | scanf("%d %d", &a, &b); 6 | 7 | printf("%d + %d = %d\n", a, b, a + b); 8 | printf("%d - %d = %d\n", a, b, a - b); 9 | printf("%d * %d = %d\n", a, b, a * b); 10 | printf("%d / %d = %d\n", a, b, a / b); 11 | printf("%d %% %d = %d\n", a, b, a % b); 12 | printf("%d / %d = %.6f\n", a, b, (double)a / b); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p02.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n; 5 | scanf("%d", &n); 6 | long long sq = (long long)n * n; 7 | long long cu = sq * n; 8 | printf("square=%lld cube=%lld\n", sq, cu); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p03.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a, b; 5 | scanf("%d %d", &a, &b); 6 | int avg_int = (a + b) / 2; 7 | double avg_f = (a + b) / 2.0; 8 | printf("avg_int=%d\n", avg_int); 9 | printf("avg_float=%.6f\n", avg_f); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p04.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n; 5 | scanf("%d", &n); 6 | printf("%d\n", n % 2); // 0 even, 1 odd (for non-negative n) 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p05.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | printf("%d\n", 5 + 2 * 3); 5 | printf("%d\n", (5 + 2) * 3); 6 | printf("%d\n", 10 - 4 + 2 * 3); 7 | printf("%d\n", (10 - 4 + 2) * 3); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p06.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int x = 10, y; 5 | scanf("%d", &y); 6 | x += y; printf("x+=y -> %d\n", x); 7 | x -= y; printf("x-=y -> %d\n", x); 8 | x *= y; printf("x*=y -> %d\n", x); 9 | x /= y; printf("x/=y -> %d\n", x); 10 | x %= y; printf("x%%=y -> %d\n", x); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p07.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a, b; 5 | scanf("%d %d", &a, &b); 6 | printf("a==b -> %d\n", a == b); 7 | printf("a!=b -> %d\n", a != b); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p08.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a, b; 5 | scanf("%d %d", &a, &b); 6 | printf("a>b -> %d\n", a > b); 7 | printf("a %d\n", a < b); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p09.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n; 5 | scanf("%d", &n); 6 | printf("%d\n", (n >= 1) && (n <= 100)); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p10.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a, b; 5 | scanf("%d %d", &a, &b); 6 | printf("%d\n", (a > 10) || (b > 10)); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p11.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n; 5 | scanf("%d", &n); 6 | int p = (n > 0); 7 | printf("n>0 -> %d\n", p); 8 | printf("!(n>0) -> %d\n", !p); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p12.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | char c; 5 | scanf(" %c", &c); 6 | printf("char=%c code=%d next=%c prev=%c\n", c, (int)c, c + 1, c - 1); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p13.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | char c; 5 | scanf(" %c", &c); 6 | int is_upper = (c >= 'A' && c <= 'Z'); 7 | int is_lower = (c >= 'a' && c <= 'z'); 8 | printf("is_upper=%d is_lower=%d\n", is_upper, is_lower); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p14.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | char c; 5 | scanf(" %c", &c); 6 | printf("%d\n", (c >= '0' && c <= '9')); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p15.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | char c; 5 | scanf(" %c", &c); 6 | printf("%c\n", c + 3); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a, b, c; 5 | scanf("%d %d %d", &a, &b, &c); 6 | printf("%d\n", a + b * c); 7 | printf("%d\n", (a + b) * c); 8 | printf("%d\n", a * b + c); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p17.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n; 5 | scanf("%d", &n); 6 | printf("%d\n", n); 7 | n += 1; printf("%d\n", n); 8 | n += 5; printf("%d\n", n); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p18.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a, b; 5 | scanf("%d %d", &a, &b); 6 | int both_pos = (a > 0 && b > 0); 7 | int any_pos = (a > 0 || b > 0); 8 | int not_both = !both_pos; 9 | printf("both_pos=%d any_pos=%d not_both=%d\n", both_pos, any_pos, not_both); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p19.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | char A='A',B='B',C='C',D='D',E='E',F='F',G='G',H='H',I='I',J='J',K='K',L='L', 5 | M='M',N='N',O='O',P='P',Q='Q',R='R',S='S',T='T',U='U',V='V',W='W',X='X',Y='Y',Z='Z'; 6 | printf("%c %d\n", A, A); printf("%c %d\n", B, B); printf("%c %d\n", C, C); 7 | printf("%c %d\n", D, D); printf("%c %d\n", E, E); printf("%c %d\n", F, F); 8 | printf("%c %d\n", G, G); printf("%c %d\n", H, H); printf("%c %d\n", I, I); 9 | printf("%c %d\n", J, J); printf("%c %d\n", K, K); printf("%c %d\n", L, L); 10 | printf("%c %d\n", M, M); printf("%c %d\n", N, N); printf("%c %d\n", O, O); 11 | printf("%c %d\n", P, P); printf("%c %d\n", Q, Q); printf("%c %d\n", R, R); 12 | printf("%c %d\n", S, S); printf("%c %d\n", T, T); printf("%c %d\n", U, U); 13 | printf("%c %d\n", V, V); printf("%c %d\n", W, W); printf("%c %d\n", X, X); 14 | printf("%c %d\n", Y, Y); printf("%c %d\n", Z, Z); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /archive/v1/src/ch03/p20.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a, b; 5 | scanf("%d %d", &a, &b); 6 | printf("%d + %d = %d\n", a, b, a + b); 7 | printf("%d - %d = %d\n", a, b, a - b); 8 | printf("%d * %d = %d\n", a, b, a * b); 9 | printf("%d / %d = %d\n", a, b, a / b); 10 | printf("%d %% %d = %d\n", a, b, a % b); 11 | printf("%d / %d = %.6f\n", a, b, (double)a / b); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p01.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n; 5 | scanf("%d", &n); 6 | if (n > 0) printf("Positive\n"); 7 | else if (n < 0) printf("Negative\n"); 8 | else printf("Zero\n"); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p02.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a, b; 5 | scanf("%d %d", &a, &b); 6 | if (a > b) printf("%d\n", a); 7 | else if (b > a) printf("%d\n", b); 8 | else printf("Equal\n"); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p03.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int score; 5 | scanf("%d", &score); 6 | if (score >= 90) printf("A\n"); 7 | else if (score >= 75) printf("B\n"); 8 | else if (score >= 50) printf("C\n"); 9 | else printf("F\n"); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p04.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | float c; 5 | scanf("%f", &c); 6 | if (c < 10.0f) printf("Cold\n"); 7 | else if (c <= 25.0f) printf("Warm\n"); 8 | else printf("Hot\n"); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p05.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a, b; 5 | char op; 6 | scanf("%d %d %c", &a, &b, &op); 7 | switch (op) { 8 | case '+': printf("%d\n", a + b); break; 9 | case '-': printf("%d\n", a - b); break; 10 | case '*': printf("%d\n", a * b); break; 11 | case '/': 12 | if (b == 0) printf("Error: divide by zero\n"); 13 | else printf("%d\n", a / b); 14 | break; 15 | default: printf("Unknown operator\n"); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p06.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int d; 5 | scanf("%d", &d); 6 | switch (d) { 7 | case 0: puts("zero"); break; 8 | case 1: puts("one"); break; 9 | case 2: puts("two"); break; 10 | case 3: puts("three"); break; 11 | case 4: puts("four"); break; 12 | case 5: puts("five"); break; 13 | case 6: puts("six"); break; 14 | case 7: puts("seven"); break; 15 | case 8: puts("eight"); break; 16 | case 9: puts("nine"); break; 17 | default: puts("not a digit"); 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p07.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | char c; 5 | scanf(" %c", &c); 6 | int vowel = (c=='a'||c=='e'||c=='i'||c=='o'||c=='u'|| 7 | c=='A'||c=='E'||c=='I'||c=='O'||c=='U'); 8 | if (vowel) puts("vowel"); 9 | else if ((c>='A'&&c<='Z') || (c>='a'&&c<='z')) puts("consonant"); 10 | else puts("not a letter"); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p08.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int i = 1; 5 | while (i <= 10) { 6 | printf("%d\n", i); 7 | i = i + 1; 8 | } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p09.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n, sum = 0; 5 | while (1) { 6 | scanf("%d", &n); 7 | if (n == 0) break; 8 | sum += n; 9 | } 10 | printf("%d\n", sum); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p10.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n; 5 | scanf("%d", &n); 6 | for (int i = 1; i <= 10; i++) { 7 | printf("%d x %d = %d\n", n, i, n * i); 8 | } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p11.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n; 5 | scanf("%d", &n); 6 | unsigned long long fact = 1ULL; 7 | for (int i = 1; i <= n; i++) fact *= i; 8 | printf("%llu\n", fact); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p12.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | for (int i = 1; i <= 20; i++) { 5 | if (i % 2 == 0) continue; 6 | printf("%d\n", i); 7 | } 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p13.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | for (int i = 1; i <= 20; i++) { 5 | if (i == 13) break; 6 | printf("%d\n", i); 7 | } 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p14.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n; 5 | while (1) { 6 | scanf("%d", &n); 7 | if (n == 0) break; 8 | if (n < 0) continue; 9 | printf("%d\n", n); 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p15.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | for (int i = 1; i <= 100; i++) { 5 | if (i % 17 == 0) { printf("%d\n", i); break; } 6 | } 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n; 5 | scanf("%d", &n); 6 | for (int r = 1; r <= n; r++) { 7 | for (int c = 1; c <= r; c++) { 8 | putchar('*'); 9 | } 10 | putchar('\n'); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p17.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int rows, cols; 5 | scanf("%d %d", &rows, &cols); 6 | for (int r = 0; r < rows; r++) { 7 | for (int c = 0; c < cols; c++) putchar('*'); 8 | putchar('\n'); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p18.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | const int secret = 42; 5 | int g; 6 | while (1) { 7 | scanf("%d", &g); 8 | if (g == secret) { puts("Correct!"); break; } 9 | if (g < secret) puts("Too low"); 10 | else puts("Too high"); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p19.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n; 5 | scanf("%d", &n); 6 | while (n >= 1) { 7 | printf("%d\n", n); 8 | n = n - 1; 9 | } 10 | puts("Blast off!"); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch04/p20.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n; 5 | scanf("%d", &n); 6 | 7 | if (n < 2) { puts("Not prime"); return 0; } 8 | 9 | int is_prime = 1; 10 | for (int i = 2; i * i <= n; i++) { 11 | if (n % i == 0) { is_prime = 0; break; } 12 | } 13 | puts(is_prime ? "Prime" : "Not prime"); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/mathlib.c: -------------------------------------------------------------------------------- 1 | #include "mathlib.h" 2 | 3 | int add(int a, int b) { return a + b; } 4 | int sub(int a, int b) { return a - b; } 5 | int mul(int a, int b) { return a * b; } 6 | int div_int(int a, int b) { return a / b; } // assume nonzero b 7 | int square(int n) { return n * n; } 8 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/mathlib.h: -------------------------------------------------------------------------------- 1 | #ifndef MATHLIB_H 2 | #define MATHLIB_H 3 | 4 | int add(int a, int b); 5 | int sub(int a, int b); 6 | int mul(int a, int b); 7 | int div_int(int a, int b); // assumes b != 0 8 | int square(int n); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p01.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void hello(void) { puts("Hello, world!"); } 4 | 5 | int main(void) { 6 | hello(); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p02.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int double_it(int n) { return 2 * n; } 4 | 5 | int main(void) { 6 | for (int i = -2; i <= 2; i++) printf("%d -> %d\n", i, double_it(i)); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p03.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int max2(int a, int b) { return (a > b) ? a : b; } 4 | 5 | int main(void) { 6 | int a, b; scanf("%d %d", &a, &b); 7 | printf("%d\n", max2(a, b)); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p04.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int min3(int a, int b, int c) { 4 | int m = a; 5 | if (b < m) m = b; 6 | if (c < m) m = c; 7 | return m; 8 | } 9 | 10 | int main(void) { 11 | int a, b, c; scanf("%d %d %d", &a, &b, &c); 12 | printf("%d\n", min3(a, b, c)); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p05.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int is_even(int n) { return (n % 2 == 0); } 4 | 5 | int main(void) { 6 | for (int i = 1; i <= 10; i++) printf("%d -> %d\n", i, is_even(i)); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p06.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double average3(int a, int b, int c) { return (a + b + c) / 3.0; } 4 | 5 | int main(void) { 6 | int a, b, c; scanf("%d %d %d", &a, &b, &c); 7 | printf("%.6f\n", average3(a, b, c)); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p07.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int square(int n) { return n * n; } 4 | 5 | int main(void) { 6 | for (int i = 1; i <= 10; i++) printf("%d^2 = %d\n", i, square(i)); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p08.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double area_circle(double r) { return 3.14159 * r * r; } 4 | 5 | int main(void) { 6 | double r; while (scanf("%lf", &r) == 1) printf("r=%.3f area=%.5f\n", r, area_circle(r)); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p09.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double c_to_f(double c) { return c * 9.0 / 5.0 + 32.0; } 4 | 5 | int main(void) { 6 | double c; while (scanf("%lf", &c) == 1) printf("%.2f C = %.2f F\n", c, c_to_f(c)); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p10.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void line(int n) { for (int i = 0; i < n; i++) putchar('-'); putchar('\n'); } 4 | 5 | int main(void) { 6 | line(5); line(10); line(3); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p11.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int counter = 0; // global 4 | 5 | void tick(void) { counter++; } 6 | 7 | int main(void) { 8 | for (int i = 0; i < 5; i++) tick(); 9 | printf("%d\n", counter); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p12.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int value = 100; // global 4 | 5 | void show_local(void) { 6 | int value = 50; // shadows global 7 | printf("local=%d\n", value); 8 | } 9 | 10 | int main(void) { 11 | show_local(); 12 | printf("global=%d\n", value); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p13.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int x = 10; 5 | { 6 | int y = 20; 7 | printf("inside: x=%d y=%d\n", x, y); 8 | } 9 | /* Uncommenting the next line causes a compile error: y not visible here */ 10 | /* printf("outside: y=%d\n", y); */ 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p14.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int add(int a, int b) { return a + b; } 4 | int sub(int a, int b) { return a - b; } 5 | int mul(int a, int b) { return a * b; } 6 | 7 | int main(void) { 8 | int a, b; scanf("%d %d", &a, &b); 9 | printf("add=%d sub=%d mul=%d\n", add(a,b), sub(a,b), mul(a,b)); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p15.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* prototypes */ 4 | int add(int a, int b); 5 | int sub(int a, int b); 6 | int mul(int a, int b); 7 | 8 | int main(void) { 9 | int a, b; scanf("%d %d", &a, &b); 10 | printf("add=%d sub=%d mul=%d\n", add(a,b), sub(a,b), mul(a,b)); 11 | return 0; 12 | } 13 | 14 | /* definitions after main */ 15 | int add(int a, int b) { return a + b; } 16 | int sub(int a, int b) { return a - b; } 17 | int mul(int a, int b) { return a * b; } 18 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double area_rectangle(double w, double h) { return w * h; } 4 | double perimeter_rectangle(double w, double h) { return 2.0 * (w + h); } 5 | 6 | int main(void) { 7 | double w, h; scanf("%lf %lf", &w, &h); 8 | printf("area=%.3f perim=%.3f\n", area_rectangle(w,h), perimeter_rectangle(w,h)); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p17.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned long long factorial(int n) { 4 | unsigned long long f = 1ULL; 5 | for (int i = 2; i <= n; i++) f *= (unsigned long long)i; 6 | return f; 7 | } 8 | 9 | int main(void) { 10 | for (int i = 1; i <= 10; i++) printf("%2d! = %llu\n", i, factorial(i)); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p18.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long sum_range(int a, int b) { 4 | long long s = 0; 5 | for (int i = a; i <= b; i++) s += i; 6 | return s; 7 | } 8 | 9 | int main(void) { 10 | int a, b; scanf("%d %d", &a, &b); 11 | printf("%lld\n", sum_range(a, b)); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p19.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "mathlib.h" 3 | 4 | int main(void) { 5 | int a = 12, b = 5; 6 | printf("add=%d sub=%d mul=%d div=%d sq=%d\n", 7 | add(a,b), sub(a,b), mul(a,b), div_int(a,b), square(a)); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/p20.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "shapes.h" 3 | 4 | int main(void) { 5 | double w = 3, h = 4, r = 2.5; 6 | printf("rect: area=%.2f perim=%.2f\n", area_rectangle(w,h), perimeter_rectangle(w,h)); 7 | printf("circle: area=%.2f\n", area_circle(r)); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/shapes.c: -------------------------------------------------------------------------------- 1 | #include "shapes.h" 2 | 3 | double area_rectangle(double w, double h) { return w * h; } 4 | double perimeter_rectangle(double w, double h) { return 2.0 * (w + h); } 5 | double area_circle(double r) { return 3.14159 * r * r; } 6 | -------------------------------------------------------------------------------- /archive/v1/src/ch05/shapes.h: -------------------------------------------------------------------------------- 1 | #ifndef SHAPES_H 2 | #define SHAPES_H 3 | 4 | double area_rectangle(double w, double h); 5 | double perimeter_rectangle(double w, double h); 6 | double area_circle(double r); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p01.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int arr[10]; 5 | for (int i = 0; i < 10; i++) arr[i] = i; 6 | for (int i = 0; i < 10; i++) printf("%d ", arr[i]); 7 | printf("\n"); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p02.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a[5], sum = 0; 5 | for (int i = 0; i < 5; i++) { scanf("%d", &a[i]); sum += a[i]; } 6 | printf("%d\n", sum); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p03.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a[8]; 5 | for (int i = 0; i < 8; i++) scanf("%d", &a[i]); 6 | int mx = a[0]; 7 | for (int i = 1; i < 8; i++) if (a[i] > mx) mx = a[i]; 8 | printf("%d\n", mx); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p04.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a[10]; 5 | for (int i = 0; i < 10; i++) scanf("%d", &a[i]); 6 | for (int i = 9; i >= 0; i--) printf("%d ", a[i]); 7 | printf("\n"); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p05.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a[10], cnt = 0; 5 | for (int i = 0; i < 10; i++) { scanf("%d", &a[i]); if (a[i] % 2 == 0) cnt++; } 6 | printf("%d\n", cnt); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p06.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a[10]; long long sum = 0; 5 | for (int i = 0; i < 10; i++) { scanf("%d", &a[i]); sum += a[i]; } 6 | printf("%.6f\n", (double)sum / 10.0); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p07.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int arr[] = {1,2,3,4,5}; 5 | int n = (int)(sizeof(arr)/sizeof(arr[0])); 6 | long long s = 0; 7 | for (int i = 0; i < n; i++) s += (long long)arr[i]*arr[i]; 8 | printf("%lld\n", s); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p08.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | char s[] = "Hello"; 5 | for (int i = 0; s[i] != '\0'; i++) printf("%c\n", s[i]); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p09.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | char name[50]; 5 | scanf("%49s", name); 6 | printf("Hello, %s!\n", name); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p10.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | char s[200]; 5 | if (!fgets(s, sizeof(s), stdin)) return 0; 6 | int len = 0; 7 | while (s[len] != '\0' && s[len] != '\n') len++; 8 | printf("%d\n", len); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p11.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | char word[] = "dog"; 5 | word[0] = 'f'; 6 | printf("%s\n", word); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p12.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | char a[50], b[50]; 5 | scanf("%49s %49s", a, b); 6 | printf("%s %s\n", b, a); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p13.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | char s[200]; 6 | if (!fgets(s, sizeof(s), stdin)) return 0; 7 | size_t len = strcspn(s, "\n"); // length without trailing newline 8 | printf("%zu\n", len); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p14.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | char src[100], dst[100]; 6 | scanf("%99s", src); 7 | strcpy(dst, src); 8 | printf("src=%s dst=%s\n", src, dst); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p15.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | char a[32] = "Hello"; 6 | char b[] = "World"; 7 | strcat(a, b); 8 | printf("%s\n", a); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p16.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | char s1[100], s2[100]; 6 | scanf("%99s %99s", s1, s2); 7 | int cmp = strcmp(s1, s2); 8 | if (cmp < 0) printf("%s\n", s1); 9 | else if (cmp > 0) printf("%s\n", s2); 10 | else printf("equal\n"); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p17.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | char src[] = "C programming"; 6 | char dst[8]; 7 | strncpy(dst, src, sizeof(dst) - 1); 8 | dst[sizeof(dst) - 1] = '\0'; 9 | printf("%s\n", dst); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p18.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | char line[256]; 6 | if (!fgets(line, sizeof(line), stdin)) return 0; 7 | 8 | int count = 0; 9 | char *tok = strtok(line, " \t\r\n"); 10 | while (tok) { count++; tok = strtok(NULL, " \t\r\n"); } 11 | printf("%d\n", count); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p19.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | char line[256]; 6 | if (!fgets(line, sizeof(line), stdin)) return 0; 7 | 8 | char longest[256] = ""; 9 | char *tok = strtok(line, " \t\r\n"); 10 | while (tok) { 11 | if (strlen(tok) > strlen(longest)) { 12 | strncpy(longest, tok, sizeof(longest) - 1); 13 | longest[sizeof(longest) - 1] = '\0'; 14 | } 15 | tok = strtok(NULL, " \t\r\n"); 16 | } 17 | printf("%s\n", longest); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /archive/v1/src/ch06/p20.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_WORDS 100 5 | #define MAX_LEN 32 6 | 7 | int main(void) { 8 | char line[512]; 9 | if (!fgets(line, sizeof(line), stdin)) return 0; 10 | 11 | char words[MAX_WORDS][MAX_LEN]; 12 | int counts[MAX_WORDS] = {0}; 13 | int total = 0; 14 | 15 | for (char *tok = strtok(line, " \t\r\n"); tok; tok = strtok(NULL, " \t\r\n")) { 16 | // truncate token if too long for storage 17 | char key[MAX_LEN]; 18 | strncpy(key, tok, MAX_LEN - 1); 19 | key[MAX_LEN - 1] = '\0'; 20 | 21 | int idx = -1; 22 | for (int i = 0; i < total; i++) { 23 | if (strcmp(words[i], key) == 0) { idx = i; break; } 24 | } 25 | if (idx >= 0) { 26 | counts[idx]++; 27 | } else if (total < MAX_WORDS) { 28 | strcpy(words[total], key); 29 | counts[total] = 1; 30 | total++; 31 | } 32 | } 33 | 34 | for (int i = 0; i < total; i++) { 35 | printf("%s %d\n", words[i], counts[i]); 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p01.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int x = 10; 5 | int *p = &x; 6 | printf("x=%d\n", x); 7 | printf("&x=%p\n", (void*)&x); 8 | printf("p=%p\n", (void*)p); 9 | printf("*p=%d\n", *p); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p02.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int x = 5; 5 | int *p = &x; 6 | *p = 42; 7 | printf("x=%d *p=%d\n", x, *p); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p03.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n = 7; 5 | int *p1 = &n, *p2 = &n; 6 | *p1 = 99; 7 | printf("*p2=%d (n=%d)\n", *p2, n); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p04.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a = 10, b = 20; 5 | int *p = &a; 6 | printf("*p (a)=%d\n", *p); 7 | p = &b; 8 | printf("*p (b)=%d\n", *p); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p05.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int arr[5] = {1,2,3,4,5}; 5 | for (int i = 0; i < 5; i++) { 6 | printf("%d ", *(arr + i)); 7 | } 8 | printf("\n"); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p06.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a[5]; 5 | for (int i = 0; i < 5; i++) scanf("%d", &a[i]); 6 | int *p = a; 7 | int *end = a + 5; 8 | long long sum = 0; 9 | while (p < end) sum += *p++; 10 | printf("%lld\n", sum); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p07.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | char word[] = "Pointers"; 5 | char *p = word; 6 | while (*p != '\0') { 7 | printf("%c\n", *p); 8 | p++; 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p08.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int arr[10]; 5 | int *p = arr; 6 | printf("sizeof(arr)=%zu\n", sizeof(arr)); 7 | printf("sizeof(p) =%zu\n", sizeof(p)); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p09.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int square(int x) { return x * x; } 4 | int cube(int x) { return x * x * x; } 5 | 6 | int main(void) { 7 | int (*fp)(int) = square; 8 | printf("square(4)=%d\n", fp(4)); 9 | fp = cube; 10 | printf("cube(3)=%d\n", fp(3)); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p10.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int square(int x) { return x * x; } 4 | int cube(int x) { return x * x * x; } 5 | 6 | void apply(int (*f)(int), int x) { 7 | printf("%d\n", f(x)); 8 | } 9 | 10 | int main(void) { 11 | apply(square, 5); 12 | apply(cube, 4); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p11.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int add(int a,int b){return a+b;} 4 | int sub(int a,int b){return a-b;} 5 | int mul(int a,int b){return a*b;} 6 | int div_int(int a,int b){return a/b;} // assume b!=0 for this exercise 7 | 8 | int main(void) { 9 | int (*ops[4])(int,int) = {add, sub, mul, div_int}; 10 | int a = 12, b = 3; 11 | const char *names[4] = {"add","sub","mul","div"}; 12 | for (int i = 0; i < 4; i++) { 13 | printf("%s=%d\n", names[i], ops[i](a,b)); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p12.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int add(int a,int b){return a+b;} 4 | int sub(int a,int b){return a-b;} 5 | int mul(int a,int b){return a*b;} 6 | int div_int(int a,int b){return a/b;} 7 | 8 | int main(void) { 9 | int a, b; char op; 10 | if (scanf("%d %d %c", &a, &b, &op) != 3) return 0; 11 | 12 | int (*f)(int,int) = NULL; 13 | switch (op) { 14 | case '+': f = add; break; 15 | case '-': f = sub; break; 16 | case '*': f = mul; break; 17 | case '/': f = div_int; break; 18 | default: puts("unknown op"); return 0; 19 | } 20 | if (op == '/' && b == 0) { puts("divide by zero"); return 0; } 21 | printf("%d\n", f(a,b)); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p13.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include // for NULL 3 | 4 | int main(void) { 5 | int *p = NULL; // In C23 you may also use: int *p = nullptr; 6 | if (p == NULL) puts("Pointer is null"); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p14.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* BAD: returns address of local variable (dangling) */ 5 | int *bad_ptr(void) { 6 | int x = 10; 7 | return &x; /* DO NOT USE */ 8 | } 9 | 10 | /* GOOD: allocate on heap, caller must free */ 11 | int *good_ptr(void) { 12 | int *p = malloc(sizeof(*p)); 13 | if (p) *p = 10; 14 | return p; 15 | } 16 | 17 | int main(void) { 18 | int *pb = bad_ptr(); 19 | /* Printing the pointer value itself is defined, but dereferencing *pb is UB. 20 | printf("*pb=%d\n", *pb); // ❌ don't do this 21 | */ 22 | printf("dangling address=%p (do not dereference)\n", (void*)pb); 23 | 24 | int *pg = good_ptr(); 25 | if (pg) { 26 | printf("*pg=%d\n", *pg); 27 | free(pg); 28 | pg = NULL; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p15.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int *p; // uninitialized (wild) — do NOT dereference 5 | // *p = 5; // ❌ undefined behavior 6 | 7 | int x = 0; 8 | p = &x; // FIX: initialize to a valid address 9 | *p = 5; 10 | printf("x=%d\n", x); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p16.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int *p = malloc(sizeof(int)); 6 | if (!p) return 0; 7 | *p = 42; 8 | free(p); 9 | // free(p); // ❌ double free (undefined behavior) 10 | 11 | // FIX: 12 | p = NULL; // reset after free 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p17.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int n = 5; 6 | int *a = malloc(n * sizeof(int)); 7 | if (!a) return 0; 8 | 9 | for (int i = 0; i < n; i++) a[i] = i + 1; 10 | for (int i = 0; i < n; i++) printf("%d ", a[i]); 11 | printf("\n"); 12 | 13 | free(a); 14 | a = NULL; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p18.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Point { int x, y; }; 4 | 5 | int main(void) { 6 | struct Point p = {2, 3}; 7 | struct Point *pp = &p; 8 | 9 | printf("(%d,%d)\n", pp->x, pp->y); 10 | pp->x = 10; pp->y = 20; 11 | printf("(%d,%d)\n", p.x, p.y); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p19.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(int *a, int *b) { 4 | int t = *a; *a = *b; *b = t; 5 | } 6 | 7 | int main(void) { 8 | int x = 3, y = 7; 9 | swap(&x, &y); 10 | printf("x=%d y=%d\n", x, y); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch07/p20.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int cmp_int(const void *a, const void *b) { 5 | int x = *(const int*)a; 6 | int y = *(const int*)b; 7 | // Avoid overflow; return negative/0/positive 8 | return (x > y) - (x < y); 9 | } 10 | 11 | int main(void) { 12 | int arr[] = {4, 2, 5, 1, 3}; 13 | size_t n = sizeof(arr)/sizeof(arr[0]); 14 | 15 | qsort(arr, n, sizeof(arr[0]), cmp_int); 16 | 17 | for (size_t i = 0; i < n; i++) printf("%d ", arr[i]); 18 | printf("\n"); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p01.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Point { int x, y; }; 4 | 5 | int main(void) { 6 | struct Point p; 7 | p.x = 3; p.y = 4; 8 | printf("(%d,%d)\n", p.x, p.y); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p02.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Student { 4 | char name[50]; 5 | int age; 6 | double gpa; 7 | }; 8 | 9 | int main(void) { 10 | struct Student s = {"Alice", 20, 3.80}; 11 | printf("%s %d %.2f\n", s.name, s.age, s.gpa); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p03.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Point { int x, y; }; 4 | 5 | int main(void) { 6 | struct Point pts[3] = { {1,2}, {3,4}, {5,6} }; 7 | for (int i = 0; i < 3; i++) 8 | printf("(%d,%d)\n", pts[i].x, pts[i].y); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p04.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Student { char name[50]; int age; double gpa; }; 4 | 5 | int main(void) { 6 | struct Student s; 7 | scanf("%49s %d %lf", s.name, &s.age, &s.gpa); 8 | printf("%s %d %.2f\n", s.name, s.age, s.gpa); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p05.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Rectangle { int width, height; }; 4 | 5 | int area(struct Rectangle r) { return r.width * r.height; } 6 | 7 | int main(void) { 8 | struct Rectangle r = {5, 7}; 9 | printf("%d\n", area(r)); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p06.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct { int x, y; } Point; 4 | 5 | int main(void) { 6 | Point p = {8, 9}; 7 | printf("(%d,%d)\n", p.x, p.y); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p07.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct { char name[50]; int age; double gpa; } Student; 4 | typedef Student* StudentPtr; 5 | 6 | int main(void) { 7 | Student s = {"Bob", 21, 3.50}; 8 | StudentPtr sp = &s; 9 | printf("%s %d %.2f\n", sp->name, sp->age, sp->gpa); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p08.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef unsigned long long BigInt; 4 | 5 | int main(void) { 6 | BigInt f = 1; 7 | for (int i = 2; i <= 10; i++) f *= (BigInt)i; 8 | printf("%llu\n", f); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p09.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | enum Weekday { MON=1, TUE, WED, THU, FRI, SAT, SUN }; 4 | 5 | int main(void) { 6 | enum Weekday d = WED; 7 | printf("%d\n", d); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p10.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | enum TrafficLight { RED=1, YELLOW=2, GREEN=3 }; 4 | 5 | int main(void) { 6 | enum TrafficLight t = GREEN; 7 | if (t == RED) puts("Stop"); 8 | else if (t == YELLOW) puts("Wait"); 9 | else puts("Go"); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p11.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | enum ErrorCode { OK=0, FAIL=1, TIMEOUT=2 }; 4 | 5 | int main(void) { 6 | enum ErrorCode e; 7 | int x; scanf("%d", &x); 8 | e = (x==0)?OK : (x==1)?FAIL : TIMEOUT; 9 | 10 | switch (e) { 11 | case OK: puts("OK"); break; 12 | case FAIL: puts("FAIL"); break; 13 | case TIMEOUT: puts("TIMEOUT"); break; 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p12.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | enum Weekday { MON=1, TUE, WED, THU, FRI, SAT, SUN }; 4 | 5 | int main(void) { 6 | for (enum Weekday d = MON; d <= SUN; d++) 7 | printf("%d\n", d); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p13.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | union Data { 4 | int i; 5 | float f; 6 | }; 7 | 8 | int main(void) { 9 | union Data d; 10 | d.i = 42; 11 | printf("i=%d\n", d.i); 12 | d.f = 3.14f; // overwrites same storage 13 | printf("f=%.2f\n", d.f); 14 | // d.i now unspecified; don't rely on it 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p14.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | union U { 4 | char c[4]; 5 | int n; 6 | }; 7 | 8 | int main(void) { 9 | union U u; 10 | u.n = 0x12345678; // endianness affects output 11 | printf("%02X %02X %02X %02X\n", 12 | (unsigned char)u.c[0], 13 | (unsigned char)u.c[1], 14 | (unsigned char)u.c[2], 15 | (unsigned char)u.c[3]); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p15.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef enum { V_INT, V_FLOAT } Tag; 4 | 5 | typedef struct { 6 | Tag tag; 7 | union { int i; float f; } val; 8 | } Variant; 9 | 10 | void print_variant(Variant v) { 11 | if (v.tag == V_INT) printf("int=%d\n", v.val.i); 12 | else printf("float=%.2f\n", v.val.f); 13 | } 14 | 15 | int main(void) { 16 | Variant a = { V_INT, .val.i = 42 }; 17 | Variant b = { V_FLOAT, .val.f = 3.5f }; 18 | print_variant(a); 19 | print_variant(b); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct { 4 | char name[50]; 5 | char phone[20]; 6 | int age; 7 | } Contact; 8 | 9 | int main(void) { 10 | Contact c[3] = { 11 | {"Alice","123-4567",20}, 12 | {"Bob","555-9876",25}, 13 | {"Cara","777-0000",30} 14 | }; 15 | for (int i = 0; i < 3; i++) 16 | printf("%s %s %d\n", c[i].name, c[i].phone, c[i].age); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p17.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct { char name[50]; char phone[20]; int age; } Contact; 5 | 6 | #define MAXC 10 7 | 8 | void add_contact(Contact a[], int *n, const char *name, const char *phone, int age) { 9 | if (*n >= MAXC) return; 10 | snprintf(a[*n].name, sizeof(a[*n].name), "%s", name); 11 | snprintf(a[*n].phone, sizeof(a[*n].phone), "%s", phone); 12 | a[*n].age = age; 13 | (*n)++; 14 | } 15 | 16 | void print_contacts(const Contact a[], int n) { 17 | for (int i = 0; i < n; i++) 18 | printf("%s %s %d\n", a[i].name, a[i].phone, a[i].age); 19 | } 20 | 21 | int main(void) { 22 | Contact book[MAXC]; int n = 0; 23 | add_contact(book, &n, "Alice", "123-4567", 20); 24 | add_contact(book, &n, "Bob", "555-9876", 25); 25 | print_contacts(book, n); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p18.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct { char name[50]; char phone[20]; int age; } Contact; 5 | 6 | #define MAXC 10 7 | 8 | int find_contact(const Contact a[], int n, const char *name) { 9 | for (int i = 0; i < n; i++) 10 | if (strcmp(a[i].name, name) == 0) return i; 11 | return -1; 12 | } 13 | 14 | int main(void) { 15 | Contact book[MAXC] = { 16 | {"Alice","123-4567",20}, 17 | {"Bob","555-9876",25}, 18 | {"Cara","777-0000",30} 19 | }; 20 | int n = 3; 21 | char key[50]; scanf("%49s", key); 22 | int idx = find_contact(book, n, key); 23 | if (idx >= 0) printf("%s %s %d\n", book[idx].name, book[idx].phone, book[idx].age); 24 | else puts("Not found"); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p19.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct { char name[50]; char phone[20]; int age; } Contact; 5 | 6 | #define MAXC 10 7 | 8 | int find_contact(const Contact a[], int n, const char *name) { 9 | for (int i = 0; i < n; i++) 10 | if (strcmp(a[i].name, name) == 0) return i; 11 | return -1; 12 | } 13 | 14 | void delete_contact(Contact a[], int *n, const char *name) { 15 | int idx = find_contact(a, *n, name); 16 | if (idx < 0) return; 17 | for (int i = idx; i < *n - 1; i++) a[i] = a[i+1]; 18 | (*n)--; 19 | } 20 | 21 | void print_contacts(const Contact a[], int n) { 22 | for (int i = 0; i < n; i++) 23 | printf("%s %s %d\n", a[i].name, a[i].phone, a[i].age); 24 | } 25 | 26 | int main(void) { 27 | Contact book[MAXC] = { 28 | {"Alice","123-4567",20}, 29 | {"Bob","555-9876",25}, 30 | {"Cara","777-0000",30} 31 | }; 32 | int n = 3; 33 | delete_contact(book, &n, "Bob"); 34 | print_contacts(book, n); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /archive/v1/src/ch08/p20.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct { char name[50]; char phone[20]; int age; } Contact; 5 | 6 | #define MAXC 100 7 | 8 | void save_contacts(const char *path, const Contact a[], int n) { 9 | FILE *f = fopen(path, "w"); 10 | if (!f) return; 11 | for (int i = 0; i < n; i++) 12 | fprintf(f, "%s;%s;%d\n", a[i].name, a[i].phone, a[i].age); 13 | fclose(f); 14 | } 15 | 16 | int load_contacts(const char *path, Contact a[], int maxn) { 17 | FILE *f = fopen(path, "r"); 18 | if (!f) return 0; 19 | int n = 0; 20 | while (n < maxn && 21 | fscanf(f, " %49[^;];%19[^;];%d", a[n].name, a[n].phone, &a[n].age) == 3) { 22 | n++; 23 | int ch = fgetc(f); (void)ch; // consume newline if present 24 | } 25 | fclose(f); 26 | return n; 27 | } 28 | 29 | int main(void) { 30 | Contact out[3] = { 31 | {"Alice","123-4567",20}, 32 | {"Bob","555-9876",25}, 33 | {"Cara","777-0000",30} 34 | }; 35 | save_contacts("contacts.txt", out, 3); 36 | 37 | Contact in[MAXC]; 38 | int n = load_contacts("contacts.txt", in, MAXC); 39 | for (int i = 0; i < n; i++) 40 | printf("%s %s %d\n", in[i].name, in[i].phone, in[i].age); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p01.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void demo(void) { 4 | int x = 42; // automatic (stack) 5 | printf("x=%d at %p\n", x, (void*)&x); 6 | } 7 | 8 | int main(void) { 9 | demo(); 10 | demo(); // new lifetime, likely different address 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p02.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* BAD: returning address of local (dangling) — shown but unused */ 5 | int *bad(void) { 6 | int x = 7; 7 | return &x; // ❌ don't use 8 | } 9 | 10 | /* GOOD: caller owns heap memory */ 11 | int *good(void) { 12 | int *p = malloc(sizeof *p); 13 | if (p) *p = 7; 14 | return p; 15 | } 16 | 17 | int main(void) { 18 | int *pb = bad(); 19 | printf("dangling pointer value=%p (do not dereference)\n", (void*)pb); 20 | 21 | int *pg = good(); 22 | if (pg) { printf("*pg=%d\n", *pg); free(pg); } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p03.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int *p = malloc(sizeof *p); 6 | if (!p) return 1; 7 | *p = 42; 8 | printf("%d\n", *p); 9 | free(p); 10 | p = NULL; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p04.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int n; if (scanf("%d", &n) != 1 || n <= 0) return 0; 6 | int *a = malloc((size_t)n * sizeof *a); 7 | if (!a) return 1; 8 | for (int i = 0; i < n; i++) a[i] = i + 1; 9 | for (int i = 0; i < n; i++) printf("%d ", a[i]); 10 | printf("\n"); 11 | free(a); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p05.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int n; if (scanf("%d", &n) != 1 || n <= 0) return 0; 6 | int *a = calloc((size_t)n, sizeof *a); // zeroed 7 | if (!a) return 1; 8 | for (int i = 0; i < n; i++) printf("%d ", a[i]); // shows zeros 9 | printf("\n"); 10 | free(a); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p06.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | size_t n = 8; 6 | int *m = malloc(n * sizeof *m); 7 | int *c = calloc(n, sizeof *c); 8 | if (!m || !c) return 1; 9 | 10 | printf("malloc contents (indeterminate/garbage): "); 11 | for (size_t i = 0; i < n; i++) printf("%d ", m[i]); // reading uninitialized is UB in strict sense; just show addresses instead: 12 | printf("\n(avoid reading uninitialized; shown only to illustrate)\n"); 13 | 14 | printf("calloc contents (zeroed): "); 15 | for (size_t i = 0; i < n; i++) printf("%d ", c[i]); 16 | printf("\n"); 17 | 18 | free(m); free(c); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p07.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int *p = malloc(sizeof *p); 6 | if (!p) return 1; 7 | *p = 99; 8 | free(p); 9 | // *p = 5; // ❌ use-after-free 10 | p = NULL; // ✅ fix 11 | if (p == NULL) puts("Pointer reset to NULL"); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p08.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int *p = malloc(sizeof *p); 6 | if (!p) return 1; 7 | free(p); 8 | // free(p); // ❌ double free 9 | p = NULL; // ✅ fix: further free is harmless 10 | free(p); // OK (no-op) 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p09.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | for (int i = 0; i < 10000; i++) { 6 | int *p = malloc(1000 * sizeof *p); 7 | if (!p) break; 8 | // ❌ leak if we forget to free 9 | free(p); // ✅ fix leak 10 | } 11 | puts("Loop done without leaking."); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p10.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | size_t n = 5; 6 | int *a = malloc(n * sizeof *a); 7 | if (!a) return 1; 8 | for (size_t i = 0; i < n; i++) a[i] = (int)i; 9 | 10 | // printf("%d\n", a[5]); // ❌ OOB (6th element) — don't do this 11 | if (n > 5) printf("%d\n", a[5]); else puts("index 5 is out of bounds"); 12 | free(a); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p11.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int *make_array(size_t n) { 5 | int *a = malloc(n * sizeof *a); 6 | if (!a) return NULL; 7 | for (size_t i = 0; i < n; i++) a[i] = (int)(i + 1); 8 | return a; // caller frees 9 | } 10 | 11 | int main(void) { 12 | size_t n = 6; 13 | int *a = make_array(n); 14 | if (!a) return 1; 15 | for (size_t i = 0; i < n; i++) printf("%d ", a[i]); 16 | printf("\n"); 17 | free(a); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p12.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) { 6 | size_t n = 16; 7 | size_t align = 32; 8 | size_t bytes = n * sizeof(int); 9 | if (bytes % align) bytes += align - (bytes % align); // size must be multiple of alignment 10 | 11 | int *a = aligned_alloc(align, bytes); 12 | if (!a) return 1; 13 | 14 | for (size_t i = 0; i < n; i++) a[i] = (int)i; 15 | printf("ptr=%p aligned_to_%zu? %s\n", (void*)a, align, 16 | ((uintptr_t)a % align == 0) ? "yes" : "no"); 17 | 18 | free(a); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p13.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include // NULL 3 | 4 | int main(void) { 5 | int *a = NULL; 6 | int *b = nullptr; // C23 7 | printf("a==NULL? %s\n", (a == NULL) ? "yes" : "no"); 8 | printf("b==nullptr? %s\n", (b == nullptr) ? "yes" : "no"); 9 | printf("b==0? %s\n", (b == 0) ? "yes" : "no"); // null equals integer constant 0 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p14.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int *p = nullptr; // start null (C23) 6 | if (p == nullptr) puts("null before allocation"); 7 | 8 | p = malloc(4 * sizeof *p); 9 | if (p == nullptr) return 1; 10 | for (int i = 0; i < 4; i++) p[i] = i * 10; 11 | for (int i = 0; i < 4; i++) printf("%d ", p[i]); 12 | printf("\n"); 13 | free(p); 14 | p = nullptr; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p15.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | size_t n = 10; 6 | int *a = malloc(n * sizeof *a); 7 | if (!a) return 1; 8 | for (size_t i = 0; i < n; i++) a[i] = (int)(i + 1); 9 | 10 | size_t m = 5; 11 | int *tmp = realloc(a, m * sizeof *a); 12 | if (!tmp) { free(a); return 1; } 13 | a = tmp; n = m; 14 | 15 | for (size_t i = 0; i < n; i++) printf("%d ", a[i]); 16 | printf("\n"); 17 | free(a); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p16.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | size_t n = 5; 6 | int *a = malloc(n * sizeof *a); 7 | if (!a) return 1; 8 | for (size_t i = 0; i < n; i++) a[i] = (int)(i + 1); 9 | 10 | size_t m = 10; 11 | int *tmp = realloc(a, m * sizeof *a); 12 | if (!tmp) { free(a); return 1; } 13 | a = tmp; 14 | 15 | for (size_t i = n; i < m; i++) a[i] = (int)((i + 1) * (i + 1)); // squares 6..10 16 | for (size_t i = 0; i < m; i++) printf("%d ", a[i]); 17 | printf("\n"); 18 | free(a); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p17.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) { 6 | enum { N = 1000 }; 7 | void *blocks[N] = {0}; 8 | srand((unsigned)time(NULL)); 9 | 10 | // allocate random sizes 11 | for (int i = 0; i < N; i++) { 12 | size_t sz = (size_t)(rand() % 4096 + 1); 13 | blocks[i] = malloc(sz); 14 | if (!blocks[i]) { puts("allocation failed"); break; } 15 | } 16 | 17 | // free every third block 18 | for (int i = 0; i < N; i += 3) { 19 | free(blocks[i]); 20 | blocks[i] = NULL; 21 | } 22 | 23 | // attempt more allocations 24 | for (int i = 0; i < N; i++) { 25 | if (!blocks[i]) { 26 | blocks[i] = malloc(2048); 27 | if (!blocks[i]) puts("later allocation failed (possible fragmentation)"); 28 | } 29 | } 30 | 31 | // cleanup 32 | for (int i = 0; i < N; i++) free(blocks[i]); 33 | puts("done"); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p18.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct { int *data; size_t size, cap; } Dyn; 5 | 6 | void init(Dyn *d) { d->data = NULL; d->size = d->cap = 0; } 7 | 8 | int push_back(Dyn *d, int v) { 9 | if (d->size == d->cap) { 10 | size_t nc = d->cap ? d->cap * 2 : 1; 11 | int *nd = realloc(d->data, nc * sizeof *nd); 12 | if (!nd) return 0; 13 | d->data = nd; d->cap = nc; 14 | } 15 | d->data[d->size++] = v; 16 | return 1; 17 | } 18 | 19 | void destroy(Dyn *d) { free(d->data); d->data = NULL; d->size = d->cap = 0; } 20 | 21 | int main(void) { 22 | Dyn d; init(&d); 23 | for (int i = 1; i <= 10; i++) push_back(&d, i); 24 | for (size_t i = 0; i < d.size; i++) printf("%d ", d.data[i]); 25 | printf("\n"); 26 | destroy(&d); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p19.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct { int *data; size_t size, cap; } Dyn; 5 | 6 | void init(Dyn *d) { d->data = NULL; d->size = d->cap = 0; } 7 | int push_back(Dyn *d, int v) { 8 | if (d->size == d->cap) { 9 | size_t nc = d->cap ? d->cap * 2 : 1; 10 | int *nd = realloc(d->data, nc * sizeof *nd); 11 | if (!nd) return 0; 12 | d->data = nd; d->cap = nc; 13 | } 14 | d->data[d->size++] = v; return 1; 15 | } 16 | int pop_back(Dyn *d, int *out) { 17 | if (d->size == 0) return 0; 18 | *out = d->data[--d->size]; 19 | return 1; 20 | } 21 | void destroy(Dyn *d) { free(d->data); d->data = NULL; d->size = d->cap = 0; } 22 | 23 | int main(void) { 24 | Dyn d; init(&d); 25 | for (int i = 1; i <= 5; i++) push_back(&d, i); 26 | int x; 27 | while (pop_back(&d, &x)) printf("popped %d, size=%zu\n", x, d.size); 28 | destroy(&d); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /archive/v1/src/ch09/p20.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct { double *data; size_t size, cap; } DynD; 5 | 6 | void init(DynD *d){ d->data=NULL; d->size=d->cap=0; } 7 | int push(DynD *d, double v){ 8 | if (d->size == d->cap) { 9 | size_t nc = d->cap ? d->cap * 2 : 1; 10 | double *nd = realloc(d->data, nc * sizeof *nd); 11 | if (!nd) return 0; 12 | d->data = nd; d->cap = nc; 13 | } 14 | d->data[d->size++] = v; return 1; 15 | } 16 | void destroy(DynD *d){ free(d->data); d->data=NULL; d->size=d->cap=0; } 17 | 18 | int main(void){ 19 | DynD d; init(&d); 20 | for (int i = 0; i < 10; i++) push(&d, i + 0.5); 21 | for (size_t i = 0; i < d.size; i++) printf("%.2f ", d.data[i]); 22 | printf("\n"); 23 | destroy(&d); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p01.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | FILE *f = fopen("hello.txt", "w"); 5 | if (!f) { perror("hello.txt"); return 1; } 6 | fputs("Hello, File!\n", f); 7 | if (fclose(f) == EOF) { perror("close"); return 1; } 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p02.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | FILE *f = fopen("numbers.txt", "w"); 5 | if (!f) { perror("numbers.txt"); return 1; } 6 | for (int i = 1; i <= 10; i++) fprintf(f, "%d\n", i); 7 | if (fclose(f) == EOF) perror("close"); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p03.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | FILE *f = fopen("numbers.txt", "r"); 5 | if (!f) { perror("numbers.txt"); return 1; } 6 | long long sum = 0; int x; 7 | while (fscanf(f, "%d", &x) == 1) sum += x; 8 | if (ferror(f)) perror("read"); 9 | if (fclose(f) == EOF) perror("close"); 10 | printf("%lld\n", sum); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p04.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | FILE *in = fopen("source.txt", "r"); 5 | if (!in) { perror("source.txt"); return 1; } 6 | FILE *out = fopen("dest.txt", "w"); 7 | if (!out) { perror("dest.txt"); fclose(in); return 1; } 8 | 9 | char buf[4096]; 10 | while (fgets(buf, sizeof buf, in)) { 11 | if (fputs(buf, out) == EOF) { perror("write"); break; } 12 | } 13 | if (ferror(in)) perror("read"); 14 | 15 | if (fclose(in) == EOF) perror("close in"); 16 | if (fclose(out) == EOF) perror("close out"); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p05.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | FILE *in = fopen("input.txt", "r"); 6 | if (!in) { perror("input.txt"); return 1; } 7 | FILE *out = fopen("long.txt", "w"); 8 | if (!out) { perror("long.txt"); fclose(in); return 1; } 9 | 10 | char line[4096]; 11 | while (fgets(line, sizeof line, in)) { 12 | if (strlen(line) > 10) { 13 | if (fputs(line, out) == EOF) { perror("write"); break; } 14 | } 15 | } 16 | if (ferror(in)) perror("read"); 17 | 18 | fclose(in); fclose(out); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p06.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | // text 5 | FILE *tx = fopen("ints.txt", "w"); 6 | if (!tx) { perror("ints.txt"); return 1; } 7 | for (int i = 1; i <= 5; i++) fprintf(tx, "%d\n", i); 8 | fclose(tx); 9 | 10 | // binary 11 | FILE *bn = fopen("ints.bin", "wb"); 12 | if (!bn) { perror("ints.bin"); return 1; } 13 | int a[5]; for (int i = 0; i < 5; i++) a[i] = i + 1; 14 | size_t n = fwrite(a, sizeof(int), 5, bn); 15 | if (n != 5) perror("fwrite"); 16 | fclose(bn); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p07.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | // text 5 | FILE *tx = fopen("ints.txt", "r"); 6 | if (!tx) { perror("ints.txt"); return 1; } 7 | int x; 8 | puts("from text:"); 9 | while (fscanf(tx, "%d", &x) == 1) printf("%d ", x); 10 | puts(""); 11 | fclose(tx); 12 | 13 | // binary 14 | FILE *bn = fopen("ints.bin", "rb"); 15 | if (!bn) { perror("ints.bin"); return 1; } 16 | int arr[5]; size_t n = fread(arr, sizeof(int), 5, bn); 17 | if (n != 5 && ferror(bn)) perror("fread"); 18 | puts("from binary:"); 19 | for (size_t i = 0; i < n; i++) printf("%d ", arr[i]); 20 | puts(""); 21 | fclose(bn); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p08.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct { int id; double score; } Rec; 4 | 5 | int main(void) { 6 | Rec v[3] = { {1, 88.5}, {2, 91.0}, {3, 76.25} }; 7 | FILE *f = fopen("scores.bin", "wb"); 8 | if (!f) { perror("scores.bin"); return 1; } 9 | size_t n = fwrite(v, sizeof(Rec), 3, f); 10 | if (n != 3) perror("fwrite"); 11 | fclose(f); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p09.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct { int id; double score; } Rec; 4 | 5 | int main(void) { 6 | FILE *f = fopen("scores.bin", "rb"); 7 | if (!f) { perror("scores.bin"); return 1; } 8 | Rec v[3]; size_t n = fread(v, sizeof(Rec), 3, f); 9 | if (n != 3 && ferror(f)) perror("fread"); 10 | for (size_t i = 0; i < n; i++) 11 | printf("id=%d score=%.2f\n", v[i].id, v[i].score); 12 | fclose(f); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p10.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | FILE *f = fopen("nofile.txt", "r"); 5 | if (!f) { perror("open nofile.txt"); return 1; } 6 | fclose(f); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p11.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | FILE *in = fopen("source.txt", "rb"); 6 | if (!in) { perror("source.txt"); return 1; } 7 | FILE *out = fopen("dest.txt", "wb"); 8 | if (!out) { perror("dest.txt"); fclose(in); return 1; } 9 | 10 | unsigned char buf[8192]; 11 | size_t n; 12 | while ((n = fread(buf, 1, sizeof buf, in)) > 0) { 13 | size_t w = fwrite(buf, 1, n, out); 14 | if (w != n) { perror("write"); fclose(in); fclose(out); return 1; } 15 | } 16 | if (ferror(in)) perror("read"); 17 | 18 | if (fclose(in) == EOF) perror("close in"); 19 | if (fclose(out) == EOF) perror("close out"); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p12.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static void log_msg(const char *level, const char *msg) { 5 | FILE *log = fopen("app.log", "a"); 6 | if (!log) return; 7 | time_t t = time(NULL); 8 | char *ts = ctime(&t); // "Www Mmm dd hh:mm:ss yyyy\n" 9 | if (ts) ts[24] = '\0'; // drop '\n' at pos 24 10 | fprintf(log, "[%s] %s: %s\n", ts ? ts : "unknown-time", level, msg); 11 | fclose(log); 12 | } 13 | 14 | int main(void) { 15 | log_msg("INFO", "Program started"); 16 | log_msg("ERROR", "Something happened"); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p13.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static void log_msg(const char *level, const char *msg) { 5 | FILE *log = fopen("app.log", "a"); 6 | if (!log) return; 7 | time_t t = time(NULL); char *ts = ctime(&t); if (ts) ts[24] = '\0'; 8 | fprintf(log, "[%s] %s: %s\n", ts ? ts : "unknown-time", level, msg); 9 | fclose(log); 10 | } 11 | static void log_info(const char *m){ log_msg("INFO", m); } 12 | static void log_warn(const char *m){ log_msg("WARN", m); } 13 | static void log_error(const char *m){ log_msg("ERROR", m); } 14 | 15 | int main(void) { 16 | log_info("start"); 17 | log_warn("low disk"); 18 | log_error("failed to connect"); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p14.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static void log_msg(const char *level, const char *msg) { 5 | FILE *log = fopen("app.log", "a"); 6 | if (!log) return; 7 | time_t t = time(NULL); char *ts = ctime(&t); if (ts) ts[24] = '\0'; 8 | fprintf(log, "[%s] %s: %s\n", ts ? ts : "unknown-time", level, msg); 9 | fclose(log); 10 | } 11 | 12 | int main(void) { 13 | char buf[64]; 14 | for (int i = 1; i <= 5; i++) { 15 | snprintf(buf, sizeof buf, "message %d", i); 16 | log_msg("INFO", buf); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p15.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static void log_msg(const char *level, const char *msg) { 5 | FILE *log = fopen("app.log", "a"); 6 | if (!log) return; 7 | time_t t = time(NULL); char *ts = ctime(&t); if (ts) ts[24] = '\0'; 8 | fprintf(log, "[%s] %s: %s\n", ts ? ts : "unknown-time", level, msg); 9 | fflush(log); // ensure on disk 10 | fclose(log); 11 | } 12 | 13 | int main(void) { 14 | log_msg("INFO", "flushed"); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p16.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static void log_msg(const char *level, const char *msg) { 6 | FILE *log = fopen("app.log", "a"); 7 | if (!log) return; 8 | time_t t = time(NULL); char *ts = ctime(&t); if (ts) ts[24] = '\0'; 9 | fprintf(log, "[%s] %s: %s\n", ts ? ts : "unknown-time", level, msg); 10 | fclose(log); 11 | } 12 | 13 | int main(int argc, char **argv) { 14 | if (argc > 1 && strcmp(argv[1], "--clear") == 0) { 15 | FILE *f = fopen("app.log", "w"); 16 | if (!f) { perror("app.log"); return 1; } 17 | fclose(f); 18 | } 19 | log_msg("INFO", "program run"); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p17.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | FILE *t = tmpfile(); 5 | if (!t) { perror("tmpfile"); return 1; } 6 | fputs("scratch data\n", t); 7 | rewind(t); 8 | char buf[64]; 9 | if (fgets(buf, sizeof buf, t)) printf("%s", buf); 10 | fclose(t); // file is removed automatically 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p18.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | if (!freopen("out.txt", "w", stdout)) { perror("freopen"); return 1; } 5 | printf("redirected output\n"); 6 | // stdout now writes to out.txt 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p19.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) { 6 | setlocale(LC_ALL, ""); // enable locale for wide I/O 7 | FILE *f = fopen("wide.txt", "w, ccs=UTF-8"); // many compilers accept this; if not, simply use "w" 8 | if (!f) { perror("wide.txt"); return 1; } 9 | fwprintf(f, L"%ls %d\n", L"こんにちは", 42); 10 | fclose(f); 11 | 12 | f = fopen("wide.txt", "r"); 13 | if (!f) { perror("wide.txt"); return 1; } 14 | wchar_t word[64]; int n; 15 | if (fwscanf(f, L"%63ls %d", word, &n) == 2) 16 | wprintf(L"Read: %ls %d\n", word, n); 17 | fclose(f); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /archive/v1/src/ch10/p20.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | FILE *f = fopen("does_not_exist.txt", "r"); 5 | if (f == nullptr) { // C23 null pointer constant 6 | perror("open failed"); 7 | return 1; 8 | } 9 | fclose(f); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/include/p17_mathlib.h: -------------------------------------------------------------------------------- 1 | #ifndef P17_MATHLIB_H 2 | #define P17_MATHLIB_H 3 | int add(int,int); 4 | #endif 5 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p01_main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "p01_mathlib.h" 3 | int main(void){ 4 | printf("%d %d %d %d\n", add(4,2), sub(4,2), mul(4,2), div_int(4,2)); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p01_mathlib.c: -------------------------------------------------------------------------------- 1 | #include "p01_mathlib.h" 2 | int add(int a,int b){return a+b;} 3 | int sub(int a,int b){return a-b;} 4 | int mul(int a,int b){return a*b;} 5 | int div_int(int a,int b){return b? a/b : 0;} 6 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p01_mathlib.h: -------------------------------------------------------------------------------- 1 | #ifndef P01_MATHLIB_H 2 | #define P01_MATHLIB_H 3 | int add(int a,int b); 4 | int sub(int a,int b); 5 | int mul(int a,int b); 6 | int div_int(int a,int b); // b!=0 7 | #endif 8 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p06_main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define SQUARE(x) ((x)*(x)) 3 | inline int square(int x){ return x*x; } 4 | 5 | int main(void){ 6 | int i=2; 7 | printf("%d %d\n", SQUARE(3), square(3)); 8 | printf("%d %d\n", SQUARE(3+1), square(3+1)); 9 | i=2; printf("macro:%d\n", SQUARE(i++)); // multiple eval 10 | i=2; printf("inline:%d\n", square(i++)); // single eval 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p07_main.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | #if defined(_WIN32) 4 | puts("Windows"); 5 | #elif defined(__linux__) 6 | puts("Linux"); 7 | #else 8 | puts("Other"); 9 | #endif 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p08_main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define LIMIT 100 3 | int main(void){ 4 | printf("LIMIT=%d\n", LIMIT); 5 | #undef LIMIT 6 | // printf("%d\n", LIMIT); // would not compile 7 | puts("LIMIT undefined"); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p09_main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "p09_shapes.h" 3 | int main(void){ 4 | printf("area=%d\n", area_rect(3,4)); 5 | Color c=RED; 6 | printf("color=%d\n", c); 7 | Point p={5,7}; 8 | printf("point=(%d,%d)\n", p.x, p.y); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p09_shapes.c: -------------------------------------------------------------------------------- 1 | #include "p09_shapes.h" 2 | int area_rect(int w,int h){ return w*h; } 3 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p09_shapes.h: -------------------------------------------------------------------------------- 1 | #ifndef P09_SHAPES_H 2 | #define P09_SHAPES_H 3 | typedef enum { RED, GREEN, BLUE } Color; 4 | typedef struct { int x,y; } Point; 5 | int area_rect(int w,int h); 6 | #endif 7 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p12_config.c: -------------------------------------------------------------------------------- 1 | #ifndef P12_CONFIG_H 2 | #define P12_CONFIG_H 3 | #define VERSION "1.0" 4 | #endif 5 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p12_main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "p12_config.h" 3 | int main(void){ 4 | puts(VERSION); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p13_file.h: -------------------------------------------------------------------------------- 1 | #ifndef P13_FILE_H 2 | #define P13_FILE_H 3 | void f1(void); 4 | void f2(void); 5 | #endif 6 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p13_file1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "p13_file.h" 3 | void f1(void){ puts("f1"); } 4 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p13_file2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "p13_file.h" 3 | void f2(void){ puts("f2"); } 4 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p13_main.c: -------------------------------------------------------------------------------- 1 | #include "p13_file.h" 2 | int main(void){ f1(); f2(); return 0; } 3 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p15_Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-std=c23 -Wall -Wextra 3 | OBJ=p13_main.o p13_file1.o p13_file2.o 4 | 5 | prog: $(OBJ) 6 | $(CC) $(OBJ) -o $@ 7 | 8 | p13_main.o: p13_main.c p13_file.h 9 | $(CC) $(CFLAGS) -c p13_main.c 10 | 11 | p13_file1.o: p13_file1.c p13_file.h 12 | $(CC) $(CFLAGS) -c p13_file1.c 13 | 14 | p13_file2.o: p13_file2.c p13_file.h 15 | $(CC) $(CFLAGS) -c p13_file2.c 16 | 17 | clean: 18 | rm -f $(OBJ) prog 19 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p16_perf.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define CUBE_MACRO(x) ((x)*(x)*(x)) 4 | inline int cube_inline(int x){ return x*x*x; } 5 | 6 | int main(void){ 7 | volatile int s=0; 8 | const int N=1000000; 9 | clock_t t1=clock(); 10 | for(int i=0;i 2 | #include "p18_shapes.h" 3 | int main(void){ 4 | Rect r={3,4}; 5 | printf("area=%d\n", area_rect(r)); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p18_mathlib.h: -------------------------------------------------------------------------------- 1 | #ifndef P18_MATHLIB_H 2 | #define P18_MATHLIB_H 3 | int add(int,int); 4 | #endif 5 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p18_shapes.c: -------------------------------------------------------------------------------- 1 | #include "p18_shapes.h" 2 | int add(int a,int b){ return a+b; } // or link real mathlib 3 | int area_rect(Rect r){ return r.w*r.h + add(0,0); } 4 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p18_shapes.h: -------------------------------------------------------------------------------- 1 | #ifndef P18_SHAPES_H 2 | #define P18_SHAPES_H 3 | #include "p18_mathlib.h" 4 | typedef struct{ int w,h; } Rect; 5 | int area_rect(Rect r); 6 | #endif 7 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p19_a.c: -------------------------------------------------------------------------------- 1 | #include "p19_bad.h" 2 | void fa(void){ bad(); } 3 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p19_b.c: -------------------------------------------------------------------------------- 1 | #include "p19_bad.h" 2 | void fb(void){ bad(); } 3 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p19_bad.c: -------------------------------------------------------------------------------- 1 | #include 2 | void bad(void){ puts("ok: defined in one .c"); } 3 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p19_bad.h: -------------------------------------------------------------------------------- 1 | #ifndef P19_BAD_H 2 | #define P19_BAD_H 3 | #include 4 | void bad(void){ puts("defined in header (bad)"); } // ❌ definition in header 5 | #endif 6 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p19_good_bad.h: -------------------------------------------------------------------------------- 1 | #ifndef P19_GOOD_BAD_H 2 | #define P19_GOOD_BAD_H 3 | void bad(void); 4 | #endif 5 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p19_main.c: -------------------------------------------------------------------------------- 1 | void fa(void); void fb(void); 2 | int main(void){ fa(); fb(); return 0; } 3 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p20_calc.c: -------------------------------------------------------------------------------- 1 | #include "p20_calc.h" 2 | int add(int a,int b){return a+b;} 3 | int sub(int a,int b){return a-b;} 4 | int mul(int a,int b){return a*b;} 5 | int div_int(int a,int b){return b? a/b:0;} 6 | int mod(int a,int b){return b? a%b:0;} 7 | int pow_int(int base,int exp){ 8 | if(exp<0) return 0; 9 | int r=1; while(exp--) r*=base; return r; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p20_calc.h: -------------------------------------------------------------------------------- 1 | #ifndef P20_CALC_H 2 | #define P20_CALC_H 3 | int add(int,int); 4 | int sub(int,int); 5 | int mul(int,int); 6 | int div_int(int,int); 7 | int mod(int,int); 8 | int pow_int(int base,int exp); 9 | #endif 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/p20_main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "p20_calc.h" 3 | int main(void){ 4 | printf("add=%d sub=%d mul=%d div=%d mod=%d pow=%d\n", 5 | add(7,3), sub(7,3), mul(7,3), div_int(7,3), mod(7,3), pow_int(2,10)); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/src/p17_main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "p17_mathlib.h" 3 | int main(void){ 4 | printf("3+4=%d\n", add(3,4)); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /archive/v1/src/ch11/src/p17_mathlib.c: -------------------------------------------------------------------------------- 1 | #include "p17_mathlib.h" 2 | int add(int a,int b){ return a+b; } 3 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p01.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int a,b; 4 | if (scanf("%d %d",&a,&b)==2) printf("%d\n", a+b); 5 | else fprintf(stderr,"input error\n"); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p02.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | char buf[256]; 5 | if (fgets(buf,sizeof buf,stdin)){ 6 | size_t n = strlen(buf); 7 | printf("%s", buf); 8 | printf("len=%zu\n", n); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p03.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | puts(" n n^2 n^3"); 4 | for(int i=1;i<=10;i++) 5 | printf("%2d %5d %6d\n", i, i*i, i*i*i); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p04.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | for(int i=1;i<=5;i++) printf("%d\n", i); // stdout 4 | fprintf(stderr, "this is an error message\n"); // stderr 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p05.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | char w[128]; int n=1; 4 | while (scanf("%127s", w)==1) printf("%d: %s\n", n++, w); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p06.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #ifndef M_PI 4 | #define M_PI 3.14159265358979323846 5 | #endif 6 | int main(void){ 7 | double r; if (scanf("%lf",&r)!=1) return 0; 8 | double A = M_PI*r*r, C = 2*M_PI*r; 9 | printf("area=%.6f circ=%.6f\n", A, C); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p07.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #ifndef M_PI 4 | #define M_PI 3.14159265358979323846 5 | #endif 6 | int main(void){ 7 | double deg=45.0, rad = deg * (M_PI/180.0); 8 | printf("sin=%.6f cos=%.6f tan=%.6f\n", sin(rad), cos(rad), tan(rad)); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p08.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | double a,b; 5 | if (scanf("%lf %lf",&a,&b)==2) printf("c=%.6f\n", hypot(a,b)); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p09.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | static void show(double x){ 4 | printf("x=% .2f ceil=% .2f floor=% .2f round=% .2f trunc=% .2f\n", 5 | x, ceil(x), floor(x), round(x), trunc(x)); 6 | } 7 | int main(void){ 8 | show(-2.7); show(2.7); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p10.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | double P,r; int n; 5 | if (scanf("%lf %lf %d",&P,&r,&n)==3) 6 | printf("%.6f\n", P*pow(1.0+r, n)); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p11.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | time_t now = time(NULL); 5 | struct tm *loc = localtime(&now); 6 | struct tm *utc = gmtime(&now); 7 | char a[64], b[64]; 8 | strftime(a,sizeof a,"%Y-%m-%d %H:%M:%S", loc); 9 | strftime(b,sizeof b,"%Y-%m-%d %H:%M:%S", utc); 10 | printf("Local: %s\nUTC: %s\n", a, b); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p12.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | time_t now = time(NULL); 5 | struct tm *loc = localtime(&now); 6 | char buf[128]; 7 | strftime(buf,sizeof buf,"%A, %B %d, %Y", loc); 8 | puts(buf); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p13.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | const int N = 100000000; 5 | clock_t t1=clock(); 6 | long long s=0; 7 | for (int i=1;i<=N;i++) s+=i; 8 | clock_t t2=clock(); 9 | printf("sum=%lld time=%.3f s\n", s, 10 | (double)(t2-t1)/CLOCKS_PER_SEC); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p14.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | int y,m,d; if (scanf("%d %d %d",&y,&m,&d)!=3) return 0; 5 | struct tm t = {0}; 6 | t.tm_year = y - 1900; t.tm_mon = m - 1; t.tm_mday = d; 7 | if (mktime(&t) == (time_t)-1) return 0; 8 | const char* wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; 9 | printf("%s\n", wday[t.tm_wday]); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p15.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | static void timestamp(void){ 4 | time_t now = time(NULL); 5 | struct tm *loc = localtime(&now); 6 | char buf[32]; 7 | strftime(buf,sizeof buf,"%Y-%m-%d %H:%M:%S", loc); 8 | printf("[%s]", buf); 9 | } 10 | int main(void){ 11 | timestamp(); puts(" example log message"); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p16.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int rand_range(int min,int max){ 6 | return min + rand() % (max - min + 1); // simple version 7 | } 8 | 9 | int main(void){ 10 | srand((unsigned)time(NULL)); 11 | for(int i=0;i<10;i++) printf("%d ", rand_range(1,6)); 12 | puts(""); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p17.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main(void){ 5 | srand((unsigned)time(NULL)); 6 | double sum=0.0; 7 | for(int i=0;i<100;i++){ 8 | double r = (double)rand() / (RAND_MAX + 1.0); 9 | sum += r; 10 | } 11 | printf("avg=%.6f\n", sum/100.0); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p18.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | static int r6(void){ return 1 + rand()%6; } 5 | int main(void){ 6 | srand((unsigned)time(NULL)); 7 | int cnt=0; 8 | for(int i=0;i<1000;i++) if (r6()+r6()==7) cnt++; 9 | printf("sevens=%d\n", cnt); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p19.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | srand(1234); 5 | printf("%d %d %d\n", rand(), rand(), rand()); 6 | srand(1234); 7 | printf("%d %d %d\n", rand(), rand(), rand()); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p20.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main(void){ 5 | srand((unsigned)time(NULL)); 6 | int streak=0, tosses=0; 7 | while(streak<3){ 8 | tosses++; 9 | if (rand()%2){ puts("Heads"); streak++; } 10 | else { puts("Tails"); streak=0; } 11 | } 12 | printf("tosses=%d\n", tosses); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p21.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void safe_copy(char *dst, size_t n, const char *src){ 4 | if (n==0) return; 5 | strncpy(dst, src, n-1); 6 | dst[n-1] = '\0'; 7 | } 8 | int main(void){ 9 | char buf[8]; 10 | safe_copy(buf,sizeof buf,"helloworld"); 11 | printf("%s\n", buf); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p22.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | char buf[32] = "Hello"; 5 | strncat(buf, " ", sizeof buf - strlen(buf) - 1); 6 | strncat(buf, "World", sizeof buf - strlen(buf) - 1); 7 | puts(buf); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p23.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | const char *text = "catapult scatter catalog"; 5 | const char *sub = "cat"; 6 | int cnt=0; 7 | for (const char *p=text; (p=strstr(p,sub)); p++) cnt++; 8 | printf("%d\n", cnt); // expected 3 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p24.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | char text[]="red,green,blue"; 5 | for (char *tok=strtok(text,","); tok; tok=strtok(NULL,",")) puts(tok); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /archive/v1/src/ch12/p25.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int is_palindrome(const char *s){ 6 | size_t i=0, j=strlen(s); 7 | if (j==0) return 1; 8 | j--; 9 | while (i %d\n", a, is_palindrome(a)); 23 | printf("%s -> %d\n", b, is_palindrome(b)); 24 | printf("%s -> %d\n", c, is_palindrome(c)); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /docs/.nojekyll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/little-book-of/c/0e8a33e9852266d417fb85ee48ce577c304e1671/docs/.nojekyll -------------------------------------------------------------------------------- /docs/book-latex/.nojekyll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/little-book-of/c/0e8a33e9852266d417fb85ee48ce577c304e1671/docs/book-latex/.nojekyll -------------------------------------------------------------------------------- /docs/book.epub: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/little-book-of/c/0e8a33e9852266d417fb85ee48ce577c304e1671/docs/book.epub -------------------------------------------------------------------------------- /docs/book.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/little-book-of/c/0e8a33e9852266d417fb85ee48ce577c304e1671/docs/book.pdf -------------------------------------------------------------------------------- /docs/site_libs/bootstrap/bootstrap-icons.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/little-book-of/c/0e8a33e9852266d417fb85ee48ce577c304e1671/docs/site_libs/bootstrap/bootstrap-icons.woff -------------------------------------------------------------------------------- /docs/site_libs/clipboard/clipboard.min.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * clipboard.js v2.0.11 3 | * https://clipboardjs.com/ 4 | * 5 | * Licensed MIT © Zeno Rocha 6 | */ 7 | !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.ClipboardJS=e():t.ClipboardJS=e()}(this,function(){return n={686:function(t,e,n){"use strict";n.d(e,{default:function(){return b}});var e=n(279),i=n.n(e),e=n(370),u=n.n(e),e=n(817),r=n.n(e);function c(t){try{return document.execCommand(t)}catch(t){return}}var a=function(t){t=r()(t);return c("cut"),t};function o(t,e){var n,o,t=(n=t,o="rtl"===document.documentElement.getAttribute("dir"),(t=document.createElement("textarea")).style.fontSize="12pt",t.style.border="0",t.style.padding="0",t.style.margin="0",t.style.position="absolute",t.style[o?"right":"left"]="-9999px",o=window.pageYOffset||document.documentElement.scrollTop,t.style.top="".concat(o,"px"),t.setAttribute("readonly",""),t.value=n,t);return e.container.appendChild(t),e=r()(t),c("copy"),t.remove(),e}var f=function(t){var e=1.anchorjs-link,.anchorjs-link:focus{opacity:1}",A.sheet.cssRules.length),A.sheet.insertRule("[data-anchorjs-icon]::after{content:attr(data-anchorjs-icon)}",A.sheet.cssRules.length),A.sheet.insertRule('@font-face{font-family:anchorjs-icons;src:url(data:n/a;base64,AAEAAAALAIAAAwAwT1MvMg8yG2cAAAE4AAAAYGNtYXDp3gC3AAABpAAAAExnYXNwAAAAEAAAA9wAAAAIZ2x5ZlQCcfwAAAH4AAABCGhlYWQHFvHyAAAAvAAAADZoaGVhBnACFwAAAPQAAAAkaG10eASAADEAAAGYAAAADGxvY2EACACEAAAB8AAAAAhtYXhwAAYAVwAAARgAAAAgbmFtZQGOH9cAAAMAAAAAunBvc3QAAwAAAAADvAAAACAAAQAAAAEAAHzE2p9fDzz1AAkEAAAAAADRecUWAAAAANQA6R8AAAAAAoACwAAAAAgAAgAAAAAAAAABAAADwP/AAAACgAAA/9MCrQABAAAAAAAAAAAAAAAAAAAAAwABAAAAAwBVAAIAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAMCQAGQAAUAAAKZAswAAACPApkCzAAAAesAMwEJAAAAAAAAAAAAAAAAAAAAARAAAAAAAAAAAAAAAAAAAAAAQAAg//0DwP/AAEADwABAAAAAAQAAAAAAAAAAAAAAIAAAAAAAAAIAAAACgAAxAAAAAwAAAAMAAAAcAAEAAwAAABwAAwABAAAAHAAEADAAAAAIAAgAAgAAACDpy//9//8AAAAg6cv//f///+EWNwADAAEAAAAAAAAAAAAAAAAACACEAAEAAAAAAAAAAAAAAAAxAAACAAQARAKAAsAAKwBUAAABIiYnJjQ3NzY2MzIWFxYUBwcGIicmNDc3NjQnJiYjIgYHBwYUFxYUBwYGIwciJicmNDc3NjIXFhQHBwYUFxYWMzI2Nzc2NCcmNDc2MhcWFAcHBgYjARQGDAUtLXoWOR8fORYtLTgKGwoKCjgaGg0gEhIgDXoaGgkJBQwHdR85Fi0tOAobCgoKOBoaDSASEiANehoaCQkKGwotLXoWOR8BMwUFLYEuehYXFxYugC44CQkKGwo4GkoaDQ0NDXoaShoKGwoFBe8XFi6ALjgJCQobCjgaShoNDQ0NehpKGgobCgoKLYEuehYXAAAADACWAAEAAAAAAAEACAAAAAEAAAAAAAIAAwAIAAEAAAAAAAMACAAAAAEAAAAAAAQACAAAAAEAAAAAAAUAAQALAAEAAAAAAAYACAAAAAMAAQQJAAEAEAAMAAMAAQQJAAIABgAcAAMAAQQJAAMAEAAMAAMAAQQJAAQAEAAMAAMAAQQJAAUAAgAiAAMAAQQJAAYAEAAMYW5jaG9yanM0MDBAAGEAbgBjAGgAbwByAGoAcwA0ADAAMABAAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAH//wAP) format("truetype")}',A.sheet.cssRules.length)),h=document.querySelectorAll("[id]"),t=[].map.call(h,function(A){return A.id}),i=0;i\]./()*\\\n\t\b\v\u00A0]/g,"-").replace(/-{2,}/g,"-").substring(0,this.options.truncate).replace(/^-+|-+$/gm,"").toLowerCase()},this.hasAnchorJSLink=function(A){var e=A.firstChild&&-1<(" "+A.firstChild.className+" ").indexOf(" anchorjs-link "),A=A.lastChild&&-1<(" "+A.lastChild.className+" ").indexOf(" anchorjs-link ");return e||A||!1}}}); 9 | // @license-end -------------------------------------------------------------------------------- /docs/site_libs/quarto-html/axe/axe-check.js: -------------------------------------------------------------------------------- 1 | class QuartoAxeReporter { 2 | constructor(axeResult, options) { 3 | this.axeResult = axeResult; 4 | this.options = options; 5 | } 6 | 7 | report() { 8 | throw new Error("report() is an abstract method"); 9 | } 10 | } 11 | 12 | class QuartoAxeJsonReporter extends QuartoAxeReporter { 13 | constructor(axeResult, options) { 14 | super(axeResult, options); 15 | } 16 | 17 | report() { 18 | console.log(JSON.stringify(this.axeResult, null, 2)); 19 | } 20 | } 21 | 22 | class QuartoAxeConsoleReporter extends QuartoAxeReporter { 23 | constructor(axeResult, options) { 24 | super(axeResult, options); 25 | } 26 | 27 | report() { 28 | for (const violation of this.axeResult.violations) { 29 | console.log(violation.description); 30 | for (const node of violation.nodes) { 31 | for (const target of node.target) { 32 | console.log(target); 33 | console.log(document.querySelector(target)); 34 | } 35 | } 36 | } 37 | } 38 | } 39 | 40 | class QuartoAxeDocumentReporter extends QuartoAxeReporter { 41 | constructor(axeResult, options) { 42 | super(axeResult, options); 43 | } 44 | 45 | createViolationElement(violation) { 46 | const violationElement = document.createElement("div"); 47 | 48 | const descriptionElement = document.createElement("div"); 49 | descriptionElement.className = "quarto-axe-violation-description"; 50 | descriptionElement.innerText = `${violation.impact.replace(/^[a-z]/, match => match.toLocaleUpperCase())}: ${violation.description}`; 51 | violationElement.appendChild(descriptionElement); 52 | 53 | const helpElement = document.createElement("div"); 54 | helpElement.className = "quarto-axe-violation-help"; 55 | helpElement.innerText = violation.help; 56 | violationElement.appendChild(helpElement); 57 | 58 | const nodesElement = document.createElement("div"); 59 | nodesElement.className = "quarto-axe-violation-nodes"; 60 | violationElement.appendChild(nodesElement); 61 | const nodeElement = document.createElement("div"); 62 | nodeElement.className = "quarto-axe-violation-selector"; 63 | for (const node of violation.nodes) { 64 | for (const target of node.target) { 65 | const targetElement = document.createElement("span"); 66 | targetElement.className = "quarto-axe-violation-target"; 67 | targetElement.innerText = target; 68 | nodeElement.appendChild(targetElement); 69 | nodeElement.addEventListener("mouseenter", () => { 70 | const element = document.querySelector(target); 71 | if (element) { 72 | element.scrollIntoView({ behavior: "smooth", block: "center" }); 73 | element.classList.add("quarto-axe-hover-highlight"); 74 | setTimeout(() => { 75 | element.style.border = ""; 76 | }, 2000); 77 | } 78 | }); 79 | nodeElement.addEventListener("mouseleave", () => { 80 | const element = document.querySelector(target); 81 | if (element) { 82 | element.classList.remove("quarto-axe-hover-highlight"); 83 | } 84 | }); 85 | nodeElement.addEventListener("click", () => { 86 | console.log(document.querySelector(target)); 87 | }); 88 | nodeElement.appendChild(targetElement); 89 | } 90 | nodesElement.appendChild(nodeElement); 91 | } 92 | return violationElement; 93 | } 94 | 95 | report() { 96 | const violations = this.axeResult.violations; 97 | const reportElement = document.createElement("div"); 98 | reportElement.className = "quarto-axe-report"; 99 | if (violations.length === 0) { 100 | const noViolationsElement = document.createElement("div"); 101 | noViolationsElement.className = "quarto-axe-no-violations"; 102 | noViolationsElement.innerText = "No axe-core violations found."; 103 | reportElement.appendChild(noViolationsElement); 104 | } 105 | violations.forEach((violation) => { 106 | reportElement.appendChild(this.createViolationElement(violation)); 107 | }); 108 | document.querySelector("main").appendChild(reportElement); 109 | } 110 | } 111 | 112 | const reporters = { 113 | json: QuartoAxeJsonReporter, 114 | console: QuartoAxeConsoleReporter, 115 | document: QuartoAxeDocumentReporter, 116 | }; 117 | 118 | class QuartoAxeChecker { 119 | constructor(opts) { 120 | this.options = opts; 121 | } 122 | async init() { 123 | const axe = (await import("https://cdn.skypack.dev/pin/axe-core@v4.10.3-aVOFXWsJaCpVrtv89pCa/mode=imports,min/optimized/axe-core.js")).default; 124 | const result = await axe.run({ 125 | exclude: [ 126 | // https://github.com/microsoft/tabster/issues/288 127 | // MS has claimed they won't fix this, so we need to add an exclusion to 128 | // all tabster elements 129 | "[data-tabster-dummy]" 130 | ], 131 | preload: { assets: ['cssom'], timeout: 50000 } 132 | }); 133 | const reporter = this.options === true ? new QuartoAxeConsoleReporter(result) : new reporters[this.options.output](result, this.options); 134 | reporter.report(); 135 | } 136 | } 137 | 138 | export async function init() { 139 | const opts = document.querySelector("#quarto-axe-checker-options"); 140 | if (opts) { 141 | const jsonOptions = JSON.parse(atob(opts.textContent)); 142 | const checker = new QuartoAxeChecker(jsonOptions); 143 | await checker.init(); 144 | } 145 | } -------------------------------------------------------------------------------- /docs/site_libs/quarto-html/popper.min.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @popperjs/core v2.11.7 - MIT License 3 | */ 4 | 5 | !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Popper={})}(this,(function(e){"use strict";function t(e){if(null==e)return window;if("[object Window]"!==e.toString()){var t=e.ownerDocument;return t&&t.defaultView||window}return e}function n(e){return e instanceof t(e).Element||e instanceof Element}function r(e){return e instanceof t(e).HTMLElement||e instanceof HTMLElement}function o(e){return"undefined"!=typeof ShadowRoot&&(e instanceof t(e).ShadowRoot||e instanceof ShadowRoot)}var i=Math.max,a=Math.min,s=Math.round;function f(){var e=navigator.userAgentData;return null!=e&&e.brands&&Array.isArray(e.brands)?e.brands.map((function(e){return e.brand+"/"+e.version})).join(" "):navigator.userAgent}function c(){return!/^((?!chrome|android).)*safari/i.test(f())}function p(e,o,i){void 0===o&&(o=!1),void 0===i&&(i=!1);var a=e.getBoundingClientRect(),f=1,p=1;o&&r(e)&&(f=e.offsetWidth>0&&s(a.width)/e.offsetWidth||1,p=e.offsetHeight>0&&s(a.height)/e.offsetHeight||1);var u=(n(e)?t(e):window).visualViewport,l=!c()&&i,d=(a.left+(l&&u?u.offsetLeft:0))/f,h=(a.top+(l&&u?u.offsetTop:0))/p,m=a.width/f,v=a.height/p;return{width:m,height:v,top:h,right:d+m,bottom:h+v,left:d,x:d,y:h}}function u(e){var n=t(e);return{scrollLeft:n.pageXOffset,scrollTop:n.pageYOffset}}function l(e){return e?(e.nodeName||"").toLowerCase():null}function d(e){return((n(e)?e.ownerDocument:e.document)||window.document).documentElement}function h(e){return p(d(e)).left+u(e).scrollLeft}function m(e){return t(e).getComputedStyle(e)}function v(e){var t=m(e),n=t.overflow,r=t.overflowX,o=t.overflowY;return/auto|scroll|overlay|hidden/.test(n+o+r)}function y(e,n,o){void 0===o&&(o=!1);var i,a,f=r(n),c=r(n)&&function(e){var t=e.getBoundingClientRect(),n=s(t.width)/e.offsetWidth||1,r=s(t.height)/e.offsetHeight||1;return 1!==n||1!==r}(n),m=d(n),y=p(e,c,o),g={scrollLeft:0,scrollTop:0},b={x:0,y:0};return(f||!f&&!o)&&(("body"!==l(n)||v(m))&&(g=(i=n)!==t(i)&&r(i)?{scrollLeft:(a=i).scrollLeft,scrollTop:a.scrollTop}:u(i)),r(n)?((b=p(n,!0)).x+=n.clientLeft,b.y+=n.clientTop):m&&(b.x=h(m))),{x:y.left+g.scrollLeft-b.x,y:y.top+g.scrollTop-b.y,width:y.width,height:y.height}}function g(e){var t=p(e),n=e.offsetWidth,r=e.offsetHeight;return Math.abs(t.width-n)<=1&&(n=t.width),Math.abs(t.height-r)<=1&&(r=t.height),{x:e.offsetLeft,y:e.offsetTop,width:n,height:r}}function b(e){return"html"===l(e)?e:e.assignedSlot||e.parentNode||(o(e)?e.host:null)||d(e)}function x(e){return["html","body","#document"].indexOf(l(e))>=0?e.ownerDocument.body:r(e)&&v(e)?e:x(b(e))}function w(e,n){var r;void 0===n&&(n=[]);var o=x(e),i=o===(null==(r=e.ownerDocument)?void 0:r.body),a=t(o),s=i?[a].concat(a.visualViewport||[],v(o)?o:[]):o,f=n.concat(s);return i?f:f.concat(w(b(s)))}function O(e){return["table","td","th"].indexOf(l(e))>=0}function j(e){return r(e)&&"fixed"!==m(e).position?e.offsetParent:null}function E(e){for(var n=t(e),i=j(e);i&&O(i)&&"static"===m(i).position;)i=j(i);return i&&("html"===l(i)||"body"===l(i)&&"static"===m(i).position)?n:i||function(e){var t=/firefox/i.test(f());if(/Trident/i.test(f())&&r(e)&&"fixed"===m(e).position)return null;var n=b(e);for(o(n)&&(n=n.host);r(n)&&["html","body"].indexOf(l(n))<0;){var i=m(n);if("none"!==i.transform||"none"!==i.perspective||"paint"===i.contain||-1!==["transform","perspective"].indexOf(i.willChange)||t&&"filter"===i.willChange||t&&i.filter&&"none"!==i.filter)return n;n=n.parentNode}return null}(e)||n}var D="top",A="bottom",L="right",P="left",M="auto",k=[D,A,L,P],W="start",B="end",H="viewport",T="popper",R=k.reduce((function(e,t){return e.concat([t+"-"+W,t+"-"+B])}),[]),S=[].concat(k,[M]).reduce((function(e,t){return e.concat([t,t+"-"+W,t+"-"+B])}),[]),V=["beforeRead","read","afterRead","beforeMain","main","afterMain","beforeWrite","write","afterWrite"];function q(e){var t=new Map,n=new Set,r=[];function o(e){n.add(e.name),[].concat(e.requires||[],e.requiresIfExists||[]).forEach((function(e){if(!n.has(e)){var r=t.get(e);r&&o(r)}})),r.push(e)}return e.forEach((function(e){t.set(e.name,e)})),e.forEach((function(e){n.has(e.name)||o(e)})),r}function C(e){return e.split("-")[0]}function N(e,t){var n=t.getRootNode&&t.getRootNode();if(e.contains(t))return!0;if(n&&o(n)){var r=t;do{if(r&&e.isSameNode(r))return!0;r=r.parentNode||r.host}while(r)}return!1}function I(e){return Object.assign({},e,{left:e.x,top:e.y,right:e.x+e.width,bottom:e.y+e.height})}function _(e,r,o){return r===H?I(function(e,n){var r=t(e),o=d(e),i=r.visualViewport,a=o.clientWidth,s=o.clientHeight,f=0,p=0;if(i){a=i.width,s=i.height;var u=c();(u||!u&&"fixed"===n)&&(f=i.offsetLeft,p=i.offsetTop)}return{width:a,height:s,x:f+h(e),y:p}}(e,o)):n(r)?function(e,t){var n=p(e,!1,"fixed"===t);return n.top=n.top+e.clientTop,n.left=n.left+e.clientLeft,n.bottom=n.top+e.clientHeight,n.right=n.left+e.clientWidth,n.width=e.clientWidth,n.height=e.clientHeight,n.x=n.left,n.y=n.top,n}(r,o):I(function(e){var t,n=d(e),r=u(e),o=null==(t=e.ownerDocument)?void 0:t.body,a=i(n.scrollWidth,n.clientWidth,o?o.scrollWidth:0,o?o.clientWidth:0),s=i(n.scrollHeight,n.clientHeight,o?o.scrollHeight:0,o?o.clientHeight:0),f=-r.scrollLeft+h(e),c=-r.scrollTop;return"rtl"===m(o||n).direction&&(f+=i(n.clientWidth,o?o.clientWidth:0)-a),{width:a,height:s,x:f,y:c}}(d(e)))}function F(e,t,o,s){var f="clippingParents"===t?function(e){var t=w(b(e)),o=["absolute","fixed"].indexOf(m(e).position)>=0&&r(e)?E(e):e;return n(o)?t.filter((function(e){return n(e)&&N(e,o)&&"body"!==l(e)})):[]}(e):[].concat(t),c=[].concat(f,[o]),p=c[0],u=c.reduce((function(t,n){var r=_(e,n,s);return t.top=i(r.top,t.top),t.right=a(r.right,t.right),t.bottom=a(r.bottom,t.bottom),t.left=i(r.left,t.left),t}),_(e,p,s));return u.width=u.right-u.left,u.height=u.bottom-u.top,u.x=u.left,u.y=u.top,u}function U(e){return e.split("-")[1]}function z(e){return["top","bottom"].indexOf(e)>=0?"x":"y"}function X(e){var t,n=e.reference,r=e.element,o=e.placement,i=o?C(o):null,a=o?U(o):null,s=n.x+n.width/2-r.width/2,f=n.y+n.height/2-r.height/2;switch(i){case D:t={x:s,y:n.y-r.height};break;case A:t={x:s,y:n.y+n.height};break;case L:t={x:n.x+n.width,y:f};break;case P:t={x:n.x-r.width,y:f};break;default:t={x:n.x,y:n.y}}var c=i?z(i):null;if(null!=c){var p="y"===c?"height":"width";switch(a){case W:t[c]=t[c]-(n[p]/2-r[p]/2);break;case B:t[c]=t[c]+(n[p]/2-r[p]/2)}}return t}function Y(e){return Object.assign({},{top:0,right:0,bottom:0,left:0},e)}function G(e,t){return t.reduce((function(t,n){return t[n]=e,t}),{})}function J(e,t){void 0===t&&(t={});var r=t,o=r.placement,i=void 0===o?e.placement:o,a=r.strategy,s=void 0===a?e.strategy:a,f=r.boundary,c=void 0===f?"clippingParents":f,u=r.rootBoundary,l=void 0===u?H:u,h=r.elementContext,m=void 0===h?T:h,v=r.altBoundary,y=void 0!==v&&v,g=r.padding,b=void 0===g?0:g,x=Y("number"!=typeof b?b:G(b,k)),w=m===T?"reference":T,O=e.rects.popper,j=e.elements[y?w:m],E=F(n(j)?j:j.contextElement||d(e.elements.popper),c,l,s),P=p(e.elements.reference),M=X({reference:P,element:O,strategy:"absolute",placement:i}),W=I(Object.assign({},O,M)),B=m===T?W:P,R={top:E.top-B.top+x.top,bottom:B.bottom-E.bottom+x.bottom,left:E.left-B.left+x.left,right:B.right-E.right+x.right},S=e.modifiersData.offset;if(m===T&&S){var V=S[i];Object.keys(R).forEach((function(e){var t=[L,A].indexOf(e)>=0?1:-1,n=[D,A].indexOf(e)>=0?"y":"x";R[e]+=V[n]*t}))}return R}var K={placement:"bottom",modifiers:[],strategy:"absolute"};function Q(){for(var e=arguments.length,t=new Array(e),n=0;n=0?-1:1,i="function"==typeof n?n(Object.assign({},t,{placement:e})):n,a=i[0],s=i[1];return a=a||0,s=(s||0)*o,[P,L].indexOf(r)>=0?{x:s,y:a}:{x:a,y:s}}(n,t.rects,i),e}),{}),s=a[t.placement],f=s.x,c=s.y;null!=t.modifiersData.popperOffsets&&(t.modifiersData.popperOffsets.x+=f,t.modifiersData.popperOffsets.y+=c),t.modifiersData[r]=a}},se={left:"right",right:"left",bottom:"top",top:"bottom"};function fe(e){return e.replace(/left|right|bottom|top/g,(function(e){return se[e]}))}var ce={start:"end",end:"start"};function pe(e){return e.replace(/start|end/g,(function(e){return ce[e]}))}function ue(e,t){void 0===t&&(t={});var n=t,r=n.placement,o=n.boundary,i=n.rootBoundary,a=n.padding,s=n.flipVariations,f=n.allowedAutoPlacements,c=void 0===f?S:f,p=U(r),u=p?s?R:R.filter((function(e){return U(e)===p})):k,l=u.filter((function(e){return c.indexOf(e)>=0}));0===l.length&&(l=u);var d=l.reduce((function(t,n){return t[n]=J(e,{placement:n,boundary:o,rootBoundary:i,padding:a})[C(n)],t}),{});return Object.keys(d).sort((function(e,t){return d[e]-d[t]}))}var le={name:"flip",enabled:!0,phase:"main",fn:function(e){var t=e.state,n=e.options,r=e.name;if(!t.modifiersData[r]._skip){for(var o=n.mainAxis,i=void 0===o||o,a=n.altAxis,s=void 0===a||a,f=n.fallbackPlacements,c=n.padding,p=n.boundary,u=n.rootBoundary,l=n.altBoundary,d=n.flipVariations,h=void 0===d||d,m=n.allowedAutoPlacements,v=t.options.placement,y=C(v),g=f||(y===v||!h?[fe(v)]:function(e){if(C(e)===M)return[];var t=fe(e);return[pe(e),t,pe(t)]}(v)),b=[v].concat(g).reduce((function(e,n){return e.concat(C(n)===M?ue(t,{placement:n,boundary:p,rootBoundary:u,padding:c,flipVariations:h,allowedAutoPlacements:m}):n)}),[]),x=t.rects.reference,w=t.rects.popper,O=new Map,j=!0,E=b[0],k=0;k=0,S=R?"width":"height",V=J(t,{placement:B,boundary:p,rootBoundary:u,altBoundary:l,padding:c}),q=R?T?L:P:T?A:D;x[S]>w[S]&&(q=fe(q));var N=fe(q),I=[];if(i&&I.push(V[H]<=0),s&&I.push(V[q]<=0,V[N]<=0),I.every((function(e){return e}))){E=B,j=!1;break}O.set(B,I)}if(j)for(var _=function(e){var t=b.find((function(t){var n=O.get(t);if(n)return n.slice(0,e).every((function(e){return e}))}));if(t)return E=t,"break"},F=h?3:1;F>0;F--){if("break"===_(F))break}t.placement!==E&&(t.modifiersData[r]._skip=!0,t.placement=E,t.reset=!0)}},requiresIfExists:["offset"],data:{_skip:!1}};function de(e,t,n){return i(e,a(t,n))}var he={name:"preventOverflow",enabled:!0,phase:"main",fn:function(e){var t=e.state,n=e.options,r=e.name,o=n.mainAxis,s=void 0===o||o,f=n.altAxis,c=void 0!==f&&f,p=n.boundary,u=n.rootBoundary,l=n.altBoundary,d=n.padding,h=n.tether,m=void 0===h||h,v=n.tetherOffset,y=void 0===v?0:v,b=J(t,{boundary:p,rootBoundary:u,padding:d,altBoundary:l}),x=C(t.placement),w=U(t.placement),O=!w,j=z(x),M="x"===j?"y":"x",k=t.modifiersData.popperOffsets,B=t.rects.reference,H=t.rects.popper,T="function"==typeof y?y(Object.assign({},t.rects,{placement:t.placement})):y,R="number"==typeof T?{mainAxis:T,altAxis:T}:Object.assign({mainAxis:0,altAxis:0},T),S=t.modifiersData.offset?t.modifiersData.offset[t.placement]:null,V={x:0,y:0};if(k){if(s){var q,N="y"===j?D:P,I="y"===j?A:L,_="y"===j?"height":"width",F=k[j],X=F+b[N],Y=F-b[I],G=m?-H[_]/2:0,K=w===W?B[_]:H[_],Q=w===W?-H[_]:-B[_],Z=t.elements.arrow,$=m&&Z?g(Z):{width:0,height:0},ee=t.modifiersData["arrow#persistent"]?t.modifiersData["arrow#persistent"].padding:{top:0,right:0,bottom:0,left:0},te=ee[N],ne=ee[I],re=de(0,B[_],$[_]),oe=O?B[_]/2-G-re-te-R.mainAxis:K-re-te-R.mainAxis,ie=O?-B[_]/2+G+re+ne+R.mainAxis:Q+re+ne+R.mainAxis,ae=t.elements.arrow&&E(t.elements.arrow),se=ae?"y"===j?ae.clientTop||0:ae.clientLeft||0:0,fe=null!=(q=null==S?void 0:S[j])?q:0,ce=F+ie-fe,pe=de(m?a(X,F+oe-fe-se):X,F,m?i(Y,ce):Y);k[j]=pe,V[j]=pe-F}if(c){var ue,le="x"===j?D:P,he="x"===j?A:L,me=k[M],ve="y"===M?"height":"width",ye=me+b[le],ge=me-b[he],be=-1!==[D,P].indexOf(x),xe=null!=(ue=null==S?void 0:S[M])?ue:0,we=be?ye:me-B[ve]-H[ve]-xe+R.altAxis,Oe=be?me+B[ve]+H[ve]-xe-R.altAxis:ge,je=m&&be?function(e,t,n){var r=de(e,t,n);return r>n?n:r}(we,me,Oe):de(m?we:ye,me,m?Oe:ge);k[M]=je,V[M]=je-me}t.modifiersData[r]=V}},requiresIfExists:["offset"]};var me={name:"arrow",enabled:!0,phase:"main",fn:function(e){var t,n=e.state,r=e.name,o=e.options,i=n.elements.arrow,a=n.modifiersData.popperOffsets,s=C(n.placement),f=z(s),c=[P,L].indexOf(s)>=0?"height":"width";if(i&&a){var p=function(e,t){return Y("number"!=typeof(e="function"==typeof e?e(Object.assign({},t.rects,{placement:t.placement})):e)?e:G(e,k))}(o.padding,n),u=g(i),l="y"===f?D:P,d="y"===f?A:L,h=n.rects.reference[c]+n.rects.reference[f]-a[f]-n.rects.popper[c],m=a[f]-n.rects.reference[f],v=E(i),y=v?"y"===f?v.clientHeight||0:v.clientWidth||0:0,b=h/2-m/2,x=p[l],w=y-u[c]-p[d],O=y/2-u[c]/2+b,j=de(x,O,w),M=f;n.modifiersData[r]=((t={})[M]=j,t.centerOffset=j-O,t)}},effect:function(e){var t=e.state,n=e.options.element,r=void 0===n?"[data-popper-arrow]":n;null!=r&&("string"!=typeof r||(r=t.elements.popper.querySelector(r)))&&N(t.elements.popper,r)&&(t.elements.arrow=r)},requires:["popperOffsets"],requiresIfExists:["preventOverflow"]};function ve(e,t,n){return void 0===n&&(n={x:0,y:0}),{top:e.top-t.height-n.y,right:e.right-t.width+n.x,bottom:e.bottom-t.height+n.y,left:e.left-t.width-n.x}}function ye(e){return[D,L,A,P].some((function(t){return e[t]>=0}))}var ge={name:"hide",enabled:!0,phase:"main",requiresIfExists:["preventOverflow"],fn:function(e){var t=e.state,n=e.name,r=t.rects.reference,o=t.rects.popper,i=t.modifiersData.preventOverflow,a=J(t,{elementContext:"reference"}),s=J(t,{altBoundary:!0}),f=ve(a,r),c=ve(s,o,i),p=ye(f),u=ye(c);t.modifiersData[n]={referenceClippingOffsets:f,popperEscapeOffsets:c,isReferenceHidden:p,hasPopperEscaped:u},t.attributes.popper=Object.assign({},t.attributes.popper,{"data-popper-reference-hidden":p,"data-popper-escaped":u})}},be=Z({defaultModifiers:[ee,te,oe,ie]}),xe=[ee,te,oe,ie,ae,le,he,me,ge],we=Z({defaultModifiers:xe});e.applyStyles=ie,e.arrow=me,e.computeStyles=oe,e.createPopper=we,e.createPopperLite=be,e.defaultModifiers=xe,e.detectOverflow=J,e.eventListeners=ee,e.flip=le,e.hide=ge,e.offset=ae,e.popperGenerator=Z,e.popperOffsets=te,e.preventOverflow=he,Object.defineProperty(e,"__esModule",{value:!0})})); 6 | 7 | -------------------------------------------------------------------------------- /docs/site_libs/quarto-html/quarto-syntax-highlighting-1fe81d0376b2c50856e68e651e390326.css: -------------------------------------------------------------------------------- 1 | /* quarto syntax highlight colors */ 2 | :root { 3 | --quarto-hl-ot-color: #003B4F; 4 | --quarto-hl-at-color: #657422; 5 | --quarto-hl-ss-color: #20794D; 6 | --quarto-hl-an-color: #5E5E5E; 7 | --quarto-hl-fu-color: #4758AB; 8 | --quarto-hl-st-color: #20794D; 9 | --quarto-hl-cf-color: #003B4F; 10 | --quarto-hl-op-color: #5E5E5E; 11 | --quarto-hl-er-color: #AD0000; 12 | --quarto-hl-bn-color: #AD0000; 13 | --quarto-hl-al-color: #AD0000; 14 | --quarto-hl-va-color: #111111; 15 | --quarto-hl-bu-color: inherit; 16 | --quarto-hl-ex-color: inherit; 17 | --quarto-hl-pp-color: #AD0000; 18 | --quarto-hl-in-color: #5E5E5E; 19 | --quarto-hl-vs-color: #20794D; 20 | --quarto-hl-wa-color: #5E5E5E; 21 | --quarto-hl-do-color: #5E5E5E; 22 | --quarto-hl-im-color: #00769E; 23 | --quarto-hl-ch-color: #20794D; 24 | --quarto-hl-dt-color: #AD0000; 25 | --quarto-hl-fl-color: #AD0000; 26 | --quarto-hl-co-color: #5E5E5E; 27 | --quarto-hl-cv-color: #5E5E5E; 28 | --quarto-hl-cn-color: #8f5902; 29 | --quarto-hl-sc-color: #5E5E5E; 30 | --quarto-hl-dv-color: #AD0000; 31 | --quarto-hl-kw-color: #003B4F; 32 | } 33 | 34 | /* other quarto variables */ 35 | :root { 36 | --quarto-font-monospace: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; 37 | } 38 | 39 | /* syntax highlight based on Pandoc's rules */ 40 | pre > code.sourceCode > span { 41 | color: #003B4F; 42 | } 43 | 44 | code.sourceCode > span { 45 | color: #003B4F; 46 | } 47 | 48 | div.sourceCode, 49 | div.sourceCode pre.sourceCode { 50 | color: #003B4F; 51 | } 52 | 53 | /* Normal */ 54 | code span { 55 | color: #003B4F; 56 | } 57 | 58 | /* Alert */ 59 | code span.al { 60 | color: #AD0000; 61 | font-style: inherit; 62 | } 63 | 64 | /* Annotation */ 65 | code span.an { 66 | color: #5E5E5E; 67 | font-style: inherit; 68 | } 69 | 70 | /* Attribute */ 71 | code span.at { 72 | color: #657422; 73 | font-style: inherit; 74 | } 75 | 76 | /* BaseN */ 77 | code span.bn { 78 | color: #AD0000; 79 | font-style: inherit; 80 | } 81 | 82 | /* BuiltIn */ 83 | code span.bu { 84 | font-style: inherit; 85 | } 86 | 87 | /* ControlFlow */ 88 | code span.cf { 89 | color: #003B4F; 90 | font-weight: bold; 91 | font-style: inherit; 92 | } 93 | 94 | /* Char */ 95 | code span.ch { 96 | color: #20794D; 97 | font-style: inherit; 98 | } 99 | 100 | /* Constant */ 101 | code span.cn { 102 | color: #8f5902; 103 | font-style: inherit; 104 | } 105 | 106 | /* Comment */ 107 | code span.co { 108 | color: #5E5E5E; 109 | font-style: inherit; 110 | } 111 | 112 | /* CommentVar */ 113 | code span.cv { 114 | color: #5E5E5E; 115 | font-style: italic; 116 | } 117 | 118 | /* Documentation */ 119 | code span.do { 120 | color: #5E5E5E; 121 | font-style: italic; 122 | } 123 | 124 | /* DataType */ 125 | code span.dt { 126 | color: #AD0000; 127 | font-style: inherit; 128 | } 129 | 130 | /* DecVal */ 131 | code span.dv { 132 | color: #AD0000; 133 | font-style: inherit; 134 | } 135 | 136 | /* Error */ 137 | code span.er { 138 | color: #AD0000; 139 | font-style: inherit; 140 | } 141 | 142 | /* Extension */ 143 | code span.ex { 144 | font-style: inherit; 145 | } 146 | 147 | /* Float */ 148 | code span.fl { 149 | color: #AD0000; 150 | font-style: inherit; 151 | } 152 | 153 | /* Function */ 154 | code span.fu { 155 | color: #4758AB; 156 | font-style: inherit; 157 | } 158 | 159 | /* Import */ 160 | code span.im { 161 | color: #00769E; 162 | font-style: inherit; 163 | } 164 | 165 | /* Information */ 166 | code span.in { 167 | color: #5E5E5E; 168 | font-style: inherit; 169 | } 170 | 171 | /* Keyword */ 172 | code span.kw { 173 | color: #003B4F; 174 | font-weight: bold; 175 | font-style: inherit; 176 | } 177 | 178 | /* Operator */ 179 | code span.op { 180 | color: #5E5E5E; 181 | font-style: inherit; 182 | } 183 | 184 | /* Other */ 185 | code span.ot { 186 | color: #003B4F; 187 | font-style: inherit; 188 | } 189 | 190 | /* Preprocessor */ 191 | code span.pp { 192 | color: #AD0000; 193 | font-style: inherit; 194 | } 195 | 196 | /* SpecialChar */ 197 | code span.sc { 198 | color: #5E5E5E; 199 | font-style: inherit; 200 | } 201 | 202 | /* SpecialString */ 203 | code span.ss { 204 | color: #20794D; 205 | font-style: inherit; 206 | } 207 | 208 | /* String */ 209 | code span.st { 210 | color: #20794D; 211 | font-style: inherit; 212 | } 213 | 214 | /* Variable */ 215 | code span.va { 216 | color: #111111; 217 | font-style: inherit; 218 | } 219 | 220 | /* VerbatimString */ 221 | code span.vs { 222 | color: #20794D; 223 | font-style: inherit; 224 | } 225 | 226 | /* Warning */ 227 | code span.wa { 228 | color: #5E5E5E; 229 | font-style: italic; 230 | } 231 | 232 | .prevent-inlining { 233 | content: " { 5 | function getTabSettings() { 6 | const data = localStorage.getItem("quarto-persistent-tabsets-data"); 7 | if (!data) { 8 | localStorage.setItem("quarto-persistent-tabsets-data", "{}"); 9 | return {}; 10 | } 11 | if (data) { 12 | return JSON.parse(data); 13 | } 14 | } 15 | 16 | function setTabSettings(data) { 17 | localStorage.setItem( 18 | "quarto-persistent-tabsets-data", 19 | JSON.stringify(data) 20 | ); 21 | } 22 | 23 | function setTabState(groupName, groupValue) { 24 | const data = getTabSettings(); 25 | data[groupName] = groupValue; 26 | setTabSettings(data); 27 | } 28 | 29 | function toggleTab(tab, active) { 30 | const tabPanelId = tab.getAttribute("aria-controls"); 31 | const tabPanel = document.getElementById(tabPanelId); 32 | if (active) { 33 | tab.classList.add("active"); 34 | tabPanel.classList.add("active"); 35 | } else { 36 | tab.classList.remove("active"); 37 | tabPanel.classList.remove("active"); 38 | } 39 | } 40 | 41 | function toggleAll(selectedGroup, selectorsToSync) { 42 | for (const [thisGroup, tabs] of Object.entries(selectorsToSync)) { 43 | const active = selectedGroup === thisGroup; 44 | for (const tab of tabs) { 45 | toggleTab(tab, active); 46 | } 47 | } 48 | } 49 | 50 | function findSelectorsToSyncByLanguage() { 51 | const result = {}; 52 | const tabs = Array.from( 53 | document.querySelectorAll(`div[data-group] a[id^='tabset-']`) 54 | ); 55 | for (const item of tabs) { 56 | const div = item.parentElement.parentElement.parentElement; 57 | const group = div.getAttribute("data-group"); 58 | if (!result[group]) { 59 | result[group] = {}; 60 | } 61 | const selectorsToSync = result[group]; 62 | const value = item.innerHTML; 63 | if (!selectorsToSync[value]) { 64 | selectorsToSync[value] = []; 65 | } 66 | selectorsToSync[value].push(item); 67 | } 68 | return result; 69 | } 70 | 71 | function setupSelectorSync() { 72 | const selectorsToSync = findSelectorsToSyncByLanguage(); 73 | Object.entries(selectorsToSync).forEach(([group, tabSetsByValue]) => { 74 | Object.entries(tabSetsByValue).forEach(([value, items]) => { 75 | items.forEach((item) => { 76 | item.addEventListener("click", (_event) => { 77 | setTabState(group, value); 78 | toggleAll(value, selectorsToSync[group]); 79 | }); 80 | }); 81 | }); 82 | }); 83 | return selectorsToSync; 84 | } 85 | 86 | const selectorsToSync = setupSelectorSync(); 87 | for (const [group, selectedName] of Object.entries(getTabSettings())) { 88 | const selectors = selectorsToSync[group]; 89 | // it's possible that stale state gives us empty selections, so we explicitly check here. 90 | if (selectors) { 91 | toggleAll(selectedName, selectors); 92 | } 93 | } 94 | }); 95 | } 96 | -------------------------------------------------------------------------------- /docs/site_libs/quarto-html/tippy.css: -------------------------------------------------------------------------------- 1 | .tippy-box[data-animation=fade][data-state=hidden]{opacity:0}[data-tippy-root]{max-width:calc(100vw - 10px)}.tippy-box{position:relative;background-color:#333;color:#fff;border-radius:4px;font-size:14px;line-height:1.4;white-space:normal;outline:0;transition-property:transform,visibility,opacity}.tippy-box[data-placement^=top]>.tippy-arrow{bottom:0}.tippy-box[data-placement^=top]>.tippy-arrow:before{bottom:-7px;left:0;border-width:8px 8px 0;border-top-color:initial;transform-origin:center top}.tippy-box[data-placement^=bottom]>.tippy-arrow{top:0}.tippy-box[data-placement^=bottom]>.tippy-arrow:before{top:-7px;left:0;border-width:0 8px 8px;border-bottom-color:initial;transform-origin:center bottom}.tippy-box[data-placement^=left]>.tippy-arrow{right:0}.tippy-box[data-placement^=left]>.tippy-arrow:before{border-width:8px 0 8px 8px;border-left-color:initial;right:-7px;transform-origin:center left}.tippy-box[data-placement^=right]>.tippy-arrow{left:0}.tippy-box[data-placement^=right]>.tippy-arrow:before{left:-7px;border-width:8px 8px 8px 0;border-right-color:initial;transform-origin:center right}.tippy-box[data-inertia][data-state=visible]{transition-timing-function:cubic-bezier(.54,1.5,.38,1.11)}.tippy-arrow{width:16px;height:16px;color:#333}.tippy-arrow:before{content:"";position:absolute;border-color:transparent;border-style:solid}.tippy-content{position:relative;padding:5px 9px;z-index:1} -------------------------------------------------------------------------------- /docs/site_libs/quarto-nav/headroom.min.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * headroom.js v0.12.0 - Give your page some headroom. Hide your header until you need it 3 | * Copyright (c) 2020 Nick Williams - http://wicky.nillia.ms/headroom.js 4 | * License: MIT 5 | */ 6 | 7 | !function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(t=t||self).Headroom=n()}(this,function(){"use strict";function t(){return"undefined"!=typeof window}function d(t){return function(t){return t&&t.document&&function(t){return 9===t.nodeType}(t.document)}(t)?function(t){var n=t.document,o=n.body,s=n.documentElement;return{scrollHeight:function(){return Math.max(o.scrollHeight,s.scrollHeight,o.offsetHeight,s.offsetHeight,o.clientHeight,s.clientHeight)},height:function(){return t.innerHeight||s.clientHeight||o.clientHeight},scrollY:function(){return void 0!==t.pageYOffset?t.pageYOffset:(s||o.parentNode||o).scrollTop}}}(t):function(t){return{scrollHeight:function(){return Math.max(t.scrollHeight,t.offsetHeight,t.clientHeight)},height:function(){return Math.max(t.offsetHeight,t.clientHeight)},scrollY:function(){return t.scrollTop}}}(t)}function n(t,s,e){var n,o=function(){var n=!1;try{var t={get passive(){n=!0}};window.addEventListener("test",t,t),window.removeEventListener("test",t,t)}catch(t){n=!1}return n}(),i=!1,r=d(t),l=r.scrollY(),a={};function c(){var t=Math.round(r.scrollY()),n=r.height(),o=r.scrollHeight();a.scrollY=t,a.lastScrollY=l,a.direction=ls.tolerance[a.direction],e(a),l=t,i=!1}function h(){i||(i=!0,n=requestAnimationFrame(c))}var u=!!o&&{passive:!0,capture:!1};return t.addEventListener("scroll",h,u),c(),{destroy:function(){cancelAnimationFrame(n),t.removeEventListener("scroll",h,u)}}}function o(t){return t===Object(t)?t:{down:t,up:t}}function s(t,n){n=n||{},Object.assign(this,s.options,n),this.classes=Object.assign({},s.options.classes,n.classes),this.elem=t,this.tolerance=o(this.tolerance),this.offset=o(this.offset),this.initialised=!1,this.frozen=!1}return s.prototype={constructor:s,init:function(){return s.cutsTheMustard&&!this.initialised&&(this.addClass("initial"),this.initialised=!0,setTimeout(function(t){t.scrollTracker=n(t.scroller,{offset:t.offset,tolerance:t.tolerance},t.update.bind(t))},100,this)),this},destroy:function(){this.initialised=!1,Object.keys(this.classes).forEach(this.removeClass,this),this.scrollTracker.destroy()},unpin:function(){!this.hasClass("pinned")&&this.hasClass("unpinned")||(this.addClass("unpinned"),this.removeClass("pinned"),this.onUnpin&&this.onUnpin.call(this))},pin:function(){this.hasClass("unpinned")&&(this.addClass("pinned"),this.removeClass("unpinned"),this.onPin&&this.onPin.call(this))},freeze:function(){this.frozen=!0,this.addClass("frozen")},unfreeze:function(){this.frozen=!1,this.removeClass("frozen")},top:function(){this.hasClass("top")||(this.addClass("top"),this.removeClass("notTop"),this.onTop&&this.onTop.call(this))},notTop:function(){this.hasClass("notTop")||(this.addClass("notTop"),this.removeClass("top"),this.onNotTop&&this.onNotTop.call(this))},bottom:function(){this.hasClass("bottom")||(this.addClass("bottom"),this.removeClass("notBottom"),this.onBottom&&this.onBottom.call(this))},notBottom:function(){this.hasClass("notBottom")||(this.addClass("notBottom"),this.removeClass("bottom"),this.onNotBottom&&this.onNotBottom.call(this))},shouldUnpin:function(t){return"down"===t.direction&&!t.top&&t.toleranceExceeded},shouldPin:function(t){return"up"===t.direction&&t.toleranceExceeded||t.top},addClass:function(t){this.elem.classList.add.apply(this.elem.classList,this.classes[t].split(" "))},removeClass:function(t){this.elem.classList.remove.apply(this.elem.classList,this.classes[t].split(" "))},hasClass:function(t){return this.classes[t].split(" ").every(function(t){return this.classList.contains(t)},this.elem)},update:function(t){t.isOutOfBounds||!0!==this.frozen&&(t.top?this.top():this.notTop(),t.bottom?this.bottom():this.notBottom(),this.shouldUnpin(t)?this.unpin():this.shouldPin(t)&&this.pin())}},s.options={tolerance:{up:0,down:0},offset:0,scroller:t()?window:null,classes:{frozen:"headroom--frozen",pinned:"headroom--pinned",unpinned:"headroom--unpinned",top:"headroom--top",notTop:"headroom--not-top",bottom:"headroom--bottom",notBottom:"headroom--not-bottom",initial:"headroom"}},s.cutsTheMustard=!!(t()&&function(){}.bind&&"classList"in document.documentElement&&Object.assign&&Object.keys&&requestAnimationFrame),s}); 8 | -------------------------------------------------------------------------------- /docs/site_libs/quarto-nav/quarto-nav.js: -------------------------------------------------------------------------------- 1 | const headroomChanged = new CustomEvent("quarto-hrChanged", { 2 | detail: {}, 3 | bubbles: true, 4 | cancelable: false, 5 | composed: false, 6 | }); 7 | 8 | const announceDismiss = () => { 9 | const annEl = window.document.getElementById("quarto-announcement"); 10 | if (annEl) { 11 | annEl.remove(); 12 | 13 | const annId = annEl.getAttribute("data-announcement-id"); 14 | window.localStorage.setItem(`quarto-announce-${annId}`, "true"); 15 | } 16 | }; 17 | 18 | const announceRegister = () => { 19 | const annEl = window.document.getElementById("quarto-announcement"); 20 | if (annEl) { 21 | const annId = annEl.getAttribute("data-announcement-id"); 22 | const isDismissed = 23 | window.localStorage.getItem(`quarto-announce-${annId}`) || false; 24 | if (isDismissed) { 25 | announceDismiss(); 26 | return; 27 | } else { 28 | annEl.classList.remove("hidden"); 29 | } 30 | 31 | const actionEl = annEl.querySelector(".quarto-announcement-action"); 32 | if (actionEl) { 33 | actionEl.addEventListener("click", function (e) { 34 | e.preventDefault(); 35 | // Hide the bar immediately 36 | announceDismiss(); 37 | }); 38 | } 39 | } 40 | }; 41 | 42 | window.document.addEventListener("DOMContentLoaded", function () { 43 | let init = false; 44 | 45 | announceRegister(); 46 | 47 | // Manage the back to top button, if one is present. 48 | let lastScrollTop = window.pageYOffset || document.documentElement.scrollTop; 49 | const scrollDownBuffer = 5; 50 | const scrollUpBuffer = 35; 51 | const btn = document.getElementById("quarto-back-to-top"); 52 | const hideBackToTop = () => { 53 | btn.style.display = "none"; 54 | }; 55 | const showBackToTop = () => { 56 | btn.style.display = "inline-block"; 57 | }; 58 | if (btn) { 59 | window.document.addEventListener( 60 | "scroll", 61 | function () { 62 | const currentScrollTop = 63 | window.pageYOffset || document.documentElement.scrollTop; 64 | 65 | // Shows and hides the button 'intelligently' as the user scrolls 66 | if (currentScrollTop - scrollDownBuffer > lastScrollTop) { 67 | hideBackToTop(); 68 | lastScrollTop = currentScrollTop <= 0 ? 0 : currentScrollTop; 69 | } else if (currentScrollTop < lastScrollTop - scrollUpBuffer) { 70 | showBackToTop(); 71 | lastScrollTop = currentScrollTop <= 0 ? 0 : currentScrollTop; 72 | } 73 | 74 | // Show the button at the bottom, hides it at the top 75 | if (currentScrollTop <= 0) { 76 | hideBackToTop(); 77 | } else if ( 78 | window.innerHeight + currentScrollTop >= 79 | document.body.offsetHeight 80 | ) { 81 | showBackToTop(); 82 | } 83 | }, 84 | false 85 | ); 86 | } 87 | 88 | function throttle(func, wait) { 89 | var timeout; 90 | return function () { 91 | const context = this; 92 | const args = arguments; 93 | const later = function () { 94 | clearTimeout(timeout); 95 | timeout = null; 96 | func.apply(context, args); 97 | }; 98 | 99 | if (!timeout) { 100 | timeout = setTimeout(later, wait); 101 | } 102 | }; 103 | } 104 | 105 | function headerOffset() { 106 | // Set an offset if there is are fixed top navbar 107 | const headerEl = window.document.querySelector("header.fixed-top"); 108 | if (headerEl) { 109 | return headerEl.clientHeight; 110 | } else { 111 | return 0; 112 | } 113 | } 114 | 115 | function footerOffset() { 116 | const footerEl = window.document.querySelector("footer.footer"); 117 | if (footerEl) { 118 | return footerEl.clientHeight; 119 | } else { 120 | return 0; 121 | } 122 | } 123 | 124 | function dashboardOffset() { 125 | const dashboardNavEl = window.document.getElementById( 126 | "quarto-dashboard-header" 127 | ); 128 | if (dashboardNavEl !== null) { 129 | return dashboardNavEl.clientHeight; 130 | } else { 131 | return 0; 132 | } 133 | } 134 | 135 | function updateDocumentOffsetWithoutAnimation() { 136 | updateDocumentOffset(false); 137 | } 138 | 139 | function updateDocumentOffset(animated) { 140 | // set body offset 141 | const topOffset = headerOffset(); 142 | const bodyOffset = topOffset + footerOffset() + dashboardOffset(); 143 | const bodyEl = window.document.body; 144 | bodyEl.setAttribute("data-bs-offset", topOffset); 145 | bodyEl.style.paddingTop = topOffset + "px"; 146 | 147 | // deal with sidebar offsets 148 | const sidebars = window.document.querySelectorAll( 149 | ".sidebar, .headroom-target" 150 | ); 151 | sidebars.forEach((sidebar) => { 152 | if (!animated) { 153 | sidebar.classList.add("notransition"); 154 | // Remove the no transition class after the animation has time to complete 155 | setTimeout(function () { 156 | sidebar.classList.remove("notransition"); 157 | }, 201); 158 | } 159 | 160 | if (window.Headroom && sidebar.classList.contains("sidebar-unpinned")) { 161 | sidebar.style.top = "0"; 162 | sidebar.style.maxHeight = "100vh"; 163 | } else { 164 | sidebar.style.top = topOffset + "px"; 165 | sidebar.style.maxHeight = "calc(100vh - " + topOffset + "px)"; 166 | } 167 | }); 168 | 169 | // allow space for footer 170 | const mainContainer = window.document.querySelector(".quarto-container"); 171 | if (mainContainer) { 172 | mainContainer.style.minHeight = "calc(100vh - " + bodyOffset + "px)"; 173 | } 174 | 175 | // link offset 176 | let linkStyle = window.document.querySelector("#quarto-target-style"); 177 | if (!linkStyle) { 178 | linkStyle = window.document.createElement("style"); 179 | linkStyle.setAttribute("id", "quarto-target-style"); 180 | window.document.head.appendChild(linkStyle); 181 | } 182 | while (linkStyle.firstChild) { 183 | linkStyle.removeChild(linkStyle.firstChild); 184 | } 185 | if (topOffset > 0) { 186 | linkStyle.appendChild( 187 | window.document.createTextNode(` 188 | section:target::before { 189 | content: ""; 190 | display: block; 191 | height: ${topOffset}px; 192 | margin: -${topOffset}px 0 0; 193 | }`) 194 | ); 195 | } 196 | if (init) { 197 | window.dispatchEvent(headroomChanged); 198 | } 199 | init = true; 200 | } 201 | 202 | // initialize headroom 203 | var header = window.document.querySelector("#quarto-header"); 204 | if (header && window.Headroom) { 205 | const headroom = new window.Headroom(header, { 206 | tolerance: 5, 207 | onPin: function () { 208 | const sidebars = window.document.querySelectorAll( 209 | ".sidebar, .headroom-target" 210 | ); 211 | sidebars.forEach((sidebar) => { 212 | sidebar.classList.remove("sidebar-unpinned"); 213 | }); 214 | updateDocumentOffset(); 215 | }, 216 | onUnpin: function () { 217 | const sidebars = window.document.querySelectorAll( 218 | ".sidebar, .headroom-target" 219 | ); 220 | sidebars.forEach((sidebar) => { 221 | sidebar.classList.add("sidebar-unpinned"); 222 | }); 223 | updateDocumentOffset(); 224 | }, 225 | }); 226 | headroom.init(); 227 | 228 | let frozen = false; 229 | window.quartoToggleHeadroom = function () { 230 | if (frozen) { 231 | headroom.unfreeze(); 232 | frozen = false; 233 | } else { 234 | headroom.freeze(); 235 | frozen = true; 236 | } 237 | }; 238 | } 239 | 240 | window.addEventListener( 241 | "hashchange", 242 | function (e) { 243 | if ( 244 | getComputedStyle(document.documentElement).scrollBehavior !== "smooth" 245 | ) { 246 | window.scrollTo(0, window.pageYOffset - headerOffset()); 247 | } 248 | }, 249 | false 250 | ); 251 | 252 | // Observe size changed for the header 253 | const headerEl = window.document.querySelector("header.fixed-top"); 254 | if (headerEl && window.ResizeObserver) { 255 | const observer = new window.ResizeObserver(() => { 256 | setTimeout(updateDocumentOffsetWithoutAnimation, 0); 257 | }); 258 | observer.observe(headerEl, { 259 | attributes: true, 260 | childList: true, 261 | characterData: true, 262 | }); 263 | } else { 264 | window.addEventListener( 265 | "resize", 266 | throttle(updateDocumentOffsetWithoutAnimation, 50) 267 | ); 268 | } 269 | setTimeout(updateDocumentOffsetWithoutAnimation, 250); 270 | 271 | // fixup index.html links if we aren't on the filesystem 272 | if (window.location.protocol !== "file:") { 273 | const links = window.document.querySelectorAll("a"); 274 | for (let i = 0; i < links.length; i++) { 275 | if (links[i].href) { 276 | links[i].dataset.originalHref = links[i].href; 277 | links[i].href = links[i].href.replace(/\/index\.html/, "/"); 278 | } 279 | } 280 | 281 | // Fixup any sharing links that require urls 282 | // Append url to any sharing urls 283 | const sharingLinks = window.document.querySelectorAll( 284 | "a.sidebar-tools-main-item, a.quarto-navigation-tool, a.quarto-navbar-tools, a.quarto-navbar-tools-item" 285 | ); 286 | for (let i = 0; i < sharingLinks.length; i++) { 287 | const sharingLink = sharingLinks[i]; 288 | const href = sharingLink.getAttribute("href"); 289 | if (href) { 290 | sharingLink.setAttribute( 291 | "href", 292 | href.replace("|url|", window.location.href) 293 | ); 294 | } 295 | } 296 | 297 | // Scroll the active navigation item into view, if necessary 298 | const navSidebar = window.document.querySelector("nav#quarto-sidebar"); 299 | if (navSidebar) { 300 | // Find the active item 301 | const activeItem = navSidebar.querySelector("li.sidebar-item a.active"); 302 | if (activeItem) { 303 | // Wait for the scroll height and height to resolve by observing size changes on the 304 | // nav element that is scrollable 305 | const resizeObserver = new ResizeObserver((_entries) => { 306 | // The bottom of the element 307 | const elBottom = activeItem.offsetTop; 308 | const viewBottom = navSidebar.scrollTop + navSidebar.clientHeight; 309 | 310 | // The element height and scroll height are the same, then we are still loading 311 | if (viewBottom !== navSidebar.scrollHeight) { 312 | // Determine if the item isn't visible and scroll to it 313 | if (elBottom >= viewBottom) { 314 | navSidebar.scrollTop = elBottom; 315 | } 316 | 317 | // stop observing now since we've completed the scroll 318 | resizeObserver.unobserve(navSidebar); 319 | } 320 | }); 321 | resizeObserver.observe(navSidebar); 322 | } 323 | } 324 | } 325 | }); 326 | -------------------------------------------------------------------------------- /index.qmd: -------------------------------------------------------------------------------- 1 | # Content 2 | 3 | *A 100-step journey to learn C from first principles* 4 | 5 | - [Download PDF](https://github.com/little-book-of/c/blob/main/releases/book.pdf) - print-ready 6 | - [Download EPUB](https://github.com/little-book-of/c/blob/main/releases/book.epub) - e-reader friendly 7 | - [View LaTex](https://github.com/little-book-of/c/blob/main/releases/book.tex) - `.tex` source 8 | - [Source code (Github)](https://github.com/little-book-of/c/blob/main/books/en-US/book.md) - Markdown source 9 | - [Read on GitHub Pages](https://little-book-of.github.io/c) 10 | 11 | ### Chapter 1. Getting Started with C 12 | 13 | 1. What Is C and Why Learn It 14 | 2. Installing a C Compiler 15 | 3. Writing Your First C Program 16 | 4. Understanding `main` and Return Values 17 | 5. Printing with `printf` 18 | 6. Comments and Code Readability 19 | 7. Variables and Basic Types 20 | 8. Declaring and Initializing Variables 21 | 9. Compiling and Running Programs 22 | 10. Common Beginner Mistakes 23 | 24 | 25 | 26 | ### Chapter 2. Working with Data 27 | 28 | 11. Integers, Floats, and Characters 29 | 12. Type Conversions and Casting 30 | 13. Constants and Literals 31 | 14. Operators and Expressions 32 | 15. Arithmetic Operators 33 | 16. Comparison and Logical Operators 34 | 17. Operator Precedence 35 | 18. Reading Input with `scanf` 36 | 19. The `sizeof` Operator 37 | 20. Debugging Type Errors 38 | 39 | 40 | 41 | ### Chapter 3. Control Flow 42 | 43 | 21. The `if` Statement 44 | 22. The `else` and `else if` Clauses 45 | 23. Nested Conditionals 46 | 24. The `switch` Statement 47 | 25. The `while` Loop 48 | 26. The `for` Loop 49 | 27. The `do-while` Loop 50 | 28. Breaking and Continuing Loops 51 | 29. Using `goto` Safely (and Why to Avoid It) 52 | 30. Patterns of Control Flow 53 | 54 | 55 | 56 | ### Chapter 4. Functions and Scope 57 | 58 | 31. Defining and Calling Functions 59 | 32. Function Parameters and Return Values 60 | 33. Local and Global Variables 61 | 34. Scope and Lifetime 62 | 35. Header Declarations (`.h` files) 63 | 36. Pass by Value Explained 64 | 37. Recursion and Base Cases 65 | 38. Function Prototypes and Order 66 | 39. Inline Functions 67 | 40. Organizing Code with Functions 68 | 69 | 70 | 71 | ### Chapter 5. Arrays and Strings 72 | 73 | 41. Declaring Arrays 74 | 42. Indexing and Bounds 75 | 43. Multidimensional Arrays 76 | 44. Iterating over Arrays 77 | 45. Strings as Character Arrays 78 | 46. String Literals and Null Terminators 79 | 47. Common String Functions (`strlen`, `strcpy`, `strcmp`) 80 | 48. Inputting Strings 81 | 49. Arrays vs. Pointers (A Gentle Intro) 82 | 50. Common Array Pitfalls 83 | 84 | 85 | 86 | ### Chapter 6. Pointers and Memory 87 | 88 | 51. What Is a Pointer 89 | 52. The Address-of (`&`) and Dereference (`*`) Operators 90 | 53. Pointer Arithmetic 91 | 54. Arrays and Pointers Revisited 92 | 55. Function Parameters with Pointers 93 | 56. Dynamic Memory Allocation with `malloc` 94 | 57. Using `free` Safely 95 | 58. Pointer to Pointer 96 | 59. `NULL` and Dangling Pointers 97 | 60. Debugging Memory Errors 98 | 99 | 100 | 101 | ### Chapter 7. Structures and Modular Design 102 | 103 | 61. Defining `struct` Types 104 | 62. Accessing Structure Members 105 | 63. Structures and Functions 106 | 64. Nested Structures 107 | 65. Arrays of Structures 108 | 66. Typedefs for Simpler Names 109 | 67. Enums and Symbolic Constants 110 | 68. Unions and Shared Memory 111 | 69. Organizing Code into Modules 112 | 70. Splitting Code into `.c` and `.h` Files 113 | 114 | 115 | 116 | ### Chapter 8. The Power of the Preprocessor 117 | 118 | 71. What Is the Preprocessor 119 | 72. `#include` and Header Guards 120 | 73. Defining Macros with `#define` 121 | 74. Working with Paths and Filenames 122 | 75. Conditional Compilation (`#if`, `#ifdef`) 123 | 76. Function-like Macros 124 | 77. Debugging with `#error` and `#warning` 125 | 78. Built-in Macros (`__FILE__`, `__LINE__`) 126 | 79. The Compilation Pipeline (Preprocess → Compile → Link) 127 | 80. Balancing Macros and Functions 128 | 129 | 130 | 131 | ### Chapter 9. Files, Tools, and Concurrency 132 | 133 | 81. File I/O Basics: `fopen`, `fclose` 134 | 82. Reading and Writing Files 135 | 83. Working with Binary Files 136 | 84. Error Handling in File Operations 137 | 85. Command-Line Arguments 138 | 86. Using `make` and Makefiles 139 | 87. Debugging with `gdb` 140 | 88. Understanding Linking and Libraries 141 | 89. Simple Threads with `` 142 | 90. Synchronization and Data Safety 143 | 144 | 145 | 146 | ### Chapter 10. Putting It All Together 147 | 148 | 91. Mini Project 1: Text Analyzer 149 | 92. Mini Project 2: Guessing Game 150 | 93. Mini Project 3: Calculator 151 | 94. Mini Project 4: File Copy Utility 152 | 95. Mini Project 5: Simple Logger 153 | 96. Mini Project 6: Contact Book 154 | 97. Mini Project 7: Matrix Operations 155 | 98. Mini Project 8: JSON-like Parser 156 | 99. Mini Project 9: Mini Shell 157 | 100. Mini Project 10: Tiny HTTP Server 158 | -------------------------------------------------------------------------------- /releases/book.epub: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/little-book-of/c/0e8a33e9852266d417fb85ee48ce577c304e1671/releases/book.epub -------------------------------------------------------------------------------- /releases/book.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/little-book-of/c/0e8a33e9852266d417fb85ee48ce577c304e1671/releases/book.pdf --------------------------------------------------------------------------------