{"id":16352816,"url":"https://github.com/mixelpixel/the-c-programming-language","last_synced_at":"2025-11-20T12:30:17.653Z","repository":{"id":106837794,"uuid":"90468097","full_name":"mixelpixel/The-C-Programming-Language","owner":"mixelpixel","description":"Reading the seminal (if not outdated) book by By Brian W. Kernighan and Dennis M. Ritchie","archived":false,"fork":false,"pushed_at":"2018-01-26T20:57:55.000Z","size":56,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-28T18:55:31.079Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mixelpixel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-06T14:21:45.000Z","updated_at":"2017-12-21T13:09:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"58bd66c1-9cd5-4890-a714-736b78f6b433","html_url":"https://github.com/mixelpixel/The-C-Programming-Language","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mixelpixel%2FThe-C-Programming-Language","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mixelpixel%2FThe-C-Programming-Language/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mixelpixel%2FThe-C-Programming-Language/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mixelpixel%2FThe-C-Programming-Language/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mixelpixel","download_url":"https://codeload.github.com/mixelpixel/The-C-Programming-Language/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239619465,"owners_count":19669449,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T01:27:40.774Z","updated_at":"2025-11-20T12:30:17.568Z","avatar_url":"https://github.com/mixelpixel.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# The C Programming Language\nReading the seminal (if not outdated) book by By Brian W. Kernighan and Dennis M. Ritchie, \"[The C Programming Language](http://alvand.basu.ac.ir/~dezfoulian/files/Programming/Prentice%20Hall%20-%20The%20C%20Programming%20Language-%20Brian%20W.%20Kernighan,%20Dennis%20M.%20Ritchie,%202nd%20ed.,%20ISBN%20.pdf)\", 2nd Edition, \"ANSI C\", 1988. \"[Preface to the Digital Edition](http://ptgmedia.pearsoncmg.com/images/9780131103627/samplepages/0131103628.pdf)\" by Brian Kernighan, 11/2012 - Rest in peace, Dennis Ritchie...\n\nThis book is a little \"outdated\", but nevertheless, very well written. Click these links for an [overview of K\u0026R vs other C standards](http://www.electronicdesign.com/dev-tools/what-s-difference-between-c-now-and-then); for a [list of C programming books](http://stackoverflow.com/a/562377/5225057); or, for the [C language standards](http://stackoverflow.com/a/83763/5225057).\n\nPer this answer: https://stackoverflow.com/a/43819183/5225057\n```\nYou can either tell the compiler to use the C language standard C90 (ANSI), which was modern when the book was written. Do it by using parameter -std=c90 or -ansi to the compiler, like this:\n\ncc -ansi FtoC.c -o FtoC.o\nor you can rewrite the program to adhere to a newer standard (C99), which is what your compiler uses by default, by adding a return type to the main function, like this:\n\nint main()\n```\n\n# FWIW\n1. I am working with **Terminal.app** version 2.7.2 (388.1) on **macOS Sierra** version 10.12.4 (16E195). So far I am compiling the Exercise programs with `cc` like so: `$  cc hello.c`. I am running the resulting executable file like so: `$  ./a.out`. Nothing fancy.\n2. Note: in general if there are quote marks around a sentence or long passage, it is from the K\u0026R text, tho I will try and clearly indicate like so: \"quoted text\"(K\u0026R).\n\u003cdetails\u003e\u003csummary\u003e ...and for reference with tricky parts\u003c/summary\u003e\u003ca href=\"http://www.eng.uerj.br/~fariasol/disciplinas/LABPROG/C_language/Kernighan_and_Ritchie/solved-exercises/solved-exercises.html/\"\u003eClick this link for answers\u003c/a\u003e.\u003c/details\u003e\u003cbr\u003e\n\n\n# Chapter 1 - A Tutorial Introduction\n# 1.1 Getting Started\nAs an example of what is now an \"outdated\" style of C in the book, [the \"Hello, world!\" program](ch1/1.1_hello_KR.c) does not include declaring a functions return type and setting a return value:\n```c\n#include \u003cstdio.h\u003e\n\nmain()\n{\n  printf(\"hello, world\\n\");\n}\n```\n...functions should instead be written to the following form:\n```\nreturn_type function_name( parameter list )\n{\n   body of the function\n   function return value\n}\n```\n...like so:\n```c\n#include \u003cstdio.h\u003e\n\nint main()        // \u003c--------- function return type declaration\n{\n  printf(\"hello, world\\n\");\n  return 0;       // \u003c-------------------- function return value\n}\n```\nAlso, per [this Stack Overflow answer](http://stackoverflow.com/a/12225214/5225057) it is also the convention that C programs should be written with main(void), e.g.\n```c\n#include \u003cstdio.h\u003e\n\nint main(void)    // \u003c--------- explicit statement of calling main with no variables\n{\n  printf(\"hello, world\\n\");\n  return 0;\n}\n```\n[see §5.1.2.2.1 Program startup, paragraph 1 (pg.13)](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf):\n\"1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:\n`int main(void) { /* ... */ }`\"\n...and [§6.7.6.3 6.7.6.3 Function declarators (including prototypes), Semantics, paragraph 10 (pg.133)](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf):\n\"10 The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.\"\n### My first C program!\n[Hello, World!](ch1/1.1_hello.c)\n### Exercise 1-1\n[See what errors you can get](ch1/1.1_ex1-1_hello.c)\n### Exercise 1-2\n[Try out valid and invalid escape characters](ch1/1.1_ex1-2_hello.c)\n# 1.2 Variables and Arithmetic Expressions\nThe following are examples of a \"pre-test loop\" - as contrasted to a \"post-test loop\", e.g. a [\"Do while loop\"](ch3).\n```\n    while ( test condition ) {\n        Code to execute while the test condition is satisfied\n    }\n```\nConverting Fahrenheit and Celsius with a \"while\" statement:\n[Fahrenheit to Celsius conversion: integer](ch1/1.2_FtoC.c)\n### Exercise 1-3\n[Fahrenheit to Celsius conversion: floating-point](ch1/1.2_ex1-3_FtoC_float.c)\n### Exercise 1-4\n[Celsius to Fahrenheit conversion: floating point](ch1/1.2_ex1-4_CtoF_float.c)\n# 1.3 The for statement\n```\n    for (1. initialization prior to loop; 2. test condition controls loop: if pass test, then execute loop, or, if fail test, then end loop; 3. incremental step after loop body action)\n        action;\n```\n[Fahrenheit to Celsius conversion with a \"for\" statement](ch1/1.3_FtoC_for.c)\nAlso a \"pre-test loop\", \"The for statement is a loop, a generalization of the while. If you compare it to the earlier while, its operation should be clear. Within the parentheses, there are three parts, separated by semicolons. **The first part, the initialization**\n`fahr = 0`\n**is done once, before the loop proper is entered. The second part is the test or condition that controls the loop:**\n`fahr \u003c= 300`\n**This condition is evaluated; if it is true, the body of the loop (here a single ptintf) is executed. Then the\nincrement step**\n`fahr = fahr + 20`\n**is executed, and the condition re-evaluated. The loop terminates if the condition has become false.** As with the while, the body of the loop can be a single statement or a group of statements enclosed in braces. The initialization, condition and increment can be any expressions.\nThe choice between while and for is arbitrary, based on which seems clearer. The for is usually appropriate for loops in which the initialization and increment are single statements and logically related, since it is more compact than while and it keeps the loop control statements together in one place.\"(K\u0026R)\n### Exercise 1-5\n[Fahrenheit to Celsius conversion with a \"for\" statement](ch1/1.3_ex1-5_FtoC_for_reverse.c)\nSome additional references for a [\" while loop in C\"](https://www.tutorialspoint.com/cprogramming/c_while_loop.htm), [\"for, while, and do while loops in C\"](http://www.cprogramming.com/tutorial/c/lesson3.html), and [\"if statements in C\"](http://www.cprogramming.com/tutorial/c/lesson2.html), especially in regards to \"true\" (a non-zero value) and \"false\" (zero).\n# 1.4 Symbolic Constants\n[Fahrenheit to Celsius conversion using SYMBOLIC CONSTANTS](ch1/1.4_FtoC_for_SYMBOL.c)\n# 1.5 Character Input and Output\nc = getchar(); - Assigns to `c` the value of `getchar();` input\nputchar(c); - \"Calls to putchar and printf may be interleaved\"\n\"A text stream is a sequence of characters divided into lines; each line consists of zero or more characters followed by a newline character...The standard library provides several functions for reading or writing one character at a time, of which getchar and putchar are the simplest. Each time it is called, getchar reads the next input character from a text stream and returns that as its value.\"(K\u0026R)\n## 1.5.1 File Copying\n```\nread a character() \u003c----- Do K\u0026R mean \"text stream\" by \"character\"?\n{\n    declare variable type;\n    initialize variable to get character;\n    while (character is not end-of-file indicator) {\n        output the character just read;\n        read a character;\n    }\n    return value;\n}\n```\n\n1. I am a little confused about how \"file\" is being used for \"file input\" and how \"character\" is being used - `getchar();` appears to read in an entire line of text in the while loop, and not \"one char at a time\" nor one \"file\" at a time... Also, it seems to only read so far as a \"new line\" or \"Enter\" (not sure how they differ?) Perhaps this is just loose use of terms (or my loose understanding), but I am left a little confused as to what the type \"char\" means, and, how K\u0026R are using \"file\" and \"character\". Also not solid on newline (10) vs. EOF (-1) ...but it's working. Mostly I am thrown by K\u0026R's description of char. Perhaps I'll understand better by [Chapter 7](ch7/) when input from files is discussed.\n2. Exercise 1-6's evaluation results in 0 or 1, but EOF - per Exercise 1-7 - has a value of -1. Okay... so EOF = -1 and `getchar() != EOF` evaluates to either 0 (false) or 1 (true)... So why/how does ctrl+d = -1? I see that ctrl+c and ctrl+z don't evaluate, they simple exit from the program. Ah-ha - it appears that C simply does not have a type class of Boolean and achieves a true/false distinction where zero is false and non-zero is true. Per: https://www.le.ac.uk/users/rjm1/cotter/page_37.htm But why/how does `getchar() = EOF` always evaluate to -1? Ahhh ::facepalm:: I was doing an assignment of c = EOF (resulting in c having the EOF value of -1) instead of a comparison operator `==`... it should be `getchar() == EOF`. And we're good: ctrl+d enters an EOF value of -1 and the equals/not equals evaluations are consistent as true (1, or not zero) and false(0). Not sure why \\\\n newline has a value of 10, but will save that question for later.\n\n[file input version 1](ch1/1.5.1_file_copying_v1.c)\n[file input version 2](ch1/1.5.1_file_copying_v2.c)\n### Exercise 1-6\n[Verify that the expression getchar() != EOF is 0 or 1.](ch1/1.5.1_ex1-6.c)\n### Exercise 1-7\n[Print the value of EOF](ch1/1.5.1_ex1-7.c)\n## 1.5.2 Character Counting\nRecall from [Section 1.3 The for statement](https://github.com/mixelpixel/The-C-Programming-Language#13-the-for-statement) the for-loop syntax:\n```\n    for (initialization prior to loop; test condition controls loop; incremental step after loop body action)\n        action; /* for loop body. after the pre-test condition test or an incremental step, condition\n                   is re-evaluated and the action repeated or exited if test condition not satisfied */\n```\nNote: \"...the grammatical rules of C require that a for statement have a body. The isolated semicolon, called a null statement, is there to satisfy that requirement. We put it on a separate line to make it visible.\"(K\u0026R) e.g.\n```\n#include \u003cstdio.h\u003e\n\nint main(void)\n{\n    double nc;\n\n    for (nc = 0; getchar() != EOF; ++nc)\n        ;       /* \u003c--------------------------- NULL STATEMENT */\n        printf(\"%.0f\\n\", nc);\n}\n```\n[counting input characters version 1](ch1/1.5.2_char_count_v1.c)\n[counting input characters version 2](ch1/1.5.2_char_count_v2.c)\n## 1.5.3 Line Counting\n\"the standard library ensures that an input text stream appears as a sequence of lines, each terminated by a newline.\"(K\u0026R)\n[count lines in input](ch1/1.5.3_line_count.c)\n### Exercise 1-8\n[count blanks, tabs and lines](ch1/1.5.3_ex1-8.c)\nAn interesting solution to exercise 1-8, the program handles input from text files which do not end with a new line](http://www.eng.uerj.br/~fariasol/disciplinas/LABPROG/C_language/Kernighan_and_Ritchie/solved-exercises/solved-exercises.html/krx108.html).\n### Exercise 1-9\n[Moore vs Mealy Finite State Machine](https://youtu.be/hJIST1cEf6A)\n[Replace one or more blanks with a single blank](ch1/1.5.3_ex1-9.c)\n### Exercise 1-10\n[Replace tabs, backspaces and slashes](ch1/1.5.3_ex1-10.c)\n## 1.5.4 Word Counting\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmixelpixel%2Fthe-c-programming-language","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmixelpixel%2Fthe-c-programming-language","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmixelpixel%2Fthe-c-programming-language/lists"}