Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: why printf("%d", arg) works with arg of type int, short, char |
| Date | 2014-03-01 09:10 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <lespp0$cjp$1@dont-email.me> (permalink) |
| References | <fc5dad84-08d7-4c7b-bd56-5ce49b177b1b@googlegroups.com> <lniory438k.fsf@nuthaus.mib.org> <937c14b6-d3fa-4735-980f-fd173ccfeee3@googlegroups.com> |
On 03/01/2014 05:49 AM, jononanon@googlemail.com wrote: > On Saturday, March 1, 2014 11:34:03 AM UTC+1, Keith Thompson wrote: >> any integer >> >> type narrower than int is convert to int if int can represent all the >> >> values of the type, otherwise to unsigned int. (The latter avoids >> >> overflow when an unsigned short value is promoted and short and int are >> >> the same size.) >> > Is this conversion to unsigned in really important? > I mean in terms of overflow! -> who cares? Isn't solely the bit-pattern important?? No - because there are real machines where overflow has bad consequences. > Because: Even if conversion were ONLY to signed int, the result via > va_arg(ap, int), or via va_arg(ap, unsigned int) is what is important. And that is not controlled by the default argument promotion, but instead by the printf-conversion-specifier. (Am I missing something?) If the type of the argument, after promotion, differs from the type that you specify in the corresponding invocation of va_arg(), the behavior is undefined - such code might work, but you shouldn't count on it. When it's just the difference between a given signed type and the corresponding unsigned type, it's fairly safe, so long as only positive values are involved: such are guaranteed to have the same representation both types. No such guarantee applies to int and unsigned short, even if they both have the same size - in principle, at least, a fully conforming implementation could, for example, have big-endian int, and little-endian unsigned short. In this particular case, you can probably get away with sloppy coding that ignores these issues - but it's better to handle it correctly. If there's a possibility that a given value that is subject to the default promotions might promote to either int or unsigned int, depending upon the characteristics of the implementation you're using, you should write your code to either remove that possibility, or deal with it. -- James Kuyper
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
why printf("%d", arg) works with arg of type int, short, char jononanon@googlemail.com - 2014-03-01 01:29 -0800
Re: why printf("%d", arg) works with arg of type int, short, char jononanon@googlemail.com - 2014-03-01 01:42 -0800
Re: why printf("%d", arg) works with arg of type int, short, char jononanon@googlemail.com - 2014-03-01 01:54 -0800
Re: why printf("%d", arg) works with arg of type int, short, char Barry Schwarz <schwarzb@dqel.com> - 2014-03-01 07:21 -0800
Re: why printf("%d", arg) works with arg of type int, short, char gazelle@shell.xmission.com (Kenny McCormack) - 2014-03-01 15:51 +0000
Re: why printf("%d", arg) works with arg of type int, short, char Richard <rgrdev_@gmail.com> - 2014-03-01 17:50 +0100
Re: why printf("%d", arg) works with arg of type int, short, char gazelle@shell.xmission.com (Kenny McCormack) - 2014-03-01 18:36 +0000
Re: why printf("%d", arg) works with arg of type int, short, char Melzzzzz <mel@zzzzz.invalid> - 2014-03-01 11:22 +0100
Re: why printf("%d", arg) works with arg of type int, short, char jononanon@googlemail.com - 2014-03-01 02:39 -0800
Re: why printf("%d", arg) works with arg of type int, short, char Keith Thompson <kst-u@mib.org> - 2014-03-01 19:44 -0800
Re: why printf("%d", arg) works with arg of type int, short, char Keith Thompson <kst-u@mib.org> - 2014-03-01 02:34 -0800
Re: why printf("%d", arg) works with arg of type int, short, char jononanon@googlemail.com - 2014-03-01 02:49 -0800
Re: why printf("%d", arg) works with arg of type int, short, char James Kuyper <jameskuyper@verizon.net> - 2014-03-01 09:10 -0500
Re: why printf("%d", arg) works with arg of type int, short, char <william@wilbur.25thandClement.com> - 2014-03-01 15:38 -0800
Re: why printf("%d", arg) works with arg of type int, short, char Keith Thompson <kst-u@mib.org> - 2014-03-01 19:47 -0800
Re: why printf("%d", arg) works with arg of type int, short, char <william@wilbur.25thandClement.com> - 2014-03-01 21:12 -0800
Re: why printf("%d", arg) works with arg of type int, short, char Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2014-03-02 07:47 -0700
Re: why printf("%d", arg) works with arg of type int, short, char Kaz Kylheku <kaz@kylheku.com> - 2014-03-02 20:53 +0000
Re: why printf("%d", arg) works with arg of type int, short, char jgk@panix.com (Joe keane) - 2014-03-04 00:23 +0000
Re: why printf("%d", arg) works with arg of type int, short, char Kaz Kylheku <kaz@kylheku.com> - 2014-03-04 00:33 +0000
Re: why printf("%d", arg) works with arg of type int, short, char glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-04 00:45 +0000
Re: why printf("%d", arg) works with arg of type int, short, char jgk@panix.com (Joe keane) - 2014-03-05 04:07 +0000
Re: why printf("%d", arg) works with arg of type int, short, char Barry Schwarz <schwarzb@dqel.com> - 2014-03-05 10:07 -0800
Re: why printf("%d", arg) works with arg of type int, short, char "BartC" <bc@freeuk.com> - 2014-03-01 11:12 +0000
Re: why printf("%d", arg) works with arg of type int, short, char Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-01 10:54 -0500
Re: why printf("%d", arg) works with arg of type int, short, char glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-01 18:55 +0000
csiph-web