Quote:
Originally posted by mola
g_printf(" Number : %d \n",atol("10020030040");
it's turn wrong number .
atol can't convert string more than 10 charchter to long int ? what can i do ?
|
I'm not familiar with
g_printf(). But assuming it works just like the standard printf(): You can tell printf() to expect a long int for %d by adding a "length modifier" (see man 3 printf):
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Number : %ld \n", atol("10020030040"));
return 0;
}
However, this doesn't help you much, because on 32-bit Linux glibc an
int is the same as
long int. Both have 4 bytes space to store the number. But there also is a
long long int which has 8 bytes space. This is enough to store 10020030040. When you run the code below you can see that this is the case:
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("int: %d, long: %d longlong: %d\n", sizeof(int), sizeof(long), sizeof(long long));
return 0;
}
A possible drawback of "long long" is that it i not part of the "ISO C90" standard, as the compiler will show when you compile with the -pedantic option:
Code:
gcc -Wall -pedantic -o nummer nummer.c
nummer.c: In function `main':
nummer.c:6: warning: ISO C90 does not support `long long'
Using the
long long int will solve your problem however. Specify a "ll" (double-el) length modifier between the "%" and the "d", and use ato
ll() instead of ato
l().
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Number : %lld \n", atoll("10020030040"));
return 0;
}
Just hope this is valid for
g_printf() as well...