LinuxQuestions.org
Help answer threads with 0 replies.
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 03-28-2009, 04:19 PM   #1
Aziz
LQ Newbie
 
Registered: Sep 2004
Location: USA
Distribution: Fedora Core 3
Posts: 29

Rep: Reputation: 15
Unable to use long long number types?


Hello guys

I am trying this simple program:

Code:
#include <iostream>
#include <math.h>
#include <ctime>

int main(int argc, char *argv[])
{
     cout << endl << argv[1] << endl
        << atol(argv[1]) << endl
        << strtoul(argv[1],NULL,36) << endl <<
        (unsigned long long) argv[1] << endl <<
        (long long) argv[1] << endl;

     return 0;
}

I want to be able to transfer the argv[1] (which is a long long type) into a variable, but I am not able to convert it to a number.

Here is the output when I test the program:

./program 1124555552555562554

Code:
1124555552555562554
541357626
4294967295
18446744073705356114
-4195502
How can I convert argv[1] into a long long type so I can use it like:

long long number = argv[1];

Thanks for any hints,
 
Old 03-28-2009, 05:15 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
maybe try sscanf and the format %ull - for unsigned long long
 
Old 03-28-2009, 06:09 PM   #3
Aziz
LQ Newbie
 
Registered: Sep 2004
Location: USA
Distribution: Fedora Core 3
Posts: 29

Original Poster
Rep: Reputation: 15
Thumbs up

Quote:
Originally Posted by graemef View Post
maybe try sscanf and the format %ull - for unsigned long long
Thanks for the answer. If I try this number:

9223372036854775807

sscanf(argv[1],"%ull",&number);

number becomes:

18446744069414584320

However, reading the sscanf man I think you meant "%llu" which worked perfectly.

Thanks alot for your help, very much appreciated.
 
Old 03-28-2009, 06:21 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Sorry for that error, I should never reply before my first coffee of the day. Glad you got it sorted out.
 
Old 03-28-2009, 06:22 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Why don't you use strtoull? As far as I know, %llu is GNU but strtoull is C99 and POSIX. You'll at least have build problems to indicate lack of support for strtoull, but that won't happen with %llu (if you ever plan to use a different OS.)
Kevin Barry

PS In both cases you should be checking for residual data. For example, try entering 1234hello as your first argument and see if it's accepted. My guess is that it is, which should be undesirable.

Last edited by ta0kira; 03-28-2009 at 06:36 PM.
 
  


Reply

Tags
argv, c++, long, number


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Long standing USB problem with XP & Linux...long cbjhawks Linux - Hardware 4 02-23-2009 09:32 AM
15 digit long number Zagi Programming 3 07-14-2008 07:26 AM
printf unsigned long long int? blackzone Programming 9 03-04-2008 01:41 PM
long filename & long directory tree slack66 Slackware 1 09-20-2006 10:56 AM
How to format a long double number in C++ to display as many digits as possible. TruongAn Programming 4 11-09-2005 11:11 AM


All times are GMT -5. The time now is 10:13 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration