473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How to read a short int variable?

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

May 10 '07 #1
7 46202
st************@gmail.com 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

May 10 '07 #2
On 5ÔÂ10ÈÕ, ÏÂÎç3ʱ13·Ö, "stefan.istr...@gmail.com"
<stefan.istr...@gmail.comwrote:
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.

May 10 '07 #3
On May 10, 3:13 am, "stefan.istr...@gmail.com"
<stefan.istr...@gmail.comwrote:
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.

May 10 '07 #4
On 10 May 2007 02:18:22 -0700, linarin <li*********@gmail.comwrote
in comp.lang.c++:
On 5ÔÂ10ÈÕ, ÏÂÎç3ʱ13·Ö, "stefan.istr...@gmail.com"
<stefan.istr...@gmail.comwrote:
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.
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
May 11 '07 #5
On May 10, 9:13 am, "stefan.istr...@gmail.com"
<stefan.istr...@gmail.comwrote:
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?)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 11 '07 #6
First of all, in my post I wanted to say "cin" instead of "cout".

On May 11, 12:23 pm, James Kanze <james.ka...@gmail.comwrote:
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

May 31 '07 #7
st************@gmail.com a écrit :
First of all, in my post I wanted to say "cin" instead of "cout".

On May 11, 12:23 pm, James Kanze <james.ka...@gmail.comwrote:
>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
May 31 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nikhil Barthwal | last post by:
Hi, I have a Java program that needs to access the value of variable PATH (Set in Unix enviroment by setenv or DOS enviroment by PATH statement). How do I access it in Java? Thanks in...
5
by: Prabhat | last post by:
Hi All, Is it possible to read any ASP variable from a javascript file (.js) ? Thanks Prabhat
2
by: Dale | last post by:
Hi All- I have a Javascript routine that sets a variable. I need to pass the variable to my C# code behind page (or bind it). Is this possible? Thanks in advance, Dale Lundgren
4
by: danman226 | last post by:
I will be using a companyname, user name, and password to authenicate users in my system. I am trying to save the company name in the session for later use. I cannot access the Session object in...
7
by: Alan | last post by:
I have a main form frmMain and second form frmMember, I declare a variable at the top of the frmMember as Dim memberStr as String In the main form, Dim MemberForm as frmMember
5
by: yinglcs | last post by:
Hi, I have a method like this: const A* getMaxArea(const vector<A*>& aList); And in my caller, i have this: A* const a = NULL;
1
by: walterbyrd | last post by:
I know I can use the file() function to read a file into an array by providing a literal file name between single quotes: $lines = file('literal_file_name'); But, what if I want file() to use...
7
by: Z-Z | last post by:
Hi, I used the following macro to determine variable signed or not, but only find that it is useless for a short or char variable. I think that it might caused by value conversion rules. I used...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.