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.
sscanf() Return value If successful, the sscanf() function returns the number of receiving arguments successfully assigned. If a matching failure occurs before the first receiving argument was assigned, returns zero.
Because the array is already passed as an address, whereas the integer variable is not (and thus explicitly needs its address passed via the & address-of operator).
In C, sscanf() is used to read formatted data. It works much like scanf() but the data will be read from a string instead of the console.
Description. The C library function int sscanf(const char *str, const char *format, ...) reads formatted input from a string.
int sscanf(const char *str, const char *format, ...) str − This is the C string that the function processes as its source to retrieve the data. format − This is the C string that contains one or more of the following items: Whitespace character, Non-whitespace character and Format specifiers
It reads data from a character string. sscanf (string,formatspecifier,&var1,&var2,……..) String refers to the char string to read from. Format string refers to char string containing certain required formatting information. Var1,var2 etc., represent the individual input data items.
The rest of the arguments of sscanf () is same as that of scanf (). It returns the number of items read from the string and -1 if an error is encountered. The following program demonstrates how sscanf () works: In line 6, we have declared and initialized a variable str of type pointer to char.
The rest of the arguments of sscanf () is same as that of scanf (). It returns the number of items read from the string and -1 if an error is encountered.
scanf
doesn't apply to characters. Once you have the characters just convert digits to integers by subtracting '0'
as a char:
for(int i=0; i<strlen(m); i++){
x = m[i] - '0'; // line to change
printf("%d",x);
}
Besides, just to make sure that the buffer won't overflow, 100 bytes is good, but you may want to use an according limit in scanf
and check the return code:
if (scanf("%99s",m) == 1) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With