473,663 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Integer promotion

Consider the following program:

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
uint32_t a = strtoul(argv[1], 0, 0);
uint32_t b = strtoul(argv[2], 0, 0);
double d = a-b;
printf("%f\n", d);
return 0;
}

$ ./a.out 5 6
4294967295.0000 00

This is what I expected.
Since a-b would be negative, the result is a-b+2^32.

However, if I replace uint32_t with uint16_t:

$ ./a.out 5 6
-1.000000

I expected 65535 (-1+2^16).

I suppose I'm forgetting some integer promotion rule?

Is the simple fix just to write:

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
uint16_t a = strtoul(argv[1], 0, 0);
uint16_t b = strtoul(argv[2], 0, 0);
uint16_t diff = a-b;
double d = diff;
printf("%f\n", d);
return 0;
}

Regards.
Sep 11 '07 #1
2 2489
On Tue, 11 Sep 2007 17:56:04 +0200, Spoon <root@localhost wrote in
comp.lang.c:
Consider the following program:

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
uint32_t a = strtoul(argv[1], 0, 0);
uint32_t b = strtoul(argv[2], 0, 0);
double d = a-b;
printf("%f\n", d);
return 0;
}

$ ./a.out 5 6
4294967295.0000 00

This is what I expected.
Since a-b would be negative, the result is a-b+2^32.

However, if I replace uint32_t with uint16_t:

$ ./a.out 5 6
-1.000000

I expected 65535 (-1+2^16).
Why? Assuming that int has more than 16 bits on your platform, the
values of both 'a' and 'b' are promoted to signed int before the
addition, and the signed int result is then converted to double and
assigned to 'd'.
I suppose I'm forgetting some integer promotion rule?

Is the simple fix just to write:

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
uint16_t a = strtoul(argv[1], 0, 0);
uint16_t b = strtoul(argv[2], 0, 0);
uint16_t diff = a-b;
double d = diff;
printf("%f\n", d);
return 0;
}

Regards.
Integer types narrower (of lesser rank) than int are always promoted
to int or unsigned int when used as operands of expressions. The
rules for this promotion from unsigned types of lesser rank than int
are:

-- if signed int can hold all the possible values of the lesser rank
unsigned type, the value is promoted to signed int.

-- if signed int cannot hold all the possible values of the lesser
rank unsigned type, the value is promoted to unsigned int.

So if you build and execute this program on a platform where int has
16 bits, the values in 'a' and 'b' would promote to unsigned int and
produced the result you expected. Since int on your platform has more
than 16 bits, the values in 'a' and 'b' promoted to signed int and
produced the result you saw.

--
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 11 '07 #2
On Tue, 11 Sep 2007 17:56:04 +0200, Spoon <root@localhost wrote:
>Consider the following program:

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
uint32_t a = strtoul(argv[1], 0, 0);
uint32_t b = strtoul(argv[2], 0, 0);
double d = a-b;
^^^^^^^^^^^^^^^ ^^^^^^^^

double d = (double)a-b;

printf("%f\n", d);
return 0;
}

$ ./a.out 5 6
4294967295.000 00
Sep 11 '07 #3

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

Similar topics

21
4107
by: Frederick Gotham | last post by:
I set about trying to find a portable way to set the value of UCHAR_MAX. At first, I thought the following would work: #define UCHAR_MAX ~( (unsigned char)0 ) However, it didn't work for me. Could someone please explain to me what's going on? I would have thought that the following happens: (1) The literal, 0, whose type is int, gets converted to an unsigned char.
4
1933
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
13815
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
34
11298
by: Cuthbert | last post by:
Hi folks, I am trying to find a more efficient way to count "How many bits are '1' in a integer variable?". I still have no idea to count the bits except using a loop and "if" statements. Could you know any other more efficient way? Cuthbert
1
3773
by: charles_gero | last post by:
Hi all, I had a question about the topics in the subject and posted to comp.std.c, but feel it may also be appropriate here. Please excuse this crosspost if it is in bad form. I have a question about whether or not I am interpreting a nuance of the standard correctly, and the implications of said nuance. The sections in the C99 standard (and possibly older standards) that I will reference are as follows (typed out hopefully to avoid...
9
2110
by: Fred | last post by:
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 */
2
1675
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 there will be no argument integer promotions (unless they are variadic)
30
4285
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Let's say we had a simple function for returning the amount of days in a month: unsigned DaysInMonth(unsigned const month) { switch (month) { case 8: case 3: case 5:
9
5335
by: tsuyois | last post by:
Hi, I just signed in to this excellent network. I hope I could get some answers to many questions I have in writing C compilers. My first question is: Is "integer demotion" required in ANSI-C? Assumption: - CPU: 32-bit RISC (int = long = 4 bytes, short = 2 bytes, char = 1 byte)
0
8858
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8771
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8548
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8634
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5657
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1757
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.