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. Advertising

  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. Advertising

  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. Advertising

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. music4
    Replies:
    3
    Views:
    914
    CBFalconer
    Jan 5, 2004
  2. Joerg Schwerdtfeger

    printf / sscanf unsigned long long int variables

    Joerg Schwerdtfeger, Apr 2, 2004, in forum: C Programming
    Replies:
    3
    Views:
    40,355
    Joerg Schwerdtfeger
    Apr 2, 2004
  3. Schraalhans Keukenmeester

    printf unsigned long long as hex

    Schraalhans Keukenmeester, Jul 1, 2007, in forum: C Programming
    Replies:
    5
    Views:
    32,707
    Army1987
    Jul 2, 2007
  4. Bart C

    Use of Long and Long Long

    Bart C, Jan 9, 2008, in forum: C Programming
    Replies:
    27
    Views:
    807
    Peter Nilsson
    Jan 15, 2008
  5. Hans Vlems

    long long int and sscanf, printf

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

Share This Page