我一直在尝试使用sscanf()
带有前导零的字符串(例如'03')扫描整数。
但是,它工作正常,但只到 '07'。从 '08' 开始,字符串将被读取为 0。
您将在下面找到我的代码和输出。感谢您的帮助!
#include <stdio.h>
#include <stdlib.h>
int main() {
char string_six[3] = "06";
char string_seven[3] = "07";
char string_eight[3] = "08";
char string_nine[3] = "09";
int six = -1;
int seven = -1;
int eight = -1;
int nine = -1;
sscanf(string_six, "%i", &six);
sscanf(string_seven, "%i", &seven);
sscanf(string_eight, "%i", &eight);
sscanf(string_nine, "%i", &nine);
printf("Six: %i\n",six);
printf("Seven: %i\n",seven);
printf("Eight: %i\n",eight);
printf("Nine: %i\n",nine);
return 0;
}
输出:
Six: 6
Seven: 7
Eight: 0
Nine: 0