2.Assuming that each of the following examples is part of a complete program, what will each one print?
a.
printf("He sold the painting for $%2.2f. ", 2.345e2);
b.
printf("%c%c%c ", 'H', 105, '41');
c.
#define Q "His Hamlet was funny without being vulgar." printf("%s has %d characters. ", Q, strlen(Q));
d.
printf("Is %2.2e the same as %2.2f? ", 1201.0, 1201.0);
a. He sold the painting for $234.50.
b. Hi!
c. "His Hamlet was funny without being vulgar."
has 44 characters.
d. Is 1.20e+03 the same as 1201.00?
3. In Question 2c, what changes could you make so that string Q is printed out enclosed in double quotation marks?
#define Q ""His Hamlet was funny without being vulgar.""
4. It's the find error time!
define B booboo
define X 10
main(int)
{
int age;
char name;
printf("Please enter your first name.");
scanf("%s", name);
printf("All right, %c, what's your age?
", name);
scanf("%f", age);
xp = age + X;
printf("That's a %s! You must be at least %d.
", B, xp);
rerun 0;
}
Here is a corrected version:
#include <stdio.h> /* don't forget this */
#define B "booboo" /* add #, quotes */
#define X 10 /* add # */
int main(void) /* instead of main(int) */
{
int age;
int xp; /* declare all variables */
char name[40]; /* make into an array */
printf("Please enter your first name.
"); /*
for readability */
scanf("%s", name);
printf("All right, %s, what's your age?
", name); /* %s for string */
scanf("%d", &age); /* %d, not %f, &age, not age */
xp = age + X;
printf("That's a %s! You must be at least %d.
", B, xp);
return 0; /* not rerun */
}
5. Suppose a program starts as follows:
#define BOOK "War and Peace"
int main(void)
{
float cost =12.99;
float percent = 80.0;
Construct a printf() statement that uses BOOK, cost, and percent to print the following:
This copy of "War and Peace" sells for $12.99.
That is 80% of list.
printf("This copy of "%s" sell for $%.2f
That is %.0f%% of list", BOOK, cost,
percent); //Note the conversion specifier for 80% here!!!
6. What conversion specification would you use to print each of the following?
a. A decimal integer with a field width equal to the number of digits
b. A hexadecimal integer in the form 8A in a field width of 4
c. A floating-point number in the form 232.346 with a field width of 10
d. A floating-point number in the form 2.33e+002 with a field width of 12
e. A string left-justified in a field of width 30
a. %d
b. %4X
c. %10.3f
d. %12.2e
e. %-30s
7. Which conversion specification would you use to print each of the following?
a. An unsigned long integer in a field width of 15
b. A hexadecimal integer in the form 0x8a in a field width of 4
c. A floating-point number in the form 2.33E+02 that is left-justified in a field width of 12
d. A floating-point number in the form +232.346 in a field width of 10
e. The first eight characters of a string in a field eight characters wide
a. %15lu
b. %#4x
c. %-12.2E
d. %+10.3f
e. %8.8s
8. What conversion specification would you use to print each of the following?
a. A decimal integer having a minimum of four digits in a field width of 6
b. An octal integer in a field whose width will be given in the argument list
c. A character in a field width of 2
d. A floating-point number in the form +3.13 in a field width equal to the number of characters in the number
e. The first five characters in a string left-justified in a field of width 7
a. %6.4d
b. %*o
c. %2c
d. %+0.2f
e. %-7.5s
9. For each of the following input lines, provide a scanf() statement to read it. Also declare any variables or arrays used in the statement.
a. 101
b. 22.32 8.34E−09
float
c. linguini
d. catch 22
e. catch 22 (but skip over catch)
a.
int a;
scanf("%d", &a);
b.
float a, b;
scanf("%f %f", &a, &b);
c.
char charArray[20];
scanf("%s", charArray);
d.
char charArray;
int num;
scanf("%s %d", charArray, num);
e.
char charArray[20];
int num;
scanf("%*s %d", &num); // Note that here we use an * to skip reading charArray
10. What's whitespace?
Whitespace consists of spaces, tabs, and newlines. C uses whitespace to separate tokens from one another; scanf() uses whitespace to separate consecutive input items from each other.
11. What’s wrong with the following statement and how can you fix it?
printf("The double type is %z bytes..
", sizeof (double));
The z in %z is a modifier, not a specifier, so it requires a specifier for it to modify. You could use %zd to print the result in base 10 or use a different specifier to print using a different base, for example, %zx for hexadecimal.