1.6 KiB
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.
c. printf(%s, '\n');
- This is invalid because it's trying to display a char
using the string formatting specifier.
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.
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.
2. Suppose that
p
has been declared as follows:
char *p = "abc";
Which of the following function calls are legal? Show the output produced by each legal call, and explain why all the others are illegal.
// A - Not legal, because putchar accepts a `char`, not a pointer.
putchar(p);
// 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`.
3. Suppose that we call
scanf
as follows:
scanf("%d%s%d", &i, s, &j);
If the user enters
12abc34
56def78
, what will be the values ofi
,s
, andj
after the call? (Assume thati
andj
areint
variables ands
is an array of characters.)
i
:12
s
:"abc34"
j
:56
7. Suppose that
str