how to print a short and long integer?

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
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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)

iD8DBQFDNiVjagVFX4UWr64RAgHHAJ0REEuapZyZpblfFP5V4dWEZMRBEgCgxzwB
6moAzVtg16kHNGrjWNmua7A=
=Djbp
-----END PGP SIGNATURE-----
 
J

Jack Klein

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

Martin Ambuhl

a said:
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
 
J

jacob navia

Martin Ambuhl a écrit :
#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.
 
J

jacob navia

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



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

Emmanuel Delahaye

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?"
 
S

SM Ryan

# 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
 
M

Mike Wahler

SM Ryan said:
# 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
 
S

SM Ryan

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

Keith Thompson

SM Ryan said:
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.
 
M

Martin Ambuhl

Keith said:
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.
 
K

Keith Thompson

Martin Ambuhl said:
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.
 
C

Charlie Gordon

Emmanuel Delahaye said:
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 ?
 
K

Kenny McCormack

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

pete

Charlie said:
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.
 
J

Joe Wright

Martin said:
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.
 
C

Charlie Gordon

Emmanuel Delahaye said:
Charlie Gordon wrote on 26/09/05 :

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 ?
 

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