how to use "long" with printf

R

Ram Prasad

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));
}
 
J

Jens Thoms Toerring

Ram Prasad said:
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
 
B

Ben Bacarisse

Ram Prasad said:
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.
 
J

Jorgen Grahn

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
 
S

Seebs

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
 

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

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top