我正在尝试获取一个非常大的数字(大于unsigned long long int
)。所以我将其作为字符串获取,然后将其逐位转换为整数并使用它。
#include <stdio.h>
#include <string.h>
int main()
{
char m[100];
int x;
scanf("%s",m);
for(int i=0; i<strlen(m); i++){
sscanf(m[i],"%d",&x);
printf("%d",x);
}
return 0;
}
在编译过程中显示warning: passing argument 1 of ‘sscanf’ makes pointer from integer without a cast
和note: expected ‘const char * restrict’ but argument is of type ‘char’
并且当我运行程序时,它将给我Segmentation fault (core dumped)
错误。我还尝试了更简单的代码来查找问题。
#include <stdio.h>
#include <string.h>
int main()
{
char m[5];
char n;
int x;
scanf("%s",m);
n = m[1];
sscanf(n,"%d",&x);
return 0;
}
但没有任何改变。
scanf
不适用于字符
'0'
减为一个字符即可将数字转换为整数:使用sscanf
将字符串的单个数字