Answers

{"content":{"id":1058,"title":"'Build Your Own Lisp' Solutions: 6.6","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Change the grammar to recognize decimal numbers such as 0.01, 5.21, or 10.2.\u003c/p\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e \r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e\" \\\r\nnumber : /-?[0-9]+(.[0-9]+)*/ ; \\\r\noperator : 'add' | 'sub' | 'mul' | 'div'; \\\r\nexpr : \u0026lt;number\u003e | '(' \u0026lt;operator\u003e \u0026lt;expr\u003e+ ')' ; \\\r\nlispy : /^/ \u0026lt;operator\u003e \u0026lt;expr\u003e+ /$/ ; \\\r\n\"\u003c/code\u003e\u003c/pre\u003e\r\n\u003c/p\u003e","publication_date":"2020-01-04T00:00:00.000Z","created_at":"2019-12-10T00:09:33.000Z","updated_at":"2020-07-06T19:13:57.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=build_your_own_lisp\"\u003ebuild_your_own_lisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e"}

'Build Your Own Lisp' Solutions: 6.6

Q: Change the grammar to recognize decimal numbers such as 0.01, 5.21, or 10.2.

A:

"                                                   \
number   : /-?[0-9]+(.[0-9]+)*/ ;                   \
operator : 'add' | 'sub' | 'mul' | 'div';           \
expr     : <number> | '(' <operator> <expr>+ ')' ;  \
lispy    : /^/ <operator> <expr>+ /$/ ;             \
"

{"content":{"id":1023,"title":"'Build Your Own Lisp' Solutions: 6.5","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e › Change the grammar to recognise operators written in textual format 'add', 'sub', 'mul', 'div'.\u003c/p\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e \r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e\" \\\r\nnumber : /-?[0-9]+/ ; \\\r\noperator : 'add' | 'sub' | 'mul' | 'div'; \\\r\nexpr : \u0026lt;number\u003e | '(' \u0026lt;operator\u003e \u0026lt;expr\u003e+ ')' ; \\\r\nlispy : /^/ \u0026lt;operator\u003e \u0026lt;expr\u003e+ /$/ ; \\\r\n\"\u003c/code\u003e\u003c/pre\u003e\r\n\u003c/p\u003e","publication_date":"2019-12-04T00:00:00.000Z","created_at":"2019-11-04T20:51:40.000Z","updated_at":"2019-11-04T21:00:09.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=build_your_own_lisp\"\u003ebuild_your_own_lisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e"}

'Build Your Own Lisp' Solutions: 6.5

Q: › Change the grammar to recognise operators written in textual format 'add', 'sub', 'mul', 'div'.


A:

"                                                   \
number   : /-?[0-9]+/ ;                             \
operator : 'add' | 'sub' | 'mul' | 'div';           \
expr     : <number> | '(' <operator> <expr>+ ')' ;  \
lispy    : /^/ <operator> <expr>+ /$/ ;             \
"

{"content":{"id":1015,"title":"'Build Your Own Lisp' Solutions: 6.4","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Change the grammar to add a new operator such as '%'.\u003c/p\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e \r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e\" \\\r\nnumber : /-?[0-9]+/ ; \\\r\noperator : '+' | '-' | '*' | '/' | '%'; \\\r\nexpr : \u0026lt;number\u003e | '(' \u0026lt;operator\u003e \u0026lt;expr\u003e+ ')' ; \\\r\nlispy : /^/ \u0026lt;operator\u003e \u0026lt;expr\u003e+ /$/ ; \\\r\n\"\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003c/p\u003e","publication_date":"2019-11-27T00:00:00.000Z","created_at":"2019-10-28T19:56:26.000Z","updated_at":"2019-10-28T20:03:09.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=build_your_own_lisp\"\u003ebuild_your_own_lisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e"}

'Build Your Own Lisp' Solutions: 6.4

Q: Change the grammar to add a new operator such as '%'.


A:

"                                                   \
number   : /-?[0-9]+/ ;                             \
operator : '+' | '-' | '*' | '/' | '%';             \
expr     : <number> | '(' <operator> <expr>+ ')' ;  \
lispy    : /^/ <operator> <expr>+ /$/ ;             \
"

{"content":{"id":982,"title":"'Build Your Own Lisp' Solutions: 6.3","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Write a regular expression matching pit, pot and respite but not peat, spit, or part.\u003c/p\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e \r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e# This works, but is a weird answer to a weird exercise.\r\n/(^|res)p[io]/\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003c/p\u003e","publication_date":"2019-11-13T00:00:00.000Z","created_at":"2019-10-16T01:07:00.000Z","updated_at":"2020-07-13T00:22:16.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=build_your_own_lisp\"\u003ebuild_your_own_lisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e"}

'Build Your Own Lisp' Solutions: 6.3

Q: Write a regular expression matching pit, pot and respite but not peat, spit, or part.

A:

# This works, but is a weird answer to a weird exercise.
/(^|res)p[io]/

{"content":{"id":927,"title":"'Build Your Own Lisp' Solutions: 6.2","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Write a regular expression matching strings of consecutive a and b such as ababab or aba.\u003c/p\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e \r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e/(ab)/\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003c/p\u003e","publication_date":"2019-10-09T00:00:00.000Z","created_at":"2019-09-08T19:17:57.000Z","updated_at":"2019-09-08T19:17:57.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=build_your_own_lisp\"\u003ebuild_your_own_lisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e"}

'Build Your Own Lisp' Solutions: 6.2

Q: Write a regular expression matching strings of consecutive a and b such as ababab or aba.


A:

/(ab)/

{"content":{"id":871,"title":"'Build Your Own Lisp' Solutions: 6.1","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Write a regular expression matching strings of all a or b such as aababa or bbaa.\u003c/p\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e \r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e/^(a|b)+$/\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003c/p\u003e","publication_date":"2019-09-05T00:00:00.000Z","created_at":"2019-08-06T21:35:19.000Z","updated_at":"2020-10-02T16:44:39.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=build_your_own_lisp\"\u003ebuild_your_own_lisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e"}

'Build Your Own Lisp' Solutions: 6.1

Q: Write a regular expression matching strings of all a or b such as aababa or bbaa.

A:

/^(a|b)+$/

{"content":{"id":806,"title":"'Build Your Own Lisp' Solutions: 5.4","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Describe textually a grammar for decimal numbers such as \u003ci\u003e0.01\u003c/i\u003e or \u003ci\u003e52.221\u003c/i\u003e.\u003c/p\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e \r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e// Using the same system he's given us to describe decimals:\r\n\" \\\r\n digit : \\\"0\\\" | \\\"1\\\" \\\"2\\\" | \\\"3\\\" \\\"4\\\" \\\r\n | \\\"5\\\" | \\\"6\\\" \\\"7\\\" | \\\"8\\\" \\\"9\\\"; \\\r\n number : \u0026lt;digit\u0026gt;+; \\\r\n decimal : \u0026lt;number\u0026gt;+\\.\u0026lt;number\u0026gt;+; \\\r\n\"\r\n/* The URL example is essentially the same, but with a all the letters \r\n * of the alphabet. The rest of the questions in the chapter are soft\r\n * enough to be left to the interested reader. */\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003c/p\u003e","publication_date":"2019-07-26T00:00:00.000Z","created_at":"2019-06-28T18:09:04.000Z","updated_at":"2020-08-27T15:39:55.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=build_your_own_lisp\"\u003ebuild_your_own_lisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e"}

'Build Your Own Lisp' Solutions: 5.4

Q: Describe textually a grammar for decimal numbers such as 0.01 or 52.221.

A:

// Using the same system he's given us to describe decimals:
"                                             \
 digit   : \"0\" | \"1\" \"2\" | \"3\" \"4\"  \
         | \"5\" | \"6\" \"7\" | \"8\" \"9\"; \
 number  : <digit>+;                          \
 decimal : <number>+\.<number>+;              \
"
/* The URL example is essentially the same, but with a all the letters 
 * of the alphabet. The rest of the  questions in the chapter are soft
 * enough to be left to the interested reader. */

{"content":{"id":722,"title":"'Build Your Own Lisp' Solutions: 5.3","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Why are there back slashes \u003ci\u003e\\\u003c/i\u003e at the end of the line in the grammar?\u003c/p\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e C will ignore a combination of a newline followed by \u003ci\u003e\\\u003c/i\u003e, which lets you format long strings more nicely. An example:\r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e#include \u0026ltstdio.h\u0026gt\r\n\r\nint main(void)\r\n{\r\n /* If you count the spaces here and in the resulting output, there are\r\n * 11. The \\ and the newline are dropped. */\r\n printf(\"this is line one \\\r\n and this is line two\\n\");\r\n\r\n return 0;\r\n}\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003ethis is line one and this is line two\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003c/p\u003e\r\n\u003c!--\r\n\u003cstyle\u003e\r\ni { background: white; font-style: normal; margin-left: 3px; margin-right: 3px }\r\n\u003c/style\u003e\r\n--\u003e","publication_date":"2019-06-20T00:00:00.000Z","created_at":"2019-05-19T18:52:47.000Z","updated_at":"2019-06-22T02:53:03.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=build_your_own_lisp\"\u003ebuild_your_own_lisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e"}

'Build Your Own Lisp' Solutions: 5.3

Q: Why are there back slashes \ at the end of the line in the grammar?


A: C will ignore a combination of a newline followed by \, which lets you format long strings more nicely. An example:

#include <stdio.h>

int main(void)
{
  /* If you count the spaces here and in the resulting output, there are
   * 11. The \ and the newline are dropped. */
  printf("this is line one \
          and this is line two\n");

  return 0;
}
this is line one           and this is line two

{"content":{"id":721,"title":"'Build Your Own Lisp' Solutions: 5.2","body":"\u003cdiv class=\"card-image-caption\"\u003e(5.1 is a fun exercise and left to the reader)\u003c/div\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Why are there back slashes \u003ci\u003e\\\u003c/i\u003e in front of the quote marks \u003ci\u003e\"\u003c/i\u003e in the grammar?\u003c/p\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e So the C compiler doesn't mistake them for quotation marks which end the string.\r\n\u003c/p\u003e\r\n\u003c!--\r\n\u003cstyle\u003e\r\ni { background: white; font-style: normal; margin-left: 3px; margin-right: 3px }\r\n\u003c/style\u003e\r\n--\u003e","publication_date":"2019-06-19T00:00:00.000Z","created_at":"2019-05-19T16:50:55.000Z","updated_at":"2019-06-22T02:53:32.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=build_your_own_lisp\"\u003ebuild_your_own_lisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e"}

'Build Your Own Lisp' Solutions: 5.2

(5.1 is a fun exercise and left to the reader)

Q: Why are there back slashes \ in front of the quote marks " in the grammar?


A: So the C compiler doesn't mistake them for quotation marks which end the string.

{"content":{"id":705,"title":"'Build Your Own Lisp' Solutions: 4.9","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e If \u003ci\u003e_WIN32\u003c/i\u003e is defined on windows, what is defined for Linux or Mac?\u003c/p\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e \u003ci\u003e__linux__\u003c/i\u003e, and \u003ci\u003e__APPLE__\u003c/i\u003e, not to mention \u003ci\u003e__ANDROID__\u003c/i\u003e (which is a type of \u003ci\u003e__linux__\u003c/i\u003e, so something like \u003ci\u003e__linux__ \u0026\u0026 !__ANDROID__\u003c/i\u003e is possible).\u003c/p\u003e\r\n\u003cstyle\u003e\r\ni { background: white; font-style: normal; margin-left: 3px; margin-right: 3px }\r\n\u003c/style\u003e","publication_date":"2019-06-08T00:00:00.000Z","created_at":"2019-05-09T21:47:37.000Z","updated_at":"2020-07-06T03:21:32.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=build_your_own_lisp\"\u003ebuild_your_own_lisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e"}

'Build Your Own Lisp' Solutions: 4.9

Q: If _WIN32 is defined on windows, what is defined for Linux or Mac?

A: __linux__, and __APPLE__, not to mention __ANDROID__ (which is a type of __linux__, so something like __linux__ && !__ANDROID__ is possible).

{"content":{"id":688,"title":"'Build Your Own Lisp' Solutions: 4.8","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e What does the preprocessor command \u003ci\u003e#define\u003c/i\u003e do?\u003c/p\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e It creates a macro, which is to say, it let's you associate a word with an expression. Before compiling your code, the precompiler will replace every instance of that word with that expression. While most macros you see are quite simple, you can do pretty amazing things with them. See \u003ca href=\"https://gcc.gnu.org/onlinedocs/cpp/Macros.html\"\u003ehere\u003c/a\u003e for a detailed reference.\u003c/p\u003e","publication_date":"2019-06-04T00:00:00.000Z","created_at":"2019-05-04T20:21:30.000Z","updated_at":"2020-07-14T18:21:02.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=build_your_own_lisp\"\u003ebuild_your_own_lisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e"}

'Build Your Own Lisp' Solutions: 4.8

Q: What does the preprocessor command #define do?

A: It creates a macro, which is to say, it let's you associate a word with an expression. Before compiling your code, the precompiler will replace every instance of that word with that expression. While most macros you see are quite simple, you can do pretty amazing things with them. See here for a detailed reference.

{"content":{"id":684,"title":"'Build Your Own Lisp' Solutions: 4.7","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e What does the preprocessor command \u003ci\u003e#ifndef\u003c/i\u003e do?\u003c/p\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e Checks if a token has already been defined earlier in the file, or in a previous file, then includes anything between it and \u003ci\u003e#else\u003c/i\u003e or \u003ci\u003e#endif\u003c/i\u003e.\u003c/p\u003e","publication_date":"2019-06-01T00:00:00.000Z","created_at":"2019-05-01T18:38:12.000Z","updated_at":"2019-06-01T02:44:45.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=build_your_own_lisp\"\u003ebuild_your_own_lisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e"}

'Build Your Own Lisp' Solutions: 4.7

Q: What does the preprocessor command #ifndef do?


A: Checks if a token has already been defined earlier in the file, or in a previous file, then includes anything between it and #else or #endif.

{"content":{"id":681,"title":"'Build Your Own Lisp' Solutions: 4.6","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e What happens when you pass \u003ci\u003eprintf\u003c/i\u003e a variable that does not match the pattern?\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e\u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e#include \u0026ltstdio.h\u0026gt\r\n\r\nint main(void)\r\n{\r\n // This depends heavily on the kind of mistake you want to make. Some \r\n // examples:\r\n\r\n // A character is really a number, so this is fine.\r\n printf(\"%c\\n\", 109);\r\n // And vice-versa.\r\n printf(\"%d\\n\", 'm');\r\n // A string really just points to the memory address of the first character,\r\n // and %d will try to interpret that address as a base ten number, so this\r\n // will be something different every time you run the program.\r\n printf(\"%d\\n\", \"moo\");\r\n\r\n // These examples all happen to give you something, but the compiler has no \r\n // idea what to do with something like 'printf(\"%s\\n\", 13);', and will give \r\n // you a Segment Fault error.\r\n\r\n return 0;\r\n}\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cbr\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e\u0026gt ./wrong \r\nm\r\n109\r\n340750348\r\n\u003c/code\u003e\u003c/pre\u003e","publication_date":"2019-05-31T00:00:00.000Z","created_at":"2019-04-30T01:26:32.000Z","updated_at":"2020-09-23T16:54:36.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=build_your_own_lisp\"\u003ebuild_your_own_lisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e"}

'Build Your Own Lisp' Solutions: 4.6

Q: What happens when you pass printf a variable that does not match the pattern?

A:

#include <stdio.h>

int main(void)
{
  // This depends heavily on the kind of mistake you want to make. Some 
  // examples:

  // A character is really a number, so this is fine.
  printf("%c\n", 109);
  // And vice-versa.
  printf("%d\n", 'm');
  // A string really just points to the memory address of the first character,
  // and %d will try to interpret that address as a base ten number, so this
  // will be something different every time you run the program.
  printf("%d\n", "moo");

  // These examples all happen to give you something, but the compiler has no 
  // idea what to do with something like 'printf("%s\n", 13);', and will give 
  // you a Segment Fault error.

  return 0;
}

> ./wrong 
m
109
340750348
{"content":{"id":641,"title":"'Build Your Own Lisp' Solutions: 4.5","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e What other patterns can be used with printf?\u003c/p\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e More than are worth memorizing (at least all at once). As the chapter states, it's best to \u003ca href=\"http://www.cplusplus.com/reference/cstdio/printf/\"\u003echeck a good reference\u003c/a\u003e.\u003c/p\u003e","publication_date":"2019-05-09T00:00:00.000Z","created_at":"2019-04-08T22:00:49.000Z","updated_at":"2019-04-08T22:01:24.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=build_your_own_lisp\"\u003ebuild_your_own_lisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e"}

'Build Your Own Lisp' Solutions: 4.5

Q: What other patterns can be used with printf?


A: More than are worth memorizing (at least all at once). As the chapter states, it's best to check a good reference.

{"content":{"id":620,"title":"K\u0026R Solutions - 1.19","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Write a function 'reverse(s)' that reverses the character string 's'. Use it to write a program that reverses the input one line at a time.\u003c/p\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e\u003c/p\u003e \r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e#include \u0026ltstdio.h\u0026gt\r\n\r\n// Using the same basic format is previous exercises and examples\r\n#define MAXLINE 1000\r\n\r\nint get_a_line(char s[], int lim);\r\nvoid reverse(char s[]);\r\n\r\nint main(void)\r\n{\r\n char line[MAXLINE];\r\n\r\n while ((get_a_line(line, MAXLINE)) \u003e 0) {\r\n reverse(line);\r\n printf(\"%s\", line);\r\n }\r\n\r\n return 0;\r\n}\r\n\r\nint get_a_line(char s[], int lim)\r\n{\r\n int c, i;\r\n\r\n for (i = 0; i \u003c lim-1 \u0026\u0026 (c=getchar()) != EOF \u0026\u0026 c != '\\n'; ++i) {\r\n s[i] = c;\r\n }\r\n\r\n if (c == '\\n') {\r\n s[i] = c;\r\n ++i;\r\n }\r\n s[i] = '\\0';\r\n\r\n return i;\r\n}\r\n\r\nvoid reverse(char s[])\r\n{\r\n char temp[MAXLINE];\r\n int i, j;\r\n\r\n i = 0;\r\n while ((temp[i] = s[i]) != '\\0')\r\n ++i;\r\n\r\n /* Reversing the newline looks ugly. In real life, we might strip that \r\n * char of whitespace before reversing it, as per the last lesson. */\r\n j = 0;\r\n --i;\r\n s[i--] = '\\n';\r\n while (i \u003e= 0) {\r\n s[i] = temp[j];\r\n ++j;\r\n --i;\r\n }\r\n}\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e./reverse \r\nhi\r\nih\r\nsananab is a good blog\r\ngolb doog a si bananas\r\ntacocat\r\ntacocat\r\n\u003c/code\u003e\u003c/pre\u003e","publication_date":"2019-04-29T00:00:00.000Z","created_at":"2019-03-30T17:08:48.000Z","updated_at":"2019-03-30T17:36:31.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=k%26r\"\u003ek\u0026amp;r\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=unix\"\u003eunix\u003c/a\u003e"}

K&R Solutions - 1.19

Q: Write a function 'reverse(s)' that reverses the character string 's'. Use it to write a program that reverses the input one line at a time.

A:

#include <stdio.h>

// Using the same basic format is previous exercises and examples
#define MAXLINE 1000

int get_a_line(char s[], int lim);
void reverse(char s[]);

int main(void)
{
  char line[MAXLINE];

  while ((get_a_line(line, MAXLINE)) > 0) {
    reverse(line);
    printf("%s", line);
  }

  return 0;
}

int get_a_line(char s[], int lim)
{
  int c, i;

  for (i = 0; i < lim-1 && (c=getchar()) != EOF && c != '\n'; ++i) {
    s[i] = c;
  }

  if (c == '\n') {
    s[i] = c;
    ++i;
  }
  s[i] = '\0';

  return i;
}

void reverse(char s[])
{
  char temp[MAXLINE];
  int i, j;

  i = 0;
  while ((temp[i] = s[i]) != '\0')
    ++i;

  /* Reversing the newline looks ugly. In real life, we might strip that 
   * char of whitespace before reversing it, as per the last lesson. */
  j = 0;
  --i;
  s[i--] = '\n';
  while (i >= 0) {
    s[i] = temp[j];
    ++j;
    --i;
  }
}
./reverse 
hi
ih
sananab is a good blog
golb doog a si bananas
tacocat
tacocat
{"content":{"id":581,"title":"'Build Your Own Lisp' Solutions: 4.4","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e What does the \\n mean in those strings?\u003c/p\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e '\\n' signifies a newline character, so the startup information doesn't appear on the same line as the first prompt.\u003c/p\u003e","publication_date":"2019-04-03T00:00:00.000Z","created_at":"2019-03-04T20:45:01.000Z","updated_at":"2019-03-04T20:45:01.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=build_your_own_lisp\"\u003ebuild_your_own_lisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e"}

'Build Your Own Lisp' Solutions: 4.4

Q: What does the \n mean in those strings?


A: '\n' signifies a newline character, so the startup information doesn't appear on the same line as the first prompt.