how to use "long" with printf

Discussion in 'C Programming' started by Ram Prasad, Feb 7, 2011.

  1. Ram  Prasad

    Ram Prasad Guest

    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));
    }
     
    Ram Prasad, Feb 7, 2011
    #1
    1. Advertisements

  2. 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
     
    Jens Thoms Toerring, Feb 7, 2011
    #2
    1. Advertisements

  3. 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.
     
    Ben Bacarisse, Feb 7, 2011
    #3
  4. Ram  Prasad

    Jorgen Grahn Guest

    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 .
     
    Jorgen Grahn, Feb 8, 2011
    #4
  5. Ram  Prasad

    Seebs Guest

    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 /
    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.
     
    Seebs, Feb 8, 2011
    #5
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    950
    Eric Sosman
    Jul 8, 2003
  2. music4
    Replies:
    3
    Views:
    1,154
    CBFalconer
    Jan 5, 2004
  3. Joerg Schwerdtfeger

    printf / sscanf unsigned long long int variables

    Joerg Schwerdtfeger, Apr 2, 2004, in forum: C Programming
    Replies:
    3
    Views:
    44,419
    Joerg Schwerdtfeger
    Apr 2, 2004
  4. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,553
    Peter Shaggy Haywood
    Sep 20, 2005
  5. Schraalhans Keukenmeester

    printf unsigned long long as hex

    Schraalhans Keukenmeester, Jul 1, 2007, in forum: C Programming
    Replies:
    5
    Views:
    35,376
    Army1987
    Jul 2, 2007
  6. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    679
    santosh
    Jul 24, 2007
  7. Bart C

    Use of Long and Long Long

    Bart C, Jan 9, 2008, in forum: C Programming
    Replies:
    27
    Views:
    1,196
    Peter Nilsson
    Jan 15, 2008
  8. Hans Vlems

    long long int and sscanf, printf

    Hans Vlems, Sep 7, 2012, in forum: C Programming
    Replies:
    5
    Views:
    2,447
    Hans Vlems
    Sep 7, 2012
Loading...