I'm trying to get a very large number (more than unsigned long long int
). So I get it as a string and then convert it, digit by digit, into integer and use it.
#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;
}
However, during compiling it shows:
warning: passing argument 1 of ‘sscanf’ makes pointer from integer without a cast
and
note: expected ‘const char * restrict’ but argument is of type ‘char’
And, when I run program it will give me the Segmentation fault (core dumped)
error.
I also tried more simple code to find the problem:
#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;
}
but nothing changed.
%d
does not mean "digit" it means "signed decimal integer". But as to the error itself,sscanf
needs aconst char *
(ie a string) but you have given a single character. – kaylumm
being only four characters plus a thanks, ”very long” is an appalling misnomer. You can't safely use%d
for really long strings of digits; the behaviour on overflow is undefined. – Jonathan Lefflersscanf(m[i],"%d",&x);
is wrong. You should dox = m[i] - '0';
instead. and your input string is too small for a big number. – Jean-François Fabre