Typedef Bug/Error

P

Pranav

#include<stdio.h>
int main()
{
typedef int R1;
typedef int R2;
typedef long int R3;

unsigned R1 n1;
long R2 n2;
R3 n3;

n1=123456789;
n2=123456789;
n3=123456789;
printf("%u..%d\n",n1,sizeof(n1));
printf("%ld..%d\n",n2,sizeof(n2));
printf("%ld..%d\n",n3,sizeof(n3));
return 0;
}

Getting Following Errors..,
Teast2.c syntax error before "n1"
Teast2.c syntax error before "n2"
Teast2.c `n1' undeclared (first use in this function)
Teast2.c `n2' undeclared (first use in this function)
 
P

Pranav

typedefs are not macros. You defined R1 as a typedef for (signed) int. You
cannot make it unsigned later.

K..., Here Lies The Problem.., Thank You Harald van D©¦k..,
 
P

Peter Nilsson

Harald van D©¦k said:
typedefs are not macros. You defined R1 as a typedef
for (signed) int. You cannot make it unsigned later.

Hence why <stdint.h> typedefs uintN_t as well as intN_t.
Note that a C99 implementation with CHAR_BIT == 32 must
provide uint32_t, but it need not provide int32_t.
 
H

Harald van Dijk

Note that a C99 implementation with CHAR_BIT == 32 must
provide uint32_t, but it need not provide int32_t.

It must provide both.

7.18.1 Integer types
1 When typedef names differing only in the absence or presence of the
initial u are defined, they shall denote corresponding signed and
unsigned types as described in 6.2.5; an implementation providing one
of these corresponding types shall also provide the other.
 
R

Robert Gamble

Hence why <stdint.h> typedefs uintN_t as well as intN_t.
Note that a C99 implementation with CHAR_BIT == 32 must
provide uint32_t, but it need not provide int32_t.


If the implementation provides 32-bit integers without padding bits
and uses two's complement representation, both uint32_t and int32_t
must be provided, otherwise neither shall be provided; in no case can
one be provided without the other (7.18.1p1).
 
P

Peter Nilsson

Harald van D©¦k said:
It must provide both.

7.18.1 Integer types
1 When typedef names differing only in the absence or
presence of the initial u are defined, they shall denote
corresponding signed and unsigned types as described in
6.2.5; an implementation providing one of these
corresponding types shall also provide the other.

It says that if that _both_ uint32_t and int32_t exist,
they must be corresponding types. It does not say the
presence of uint32_t requires the corresponding signed
integer to be two's complement and without padding.
 
V

vippstar

It says that if that _both_ uint32_t and int32_t exist,
they must be corresponding types. It does not say the
presence of uint32_t requires the corresponding signed
integer to be two's complement and without padding.

intN_t is *required* to be two's complement and without padding bits.
See 7.18.1.1 p 1
 
P

Peter Nilsson

Robert Gamble said:
If the implementation provides 32-bit integers without
padding bits and uses two's complement representation,

Let's say it does. Let's suppose int32_t is signed int.
both uint32_t and int32_t must be provided,

I see nothing preventing the corresponding unsigned
int from having the range 0..INT_MAX.
otherwise neither shall be provided; in no case can
one be provided without the other (7.18.1p1).

If both exist, they must be corresponding types. That
does not imply that if one meets the relevant criteria
of an exact width integer type, so must the other.
 
H

Harald van Dijk

It says that if that _both_ uint32_t and int32_t exist, they must be
corresponding types. It does not say the presence of uint32_t requires
the corresponding signed integer to be two's complement and without
padding.

I'm not seeing how you interpret the text. Could you give a concrete
example of an implementation you believe would be disallowed by "an
implementation providing one of these corresponding types shall also
provide the other"?
 
P

Peter Nilsson

Harald van D©¦k said:
I'm not seeing how you interpret the text. Could you give
a concrete example of an implementation you believe would
be disallowed by "an implementation providing one of these
corresponding types shall also provide the other"?

How about I provide you with allowed implementations that
don't meet your claim... :)

Example 1:

CHAR_BIT == 8
sizeof(int) == 4
UINT_MAX == 4294967295
INT_MAX == 2147483647
INT_MIN == -2147483647 /* sm or 1c, not 2c */

Here, uint32_t exists as unsigned int and has a corresponding
type under 6.2.5, namely signed int. However, signed int does
not meet the criteria needed to be int32_t.

Example 2:

CHAR_BIT == 8
sizeof(int) == 4
INT_MAX == 2147483647
INT_MIN == -2147483647-1
UINT_MAX == 2147483647

Here, int32_t exists and is signed int and has a corresponding
type under 6.2.5, namely unsigned int. However unsigned int
does not meet the criteria needed to be uint32_t.

What 7.18.1 says is this that if int32_t is chosen as say
signed int, and both unsigned int and unsigned long meet all
other criteria for being uint32_t, then uint32_t must be
unsigned int. It cannot be not unsigned long because that
is not the corresponding type to int32_t/signed int under
6.2.5.
 
H

Harald van Dijk

How about I provide you with allowed implementations that don't meet
your claim... :)

That does not help me understand your position. If they don't meet my
claim, which comes from the standard, they are not conforming
implementations, unless my claim is an incorrect interpretation. I am
trying to understand why you think it _is_ incorrect. Your examples show
why it _should be_ incorrect.
 
P

Peter Nilsson

intN_t is *required* to be two's complement and without
padding bits.

Perhaps I should have been clearer. I meant the
corresponding type under 6.2.5. An int must have a
corresponding unsigned int under 6.2.5. Similarly intN_t
must have a corresponding unsigned integer type under
6.2.5. That does not imply that intN_t's corresponding
integer type under 6.2.5 meets the criteria for uintN_t.

7.18.1p1 does not say that intN_t implies the presence of
uintN_t and vice versa. It say that if both exist,
then their corresponding types under 6.2.5 must be each
other.

If only one of intN_t or uintN exist for a given N,
7.18.1p1 does not apply.
 
R

Robert Gamble

Let's say it does. Let's suppose int32_t is signed int.


I see nothing preventing the corresponding unsigned
int from having the range 0..INT_MAX.


If both exist, they must be corresponding types. That
does not imply that if one meets the relevant criteria
of an exact width integer type, so must the other.

I fail to see how what you wrote above has any impact on what the
standard says:

7.18.1p1:
"When typedef names differing only in the absence or presence of the
initial u are defined,
they shall denote corresponding signed and unsigned types as described
in 6.2.5;
an implementation providing one of these corresponding types shall
also provide the other."

I don't know how this could be any clearer. This requirement was
referenced in DR 269 by Clive Feather:
"...it can be derived from the requirement to provide both or neither
of these types..."
and there was no contradictory comment in the Committee Response.

Can you provide any normative text that contradicts this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top