By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,201 Members | 937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,201 IT Pros & Developers. It's quick & easy.

how to print a short and long integer?

P: n/a
a
short s;
long l;
s= -2;
l= -3;
printf("% _ %_",s, l);
What characters should be filled out in the formatted string for output?
Thanx
Nov 15 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
On Sun, 25 Sep 2005 04:14:29 GMT, "a" <a@mail.com> wrote in
comp.lang.c:
short s;
long l;
s= -2;
l= -3;
printf("% _ %_",s, l);
What characters should be filled out in the formatted string for output?
Thanx


What does your C reference book say?

What does your compiler's library manual, online help file, or man
pages tell you?

Why do you think usenet is a substitute for looking up basic
information in standard references?

Type this string into the search box at google.com:

printf "conversion specifiers"

....and follow the first link.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #2

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

a wrote:
short s;
long l;
s= -2;
l= -3;
printf("% _ %_",s, l);
What characters should be filled out in the formatted string for output?


printf("%d %ld",s,l);

printf is a variadic function, so it's prototype does not include definitions
for most of it's arguments (only the format string argument is defined).

Thus, for the other arguments, the standard promotion rules apply:
- - short int promotes to int
- - long int stays as long int

The format strings for each argument must anticipate the format of the
arguments. %d is used for int, so it will be used for a short int which gets
promoted to int. %ld is used for long int.

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFDNiVjagVFX4UWr64RAgHHAJ0REEuapZyZpblfFP5V4d WEZMRBEgCgxzwB
6moAzVtg16kHNGrjWNmua7A=
=Djbp
-----END PGP SIGNATURE-----
Nov 15 '05 #3

P: n/a
a wrote:
short s;
long l;
s= -2;
l= -3;
printf("% _ %_",s, l);
What characters should be filled out in the formatted string for output?
Thanx


#include <stdio.h>
int main(void)
{
short s = -2;
long l = -3;
printf("The standard way using \"%%hd %%ld\": %hd %ld\n", s, l);
printf(" (but \"%%d %%ld\" should work as well: %d %ld\n", s, l);
return 0;
}
The standard way using "%hd %ld": -2 -3
(but "%d %ld" should work as well: -2 -3
Nov 15 '05 #4

P: n/a
Martin Ambuhl a écrit :
a wrote:
short s;
long l;
s= -2;
l= -3;
printf("% _ %_",s, l);
What characters should be filled out in the formatted string for output?
Thanx

#include <stdio.h>
int main(void)
{
short s = -2;
long l = -3;
printf("The standard way using \"%%hd %%ld\": %hd %ld\n", s, l);
printf(" (but \"%%d %%ld\" should work as well: %d %ld\n", s, l);
return 0;
}
The standard way using "%hd %ld": -2 -3
(but "%d %ld" should work as well: -2 -3


Why did you do his homework Martin?

It doesn't really help him. He will stay a lazy
student, used to get around without doing any effort.

This question can be answered with a *minimal* effort,
but he doesn't even want to do that little.

Nov 15 '05 #5

P: n/a
Lew Pitcher a écrit :
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

a wrote:
short s;
long l;
s= -2;
l= -3;
printf("% _ %_",s, l);
What characters should be filled out in the formatted string for output?

printf("%d %ld",s,l);

Why did you do his homework Lew?

It doesn't really help him. He will stay a lazy
student, used to get around without doing any effort.

This question can be answered with a *minimal* effort,
but he doesn't even want to do that little.
Nov 15 '05 #6

P: n/a
a wrote on 25/09/05 :
short s;
long l;
s= -2;
l= -3;
printf("% _ %_",s, l);
What characters should be filled out in the formatted string for output?


What about opening your C-book at 'printf' or 'formatted outputs' ?

#include <stdio.h>

int main (void)
{
short s = -2;
long l = -3;

printf ("%hd %ld\n", s, l);

return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
Nov 15 '05 #7

P: n/a
Jack Klein <ja*******@spamcop.net> wrote:
# On Sun, 25 Sep 2005 04:14:29 GMT, "a" <a@mail.com> wrote in
# comp.lang.c:
#
# > short s;
# > long l;
# > s= -2;
# > l= -3;
# > printf("% _ %_",s, l);
# > What characters should be filled out in the formatted string for output?
# > Thanx
#
# What does your C reference book say?
#
# What does your compiler's library manual, online help file, or man
# pages tell you?
#
# Why do you think usenet is a substitute for looking up basic
# information in standard references?
#
# Type this string into the search box at google.com:
#
# printf "conversion specifiers"
#
# ...and follow the first link.

Amazing. You took more time and bandwidth not answering the
question than the answer would've taken.

%d and %ld

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Wow. A sailboat.
Nov 15 '05 #8

P: n/a

"SM Ryan" <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote in
message news:11*************@corp.supernews.com...
Jack Klein <ja*******@spamcop.net> wrote:
# On Sun, 25 Sep 2005 04:14:29 GMT, "a" <a@mail.com> wrote in
# comp.lang.c:
#
# > short s;
# > long l;
# > s= -2;
# > l= -3;
# > printf("% _ %_",s, l);
# > What characters should be filled out in the formatted string for
output?
# > Thanx
#
# What does your C reference book say?
#
# What does your compiler's library manual, online help file, or man
# pages tell you?
#
# Why do you think usenet is a substitute for looking up basic
# information in standard references?
#
# Type this string into the search box at google.com:
#
# printf "conversion specifiers"
#
# ...and follow the first link.

Amazing. You took more time and bandwidth not answering the
question than the answer would've taken.


The technique is known as teaching.

-Mike
Nov 15 '05 #9

P: n/a
# > Amazing. You took more time and bandwidth not answering the
# > question than the answer would've taken.
#
# The technique is known as teaching.

The technique used to be known as a temper tantrum.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I'm not even supposed to be here today.
Nov 15 '05 #10

P: n/a
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> writes:
Amazing. You took more time and bandwidth not answering the
question than the answer would've taken.


The technique is known as teaching.


The technique used to be known as a temper tantrum.


[ Obnoxious '#' quoting character corrected. ]

Nonsense. The OP apparently was unwilling or unable to use whatever
reference materials he has available to answer a very simple question.
Telling him how to print short and long integers would teach him how
to print short and long integers, and nothing else. It would also
encourage him to come back here to ask, for example, how to print
floats and doubles. Jack Klein's response was intended to help the OP
to answer the question for himself, which, if he pays attention, will
be of much greater benefit.

There's a parable about giving a man a fish that you might find
instructive. (Yes, it's a religious parable, but the point is valid
whatever your beliefs happen to be.) I'd quote it for you, but I'm
sure your capable of looking it up yourself.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #11

P: n/a
Keith Thompson wrote:
There's a parable about giving a man a fish that you might find
instructive.


Give a man a fish, and you've hooked a customer.
Teach a man to fish, and he'll open a competing fish market.
Nov 15 '05 #12

P: n/a
Martin Ambuhl <ma*****@earthlink.net> writes:
Keith Thompson wrote:
There's a parable about giving a man a fish that you might find
instructive.


Give a man a fish, and you've hooked a customer.
Teach a man to fish, and he'll open a competing fish market.


Give a man a fire, and he's warm for today.
Set a man on fire, and he's warm for the rest of his life.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #13

P: n/a
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> wrote in message
news:mn***********************@YOURBRAnoos.fr...
a wrote on 25/09/05 : What about opening your C-book at 'printf' or 'formatted outputs' ?

#include <stdio.h>

int main (void)
{
short s = -2;
long l = -3;

printf ("%hd %ld\n", s, l);
Giving the OP such an answer is a bit twisted :
- you do his homework, but the teacher will know.
- he will be clueless as to why %hd instead of %d
- their compiler might not even support %hd
- I for example do not even understand why there is a need for %hd in printf
- as a matter of fact, %hd may be needed if the argument is not of type short
but unsigned short for example.
- the intricacies of integer promotion and printf format strings are not your
average beginners homework.
- if the teacher expected %d, the student will be unable to explain the error.
- if the teacher expected %hd in a beginners course, she is wrong,
- if this is an AP course, the next question will be why? same problem for the
student.
- was this really a trick question, was your answer purposely misleading ?
return 0;
}

--
Chqrlie.

PS: C aint for sissies
Nov 15 '05 #14

P: n/a
In article <p2*****************@newsread1.news.atl.earthlink. net>,
Martin Ambuhl <ma*****@earthlink.net> wrote:
Keith Thompson wrote:
There's a parable about giving a man a fish that you might find
instructive.


Give a man a fish, and you've hooked a customer.
Teach a man to fish, and he'll open a competing fish market.


That's it in a nutshell, isn't it?

That the people who give direct answers (that is, "helping" the newbs)
are doing it specifically to promote dependency. Make no mistake, even
though there's no money changing hands, there still must be some reason why
people bother to post to Usenet, and promoting dependency is here, as it is
in real businesses, a good ego boo for the one upon who the dependency
exists.

Of course, in a real business, there's actual cash involved, but that may,
in fact, be secondary to the (psychological) value of the dependency.

Nov 15 '05 #15

P: n/a
Charlie Gordon wrote:

"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> wrote in message
news:mn***********************@YOURBRAnoos.fr...
a wrote on 25/09/05 :
What about opening your C-book at 'printf' or 'formatted outputs' ?

#include <stdio.h>

int main (void)
{
short s = -2;
long l = -3;

printf ("%hd %ld\n", s, l);


Giving the OP such an answer is a bit twisted :
- you do his homework, but the teacher will know.
- he will be clueless as to why %hd instead of %d


Because h is for short.
- their compiler might not even support %hd
The standard makes %hd support mandatory, not optional.
- I for example do not even understand why there is a need for %hd in
printf
Symetry with %hu.
- as a matter of fact,
%hd may be needed if the argument is not of type short
but unsigned short for example.
%hu is for unsigned short.
- if the teacher expected %hd in a beginners course, she is wrong,


The standard says %hd for type short argument.

h Specifies that a following d, i, o, u, x, or X
conversion specifier applies to a short int or
unsigned short int argument (the argument will
have been promoted according to the integer
promotions, but its value shall be converted to
short int or unsigned short int before
printing); or that a following n conversion
specifier applies to a pointer to a short int
argument.
--
pete
Nov 15 '05 #16

P: n/a
Charlie Gordon wrote on 26/09/05 :
- their compiler might not even support %hd


Come on. The "h" qualifier has been standard since 1989...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
Nov 15 '05 #17

P: n/a
Martin Ambuhl wrote:
Keith Thompson wrote:
There's a parable about giving a man a fish that you might find
instructive.

Give a man a fish, and you've hooked a customer.
Teach a man to fish, and he'll open a competing fish market.


I really like that. I hope I remember it. Thanks.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #18

P: n/a
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> wrote in message
news:mn***********************@YOURBRAnoos.fr...
Charlie Gordon wrote on 26/09/05 :
- their compiler might not even support %hd


Come on. The "h" qualifier has been standard since 1989...


I understand the symmetry with scanf() where the short specifier is definitely
needed.

For printf, it is there just for the sake of consistency, but is not strictly
needed for d,i,o,u,x or X conversions.
specifying required behaviour for %hhn or %jn and friends is ridiculous, and
leads to unnecessary bloat in the C library.
Even %zn is debatable. As a matter of fact, the whole %n stuff is of
questionable value and indeed removed from the "Secure C" proposal.

If a short or insigned short is passed to printf, %d or %u will do the job. If
I'm wrong, show us a counterexample.

If a int is passed and you want want it converted as a short, why not do that
with a cast in the parameter list ?

--
Chqrlie.
Nov 15 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.