Page 1 of 1

something strange about sizeof struct ?

Posted: Tue Aug 15, 2006 8:56 am
by surya ss
I have a problem with sizeof struct
with this code

Code: Select all

#include <stdio.h>
struct A
{
   int a;
   short b;
};
struct B
{
   int a;
   short b,c;
};
int main()
{
    printf("%d %d\n",sizeof(struct A),sizeof(struct B));
    return 0;
}
how can sizeof( struct A ) and sizeof( struct B ) is 8 ???
can anyone explain ?
and this

Code: Select all

#include <stdio.h>
struct A
{
   long long a;
   short b;
};
struct B
{
   long long a;
   short b,c;
};
int main()
{
    printf("%d %d\n",sizeof(struct A),sizeof(struct B));
    return 0;
}
how can the code above give result 16 for both sizeof(struct A) and sizeof(struct B) ??
can someone please explain ?

Posted: Tue Aug 15, 2006 9:47 am
by Krzysztof Duleba
Have you tried using Google? Hint: "sizeof struct".

Posted: Tue Aug 15, 2006 9:57 am
by surya ss
Krzysztof Duleba wrote:Have you tried using Google? Hint: "sizeof struct".
well thanks, it doesn't come to my mind
because I just found it to be strange...