LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-12-2005, 05:01 AM   #1
mola
LQ Newbie
 
Registered: Oct 2004
Distribution: Fedora core 3
Posts: 25

Rep: Reputation: 15
C problem !


Hi all

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 ?
 
Old 07-12-2005, 05:06 AM   #2
marghorp
Senior Member
 
Registered: Jan 2004
Location: Slovenia
Distribution: Slackware 10.1, SLAX to the MAX :)
Posts: 1,040

Rep: Reputation: 45
Can you use atof?
 
Old 07-12-2005, 07:47 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: C problem !

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 atoll() instead of atol().
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...
 
Old 07-12-2005, 03:23 PM   #4
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
g_print () is part of the gtk2.0 programming functions

they are used when you use the include
#include <gtk/gtk.h>

not sure on the whole atol thing.

but may i ask why do this why not just put the number into an int ??

sorry for my ignorance if this is obvious
 
Old 07-13-2005, 04:53 AM   #5
mola
LQ Newbie
 
Registered: Oct 2004
Distribution: Fedora core 3
Posts: 25

Original Poster
Rep: Reputation: 15
Tanks every body
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration