How to read a short int variable?

S

stefan.istrate

I have a short int variable and I'm trying to read it with
scanf("%d",&x);
but I'm getting a warning message which sounds like this:
warning: format '%d' expects type 'int*', but argument 2 has type
'short int*'
How can I avoid this warning and still read with "scanf"? I mention
that I don't want to use "cout". Maybe this problem is for C (not for C
++), but the program is in C++ because i use after this the Standard
Template Library.

Thank you,
Stefan Istrate
 
M

Michael DOUBEZ

(e-mail address removed) a écrit :
I have a short int variable and I'm trying to read it with
scanf("%d",&x);

Use h modifier to indicate type is short flavor:
scanf("%hd",&x);
[snip] I mention
that I don't want to use "cout". Maybe this problem is for C (not for C
++), but the program is in C++ because i use after this the Standard
Template Library.

scanf is in <cstdio>. It is C++.

But be aware that scanf is error prone.

Michael
 
L

linarin

I have a short int variable and I'm trying to read it with
scanf("%d",&x);
but I'm getting a warning message which sounds like this:
warning: format '%d' expects type 'int*', but argument 2 has type
'short int*'
How can I avoid this warning and still read with "scanf"? I mention
that I don't want to use "cout". Maybe this problem is for C (not for C
++), but the program is in C++ because i use after this the Standard
Template Library.

Thank you,
Stefan Istrate

I suggest that you declare a new variable of type int,and pass it to
scanf function.

after read from the scanf, cast it to short int.

or you will over written 2 byte of data more to the address pointed by
the "short int" variable.
 
F

faceman28208

I have a short int variable and I'm trying to read it with
scanf("%d",&x);
but I'm getting a warning message which sounds like this:
warning: format '%d' expects type 'int*', but argument 2 has type
'short int*'
How can I avoid this warning and still read with "scanf"? I mention
that I don't want to use "cout". Maybe this problem is for C (not for C
++), but the program is in C++ because i use after this the Standard
Template Library.

This is precisely the type of problem the standard _C++_ library
solves.
 
J

Jack Klein

I suggest that you declare a new variable of type int,and pass it to
scanf function.

No, you must pass the address of the variable to scanf().
after read from the scanf, cast it to short int.

No, don't cast it to short, just assign it. The behavior is the same
either way.
or you will over written 2 byte of data more to the address pointed by
the "short int" variable.

There are platforms where sizeof(int) - sizeof(short) does not equal
2. There are platforms, a fair number of them, where sizeof(int) is
equal to sizeof(short). There are platforms where sizeof(int) is
equal to sizeof(short) is equal to sizeof(char), and all are equal to
1.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
J

James Kanze

I have a short int variable and I'm trying to read it with
scanf("%d",&x);
but I'm getting a warning message which sounds like this:
warning: format '%d' expects type 'int*', but argument 2 has type
'short int*'
How can I avoid this warning and still read with "scanf"? I mention
that I don't want to use "cout". Maybe this problem is for C (not for C
++), but the program is in C++ because i use after this the Standard
Template Library.

Let me get this straight. You are using a tool which is error
prone, complex, and difficult, and you don't want to replace it
with one which is simple to use and robust. Strange.

In scanf, you have to specify the type of the argument exactly,
and it must match the type of the target. If you say "%d", then
the argument must be an int*; anything else is undefined
behavior. Similarly, for "%hhd", it must be a signed char*, for
"%hd" a short*, for "%ld" a long*, for "%lld" a long long*, for
"%jd" an intmax_t*, and for "%td" a ptrdiff_t. And woe be it if
the type your interested in is a typedef in a library, which
might change in a future version. (And can anyone really
believe that learning all of these exotic prefixes is in any way
intuitive?)
 
S

stefan.istrate

First of all, in my post I wanted to say "cin" instead of "cout".

Let me get this straight. You are using a tool which is error
prone, complex, and difficult, and you don't want to replace it
with one which is simple to use and robust. Strange.

Well, in some algorithm contests, scanf is a little bit faster than
cin. Considering that the total running time is 0.1 seconds, every
optimization might be useful.
Thank you everyone for reply. It seems that the h modifier is really
nice.

Stefan Istrate
 
M

Michael DOUBEZ

(e-mail address removed) a écrit :
First of all, in my post I wanted to say "cin" instead of "cout".

Let me get this straight. You are using a tool which is error
prone, complex, and difficult, and you don't want to replace it
with one which is simple to use and robust. Strange.

Well, in some algorithm contests, scanf is a little bit faster than
cin. Considering that the total running time is 0.1 seconds, every
optimization might be useful.
[snip]
IMHO it is more an implementation issue. Theoretically, stream scheme
should be more efficient because type is identified at compile time
whereas scanf interprets a parameter.

Michael
 

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
474,294
Messages
2,571,511
Members
48,197
Latest member
ปั๊มเฟส|JoyLikeSEO

Latest Threads

Top