String to long integer strtol

K

knight

I tried to convert a string to long integer using the following code
but it is not working. I tried with strtolI and strtod. I think the
input exceeds long integer range. What function shall i use in c to
accomplish this ? Please help me.


void main()
{
char a[20]="4051592851";
long int i;
double d;
i = strtoll(a,NULL,10);
printf("%ld\n",i);
d = strtod(a,NULL,10);
printf("%lf\n",d);
}


O/p
-243374445
-765460480.000000
 
F

Fred Zwarts

knight said:
I tried to convert a string to long integer using the following code
but it is not working. I tried with strtolI and strtod. I think the
input exceeds long integer range. What function shall i use in c to

You ask a C question in a C++ group. Why?
accomplish this ? Please help me.


void main()
{
char a[20]="4051592851";
long int i;
double d;
i = strtoll(a,NULL,10);
printf("%ld\n",i);
d = strtod(a,NULL,10);
printf("%lf\n",d);
}

My C compiler has the following diagnostics:


i = strtoll(a,NULL,10);
.....^
%CC-I-IMPLICITFUNC, In this statement, the identifier "strtoll" is implicitly declared as a function.
at line number 6 in file SCRATCH_ROOT:<KLAD>T.CPP;1

i = strtoll(a,NULL,10);
...............^
%CC-E-UNDECLARED, In this statement, "NULL" is not declared.
at line number 6 in file SCRATCH_ROOT:<KLAD>T.CPP;1

printf("%ld\n",i);
^
%CC-I-IMPLICITFUNC, In this statement, the identifier "printf" is implicitly declared as a function.
at line number 7 in file SCRATCH_ROOT:<KLAD>T.CPP;1

d = strtod(a,NULL,10);
.....^
%CC-I-IMPLICITFUNC, In this statement, the identifier "strtod" is implicitly declared as a function.
at line number 8 in file SCRATCH_ROOT:<KLAD>T.CPP;1




Apparently at least one include is missing.

With the right includes, stroll returns a long long, but you assign it to a long int,
which might cause truncation.
 
J

Jens Thoms Toerring

knight said:
I tried to convert a string to long integer using the following code
but it is not working. I tried with strtolI and strtod. I think the
input exceeds long integer range. What function shall i use in c to
accomplish this ? Please help me.

If you have questions about C (and not C++) comp.lang.c would
be a better place to ask (you posted this to comp.lang.c++).
So just a few remarks:

You're missing includes, for C use

#include <stdio.h>
#include <stdlib.h>

and for C++

#include <cstdio>
#include said:
void main()

Make that

int main( void )

for C or

int main( )

for C++.
{
char a[20]="4051592851";
long int i;

Since you're going to use strtoll() you should
then use

long long int i;

instead.
double d;
i = strtoll(a,NULL,10);
printf("%ld\n",i);

If you use a long long int then you would need

printf( "%lld\n:, i );
d = strtod(a,NULL,10);

This isn't correct, strtod() takes only two arguments.
How did you get this to compile? It makes me wonder if
you compiled this with a C compiler and didn't have
the proper header files included. That would easily
explain the results you get.
printf("%lf\n",d);

The correct format specifier for double is "%f".
O/p
-243374445
-765460480.000000

While 4051592851 could be too large for a long int
(but it should fit into an unsigned long int) it de-
finitely isn't too large for a double.

Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top