dubious sizeof operator

This is a discussion on dubious sizeof operator within the C Programming forums, part of the General Programming Boards category; Code: code: printf("%d %d %d",sizeof(3),sizeof('3'),sizeof("3")); Code: ans: 4 4 2 can somone tell me why sizeof('3') comes out to be ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    5

    Question dubious sizeof operator

    Code:
    code:
    
    printf("%d %d %d",sizeof(3),sizeof('3'),sizeof("3"));
    Code:
    ans:
    
    4    4    2
    can somone tell me why sizeof('3') comes out to be 4 though it is a character of size 1.
    thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    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.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    5

    character const..

    Quote Originally Posted by bithub View Post
    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.
    good that i asked. I couldnt have figured out that one. Man, why to keep a character const. an integer! I think thats why they developed C++ to remove its faults!!!!!

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    60
    Quote Originally Posted by lazy_hack View Post
    good that i asked. I couldnt have figured out that one. Man, why to keep a character const. an integer! I think thats why they developed C++ to remove its faults!!!!!
    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.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Quote Originally Posted by bvkim
    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?
    However, the point is that a character literal is of type (const) int, not char.

    Quote Originally Posted by bvkim
    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.
    sizeof is not a macro. sizeof is an operator whose result is the size, in bytes, of its operand.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    And it doesn't convert its argument into an int either...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  2. Replies: 6
    Last Post: 10-15-2007, 09:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21