473,569 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Integer Promotions

I'm having terrible trouble trying to work out exactly which of the
promotions one reads about in old books are still present in current C -
and there seems to be a distinction between promotion for function
arguments versus expressions?

For example, consider the following code:

short u; /* declare u as a short */
(void) (u=10); /* set u to 10 */

Am I right in thinking the following happens:
1. 10 is promoted to an int.
2. This expression is evaluated as an int to yield 10.
3. Then the result is demoted to a short (equal to 10) and stored in u.
(4. And as usual we discard the value of the assigment expression)

Is this also why there are suffixes for long and unsigned integer
constants, but no suffixes for shorts and chars? Because these
automatically become ints as soon as they appear?

It does seem unnecessarily indirect to do this promotion and demotion
all the time - half of one's compiled code must end up just moving back
and forth between different integer types!

Sep 27 '07 #1
9 2105
Fred wrote:
>
I'm having terrible trouble trying to work out exactly which of the
promotions one reads about in old books are still present in current C -
and there seems to be a distinction between promotion for function
arguments versus expressions?

For example, consider the following code:

short u; /* declare u as a short */
(void) (u=10); /* set u to 10 */

Am I right in thinking the following happens:
1. 10 is promoted to an int.
2. This expression is evaluated as an int to yield 10.
No. (10) starts out as an int.
sizeof(10) equals sizeof(int) always everywhere.
3. Then the result is demoted to a short (equal to 10)
and stored in u.
(4. And as usual we discard the value of the assigment expression)
Points 3 and 4 seem OK
>
Is this also why there are suffixes for long and unsigned integer
constants, but no suffixes for shorts and chars? Because these
automatically become ints as soon as they appear?
--
pete
Sep 27 '07 #2
Fred said:

<snip>
For example, consider the following code:

short u; /* declare u as a short */
(void) (u=10); /* set u to 10 */

Am I right in thinking the following happens:
1. 10 is promoted to an int.
2. This expression is evaluated as an int to yield 10.
3. Then the result is demoted to a short (equal to 10) and stored in u.
Pete has already dealt with these.
(4. And as usual we discard the value of the assigment expression)
That isn't actually "as usual". Hardly anyone does that, as it's utterly
pointless.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 28 '07 #3
Fred wrote:
I'm having terrible trouble trying to work out exactly which of the
promotions one reads about in old books are still present in current C -
and there seems to be a distinction between promotion for function
arguments versus expressions?

For example, consider the following code:

short u; /* declare u as a short */
(void) (u=10); /* set u to 10 */

Am I right in thinking the following happens:
1. 10 is promoted to an int.
2. This expression is evaluated as an int to yield 10.
3. Then the result is demoted to a short (equal to 10) and stored in u.
(4. And as usual we discard the value of the assigment expression)

Is this also why there are suffixes for long and unsigned integer
constants, but no suffixes for shorts and chars? Because these
automatically become ints as soon as they appear?

It does seem unnecessarily indirect to do this promotion and demotion
all the time - half of one's compiled code must end up just moving back
and forth between different integer types!
As stated before 1) and 2) are one operation. The constant starts
as an integer, since it is not suffixed and it is not floating
point.

3) In some architectures, you can assign directly a constant to a memory
location, without storing them in a register first. This means that 3)
can be optimized to just

Assign to the address of u, the 16 bit constant 10.

In other architectures, a constant can only be loaded into a register
and then stored. This would be two instructions instead of one.
Sep 28 '07 #4
Fred wrote:
I'm having terrible trouble trying to work out exactly which of the
promotions one reads about in old books are still present in current C -
and there seems to be a distinction between promotion for function
arguments versus expressions?

For example, consider the following code:

short u; /* declare u as a short */
(void) (u=10); /* set u to 10 */

Am I right in thinking the following happens:
1. 10 is promoted to an int.
2. This expression is evaluated as an int to yield 10.
3. Then the result is demoted to a short (equal to 10) and stored in u.
(4. And as usual we discard the value of the assigment expression)

Is this also why there are suffixes for long and unsigned integer
constants, but no suffixes for shorts and chars? Because these
automatically become ints as soon as they appear?

It does seem unnecessarily indirect to do this promotion and demotion
all the time - half of one's compiled code must end up just moving back
and forth between different integer types!
It depends what you mean by "happens". This is the way what is happening is
described by the standard, and the correct way to think about it.

However, the compiler is allowed to compile this to any code which behaves "as
if" the above steps had been followed, that is, any code which has identical and
indistinguishab le results.

In the above program, the compiler can directly set u to the short constant 10,
without involving an intermediate int constant. It can do this because there is
no way the program could tell the difference. However, the version of events
above, with the intermediate int constant, is the gold standard by which any
program will be measured for conformity. (Conformingness ?)

So no, there is no need to worry about all these promotions and demotions
wasting your time.

--
Philip Potter pgp <atdoc.ic.ac. uk
Sep 28 '07 #5
Philip Potter said:
[...] the gold standard by which any program will be measured for
conformity. (Conformingness ?)
Conformance.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 28 '07 #6
Richard Heathfield wrote:
Philip Potter said:
>[...] the gold standard by which any program will be measured for
conformity. (Conformingness ?)

Conformance.
Thank you!

--
Philip Potter pgp <atdoc.ic.ac. uk
Sep 28 '07 #7
jacob navia <ja***@jacob.re mcomp.frwrites:
Fred wrote:
>I'm having terrible trouble trying to work out exactly which of the
promotions one reads about in old books are still present in current C -
<snip>
>short u; /* declare u as a short */
(void) (u=10); /* set u to 10 */

Am I right in thinking the following happens:
1. 10 is promoted to an int.
2. This expression is evaluated as an int to yield 10.
3. Then the result is demoted to a short (equal to 10) and stored in u.
(4. And as usual we discard the value of the assigment expression)

Is this also why there are suffixes for long and unsigned integer
constants, but no suffixes for shorts and chars? Because these
automaticall y become ints as soon as they appear?
<snip>
As stated before 1) and 2) are one operation. The constant starts
as an integer, since it is not suffixed and it is not floating
point.
It would be an integer even with a suffix. You mean int. Also one
needs to add that, for it to be an int, it must fit in an int.
Hexadecimal and octal constants might also be unsigned, so one needs
to consider the prefix also.

--
Ben.
Sep 28 '07 #8
On 28 Sep 2007 at 5:38, Richard Heathfield wrote:
Fred said:

<snip>
>For example, consider the following code:

short u; /* declare u as a short */
(void) (u=10); /* set u to 10 */

Am I right in thinking the following happens:
1. 10 is promoted to an int.
2. This expression is evaluated as an int to yield 10.
3. Then the result is demoted to a short (equal to 10) and stored in u.

Pete has already dealt with these.
>(4. And as usual we discard the value of the assigment expression)

That isn't actually "as usual". Hardly anyone does that, as it's utterly
pointless.
Well, I try to make my code keep lint quiet even with strict settings...
cures a lot of bugs.

Sep 28 '07 #9
On Fri, 28 Sep 2007 16:24:32 +0200 (CEST), Fred <no****@nospam. com>
wrote in comp.lang.c:
On 28 Sep 2007 at 5:38, Richard Heathfield wrote:
Fred said:

<snip>
For example, consider the following code:

short u; /* declare u as a short */
(void) (u=10); /* set u to 10 */

Am I right in thinking the following happens:
1. 10 is promoted to an int.
2. This expression is evaluated as an int to yield 10.
3. Then the result is demoted to a short (equal to 10) and stored in u.
Pete has already dealt with these.
(4. And as usual we discard the value of the assigment expression)
That isn't actually "as usual". Hardly anyone does that, as it's utterly
pointless.

Well, I try to make my code keep lint quiet even with strict settings...
cures a lot of bugs.
If you have a version of lint that complains about discarding the
value resulting from a simple assignment, either it provides, and you
are using, an insanely paranoid option, or the particular lint utility
(or its author(s)) are pathologically unfamiliar with C.

My quick mental review of C code written and read over the past
quarter century comes up with the unscientific conclusion that the
value of an assignment statement is ignored more than 90% of the time.

In fact, if one uses MISRA rules with PC Lint, one gets a MISRA
violation for:

int x, y;
x = y = 3;

....unless you parenthesize as:

x = (y = 3);

....as depending on operator precedence.

Personally I think that warning is worded incorrectly, and superfluous
when the objects are of the same type, but with different wording
would be valid for:

double d1, d2 = sqrt(2.0);
int x;
d1 = x = d2;

....because there is a distinct possibility of unintentional data loss
in this statement. d1 will receive the value of 1.0, and not 1.414...

--
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
Sep 29 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2999
by: Mantorok Redgormor | last post by:
Would the following determine if the bit pattern for floating point 0.0 and integer 0 are the same? #include <stdio.h> int main(void) <% if(0 == 0.0f) puts("Bit pattern is the same."); return 0;
4
10445
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is an unsigned integer, so I see a +1 if the bit is set. Microsoft VC++ thinks it's signed, so I see -1 if the bit is set. Ex. typedef enum {
14
1690
by: jimjim | last post by:
Hi all, Assume x is declared as char x, and a function f( ) with prototype void f(char y). Assume f( ) is invoked somewhere in the code and: 1. The f( )'s prototype is provided, then an one-byte copy of x is passed to f( ) (unix/intel platform). 2. The prototype is not provided, then a four-byte int, which contains the
4
1925
by: spibou | last post by:
On 6.3.1.1 of N1124 we read: If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. A few lines above that we read: The following may be used in an expres-
6
13811
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
232
13119
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an...
7
12247
by: Spoon | last post by:
Hello everyone, In my code, I use uint16_t (exactly 16-bit-wide unsigned integer type) and I need to print their value in base 10. http://www.opengroup.org/onlinepubs/009695399/basedefs/inttypes.h.html As far as I understand, the recommended method is: #include <inttypes.h>
2
1671
by: Sune | last post by:
Hi all, there are several situations where integer promotion is performed in C and I need to confirm my understanding of it. Let's contain the discussion to workstation/server CPUs of 32/64 bits, I'm afraid 8/16 bits CPUs may add details I'm not interested in. Hopefully someone here can help out: - As long as I use function prototypes...
3
3223
by: =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire | last post by:
Hello, I'm having a discussion with someone who sustains that function parameters, when they are smaller than an int (say short or char) are automatically promoted to int before being passed to the caller. Despite looking in the Standard (ISO/IEC 9899:TC2), &6.5.2.2 "Function calls", I'm still unsure, because this is rather technical...
0
7703
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...
0
7618
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...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7678
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...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5222
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...
0
3656
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...
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
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...

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.