473,506 Members | 11,491 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.000000

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 2478
On Tue, 11 Sep 2007 17:56:04 +0200, Spoon <root@localhostwrote 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.000000

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.learn.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@localhostwrote:
>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.00000
Sep 11 '07 #3

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

Similar topics

21
4078
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....
4
1917
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
13800
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
34
11267
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....
1
3759
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...
9
2096
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...
2
1666
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...
30
4244
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
5316
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...
0
7220
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,...
0
7308
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,...
0
7371
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...
0
5617
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,...
1
5037
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...
0
4702
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...
0
3188
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...
0
1534
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 ...
0
410
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...

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.