Format Specifier for short

Discussion in 'C Programming' started by kaustubh, Jan 18, 2010.

  1. kaustubh

    kaustubh Guest

    What is the format specifier for short in a scanf statement? I tried
    using %d and %i but that results in corrupted stack around some
    variables (though all computations are being done correctly).
    kaustubh, Jan 18, 2010
    #1
    1. Advertising

  2. kaustubh

    Tom St Denis Guest

    On Jan 18, 7:09 am, kaustubh <> wrote:
    > What is the format specifier for short in a scanf statement? I tried
    > using %d and %i but that results in corrupted stack around some
    > variables (though all computations are being done correctly).


    According to "man scanf" %h will read a short.

    Apparently, if you RTFM you can find answers to questions like this in
    short [har har] order.

    Tom
    Tom St Denis, Jan 18, 2010
    #2
    1. Advertising

  3. kaustubh

    kaustubh Guest

    On Jan 18, 5:29 pm, Tom St Denis <> wrote:
    > According to "man scanf" %h will read a short.
    >
    > Apparently, if you RTFM you can find answers to questions like this in
    > short [har har] order.
    >
    > Tom


    Actually I checked %h and it doesn't work. I'm using Visual C++ 2008
    express edition, but i didn't think it should make any difference.
    The program prints factorial values.
    Here's the code for main function

    int main() {
    short num_cases,num;
    short result[200];
    scanf("%h",&num_cases);
    for(short i=0;i<num_cases;i++){
    scanf("%h",&num);
    memset(result,0,sizeof(short)*200);
    short num_digits = fact(num,result);
    printNum(result,num_digits);
    }
    return 0;
    }

    If I change short to int and %h to %d, it runs flawlessly.
    kaustubh, Jan 18, 2010
    #3
  4. kaustubh

    Eric Sosman Guest

    On 1/18/2010 7:29 AM, Tom St Denis wrote:
    > On Jan 18, 7:09 am, kaustubh<> wrote:
    >> What is the format specifier for short in a scanf statement? I tried
    >> using %d and %i but that results in corrupted stack around some
    >> variables (though all computations are being done correctly).

    >
    > According to "man scanf" %h will read a short.


    Read again, and if it still says so, fire the man: "%h"
    is not a valid conversion specifier. Try "%hd", or some other
    conversion with the 'h' length modifier.

    > Apparently, if you RTFM you can find answers to questions like this in
    > short [har har] order.


    Speed kills.

    --
    Eric Sosman
    lid
    Eric Sosman, Jan 18, 2010
    #4
  5. kaustubh

    kaustubh Guest

    On Jan 18, 6:52 pm, Eric Sosman <> wrote:
    > > According to "man scanf" %h will read a short.

    >
    >      Read again, and if it still says so, fire the man: "%h"
    > is not a valid conversion specifier.  Try "%hd", or some other
    > conversion with the 'h' length modifier.
    >
    >      Speed kills.


    %hd does the trick. In real world programming, using short might be an
    overkill, but I'm still a student. I took it as an opportunity to
    learn more.. Thanks
    kaustubh, Jan 18, 2010
    #5

  6. > int main() {
    > short num_cases,num;
    > short result[200];
    > scanf("%h",&num_cases);
    > for(short i=0;i<num_cases;i++){
    > scanf("%h",&num);
    > memset(result,0,sizeof(short)*200);
    > short num_digits = fact(num,result);
    > printNum(result,num_digits);
    > }
    > return 0;
    > }
    >
    > If I change short to int and %h to %d, it runs flawlessly.


    Change the warning level of your compiler, or get a decent one. It
    should have warned you about the things you pass to scanf() in t his case.


    Igmar
    Igmar Palsenberg, Jan 18, 2010
    #6
  7. kaustubh

    John Bode Guest

    On Jan 18, 6:09 am, kaustubh <> wrote:
    > What is the format specifier for short in a scanf statement? I tried
    > using %d and %i but that results in corrupted stack around some
    > variables (though all computations are being done correctly).


    Use the size specifier "h" with any of the "d", "i", "o", "u", "x",
    "X", or "n" conversion specifiers to specify a short or unsigned short
    int: "%hd", "%hi", "%hu", etc.
    John Bode, Jan 18, 2010
    #7
  8. kaustubh

    Noob Guest

    John Bode wrote:

    > Use the size specifier "h" with any of the "d", "i", "o", "u", "x",
    > "X", or "n" conversion specifiers to specify a short or unsigned short
    > int: "%hd", "%hi", "%hu", etc.


    %hi %ho, %hi %ho
    It's home from work we go
    (whistle)
    Noob, Jan 18, 2010
    #8
  9. kaustubh

    Seebs Guest

    On 2010-01-18, kaustubh <> wrote:
    > %hd does the trick. In real world programming, using short might be an
    > overkill, but I'm still a student. I took it as an opportunity to
    > learn more.


    Good attitude, overall. Sooner or later you'll probably need to know
    this stuff.

    -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!
    Seebs, Jan 18, 2010
    #9
    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. =?Utf-8?B?UGF1bA==?=
    Replies:
    1
    Views:
    464
    =?Utf-8?B?UGF1bA==?=
    Feb 1, 2005
  2. David Geering

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

    David Geering, Jan 8, 2007, in forum: C Programming
    Replies:
    15
    Views:
    546
    Keith Thompson
    Jan 11, 2007
  3. Replies:
    4
    Views:
    806
    Kaz Kylheku
    Oct 17, 2006
  4. Ioannis Vranos

    unsigned short, short literals

    Ioannis Vranos, Mar 4, 2008, in forum: C Programming
    Replies:
    5
    Views:
    661
    Eric Sosman
    Mar 5, 2008
  5. Andre
    Replies:
    5
    Views:
    522
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page