今天下午用了一下午發現了一個問題:在Dev C++5.1.1中使用scanf輸入多個值得時候,格式為應該是
scanf("%d" "%d" "%d",&a,&b,&c),
或者是
scanf("%d %d %d",&a,&b,&c),
而不是教材上寫的
scanf("%d,%d,%d",&a,&b,&c)。
如果使用一個冒號加逗號的格式會導致輸入的值a、b、c中只有a的值準確,b、c的值都不準確,不信可以試試。
uj5u.com熱心網友回復:
你寫成這種格式scanf("%d,%d,%d",&a,&b,&c),意味著你輸入的時候也要用逗號分隔,而不是空格比如:1,2,3
uj5u.com熱心網友回復:
第一種是錯的第二種輸入時可用空格分
第三種輸入時要用逗號分隔
uj5u.com熱心網友回復:
驗證了確實如此。VS就不是這樣?
uj5u.com熱心網友回復:
Format Specification Fields: scanf and wscanf FunctionsA format specification has the following form:
%[*] [width] [{h | l | I64 | L}]type
The format argument specifies the interpretation of the input and can contain one or more of the following:
White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input.
Non–white-space characters, except for the percent sign (%). A non–white-space character causes scanf to read, but not store, a matching non–white-space character. If the next character in stdin does not match, scanf terminates.
Format specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list.
The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in stdin; the matching characters in stdin are scanned but not stored. If a character in stdin conflicts with the format specification, scanf terminates, and the character is left in stdin as if it had not been read.
When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string.
An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification.
Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number.
The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%.
An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.
不要迷信書、考題、老師、回帖;
要迷信CPU、編譯器、除錯器、運行結果。
并請結合“盲人摸太陽”和“駕船出海時一定只帶一個指南針。”加以理解。
任何理論、權威、傳說、真理、標準、解釋、想象、知識……都比不上擺在眼前的事實!
uj5u.com熱心網友回復:
c語言連著寫的字串常量編譯器會自動合并的。uj5u.com熱心網友回復:
僅供參考:#include <stdio.h>
int main()
{
int a,b,n,v,r,k;
char buf[80];
char *p;
k=0;
r=EOF;
while (1) {
if (EOF==r) {
fgets(buf,80,stdin);
p=buf;
}
while (1) {
r=sscanf(p,"%d%n",&v,&n);
if (1==r) {k++;break;}
if (EOF==r) break;
p++;
}
if (EOF==r) continue;
p+=n;
if (1==k) a=v;
if (2==k) {
b=v;
break;
}
}
printf("%d,%d\n",a,b);
return 0;
}
uj5u.com熱心網友回復:
http://en.cppreference.com/w/c/io/fscanfuj5u.com熱心網友回復:
+1
uj5u.com熱心網友回復:
6樓的代碼實作了輸入兩個整數時隨便你怎么分隔。
uj5u.com熱心網友回復:
在+1
uj5u.com熱心網友回復:
再+10086
uj5u.com熱心網友回復:
這個也不好解釋吧.....http://download.csdn.net/detail/huangzhengdoc/9618890
還是自己看一下吧......
uj5u.com熱心網友回復:
第一個 自動跳白 ,格式等同于 “%d%d%d”,輸入的時候,依然需要空格,等空白符號分開資料。不然就會產生輸入混亂第二個按照格式跳白-----空格的作用是跳白-----
第三個輸入逗號作為分隔符,并跳過逗號,自動跳白
這幾個格式本身并沒有錯誤
只是 格式串,和大家所能想象的輸入格式未必相同,和預期的格式不太一致。
uj5u.com熱心網友回復:
"%d" "%d" "%d"這是三個字串連接成一個字串中間的空格被忽略
uj5u.com熱心網友回復:
scanf 格式串中非格式字符部分,表示期待輸入的(資料以外的)字符格式串也是字串,所以字串連接,字串中的轉義序列,對格式串同樣適用。
只有% 號序列,是格式串獨有的序列,表示要輸入的資料型別,格式。
uj5u.com熱心網友回復:
指明了什么樣的格式,就要有什么樣的輸入。
"%d" "%d" "%d"其實就是"%d%d%d"
對應輸入:1<space>2<space>3
"%d %d %d"由于格式中有額外的空格,所以輸入中除了原先的空格之外,還需要有對應的額外的空格:
對應輸入:1<space><space>2<space><space>3
"%d,%d,%d"由于格式中用于分隔數字的符號是,,所以輸入中要用,來分隔
對應輸入:1,2,3
指明了什么樣的格式,就要有什么樣的輸入。
uj5u.com熱心網友回復:
scanf("%d %d %d",&a,&b,&c),由于 整型和浮點型資料讀取的時候,預設會跳過(吃掉)所有前導空白(空格符,制表符,回車符等空白符)
所以加個空格,跟沒加空格,效果是一致的
------------------------------------------------------------------------
格式串中的空格作用是跳白,也就是吃掉空白符
---跳過空白符,直到遇到非空白符號為止。-------------
所以:
不打算輸入空行,以及前導空白,可以在%s格式前加空格
不打算輸入空白字符,也可以在%c的前面加上空格
uj5u.com熱心網友回復:
對輸入容錯才是好的設計。
uj5u.com熱心網友回復:

確實如樓主所說,即使輸入時按逗號要求輸入,也是得不到正確的值
不知道別的編譯器是不是
但這樣題目能過,我猜是編譯器的原因
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/69208.html
標籤:C語言
上一篇:求助大佬。。。。
下一篇:C語言空函式分析
