tags:

views:

209

answers:

3

I want to use a unsigned int32, using gcc 4.3.3 on Ubuntu 9.04.

However, when I declare this:

unsigned int32 dev_number;

I get an compile error:

error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘dev_number’

Any suggestions?

+5  A: 

I'm sure exactly what you're trying to achieve. Are you trying to have an unsigned int called int32? Or are you trying to have an unsigned 32 bit integer variable?

In the latter case, you can try to use stdint.h, which defines a set of types guaranteed (I believe?) to contain at least the set of bits specified.

#include <stdint.h>

uint32_t my_var;

In the former case, I can't see why your line shouldn't work.

unsigned int32 = 42;

printf ("int32 = %u\n", int32);

prints int32 = 42 as expected.

HTH

roe
Sorry for not being clear. I am after a unsigned 32 bit integer variable called dev_number.
robUK
You might want to print the unsigned using %u, not %d. Marking the constant as 42u is nice too, for clarity/consistency.
unwind
@unwind; of course.. silly me.. got thrown off by the variable name... :)
roe
Using the stdint.h. Is that portable across platforms i.e. win32, unix, linux?
robUK
@robUK: yes it is.
roe
No, it isn't. It's part of C99, and MSVC doesn't implement it. However, you can easily get a version of stdint for Windows, and add it to your source.
Steve Jessop
Stephen Canon
C89 is a language standard. C99 is a language standard. MSVC "implements" C89 (possibly with some lapses, certainly with extensions). You may choose to call C99 "C", and claim that C89 is "not C", but that usage is not in line with the majority of C programmers. The publishing of the C99 standard did not "change C", or replace C89. It created a new language, confusingly also called "C". For your information, GCC does not fully implement C99 either, although it's close enough that it has a useful C99 mode.
Steve Jessop
.. so, while it's fair to say that use of stdint.h is portable across C99 compilers, the question was in part, "is it portable to win32?". The answer to that is "no", or at least "not to MSVC", since MSVC is the most common C compiler for Windows, and it doesn't implement C99.
Steve Jessop
C99 is *the* C standard. It says so right in the forward: "This second edition cancels and replaces the first edition, ISO/IEC 9899:1990". The fact that "the vast majority" of C programmers think otherwise doesn't make it true. The vast majority of C programmers believe that eliminating lines of source code constitutes an optimization, and all kinds of other wacky things.
Stephen Canon
Nonsense. The foreword is not normative, and even if it were, it cannot cause C89 to cease to exist, or C89 compilers to no longer be C compilers. All it means is that C89 is no longer ISO's recommended version of C. But supposing it did de-standardise C89, then there does not exist a practical C compiler (I'm sure Comeau implements C99 fully, but for some reason not many people use it for production code). Therefore C99 would be a failed standard, since its alleged requirement that C89 disappear, has not been adopted by C programmers. Where next?
Steve Jessop
By the way, you don't need to argue that what "the vast majority of C programmers think" doesn't matter. You could instead argue that those Windows and linux programmers who call themselves "C programmer", *are not in fact C programmers*, since they are not programming C99 ;-)
Steve Jessop
+3  A: 

I can reproduce your exact error with this line:

unsigned int32 dev_number = 0;

Are you sure you haven't written something like that? There is no int32 type in C.

If you want to declare an unsigned 32 bit variable, use uint32_t and make sure you #include <stdint.h>

Michael Foukarakis
I was looking for a unsigned int32 bit variable. I am coming from C#, I guess that is where the confusion is coming from.
robUK
+7  A: 

In your code, use stdint.h as header file and use uint32_t instead of unsigned int32.

stdint.h is a standard header file introduced in the C99 standard library. It allows programmers to write more portable code by providing a set of typedefs that specify exact-width integer types, together with the defined minimum and maximum allowable values for each type, using macros. Please refer to wiki page here,

Here is sample code,

    #include <stdio.h>
    #include <stdint.h>

    int main(void)
    {
            uint32_t i = 50;

            printf("heloo world  %d\n", i);
            return 0;

    }
vinit dhatrak
`stdint.h` is portable except that Microsoft doesn't provide one with MSVC (until VS2010). Get one here: http://snipplr.com/view/18199/stdinth/
Michael Burr
@Michael Thank you for the link.
vinit dhatrak