473,473 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Re: Promoting unsigned long int to long int

pereges <Br*****@gmail.comwrites:
[...]
#define ulong unsigned long int
#define uchar unsigned char
[...]

These types already have perfectly good names already. Why give them
new ones?

If you must rename them for some reason, use typedefs, not macros.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 30 '08
105 6069
On Jul 14, 7:14 am, David Thompson <dave.thomps...@verizon.netwrote:
<snip>
Conversion to a signed integer type of an integer value out of range
is implementation-defined, and in C99 (explicitly) may raise a signal.
On _most_ implementations, for a signed/unsigned pair of types, half
the range of the unsigned type is out of range for the signed type.
It's undefined behavior, not implementation defined.
Also, it's perfectly valid if INT_MAX UINT_MAX (though not INT_MAX >
LONG_MAX or INT_MAX ULONG_MAX)
Jul 14 '08 #101
On Mon, 14 Jul 2008 11:16:16 -0700, vippstar wrote:
On Jul 14, 7:14 am, David Thompson <dave.thomps...@verizon.netwrote:
<snip>
>Conversion to a signed integer type of an integer value out of range is
implementation-defined, and in C99 (explicitly) may raise a signal. On
_most_ implementations, for a signed/unsigned pair of types, half the
range of the unsigned type is out of range for the signed type.
It's undefined behavior, not implementation defined.
It's implementation-defined, not undefined.

6.3.1.3 Signed and unsigned integers
1 When a value with integer type is converted to another integer type
other than _Bool, if the value can be represented by the new type, it is
unchanged.
2 Otherwise, if the new type is unsigned, [...]
3 Otherwise, the new type is signed and the value cannot be represented in
it; either the result is implementation-defined or an implementation-
defined signal is raised.
Also, it's
perfectly valid if INT_MAX UINT_MAX (though not INT_MAX LONG_MAX or
INT_MAX ULONG_MAX)
No, this is not valid.

6.2.5 Types
9 The range of nonnegative values of a signed integer type is a
subrange of the corresponding unsigned integer type, [...]
Jul 14 '08 #102
On Jul 14, 11:07 pm, Harald van D©¦k <true...@gmail.comwrote:
On Mon, 14 Jul 2008 11:16:16 -0700, vippstar wrote:
On Jul 14, 7:14 am, David Thompson <dave.thomps...@verizon.netwrote:
<snip>
Conversion to a signed integer type of an integer value out of range is
implementation-defined, and in C99 (explicitly) may raise a signal. On
_most_ implementations, for a signed/unsigned pair of types, half the
range of the unsigned type is out of range for the signed type.
It's undefined behavior, not implementation defined.

It's implementation-defined, not undefined.

6.3.1.3 Signed and unsigned integers
1 When a value with integer type is converted to another integer type
other than _Bool, if the value can be represented by the new type, it is
unchanged.
2 Otherwise, if the new type is unsigned, [...]
3 Otherwise, the new type is signed and the value cannot be represented in
it; either the result is implementation-defined or an implementation-
defined signal is raised.
Weird, in a recent discussion I've been told that %d in scanf invokes
undefined behavior because of overflow. I thought it's the same, for
example
scanf("%d", &i);
input: INT_MAX + 1
i = INT_MAX + 1; /* overflow, undefined behavior OR conversion &
implementation-defined? */
Also, it's
perfectly valid if INT_MAX UINT_MAX (though not INT_MAX LONG_MAX or
INT_MAX ULONG_MAX)

No, this is not valid.

6.2.5 Types
9 The range of nonnegative values of a signed integer type is a
subrange of the corresponding unsigned integer type, [...]
Ah, thanks. :-)
Jul 15 '08 #103
On Tue, 15 Jul 2008 03:40:12 -0700, vippstar wrote:
On Jul 14, 11:07 pm, Harald van Dijk <true...@gmail.comwrote:
>On Mon, 14 Jul 2008 11:16:16 -0700, vippstar wrote:
On Jul 14, 7:14 am, David Thompson <dave.thomps...@verizon.net>
wrote: <snip>
Conversion to a signed integer type of an integer value out of range
is implementation-defined, and in C99 (explicitly) may raise a
signal. On _most_ implementations, for a signed/unsigned pair of
types, half the range of the unsigned type is out of range for the
signed type.
It's undefined behavior, not implementation defined.

It's implementation-defined, not undefined.

6.3.1.3 Signed and unsigned integers
1 When a value with integer type is converted to another integer type
other than _Bool, if the value can be represented by the new type,
it is unchanged.
2 Otherwise, if the new type is unsigned, [...]
3 Otherwise, the new type is signed and the value cannot be
represented in
> it; either the result is implementation-defined or an
implementation- defined signal is raised.

Weird, in a recent discussion I've been told that %d in scanf invokes
undefined behavior because of overflow. I thought it's the same, for
example
scanf("%d", &i);
input: INT_MAX + 1
There is no relevant conversion here; the behaviour is undefined because
the specification of scanf says the behaviour is undefined. The
specification of scanf refers to "the result of the conversion", but
"conversion" is the plain English word here, and has nothing to do with
the type conversions -- those conversions that could be made explicit by a
cast.
i = INT_MAX + 1; /* overflow, undefined behavior OR conversion &
implementation-defined? */
Overflow. If INT_MAX + 1 is evaluated, the behaviour is undefined, and you
don't even have to store it in an object for that.

If you had used

i = INT_MAX + 1u;

the addition itself would have defined behaviour. If the result is not
within the range of i, and i is signed, then 6.3.1.3p3 applies.
Jul 15 '08 #104
On Jul 15, 4:49 pm, Harald van D©¦k <true...@gmail.comwrote:
<snip explanation>
Thanks. I have a better understanding now.
Jul 15 '08 #105
On Mon, 14 Jul 2008 04:14:03 GMT, I wrote:

Aargh! I thought I proofread this.
If you really want to accept a _signed_ (possibly but not necessarily
negative) number, strol (or ll or max) is usually the right way. If
strtol
you verify that the value is nonnegative, you can then (guaranteed)
convert it to the corresponding signed type without loss of
unsigned !
information. If negative, or in a larger/higher-rank type and
(actually) out of range, you can still convert to unsigned _safely_,
without any impl-def or undefined behavior, but with loss of info.
Sorry.
- formerly david.thompson1 || achar(64) || worldnet.att.net
Jul 28 '08 #106

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

Similar topics

1
by: George Marsaglia | last post by:
The essence of a multiply-with-carry RNG is to have declarations in the RNG proc, such as unsigned long mwc( ){ static unsigned long x = 123456789, c = 362436; unsigned long long t, a =...
29
by: Richard A. Huebner | last post by:
Is the unsigned long long primitive data type supported in ANSI standard C? I've tried using it a couple of times in standard C, but to no avail. I'm using both MS VIsual C++ 6, as well as the...
1
by: Sushil | last post by:
hi Gurus I'm a newbie learning C. I've a question, relevant code snippet is as follows : typedef unsigned long long ulonglong; struct foo { unsigned dummy : 2; unsigned n : 24;
12
by: Peter Ammon | last post by:
When I add an unsigned long long and an int, what type do each of the values get promoted to before the addition is performed? What is the type of the resulting expression? What occurs if the...
36
by: Digital Puer | last post by:
Hi, suppose I have an unsigned long long. I would like to extract the front 'n' bits of this value and convert them into an integer. For example, if I extract the first 3 bits, I would get an int...
9
by: luke | last post by:
Hi everybody, please, can someone explain me this behaviour. I have the following piece of code: long long ll; unsigned int i = 2; ll = -1 * i; printf("%lld\n", ll);
3
by: Nicholas Zhou | last post by:
Hi, I was writing a testing program to test the ranges of char, short, int and long variables on my computer, both signed and unsigned. Everything was fine except for unsigned int and unsigned...
21
by: Bart C | last post by:
I've always had a problem knowing exactly how wide my integer variables were in C, and the little program below has increased my confusion. Run on 3 compilers on the same cpu (32-bit pentium),...
17
by: Tarique | last post by:
This program was compiled on MS Visual C++ 08 /*Fibonacci Numbers*/ #include<stdio.h> #include<limits.h> void fibonacci(int n) { unsigned long long fib0 = 0; /*First Fibonacci Number*/
0
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
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
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...
1
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...
1
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
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
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
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
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.