Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to use "long" with printf

Reply
Thread Tools

how to use "long" with printf

 
 
Ram Prasad
Guest
Posts: n/a
 
      02-07-2011
I have a simple demo program that demonstates use of recursion ( on linux )
The problem is that it does not work for "long" integers. I assume this is a problem with printf

I tried %l instead of %d but that doesnot print any integer.

########## This is fine
../fib 10000
50005000

######### Oops this is not working
../fib 100000
705082704


----------------
#include <stdio.h>
#include <stdlib.h>
long fibonacci(long n) {
return ((n == 1 ) ? 1 : (n + (fibonacci(n - 1 ))));
}
int main(int argc , char *argv[]) {
long n = atol(argv[1]);
printf("%d\n",fibonacci(n));
}



 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      02-07-2011
Ram Prasad <> wrote:
> I have a simple demo program that demonstates use of recursion ( on linux )
> The problem is that it does not work for "long" integers. I assume this is a problem with printf


> I tried %l instead of %d but that doesnot print any integer.


You need "%ld".
Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-07-2011
Ram Prasad <> writes:

> I have a simple demo program that demonstates use of recursion ( on
> linux ) The problem is that it does not work for "long" integers. I
> assume this is a problem with printf
>
> I tried %l instead of %d but that doesnot print any integer.


You need %ld. The 'l' is a length modifier not a conversion character.

> ########## This is fine
> ./fib 10000
> 50005000


That's not fine -- fib(10000) is over 2000 digits long! The code will
reveal all...

> ######### Oops this is not working
> ./fib 100000
> 705082704
>
>
> ----------------
> #include <stdio.h>
> #include <stdlib.h>
> long fibonacci(long n) {
> return ((n == 1 ) ? 1 : (n + (fibonacci(n - 1 ))));
> }


This is not the Fibonacci function. It sums the numbers less that n.
None the less, you have still come up against the fact that C's long
type does not have to be able to hold numbers any bigger than
2147483647.

> int main(int argc , char *argv[]) {
> long n = atol(argv[1]);
> printf("%d\n",fibonacci(n));
> }


To calculate large Fibonacci numbers you need to use a library that
gives you "big nums" like gmp or you need to switch to one of the many
languages that provide such things "built in". Note also that the
"obvious" recursive algorithm for Fibonacci numbers is very inefficient.

--
Ben.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      02-08-2011
On Mon, 2011-02-07, Jens Thoms Toerring wrote:
> Ram Prasad <> wrote:
>> I have a simple demo program that demonstates use of recursion ( on linux )
>> The problem is that it does not work for "long" integers. I assume this is a problem with printf

>
>> I tried %l instead of %d but that doesnot print any integer.

>
> You need "%ld".


And (since he's on Linux) "man 3 printf" (to learn the rest) and full
warning options to gcc (so he gets told when he makes this mistake).

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      02-08-2011
On 2011-02-07, Ram Prasad <> wrote:
> I have a simple demo program that demonstates use of recursion ( on linux )
> The problem is that it does not work for "long" integers. I assume this is a problem with printf


No.

> I tried %l instead of %d but that doesnot print any integer.


Try %ld.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
printf affects following printf/s azza C Programming 0 10-17-2010 09:43 AM
Extracting printf(...) from (void) printf(....) guru Perl Misc 8 02-03-2009 10:37 PM
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
bus error with printf line included, error without printf line? ben C Programming 4 06-26-2004 04:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57