Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to print a short and long integer?

Reply
Thread Tools

how to print a short and long integer?

 
 
Keith Thompson
Guest
Posts: n/a
 
      09-25-2005
SM Ryan <> 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) kst- <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.
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-25-2005
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.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      09-25-2005
Martin Ambuhl <> 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) kst- <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.
 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-25-2005
"Emmanuel Delahaye" <> wrote in message
news:...
> 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


 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      09-26-2005
In article <p2GZe.3900$. net>,
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.


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.

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      09-26-2005
Charlie Gordon wrote:
>
> "Emmanuel Delahaye" <> wrote in message
> news:...
> > 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
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      09-26-2005
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


 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      09-26-2005
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 ---
 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-29-2005
"Emmanuel Delahaye" <> wrote in message
news:...
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Difference of extern short *x and extern short x[]? Andre C Programming 5 07-17-2012 07:38 PM
Having compilation error: no match for call to ‘(const __gnu_cxx::hash<long long int>) (const long long int&)’ veryhotsausage C++ 1 07-04-2008 05:41 PM
long long and long Mathieu Dutour C Programming 4 07-24-2007 11:15 AM
longs, long longs, short short long ints . . . huh?! David Geering C Programming 15 01-11-2007 09:39 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57