473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2086
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
indistinguishable 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.remcomp.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
automatically 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.learn.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
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."); ...
4
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...
14
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...
4
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...
6
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
232
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...
7
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. ...
2
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...
3
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.