how to print a short and long integer?

Discussion in 'C Programming' started by a, Sep 25, 2005.

  1. a

    a Guest

    short s;
    long l;
    s= -2;
    l= -3;
    printf("% _ %_",s, l);
    What characters should be filled out in the formatted string for output?
    Thanx
    a, Sep 25, 2005
    #1
    1. Advertising

  2. a

    Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    a wrote:
    > short s;
    > long l;
    > s= -2;
    > l= -3;
    > printf("% _ %_",s, l);
    > What characters should be filled out in the formatted string for output?


    printf("%d %ld",s,l);

    printf is a variadic function, so it's prototype does not include definitions
    for most of it's arguments (only the format string argument is defined).

    Thus, for the other arguments, the standard promotion rules apply:
    - - short int promotes to int
    - - long int stays as long int

    The format strings for each argument must anticipate the format of the
    arguments. %d is used for int, so it will be used for a short int which gets
    promoted to int. %ld is used for long int.

    - --
    Lew Pitcher

    Master Codewright & JOAT-in-training | GPG public key available on request
    Registered Linux User #112576 (http://counter.li.org/)
    Slackware - Because I know what I'm doing.
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.7 (GNU/Linux)

    iD8DBQFDNiVjagVFX4UWr64RAgHHAJ0REEuapZyZpblfFP5V4dWEZMRBEgCgxzwB
    6moAzVtg16kHNGrjWNmua7A=
    =Djbp
    -----END PGP SIGNATURE-----
    Lew Pitcher, Sep 25, 2005
    #2
    1. Advertising

  3. a

    Jack Klein Guest

    On Sun, 25 Sep 2005 04:14:29 GMT, "a" <> wrote in
    comp.lang.c:

    > short s;
    > long l;
    > s= -2;
    > l= -3;
    > printf("% _ %_",s, l);
    > What characters should be filled out in the formatted string for output?
    > Thanx


    What does your C reference book say?

    What does your compiler's library manual, online help file, or man
    pages tell you?

    Why do you think usenet is a substitute for looking up basic
    information in standard references?

    Type this string into the search box at google.com:

    printf "conversion specifiers"

    ....and follow the first link.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Sep 25, 2005
    #3
  4. a wrote:
    > short s;
    > long l;
    > s= -2;
    > l= -3;
    > printf("% _ %_",s, l);
    > What characters should be filled out in the formatted string for output?
    > Thanx


    #include <stdio.h>
    int main(void)
    {
    short s = -2;
    long l = -3;
    printf("The standard way using \"%%hd %%ld\": %hd %ld\n", s, l);
    printf(" (but \"%%d %%ld\" should work as well: %d %ld\n", s, l);
    return 0;
    }


    The standard way using "%hd %ld": -2 -3
    (but "%d %ld" should work as well: -2 -3
    Martin Ambuhl, Sep 25, 2005
    #4
  5. a

    jacob navia Guest

    Martin Ambuhl a écrit :
    > a wrote:
    >
    >> short s;
    >> long l;
    >> s= -2;
    >> l= -3;
    >> printf("% _ %_",s, l);
    >> What characters should be filled out in the formatted string for output?
    >> Thanx

    >
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > short s = -2;
    > long l = -3;
    > printf("The standard way using \"%%hd %%ld\": %hd %ld\n", s, l);
    > printf(" (but \"%%d %%ld\" should work as well: %d %ld\n", s, l);
    > return 0;
    > }
    >
    >
    > The standard way using "%hd %ld": -2 -3
    > (but "%d %ld" should work as well: -2 -3


    Why did you do his homework Martin?

    It doesn't really help him. He will stay a lazy
    student, used to get around without doing any effort.

    This question can be answered with a *minimal* effort,
    but he doesn't even want to do that little.
    jacob navia, Sep 25, 2005
    #5
  6. a

    jacob navia Guest

    Lew Pitcher a écrit :
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > a wrote:
    >
    >>short s;
    >>long l;
    >>s= -2;
    >>l= -3;
    >>printf("% _ %_",s, l);
    >>What characters should be filled out in the formatted string for output?

    >
    >
    > printf("%d %ld",s,l);
    >

    Why did you do his homework Lew?

    It doesn't really help him. He will stay a lazy
    student, used to get around without doing any effort.

    This question can be answered with a *minimal* effort,
    but he doesn't even want to do that little.
    jacob navia, Sep 25, 2005
    #6
  7. a wrote on 25/09/05 :
    > short s;
    > long l;
    > s= -2;
    > l= -3;
    > printf("% _ %_",s, l);
    > What characters should be filled out in the formatted string for output?


    What about opening your C-book at 'printf' or 'formatted outputs' ?

    #include <stdio.h>

    int main (void)
    {
    short s = -2;
    long l = -3;

    printf ("%hd %ld\n", s, l);

    return 0;
    }

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    I once asked an expert COBOL programmer, how to
    declare local variables in COBOL, the reply was:
    "what is a local variable?"
    Emmanuel Delahaye, Sep 25, 2005
    #7
  8. a

    SM Ryan Guest

    Jack Klein <> wrote:
    # On Sun, 25 Sep 2005 04:14:29 GMT, "a" <> wrote in
    # comp.lang.c:
    #
    # > short s;
    # > long l;
    # > s= -2;
    # > l= -3;
    # > printf("% _ %_",s, l);
    # > What characters should be filled out in the formatted string for output?
    # > Thanx
    #
    # What does your C reference book say?
    #
    # What does your compiler's library manual, online help file, or man
    # pages tell you?
    #
    # Why do you think usenet is a substitute for looking up basic
    # information in standard references?
    #
    # Type this string into the search box at google.com:
    #
    # printf "conversion specifiers"
    #
    # ...and follow the first link.

    Amazing. You took more time and bandwidth not answering the
    question than the answer would've taken.

    %d and %ld

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Wow. A sailboat.
    SM Ryan, Sep 25, 2005
    #8
  9. a

    Mike Wahler Guest

    "SM Ryan" <> wrote in
    message news:...
    > Jack Klein <> wrote:
    > # On Sun, 25 Sep 2005 04:14:29 GMT, "a" <> wrote in
    > # comp.lang.c:
    > #
    > # > short s;
    > # > long l;
    > # > s= -2;
    > # > l= -3;
    > # > printf("% _ %_",s, l);
    > # > What characters should be filled out in the formatted string for
    > output?
    > # > Thanx
    > #
    > # What does your C reference book say?
    > #
    > # What does your compiler's library manual, online help file, or man
    > # pages tell you?
    > #
    > # Why do you think usenet is a substitute for looking up basic
    > # information in standard references?
    > #
    > # Type this string into the search box at google.com:
    > #
    > # printf "conversion specifiers"
    > #
    > # ...and follow the first link.
    >
    > Amazing. You took more time and bandwidth not answering the
    > question than the answer would've taken.


    The technique is known as teaching.

    -Mike
    Mike Wahler, Sep 25, 2005
    #9
  10. a

    SM Ryan Guest

    # > Amazing. You took more time and bandwidth not answering the
    # > question than the answer would've taken.
    #
    # The technique is known as teaching.

    The technique used to be known as a temper tantrum.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    I'm not even supposed to be here today.
    SM Ryan, Sep 25, 2005
    #10
  11. SM Ryan <> writes:
    > > > Amazing. You took more time and bandwidth not answering the
    > > > question than the answer would've taken.

    > >
    > > The technique is known as teaching.

    >
    > The technique used to be known as a temper tantrum.


    [ Obnoxious '#' quoting character corrected. ]

    Nonsense. The OP apparently was unwilling or unable to use whatever
    reference materials he has available to answer a very simple question.
    Telling him how to print short and long integers would teach him how
    to print short and long integers, and nothing else. It would also
    encourage him to come back here to ask, for example, how to print
    floats and doubles. Jack Klein's response was intended to help the OP
    to answer the question for himself, which, if he pays attention, will
    be of much greater benefit.

    There's a parable about giving a man a fish that you might find
    instructive. (Yes, it's a religious parable, but the point is valid
    whatever your beliefs happen to be.) I'd quote it for you, but I'm
    sure your capable of looking it up yourself.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Sep 26, 2005
    #11
  12. Keith Thompson wrote:

    > There's a parable about giving a man a fish that you might find
    > instructive.


    Give a man a fish, and you've hooked a customer.
    Teach a man to fish, and he'll open a competing fish market.
    Martin Ambuhl, Sep 26, 2005
    #12
  13. Martin Ambuhl <> writes:
    > Keith Thompson wrote:
    >
    >> There's a parable about giving a man a fish that you might find
    >> instructive.

    >
    > Give a man a fish, and you've hooked a customer.
    > Teach a man to fish, and he'll open a competing fish market.


    Give a man a fire, and he's warm for today.
    Set a man on fire, and he's warm for the rest of his life.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Sep 26, 2005
    #13
  14. "Emmanuel Delahaye" <> wrote in message
    news:...
    > a wrote on 25/09/05 :


    > What about opening your C-book at 'printf' or 'formatted outputs' ?
    >
    > #include <stdio.h>
    >
    > int main (void)
    > {
    > short s = -2;
    > long l = -3;
    >
    > printf ("%hd %ld\n", s, l);


    Giving the OP such an answer is a bit twisted :
    - you do his homework, but the teacher will know.
    - he will be clueless as to why %hd instead of %d
    - their compiler might not even support %hd
    - I for example do not even understand why there is a need for %hd in printf
    - as a matter of fact, %hd may be needed if the argument is not of type short
    but unsigned short for example.
    - the intricacies of integer promotion and printf format strings are not your
    average beginners homework.
    - if the teacher expected %d, the student will be unable to explain the error.
    - if the teacher expected %hd in a beginners course, she is wrong,
    - if this is an AP course, the next question will be why? same problem for the
    student.
    - was this really a trick question, was your answer purposely misleading ?

    > return 0;
    > }



    --
    Chqrlie.

    PS: C aint for sissies
    Charlie Gordon, Sep 26, 2005
    #14
  15. In article <p2GZe.3900$>,
    Martin Ambuhl <> wrote:
    >Keith Thompson wrote:
    >
    >> There's a parable about giving a man a fish that you might find
    >> instructive.

    >
    >Give a man a fish, and you've hooked a customer.
    >Teach a man to fish, and he'll open a competing fish market.


    That's it in a nutshell, isn't it?

    That the people who give direct answers (that is, "helping" the newbs)
    are doing it specifically to promote dependency. Make no mistake, even
    though there's no money changing hands, there still must be some reason why
    people bother to post to Usenet, and promoting dependency is here, as it is
    in real businesses, a good ego boo for the one upon who the dependency
    exists.

    Of course, in a real business, there's actual cash involved, but that may,
    in fact, be secondary to the (psychological) value of the dependency.
    Kenny McCormack, Sep 26, 2005
    #15
  16. a

    pete Guest

    Charlie Gordon wrote:
    >
    > "Emmanuel Delahaye" <> wrote in message
    > news:...
    > > a wrote on 25/09/05 :

    >
    > > What about opening your C-book at 'printf' or 'formatted outputs' ?
    > >
    > > #include <stdio.h>
    > >
    > > int main (void)
    > > {
    > > short s = -2;
    > > long l = -3;
    > >
    > > printf ("%hd %ld\n", s, l);

    >
    > Giving the OP such an answer is a bit twisted :
    > - you do his homework, but the teacher will know.
    > - he will be clueless as to why %hd instead of %d


    Because h is for short.

    > - their compiler might not even support %hd


    The standard makes %hd support mandatory, not optional.

    > - I for example do not even understand why there is a need for %hd in
    > printf


    Symetry with %hu.

    > - as a matter of fact,
    > %hd may be needed if the argument is not of type short
    > but unsigned short for example.


    %hu is for unsigned short.

    > - if the teacher expected %hd in a beginners course, she is wrong,


    The standard says %hd for type short argument.

    h Specifies that a following d, i, o, u, x, or X
    conversion specifier applies to a short int or
    unsigned short int argument (the argument will
    have been promoted according to the integer
    promotions, but its value shall be converted to
    short int or unsigned short int before
    printing); or that a following n conversion
    specifier applies to a pointer to a short int
    argument.


    --
    pete
    pete, Sep 26, 2005
    #16
  17. Charlie Gordon wrote on 26/09/05 :
    > - their compiler might not even support %hd


    Come on. The "h" qualifier has been standard since 1989...

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "It's specified. But anyone who writes code like that should be
    transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
    Emmanuel Delahaye, Sep 26, 2005
    #17
  18. a

    Joe Wright Guest

    Martin Ambuhl wrote:
    > Keith Thompson wrote:
    >
    >> There's a parable about giving a man a fish that you might find
    >> instructive.

    >
    >
    > Give a man a fish, and you've hooked a customer.
    > Teach a man to fish, and he'll open a competing fish market.


    I really like that. I hope I remember it. Thanks.
    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Sep 27, 2005
    #18
  19. "Emmanuel Delahaye" <> wrote in message
    news:...
    > Charlie Gordon wrote on 26/09/05 :
    > > - their compiler might not even support %hd

    >
    > Come on. The "h" qualifier has been standard since 1989...


    I understand the symmetry with scanf() where the short specifier is definitely
    needed.

    For printf, it is there just for the sake of consistency, but is not strictly
    needed for d,i,o,u,x or X conversions.
    specifying required behaviour for %hhn or %jn and friends is ridiculous, and
    leads to unnecessary bloat in the C library.
    Even %zn is debatable. As a matter of fact, the whole %n stuff is of
    questionable value and indeed removed from the "Secure C" proposal.

    If a short or insigned short is passed to printf, %d or %u will do the job. If
    I'm wrong, show us a counterexample.

    If a int is passed and you want want it converted as a short, why not do that
    with a cast in the parameter list ?

    --
    Chqrlie.
    Charlie Gordon, Sep 29, 2005
    #19
    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. Dan Pop
    Replies:
    0
    Views:
    1,167
    Dan Pop
    Jun 24, 2003
  2. cyberdude
    Replies:
    2
    Views:
    5,104
    Keith Thompson
    Jun 25, 2003
  3. David Geering

    longs, long longs, short short long ints . . . huh?!

    David Geering, Jan 8, 2007, in forum: C Programming
    Replies:
    15
    Views:
    544
    Keith Thompson
    Jan 11, 2007
  4. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    457
    santosh
    Jul 24, 2007
  5. Suresh V
    Replies:
    5
    Views:
    3,675
    SaticCaster
    Jul 5, 2010
Loading...

Share This Page