2

I have tried doing research through google and SO but I can only find results for sizeof('a') which is type int and thus should give me 4 bytes, but when I run this I get 1 byte (I think it might be because the link I have is a reference for C and not C++). HOWEVER what is interesting here is that when I type sizeof('aa') I get 4 bytes. And anymore characters after aa (for example aaa or aaaa) still gives me 4 bytes. Why is there a sudden increase in 3 bytes that remains constant with the addition of more chars? Also why does the sizeof(int) here give me 1 byte?

Thanks!

jlcv
  • 1,688
  • 5
  • 21
  • 50
  • 12
    Character literals have type `char` in C++. Multicharacter literals have type `int`. – chris Mar 16 '13 at 07:04
  • 3
    This is one place where C and C++ are different. In C `'a'` is an `int`, in C++ it is not for reasons of overloading functions. – Bo Persson Mar 16 '13 at 07:09
  • [Size of character ('a') in C/C++](http://stackoverflow.com/questions/2172943/size-of-character-a-in-c-c?lq=1) [In C, why is sizeof(char) 1, when 'a' is an int?](http://stackoverflow.com/questions/2252033/in-c-why-is-sizeofchar-1-when-a-is-an-int?rq=1) – phuclv Jan 30 '15 at 04:06

1 Answers1

6

sizeof('a') which is type int

Neither one of 'a' or sizeof('a') is an int. In C++, one-character literals are of type char, and the type of a sizeof() expression is size_t.

Furthermore:

type int [...] should give me 4 bytes

No, int need not necessarily be exactly 4 bytes long.

  • Since we are on the topic of sizeof(), I have one more question. I noticed that you said int need not necessarily be exactly 4 bytes long so I decided to play around with my code and test what range is for a type int variable. Here I made a declaration: `int x = 18446744073709551615`, which is just 2^64 or in other words int is 16 byte size. Then why is sizeof(int) giving me 4 bytes? Also according to http://www.learncpp.com/cpp-tutorial/24-integers/, a size of 4 bytes should give me a range between -2,147,483,648 to 2,147,483,647. Thanks again! – jlcv Mar 16 '13 at 07:25
  • Maybe you should explain about `aa`? – juanchopanza Mar 16 '13 at 07:26
  • @JustinLiang "2^64 or in other words int is 16 byte size." - ***What?*** 64 bits are 8 bytes, assuming a 8-bit byte. Or are bytes 4-bit long on your platform? Also, what do `INT_MAX` and `INT_MIN` evaluate to? –  Mar 16 '13 at 07:27
  • Oops sorry I meant 64 bits are 8 bytes. INT_MAX and INT_MIN evaluate to -2,147,483,648 and 2,147,483,647 respectively. – jlcv Mar 16 '13 at 07:31
  • @JustinLiang Then I don't see what's that with the 8 bytes. With these values of `INT_MAX` and `INT_MIN` and `sizeof(int)` yielding 4, `int` is ***100% positively certainly 4 bytes long.*** –  Mar 16 '13 at 07:32
  • Hmmm, then how come `int x = 18446744073709551615` works? Why is it that `int` can store numbers bigger than 2,147,483,647? Shouldn't we expect overflow? – jlcv Mar 16 '13 at 07:34
  • Ah, I see what you mean now. I used `cout` after declaring `int x = 18446744073709551615` and it printed 1 onto the screen. The reason why I thought it worked was because initially I did not use `cout` and the program compiled fine. HOWEVER when I changed my declaration to `int x = 18446744073709551616` (here I added 1 to 18446744073709551615) I would get the compilation error `integer constant too large`. But now I think it makes sense to get an error because the largest size of any variable type is 8 bytes, so we should expect an error when trying to store anything bigger than 8 bytes. – jlcv Mar 16 '13 at 07:43