1.
c = getchar(); //字符輸入函數
#include<iostream>
#include<string.h>
#include<stdio.h>
int main(){
char c;
while(c = getchar()){
printf("%c",c);
}
system("pause");
return 0;
}
Q:什麼時候返回呢,輸入第一個字符後,第二個字符後,還是。。。
A:輸入回車之後
如果是正常輸入
a
b
c
輸出也會是
a
b
c
看起來很正常,但是
如果輸入爲:
abcdefgh
那輸出一定等到我敲了回車以後纔給我輸出:
abcdefgh
如果我直接敲回車符呢,事實證明他會直接輸出回車符不會等待第二個回車。
2.
sscanf(s,"????",&a,&b);//從一個字符串中讀進與指定格式相符的數據
如果說printf() fprintf() sprintf() 是三兄弟,那scanf() 和 sscanf() 就是親姐妹了,看個實例
#include<iostream>
#include<string.h>
#include<stdio.h>
int main(){
char str[20]={"12:34:56"};
int hh,mm,ss;
char h[3],m[3],s[3];
sscanf(str,"%d:%d:%d",&hh,&mm,&ss);
printf("one:%d:%d:%d\n",hh,mm,ss);
sscanf(str,"%[^:]:%[^:]:%s",h,m,s);
printf("two:%s:%s:%s\n",h,m,s);
sscanf(str,"%2s%*1s%2s%*1s%s",h,m,s);
printf("three:%s:%s:%s\n",h,m,s);
system("pause");
return 0;
}
one:12:34:56two:12:34:56
three:12:34:56
請按任意鍵繼續. . .
下面是百度的表達
1. 常見用法。