vault backup: 2024-11-03 14:20:04

This commit is contained in:
zleyyij 2024-11-03 14:20:04 -07:00
parent a46466ade3
commit 0e2586459f

View File

@ -1,4 +1,4 @@
> 1. The following function calls supposedly write a single new-line character, but some are incorrect. Identify which calls don't work and explain why. > **1.** The following function calls supposedly write a single new-line character, but some are incorrect. Identify which calls don't work and explain why.
b. `printf("%c", "\n");` - This is invalid because the double quotes make `\n` a string, but it's being displayed with the `%c`formatting specifier. b. `printf("%c", "\n");` - This is invalid because the double quotes make `\n` a string, but it's being displayed with the `%c`formatting specifier.
c. `printf(%s, '\n');` - This is invalid because it's trying to display a `char` using the string formatting specifier. c. `printf(%s, '\n');` - This is invalid because it's trying to display a `char` using the string formatting specifier.
@ -6,8 +6,9 @@ e. `printf('\n');` - `printf`'s first argument should be a string, not a `char`.
h. `putchar("\n");` - `putchar`'s first argument should be a `char`, not a string. h. `putchar("\n");` - `putchar`'s first argument should be a `char`, not a string.
i. `puts('\n');` - `puts`'s first argument should be a string, not a `char`. i. `puts('\n');` - `puts`'s first argument should be a string, not a `char`.
j. `puts("\n");` - `puts` will write a newline after writing a string, so this will write two newlines. j. `puts("\n");` - `puts` will write a newline after writing a string, so this will write two newlines.
<hr>
> 2. Suppose that `p` has been declared as follows: > **2.** Suppose that `p` has been declared as follows:
```c ```c
char *p = "abc"; char *p = "abc";
``` ```
@ -16,6 +17,15 @@ char *p = "abc";
// A - Not legal, because putchar accepts a `char`, not a pointer. // A - Not legal, because putchar accepts a `char`, not a pointer.
putchar(p); putchar(p);
// B - Legal, output: `a` // B - Legal, output: `a`
putchar(*p);
// C - Legal, output: `abc`
puts(p)
// D - Illegal, `puts` accepts a pointer to a null terminated string, not a `char`.
``` ```
<hr>
> **3.** Suppose that we call `scanf` as follows:
```c
scanf("%d%s%d", &i, s, &j);
```
> If the user enters `12abc34` `56def78`, what will be the values of `i`, `s`, and `j` after the call? (Assume that `i` and `j` are `int` variables and `s` is an array of characters.)