473,406 Members | 2,369 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,406 software developers and data experts.

unsigned wierdness

Hello!
I have the following piece of code:

#include <stdio.h>

int main(void){

unsigned int u;
int i1, i2;
double d1,d2;

u = 0;

i1 = u - 1;
i2 = (signed int) u - 1;

d1 = u - 1;
d2 = (signed int) u - 1;

printf("i1 = %d, i2 = %d, d1 = %f, d2 = %f\n",i1,i2,d1,d2);
}

and don't understand the output:
tobsbox:~/C$ cc -std=c99 -Wall -W -pedantic unsigned.c && ./a.out
i1 = -1, i2 = -1, d1 = 4294967295.000000, d2 = -1.000000

It looks like u is promoted to signed int for i2 but not d2. Is
this normal or is it a compiler bug?
Tobias
ps: My compiler is
cc (GCC) 3.4.3 20050227 (Red Hat 3.4.3-22.fc3)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

Nov 14 '05 #1
3 1106

tobias...@hotmail.com wrote:

It looks like u is promoted to signed int for i2 but not d2.


Ooups, I meant i1 and d1 of course
Tobias

Nov 14 '05 #2

tobias...@hotmail.com wrote:
Hello!
I have the following piece of code:

#include <stdio.h>

int main(void){

unsigned int u;
int i1, i2;
double d1,d2;

u = 0;

i1 = u - 1;
i2 = (signed int) u - 1;

d1 = u - 1;
d2 = (signed int) u - 1;

printf("i1 = %d, i2 = %d, d1 = %f, d2 = %f\n",i1,i2,d1,d2);
}

and don't understand the output:
tobsbox:~/C$ cc -std=c99 -Wall -W -pedantic unsigned.c && ./a.out
i1 = -1, i2 = -1, d1 = 4294967295.000000, d2 = -1.000000


Never mind. The whole rhs is done in unsigned arithmetic and gives
-1U which is 4294967295 here. The assignement to i1 'overflows' and
happens to give -1. The assignement to d1 doesn't and preserves the
value. I mistakenly thought that the rhs would all be done in
signed.
Tobias.

Nov 14 '05 #3
tobias...@hotmail.com wrote:
tobias...@hotmail.com wrote:

int main(void){

unsigned int u;
int i1, i2;
double d1,d2;

u = 0;
i1 = u - 1;
i2 = (signed int) u - 1;
d1 = u - 1;
d2 = (signed int) u - 1;
printf("i1 = %d, i2 = %d, d1 = %f, d2 = %f\n",i1,i2,d1,d2);
}

i1 = -1, i2 = -1, d1 = 4294967295.000000, d2 = -1.000000


Never mind. The whole rhs is done in unsigned arithmetic and
gives -1U which is 4294967295 here. The assignement to i1
'overflows' and happens to give -1.


To be precise: since 4294967295 is outside the range of
signed int, the assignment to i1 either gives an
implementation-defined result, or raises an implementation-
defined signal.

Nov 14 '05 #4

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

Similar topics

0
by: Matthew Alton | last post by:
The appended program freaks python 2.2 & 2.3 completely out. To reproduce the wierdness: i) copy the source to a file called consarn.py ii) $ python consarn.py; iii) the program is now doing a...
13
by: Tek Boy | last post by:
I've been experiencing some (reproducable) wierdness when I try to generate some very basic HTML using ASP. Check out the following (basic) ASP code: ===========================================...
0
by: amber | last post by:
Hello, I'm having some wierdness with a report I've created in VB.NET (with Crysal Reports). The report is called repCPDocSubmissionSummary.vb I created it a while ago, and have been using it...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
5
by: Eduardo Olivarez | last post by:
The following code does not work correctly on my machine. Either one of the scanf()'s alone work perfectly. However, when they are combined, the second scanf() call just reads what the first one...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
20
by: Hanzac Chen | last post by:
Hi, I don't understand why this could happen? The Code 1 will output `fff9' and the Code 2 will output `1' How could the `mod 8' not have effect? /* Code 1 */ #include <stdio.h> #include...
9
by: Bobby Edward | last post by:
Are there any add-ons to Visual Studio 2008 that will help me troubleshoot CSS wierdness? Like showing the padding/margin with difference colors, etc...? I'm trying to figure out some wierd...
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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
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...
0
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,...

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.