Format Specifier for short

K

kaustubh

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).
 
T

Tom St Denis

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
 
K

kaustubh

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

Eric Sosman

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

kaustubh

     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
 
I

Igmar Palsenberg

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
 
J

John Bode

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

Noob

John said:
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)
 
S

Seebs

%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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top