| %d | 有正負號的十進位整數 | int * |
| %i | 有正負號的十進位整數 | int * |
| %u | 無正負號的十進位整數 | int * |
| %o | 無正負號的八進位整數 | int * |
| %x, %X | 無正負號的十六進位整數 | int * |
| %c | 字元 | char |
| %s | 字串 | char * |
| %f | 浮點數 | double |
| %p | 記憶體位址的編碼 | void * |
| %% | 百分比符號 | % |
scanf() 的第一個參數為接受使用者輸入的格式化字串,隨後可接多個參數,依格式化字串中的轉換格式的數量而定。例如要求使用者輸入兩個整數,如下
scanf("%d%d", &n1, &n2);格式化字串中只需標明轉換格式, scanf() 會自動依輸入的順序將值存入其後的參數中。這裡要注意,其後的參數必須是指標。
以下程式示範使用 scanf() 的結果
#include <stdio.h>
int main(void)
{
int n;
printf("請輸入整數數字:");
scanf("%d", &n);
printf("剛剛輸入的整數數字為 %d\n", n);
return 0;
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:cscanf.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);
沒有留言:
張貼留言