so even though I do:
typedef long int int_4
and I do sizeof(int_4) that will be the same wherever machine I am?? I guess not right
so even though I do:
typedef long int int_4
and I do sizeof(int_4) that will be the same wherever machine I am?? I guess not right
oh okay by the way I have to slip in one unrelated question to this thread, say I have a code:
why doesn't it read directly from the command line when I typed in ./tiesto 0.5Code:#include <stdio.h> #include <stdlib.h> int main() { float number; scanf("%f", &number); printf("%f\n", number); return 0; }
it doesn't print 0.5 directly but it goes as I haven't typed in anything but after I typed it in again underneath it works...
well I thought I can do that.. in the previous assignment I have it works
Code:#ifdef NO_WAI_64_BITS typedef int_8 something; #elif NO_WAI_32_BITS typedef int_4 something; #endif
oh nevermind it works if I did a redirect from a file.. okay back to our topic again.. the point to all of this is this.. I want main to create a message say that I run the program and defining that it's on 64 bit machine but however it's on a 32 bit machine.. then I want to be able to print this error:
gcc -ansi -Wall -DMACHINE32 extract.c main.c -o main
ERROR: sizeof(int_u8) is not 8, it's 4
ERROR: sizeof(int_8) is not 8, it's 4
okay so the type def should all be the same either for 32 bit machine or 64 bit machine??
The point is that if you use, for example, uint32_t, then
sizeof(uint32_t) on a 32-bit machine will give 4
and
sizeof(uint32_t) on a 64-bit machine will give 4.
The headers are full of #ifs and such to make sure this is the case.
And of course, each compiler has its own such header to make sure it uses the correct type to make it's the right size when compiled.
The whole point with declaring typedefs (in this discussion) is that "you know what size the integer is". This helps in making sure for example that data structures are always the same size, data types match between different functions, etc.
In many cases, a regular "int", whatever size it happens to be, is a fine thing to use, but sometimes you need a type that matches some specific size, such as "32 bits", and if you want to be SURE that the type is exactly 32 bits, then you need a typedef that depends on the compiler/processor architecture, etc.
For example, a 64-bit compiler will make "long" a 64-bit number, but in a 32-bit compiler you need "long long" to make a 64-bit number.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.