sizeof (int) gives me 4 bytes...is it sufficient to say that my machine is of type 32-bit

Is it the processor, the operating system or the compiler?

Old Albert is wrong, human stupidity is rather limited. Infinity is much too perfect!

The size of ints is dependant upon the compiler or compiler setting. It would seem that your compiler defaultly uses 32-bit ints, as most "standard" C compilers should. The same code run on a 64-bit chip with a 64-bit OS will provide the same answer.

It wouldn't make much sense that the size of your variables would change with the processor that the code is run on. If this were true, there would be great problems with running code on different chips. It is for this reason that code has to be specifically compiled to take full advantage of 64-bit chips.

I would feel more comfortable trusting sizeof(void*) than sizeof(int).

I'm tempted to trust sizeof(size_t) too, but you never know...

>sizeof (int) gives me 4 bytes...is it sufficient to say that my machine is of type 32-bit
For your intended purposes, it probably is sufficient. But even if a machine supports 32-bit integers or has a 32-bit address space that doesn't mean it's a 32-bit machine. The term 32-bit machine is referring to the size of the data bus, and it means that 32 bits can be moved around with a single instruction, as opposed to a 16-bit machine which can only move 16 bits in a single instruction.

Also, just because sizeof(int) is 4 doesn't mean that an integer is 32 bits. The size of char is always 1 even if char is more than 8 bits, which is certainly possible. sizeof(int) * CHAR_BIT would be more accurate even though that still doesn't guarantee that your system is 32-bit. ;)

that still doesn't guarantee that your system is 32-bit. ;)

Actually, it says nothing at all about the system. I have a new 64-bit system and sizeof(int) is still 32 because that is what the compiler said it is, not the system. It would be impossible to run 32-bit programs on a 64-bit machine otherwise. ;)

>Actually, it says nothing at all about the system.
That's what I said. There's no guarantee.

True. For added fun, consider the Saturn processor in the HP48, with a 4 bit bus size, 4 bit word size, 3-5 word relative and absolute memory addresses (with nibble-addressable memory), 64-bit registers, 20-bit registers, a 16-bit register, a 12-bit register........

Now let's see you predict the values for various sizeofs :-)

>
Also, just because sizeof(int) is 4 doesn't mean that an integer is 32 bits. The size of char is always 1 even if char is more than 8 bits, which is certainly possible.

I guess byte is always equal to 8 bits...so sizeof(int) =4 will always suggest int is of 32 bits

>I guess byte is always equal to 8 bits
Maybe it is in your sheltered little world. But no, a byte is not always an octet (which is 8 bits).

>I guess byte is always equal to 8 bits
Maybe it is in your sheltered little world. But no, a byte is not always an octet (which is 8 bits).

cmon speak up in detail...why do u want me to post again
plz help me know more about the outside world

Detail doesn't matter in this case. All you need to know is that you shouldn't assume the size of a byte in portable code because the code could be ported to an exotic system. If you stick to the most common modern machines (such as the x86 that you're probably using) a byte is 8 bits. And yet, despite the fact that most modern machines use 8 bit addressing, it's very rare when you would need to exploit that knowledge.

I just found this while googleling

Some TI DSP platforms address all data in words, so while sizeof(char) = 1, a char actually occupies 16 bits of data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.