summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authormo <mo.khan@gmail.com>2019-05-22 20:57:11 -0600
committermo <mo.khan@gmail.com>2019-05-22 20:57:11 -0600
commit724b3d5335df7cf18c38b5e29288112f7b4ab413 (patch)
treef435568fea932a85b3a3447367b6c54f42966847 /README.md
parent31c82d994681bc3f846c02a039dea31af81a9292 (diff)
answer questions on the c program
Diffstat (limited to 'README.md')
-rw-r--r--README.md50
1 files changed, 49 insertions, 1 deletions
diff --git a/README.md b/README.md
index 868f2c2..7a817b8 100644
--- a/README.md
+++ b/README.md
@@ -331,11 +331,59 @@ Your program must have declaration statements, such as float c, f;
**Delete the semicolon from the end of the statement.** Recompile and report the kind of error.
The C compiler often provides cryptic error messages. Interpret this message.
+The compiler error is the following:
+
+```bash
+も make
+cc -Wall -g -std=c99 -Isrc -c src/temperature.c
+src/temperature.c: In function ‘main’:
+src/temperature.c:5:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘printf’
+ 5 | printf("Degrees in Celsius?\n");
+ | ^~~~~~
+src/temperature.c:8:3: error: ‘f’ undeclared (first use in this function)
+ 8 | f = 9*c/5 + 32;
+ | ^
+src/temperature.c:8:3: note: each undeclared identifier is reported only once for each function it appears in
+make: *** [Makefile:14: temperature.o] Error 1
+```
+
+It states that a `;` colon is missing from before line 5 to terminate
+the previous line.
+
**Change the float c, f; statement to (float c; char f;)**
-* Do you get any errors during compilation?
+Do you get any errors during compilation?
+
+No. I do get a compiler warning, but not an error.
+```bash
+も make
+cc -Wall -g -std=c99 -Isrc -c src/temperature.c
+src/temperature.c: In function ‘main’:
+src/temperature.c:9:35: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
+ 9 | printf("Degrees in Fahrenheit: %f\n", f);
+ | ~^ ~
+ | | |
+ | | int
+ | double
+ | %d
+cc -Wall -g -std=c99 -Isrc -o ./bin/temperature temperature.o
+./bin/temperature
+Degrees in Celsius?
+100
+Degrees in Fahrenheit: 0.000000
+```
+
* What are they and why?
+
+The compiler warning says that the `%f` format specifier was expecting a
+type of double, but received a type of `int`. The `char f` is being
+interpreted as a memory address rather than a value.
+
* Do you see any difference between running this program and the earlier version? Why?
+
+Yes, the output is now incorrect because the value for `f` is coming
+from the memory address instead of the value.
+
* If your program uses a `cout` statement, then replace it with a `printf` statement that does the same thing and vice versa.
* If you have used neither `cout` nor `printf` in the first version, then replace what you have with `printf`.