-2

If I'm getting different outputs on different compilers while running the below program:

#include <stdio.h>
void main()
{
  struct s
  {
    int a;
    char b;
  } s1, s2;

  int x = sizeof(s1);
  printf("%d", x);
}

OUTPUT (Turbo c):

(int 2 + char 1 = 3)

I am getting 3 here.

OUTPUT (Code blocks):

8

Why is 8 is output here?

halfer
  • 19,824
  • 17
  • 99
  • 186
Prakash Bala
  • 315
  • 5
  • 12
  • 4
    there are lot of answers reagrding `sizeof(struct)` which considers padding so it is implementation defined – Gopi Sep 01 '15 at 08:49
  • @Prakash: maybe the 8 comes from 1 (char) plus 4 (int) rounded to the nearest 8. – halfer Sep 13 '15 at 15:57

1 Answers1

0

The size of C standard types (except char and its variants which are 1) is implementation dependent as is the padding amount between structure members.

ouah
  • 142,963
  • 15
  • 272
  • 331