I think I'm starting to understand and I can verify that an int type
on my GNU system is indeed 32 bits. I added the following line:
printf("%d %d %d %d
\n",sizeof(apa),sizeof(kaka),sizeof(mazarin),sizeof(int));
which will output: 2 2 4 4
So if I understand this correctly, the result of an integer
multiplication is always an int no matter of the type of the operands?
Or does it mean that both operands are always up-casted to an int
before the operation?
What would happen if one of the operands would be 64 bits and the
values such that the result exceeds MAX_INT?
On Feb 2, 2:02 pm, Harald van D=A9=A6k <true...@[EMAIL PROTECTED]
> wrote:
> On Sat, 02 Feb 2008 04:46:32 -0800, Mamluk Caliph wrote:
> > The following code executes as I would expect on gcc:
>
> > #include <inttypes.h>
> > #include <stdio.h>
>
> > int main(int argc, char **argv){
> > uint16_t apa =3D 10000;
> > uint16_t kaka =3D 7000;
> > uint32_t mazarin;
>
> > mazarin =3D apa*kaka;
> > printf("%lu \n",mazarin);
>
> %lu is used to print a value of type unsigned long. What is the type of
> mazarin?
I see your point. I assumed that unsigned long would be larger than an
int, i.e. 32 bit's. On my GNU system a long actually is 32 bits - but
so on the other hand is int.
I added yet the following line:
printf("%d %d %d %d
\n",sizeof(char),sizeof(int),sizeof(long),sizeof(long long));
which prints out: 1 4 4 8
I.e. mazarin (uint32_t) could be either unsigned int or unsigned long.
I don't know if it matters which since both seems to be of the same
size on my system.
By the way, I tried defining my own types and thought I could do
something as follows:
#if sizeof(int)=3D=3D4
typedef int myInt32_t;
#endif
This results in a compilation error `missing binary operator before
token "("`, why is that? I thought sizof was just a macro.
>
> > return 0;
> > }
>
> > I.e. it will print out the value 70000000. However, when I compile
this
> > another compiler the result is truncated and I have to modify the code
> > to the following to make it run:
>
> > mazarin =3D (uint32_t)apa*kaka;
>
> > Could anybody explain this?
>
> If uint16_t is a typedef for unsigned int (meaning unsigned int has 16
> bits), multiplying one uint16_t by another uint16_t produces a value of
> type uint16_t. It doesn't matter that you're going to store it in an
> uint32_t. You need to force the multiplication to be done in a way that
> it produces a result of type uint32_t (or wider), which is what the cast
> does.
>
> It happens to work on your GCC setup, because on that system (I'm going
> to make assumptions), int happens to have 32 bits, and multiplying an
> uint16_t (unsigned short) by another uint16_t produces a result of type
> int, or unsigned int, depending on the system. Since your int has enough
> bits to store the result, you get to actually see the result.
>
> > Is this due to C99 improvements regarding
> > integer overflow? Is there any predefined macro one could use to
> > determine this special case?
>
> There is nothing special happening here, and either result (or others)
is
> possible in both C90 and C99.
>
> > (Note: The extended types in inttypes.h were added for the second
> > compiler.)


|