[英]Why is this code printing “1” for all inputs?
#include <stdio.h>
int main(){
char testChar;
printf("%d\n", scanf("%c", &testChar));
return 0;
}
我只想打印掃描的字符數。 但是,當我所有的輸入都具有相同的輸出時,即1。
scanf()
only scans your first character input and returns 1.
您可以嘗試打印從stdin
讀取的字符數。 與scanf
循環使用。
#include <stdio.h>
#include <stdlib.h>
int
main(void) {
char testChar;
int numchar = 0;
printf("Enter characters, CTRL-C to end:\n");
while (scanf("%c", &testChar) == 1) {
numchar++;
}
printf("Number of characters scanned: %d\n", numchar);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.