Code:code: printf("%d %d %d",sizeof(3),sizeof('3'),sizeof("3"));can somone tell me why sizeof('3') comes out to be 4 though it is a character of size 1.Code:ans: 4 4 2
thanks in advance.
Code:code: printf("%d %d %d",sizeof(3),sizeof('3'),sizeof("3"));can somone tell me why sizeof('3') comes out to be 4 though it is a character of size 1.Code:ans: 4 4 2
thanks in advance.
In C, a character constant is an integer (weird huh?). In C++ though, a character constant is an actual character, and should give you a value of 1.
bit∙hub [bit-huhb] n. A source and destination for information.
machine only understands number, you got that?
therefore, in order to represent a character, each unique number is assigned to a character. Example: 0x41 is char 'A', so whenever you print out 0x41, machine will translate it into letter 'A', ok?
sizeof: is a macro that accepts a number as its param.
'3' <--- is a letter but the macro sizeof only accept number, then it will translate '3' into its assigned number, which is 0x41. By default, this number is an int, that is 4-byte in size. Therefore, the result is 4, instead of 2.
However, the point is that a character literal is of type (const) int, not char.Originally Posted by bvkim
sizeof is not a macro. sizeof is an operator whose result is the size, in bytes, of its operand.Originally Posted by bvkim
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)