C也同樣不分int、char什麽的。
授人以渔,不授人以鱼。
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <conio.h> const int K_ENTER = 0x0D; int InputInteger(void); int main(void) { printf_s("\n%d\n", InputInteger()); _getch(); return EXIT_SUCCESS; } int InputInteger(void) { char textBuffer[20]; int ch, index = 0; do { ch = _getch(); if (isdigit(ch) || ((index == 0) && (ch == '-'))) { _putch(ch); textBuffer[index++] = ch; } } while ((index < sizeof(textBuffer) - 1) && (ch != K_ENTER)); textBuffer[index] = '\0'; return atoi(textBuffer); }
#include <stdio.h> #include <string.h> int main(void) { char input[16] = {0}; char tmp[16] = {0}; int num = 0; printf("请输入数字:"); scanf("%10s", input); if (sscanf(input, "%d", &num) != 1) { printf("你看看你输入的破数\n"); return 0; } sprintf(tmp, "%d", num); if (strcmp(tmp, input)) { printf("你看看你输入的破数\n"); /* Not identical. */ return 0; } printf("你输入了%d\n", num); return 0; }