Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Format Specifier for short

Reply
Thread Tools

Format Specifier for short

 
 
kaustubh
Guest
Posts: n/a
 
      01-18-2010
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).
 
Reply With Quote
 
 
 
 
Tom St Denis
Guest
Posts: n/a
 
      01-18-2010
On Jan 18, 7:09*am, kaustubh <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
kaustubh
Guest
Posts: n/a
 
      01-18-2010
On Jan 18, 5:29*pm, Tom St Denis <(E-Mail Removed)> 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.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-18-2010
On 1/18/2010 7:29 AM, Tom St Denis wrote:
> On Jan 18, 7:09 am, kaustubh<(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
kaustubh
Guest
Posts: n/a
 
      01-18-2010
On Jan 18, 6:52*pm, Eric Sosman <(E-Mail Removed)> 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
 
Reply With Quote
 
Igmar Palsenberg
Guest
Posts: n/a
 
      01-18-2010

> 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
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      01-18-2010
On Jan 18, 6:09*am, kaustubh <(E-Mail Removed)> 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.
 
Reply With Quote
 
Noob
Guest
Posts: n/a
 
      01-18-2010
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)
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      01-18-2010
On 2010-01-18, kaustubh <(E-Mail Removed)> 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 / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
format specifier for long long ints.... Ray Dillinger C Programming 4 03-27-2006 02:47 AM
Does wrong format specifier leads to memory corruption? indushekara C Programming 4 06-22-2005 07:56 PM
limiting string size in data grid format specifier for column =?Utf-8?B?UGF1bA==?= ASP .Net 1 02-01-2005 12:47 AM
Struggling with struct.unpack() and "p" format specifier Geoffrey Python 5 12-01-2004 02:20 PM
format specifier Ravi Uday C Programming 2 07-16-2003 09:24 AM



Advertisments