-1
#include <stdio.h>

int main(){
printf("%d\t",sizeof(6.5));
printf("%d\t",sizeof(90000));
printf("%d\t",sizeof('a'));

return 0;
}

When I'm compiling my code, the output will be: "842". Can somebody explain why I get this output?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 7
    What output do you expect? – Yu Hao Apr 08 '15 at 13:15
  • 2
    Note [sizeof returns size_t so you should use %zu](http://stackoverflow.com/a/21128106/1708801). – Shafik Yaghmour Apr 08 '15 at 13:16
  • 1
    Note that your code won't even compile as it stands, which makes me think it's not your actual code. Also I'd expect "8 4 1", unless your compiler has wide char support enabled. – Paul R Apr 08 '15 at 13:17
  • 2
    @PaulR: I'd expect the last number to be "4", since `'a'` is (oddly enough) of type `int` (rather than `char` as one might reasonably suppose)... – psmears Apr 08 '15 at 13:21
  • @psmears: you may well be right - I think you get a different answer depending on whether you compile as C or C++ (`sizeof(int)` for C, `sizeof(char)` for C++) ? – Paul R Apr 08 '15 at 13:26
  • 1
    @RobertWalton please copy and paste your real code instead of typing something out and getting it wrong – M.M Apr 08 '15 at 13:28
  • 1
    @RobertWalton It would help if you specify what platform and compiler you're using. Your output would be perfectly normal if you're on a platform with 8 byte doubles, 4 byte longs and 2 byte ints. – nos Apr 08 '15 at 13:30
  • 1
    @PaulR: Yes, indeed! – psmears Apr 08 '15 at 14:06

2 Answers2

5

First of all syntax error in your code

printf("%d\t";sizeof('a')); 

change this to

printf("%zu\t",sizeof('a'));   //note the change in format specifier also
             ^
             |
            see here

Then, assuming your platfrom is 32-bit

  • sizeof(6.5) == sizeof(double) == 8
  • sizeof(90000) == sizeof(int) == 4
  • sizeof('a') == sizeof(int) == 4

To clarify, a represents a value of 97 which defaults to int. so, sizeof('a') should give a value of 4, not 2 or 1.


Edit:

To add, you will get an output of 8 4 2, if, in 16-bit arch

  • sizeof(6.5) == sizeof(double) == 8
  • sizeof(90000) == sizeof(long) == 4
  • sizeof('a') == sizeof(int) == 2
Dilip Kumar
  • 1,736
  • 11
  • 22
  • 3
    The size of `int` is implementation-defined. It could be `2` on OP's system. – M.M Apr 08 '15 at 13:27
  • @MattMcNabb but it should not vary, right? So it should be either `822` or `844`, no way it ca be `842`, right? – Dilip Kumar Apr 08 '15 at 13:29
  • @MattMcNabb However, added a note about platform dependency. Hope it is ok now? – Dilip Kumar Apr 08 '15 at 13:30
  • @DilipKumar if you're on a platform with a 2 byte int, 90000 could be a long of 4 bytes. – nos Apr 08 '15 at 13:32
  • @DilipKumar it can be 842. 90000 doesn't fit in a 16 bit type, so if int is a 16 bit type, then 90000 will be a long, which may be twice as big as an int. – Art Apr 08 '15 at 13:33
  • @WernerHenze no, it's not. – Dilip Kumar Apr 08 '15 at 13:34
  • @nos you're right, that's why i was explicit in mentioning `assuming your platfrom is 32-bit` – Dilip Kumar Apr 08 '15 at 13:35
  • @ArunA.S IMO, the question is tagged `C`, so let's concentrate on `c` compilers only. BTW, it's not a good idea to compile `c` code with `c++` compiler and vice versa. :-) – Dilip Kumar Apr 08 '15 at 13:42
  • As I noted in my [comment above](http://stackoverflow.com/questions/29515695/how-and-is-the-output-of-my-code-is-842#comment47186773_29515695) using `%d` is incorrect and leads to undefined behavior. – Shafik Yaghmour Apr 08 '15 at 13:56
  • @ShafikYaghmour Thank you. I somehow missed that part. Will update accordingly. – Dilip Kumar Apr 08 '15 at 13:57
-1

If you're on a 32 bit compiler

printf("%d\t",sizeof(6.5));

6.5 is a double, so sizeof(double) gives 8.

printf("%d\t",sizeof(90000));

90000 is an int ( or a long ), so sizeof(int) gives 4.

printf("%d\t";sizeof('a'));
             ^
             you left a semicolon here, change it to a comma

'a' is converted to int, so sizeof(int) gives 4.

So the actual output is

8     4      4

ideone link

But , if you're on a 16 bit compiler, you will get

sizeof(6.5) = sizeof(double) = 8
sizeof(90000) = sizeof(long) = 4
sizeof('a') = sizeof(int) = 2

So that would explain your output.

Arun A S
  • 6,421
  • 4
  • 29
  • 43