在scanf函數中,*修飾符能夠跳過所在項的輸入。以下:函數
#include <stdio.h> int main() { int a=0,b=0,c=0; printf("請輸入:"); scanf("%*d%d%d",&a,&b,&c); printf("a=%d,b=%d,c=%d",a,b,c); return 0; }
依次輸入1 2 3,運行結果:spa
請輸入:1 2 3 a=2,b=3,c=0
這裏三個%d對應三個輸入,第一個%d用*修飾,因此其對應輸入的1被直接跳過,而後2,3,分別被寫進a,b,而c未被存入。code
特別須要注意的是,*修飾跳過是跳過轉換類型對應字節,如上例,輸入的1,2,3分別佔4字節(一個int類型的字節數),共12字節,跳過期即跳過前4字節。舉個例子:blog
#include <stdio.h> int main() { char ch[4]; printf("請輸入:"); scanf("%*c%s",ch); printf("%s",ch); return 0; }
這裏咱們輸入ABCD,結果以下:io
請輸入:ABCD
BCD
這裏就很是明顯,ABCD共4字節,而%*c跳過了1字節,剩下3字節存入ch中,因此打印爲BCD。class