Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Typedef Bug/Error

Reply
Thread Tools

Typedef Bug/Error

 
 
Pranav
Guest
Posts: n/a
 
      09-01-2008
#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)

 
Reply With Quote
 
 
 
 
Harald van D캐k
Guest
Posts: n/a
 
      09-01-2008
On Mon, 01 Sep 2008 10:43:54 -0700, Pranav wrote:
> typedef int R1;
> unsigned R1 n1;


typedefs are not macros. You defined R1 as a typedef for (signed) int. You
cannot make it unsigned later.
 
Reply With Quote
 
 
 
 
Pranav
Guest
Posts: n/a
 
      09-01-2008
On Sep 1, 10:51 pm, Harald van Dijk <(E-Mail Removed)> wrote:
> On Mon, 01 Sep 2008 10:43:54 -0700, Pranav wrote:
> > typedef int R1;
> > unsigned R1 n1;

>
> 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 Dijk..,
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      09-02-2008
Harald van Dijk <(E-Mail Removed)> wrote:
> Pranav wrote:
> > typedef int R1;
> > unsigned R1 n1;

>
> 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.

--
Peter
 
Reply With Quote
 
Harald van D캐k
Guest
Posts: n/a
 
      09-02-2008
On Mon, 01 Sep 2008 18:01:04 -0700, Peter Nilsson wrote:
> 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.
 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      09-02-2008
On Sep 1, 9:01 pm, Peter Nilsson <(E-Mail Removed)> wrote:
> Harald van Dijk <(E-Mail Removed)> wrote:
>
> > Pranav wrote:
> > > typedef int R1;
> > > unsigned R1 n1;

>
> > 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.



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).

--
Robert Gamble
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      09-02-2008
Harald van Dijk <(E-Mail Removed)> wrote:
> Peter Nilsson wrote:
> > 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.


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.

--
Peter
 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      09-02-2008
On Sep 3, 1:24 am, Peter Nilsson <(E-Mail Removed)> wrote:
> Harald van Dijk <(E-Mail Removed)> wrote:
>
> > Peter Nilsson wrote:
> > > 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.

>
> 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
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      09-02-2008
Robert Gamble <(E-Mail Removed)> wrote:
> Peter Nilsson <(E-Mail Removed)> wrote:
> > Harald van Dijk <(E-Mail Removed)> wrote:
> > > Pranav wrote:
> > > > typedef int R1;
> > > > unsigned R1 n1;
> > >
> > > 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.

>
> 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.

--
Peter
 
Reply With Quote
 
Harald van D캐k
Guest
Posts: n/a
 
      09-02-2008
On Tue, 02 Sep 2008 15:24:22 -0700, Peter Nilsson wrote:
> Harald van D캐k <(E-Mail Removed)> wrote:
>> Peter Nilsson wrote:
>> > 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.

>
> 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"?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CRTP-problem: How can the base class typedef a derived class' typedef? oor C++ 0 05-20-2008 12:39 PM
java needs typedef Steve Green Java 11 03-25-2005 09:52 AM
Typedef of a template? Richard van Wegen C++ 3 07-15-2003 07:22 AM
template typedef as return type Robert A. T. Kaldy C++ 1 07-09-2003 06:25 PM
typedef enum qazmlp C++ 2 07-02-2003 11:55 AM



Advertisments