473,804 Members | 3,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 46402
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*********@gm ail.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.l earn.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 objektorientier ter Datenverarbeitu ng
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...@gm ail.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...@gm ail.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
5167
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 advance, Nikhil
5
4818
by: Prabhat | last post by:
Hi All, Is it possible to read any ASP variable from a javascript file (.js) ? Thanks Prabhat
2
1183
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
4007
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 the Application_AuthenticateRequest function. I need the companyname to lookup the users group to build the GenericPrincipal for the user. My code is below. Login Page: int c = SiteSecurity.DBAuthenticate(txtCompanyName.Text, txtUsername.Text,...
7
1481
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
19054
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
366
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 a variable name? $variable_file_name = "literal_file_name"; $lines = file($variable_file_name);
7
1641
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 VC. #define ISUNSIGNED(a) (a>=0 && ~a>= 0) Who knows the reason and other methods to determine short varible signed or not?
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9571
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10318
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.