| %d | 有正負號的十進位整數 | int * |
| %i | 有正負號的十進位整數 | int * |
| %u | 無正負號的十進位整數 | int * |
| %o | 無正負號的八進位整數 | int * |
| %x, %X | 無正負號的十六進位整數 | int * |
| %c | 字元 | char |
| %s | 字串 | char * |
| %f | 浮點數 | double |
| %p | 記憶體位址的編碼 | void * |
| %% | 百分比符號 | % |
sscanf() 的第一個參數為所接受的陣列名稱,第二個參數為格式化字串,隨後可接多個參數,依格式化字串中的轉換格式的數量而定。例如要求使用者輸入兩個整數,如下
sscanf(s1, "%d%d", &n1, &n2);
格式化字串中只需標明轉換格式, sscanf() 會自動依輸入的順序將值存入其後的參數中。這裡要注意,其後的參數必須是指標。
以下程式示範使用 sscanf() 的結果
#include <stdio.h>
int main(void)
{
char c1[20] = "He has 5 books.\n";
char c2[10], c3[10], c4[10];
int n;
sscanf(c1, "%s%s%d%s", c2, c3, &n, c4);
printf("%s\n", c2);
printf("%s\n", c3);
printf("%d\n", n);
printf("%s\n", c4);
return 0;
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:csscanf.c
功能:示範 stdio.h 中函數 scanf() 的使用
作者:張凱慶
時間:西元2010年6月 */編譯後執行,結果如下

您可以繼續參考
輸出與輸入 stdio.h
- int getchar(void);
- char *gets(char *s);
- int putchar(int c);
- int puts(const char *s);
- int printf(const char *format, ...);
- int sprintf(char *s, const char *format, ...);
- int scanf(const char *format, ...);
- int sscanf(char *s, const char *format, ...);
- FILE *fopen(const char *filename, const char *mode);
- FILE *freopen(const char *filename, const char *mode, FILE *stream);
- int fflush(FILE *stream);
- int fclose(FILE *stream);
- int remove(const char *filename);
- int rename(const char *oldname, const char *newname);
- int fprintf(FILE *stream, const char *format, ...);
- int fscanf(FILE *stream, const char *format, ...);
- int fgetc(FILE *stream);
- char *fgets(char *s, int n, FILE *stream);
- int fputs(const char *s, FILE *stream);
- int fputs(const char *s, FILE *stream);
- size_t fread(void *ptr, size_t size, siz_t nobj, FILE *stream)
- size_t fwrite(const void *ptr, size_t size, size_t nobj, FILE *stream)
- int fseek(FILE *stream, long offset, int origin);
- long ftell(FILE *stream);
- void rewind(FILE *stream);
沒有留言:
張貼留言