tags:

views:

842

answers:

3

I'm trying to learn me some C, and have run into what is probably a simple problem. I'm trying to compile some code which contains the following declaration:

int32 count;

However, this results in an error at compile time:

test.c:21: error: ‘int32’ undeclared (first use in this function)

Is there a particular compile-time option I need to set for gcc, or an #include directive that will solve this?

p.s. I'm running on Ubuntu Intrepid.

A: 

At the start of the code place: typedef long int32; The type int32 doesn't exist sinse int already stands for "integer with 32 bits". Actually int may vary depending on the compiler, but you still can explicitely use "a 32 bits integer" using the type long.

Havenard
shouldn't that be the other way around? i.e., "typedef int int32;"? I tried it this way and it worked.
blackkettle
Sorry, that's completely wrong. long is generally 64 bits on a 64-bit platform. int is allowed to be a 16-bit type. If you want a specific width you should use the typedefs in <stdint.h> as caf suggests in his answer.
Jim Lewis
I'm not familiar to 64-bit architectures, if int is allowed to be 16-bit and long is allowed to be 64-bit, howtf I explicitely declare a 32-bit integer? As far as I know, to declare a "64 bit" integer you use **long long**, not long (alone).
Havenard
Actually, it's even more complicated. On some 64-bit systems (windows), int and long are 32 bits and long long is 64 bits (this is often called "LLP64"). On some other systems (OS X, Linux), int is 32 bits, but long and long long are both 64 bits (called "LP64"). On still other systems, int, long, and long long are all 64 bits (called "ILP64").If you want to have a (relatively) portable integer type that you know is exactly 64 bits, you should use the types int64_t and uint64_t defined in <stdint.h>.
Stephen Canon
From Stroustrup C++ Special Edition, p.75: This is what is guaranteed about sizes of fundamental types: 1 = sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long). Also, 1 <= sizeof(bool) <= sizeof(long). Also, sizeof(char) <= sizeof(wchar_t) <= sizeof(long). Also, sizeof(float) <= sizeof(double) <= sizeof(long double). Also, sizeof(N) = sizeof(signed N) = sizeof(unsigned N). Char is at least 8 bits, short at least 16 bits, long at least 32 bits. Entirely possible for int to be 16 bits and long to be 128 bits, with no 32-bit data type.
ChrisInEdmonton
Not only that, but it's also entirely possible for `char` to be 10 bits, and `int` to be 30 bits. On the other hand, I've been told that on some DSPs you have `sizeof(char)==sizeof(short)==sizeof(int)==2`, or 16 bits, because it can only work with words and not individual bytes.
Pavel Minaev
+9  A: 

The int32 type isn't standard C - the standard equivalent is to #include <stdint.h> and use int32_t.

However, as a POSIX system, on Ubuntu plain int is 32 bit so you could just use that.

caf
Two things of note: 1) `<stdint.h>` is standard, but it's C99 - not a problem for gcc, but can be elsewhere (e.g. MSVC); and 2) POSIX doesn't require `int` to be 32-bit, and there are Unixes out there for which it's not true.
Pavel Minaev
There are four programming environments allowed by POSIX to be the default (listed here: http://opengroup.org/onlinepubs/009695399/utilities/c99.html#tagtcjh_11 ), all of which require `int` to be at least 32 bits.
caf
A: 

Size of 'int' depends on the compiler you use. For same target architecture different compilers can have different sizes for 'int'. It depends on how they are utilizing the target features. For example, Turbo C running on windows has 'int' size as 2 bytes (its a DOS executable). While Visual C gives the size of 'int' as 32 (win32 executable).

In general, size of 'int' is determined by the size of word as per the target architecture. Size of word is reflected by 1. Processing size of arithmetic operations. 2. Size of general purpose registers. 3. Size of the address used to designate a location.

However in your case you are wrongly using a keyword 'int32' for a data type. Check the target machine and fix up the keyword used accordingly.

Ganesh Gopalasubramanian