Thread: dubious sizeof operator

  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,268
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 07:05 AM
  2. Replies: 6
    Last Post: 10-15-2007, 08:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

Tags for this Thread