1.再次運(yùn)行程序清單4.1讼庇,但是在要求輸入名字時,請輸入名和姓做祝,中間保有一個空格砾省,看看會發(fā)生什么?
#include <stdio.h>
#include <string.h>
#define DENSITY 62.4 // human density in lbs per cu ft
int main(){
float weight, volume;
int size, letters;
char name[40]; // name is an array of 40 chars
printf("Hi! What's your first name?\n");
scanf("%s", name);
printf("%s, what's your weight in pounds?\n", name);
scanf("%f", &weight);
size = sizeof name;
letters = strlen(name);
volume = weight / DENSITY;
printf("Well, %s, your volume is %2.2f cubic feet.\n",name, volume);
printf("Also, your first name has %d letters,\n",letters);
printf("and we have %d bytes to store it.\n", size);
return 0;
}
程序不能正常運(yùn)行混槐,第一個scanf()語句只讀取用戶輸入的名编兄,而用戶輸入的姓仍留在緩沖區(qū)中(緩沖區(qū)是用于存儲輸入的臨時存儲區(qū))。下一條scang()語句声登,在輸入緩沖區(qū)查找重量時狠鸳,從上次讀入結(jié)束的地方開始讀取。這樣就把留在緩沖區(qū)的姓作為體重來讀取悯嗓,導(dǎo)致sacnf()讀取失敗件舵。另一方面,如果在要求輸入姓名時輸入Lasha 114绅作,那么程序會把144作為用戶的體重(雖然用戶是在程序提示輸入體重之前輸入了144)
2.假設(shè)下列事例都是完整程序中的一部分芦圾,它們的打印結(jié)果分別是什么?
a.
printf("He sold the painting for $%2.2f.\n", 2.345e2);
打印出:He sold the painting for $234.50.
b.
printf("%c%c%c\n", 'H', 105, '\41');
'' 打印出:Hi!
c.
#define Q "His Hamlet was funny without being vulgar."
printf("%s\nhas %d characters.\n", Q, strlen(Q));
打印出:
His Hamlet was funny without being vulgar.
has 42 characters.
d.
printf("Is %2.2e the same as %2.2f?\n", 1201.0, 1201.0);
打印出:Is 1.20e+03 the same as 1201.00?
3在第二題的C中俄认,如果要輸出包含雙引號的字符串Q个少,該如何修改?
#define Q "His Hamlet was funny without being vulgar."
printf("\"%s\"\nhas %d characters.\n", Q, strlen(Q));
4找出下面程序中的錯誤 下面這個已經(jīng)被我改好了
#include <stdio.h> //缺少這個頭文件不可以
define B "booboo"
'' #define X 10
int main() {
int age;
int xp;
char name[40];
'' printf("Please enter your first name.\n");
scanf("%s", name);
printf("All right, %s, what's your age?\n", name);
scanf("%d", &age);
xp = age + X;
printf("That's a %s! You must be at least %d.\n", B, xp);
retrun 0;
}
5.假設(shè)一個程序的開頭是這樣的,
#define BOOK "War and Peace"
int main(void) {
float cost =12.99;
float percent = 80.0;
請構(gòu)造一個使用BOOK,cost和percent的printf()語句眯杏,打印以下內(nèi)容:
This copy of "War and Peace" sells for $12.99.
That is 80% of list.
語句應(yīng)該這么寫:
printf("This copy of "%s" sells for $%.2f.\nThat is %.1f%% of list\n",BOOK,cost,percent);
6.打印下列各項(xiàng)內(nèi)容要分別使用什么轉(zhuǎn)換說明夜焦?
- 一個字段寬度與位數(shù)相同的十進(jìn)制整數(shù) %d
- 一個形如8A、字段寬度為4的十六進(jìn)制整數(shù) %4x
- 一個形如232.346岂贩、字段寬度為10的浮點(diǎn)數(shù) %10.3f
- 一個形如2.33e+002茫经、字段寬度為12的浮點(diǎn)數(shù) %12.2e
- 一個字段寬度為30、左對齊的字符串 %-30s
7.打印下面各項(xiàng)內(nèi)容要分別使用什么轉(zhuǎn)換說明?
a. An unsigned long integer in a field width of 15 %15lu
b. A hexadecimal integer in the form 0x8a in a field width of 4 %#4x
c. A floating-point number in the form 2.33E+02 that is left-justified in a field width of 12 %-12.2E
d. A floating-point number in the form +232.346 in a field width of 10 %+10.3f
e. The first eight characters of a string in a field eight characters wide %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 %6.4d
b. An octal integer in a field whose width will be given in the argument list
%o
c. A character in a field width of 2 #2c
d. A floating-point number in the form +3.13 in a field width equal to the number of characters in the number %+.2f
e. The first five characters in a string left-justified in a field of width 7
%-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
int number; scanf("%d",&number);
b. 22.32 8.34E?09
float kgs,share; scanf("%f%f",&kgs,&share);
c. linguini
char pasta[20]; scanf("%s",pasta);
d. catch 22
char action[20];
int value;
scanf("%s %d",action. &value);
e. catch 22 (but skip over catch)
int value;
scanf("%*s %d",&value);
10. What is whitespace?
空白包括空格卸伞、制表符和換行符抹镊。C語言使用空白分隔記號。scanf()使用空白分隔連續(xù)的輸入項(xiàng)荤傲。
11.What’s wrong with the following statement and how can you fix it?
printf("The double type is %z bytes..\n", sizeof (double));
%z中的z是修飾符垮耳,不是轉(zhuǎn)換字符,所以要在修飾符后面加上一個它修飾的轉(zhuǎn)換字符遂黍≈辗穑可以使用%zd打印十進(jìn)制數(shù),或用不同的說明符打印不同進(jìn)制的數(shù)雾家,例如铃彰,%zx打印十六進(jìn)制的數(shù)。
12. Suppose that you would rather use parentheses than braces in your programs. How well would the following work?
#define ( {
#define ) }
可以是可以的芯咧,但是以后你就沒有圓括號了牙捉,都變成方括號了。