Thread: Quick Sizeof Question

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    15

    Quick Sizeof Question

    At the top of my program I created a definition:

    Code:
    #define CRLF "\r\n"
    Which seems to work just fine except for the fact that the sizeof(CRLF) returns 3 when I would expect the result to be a 2. Is this just a mistake on my part?

    Thanks,

    PetrolMan

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    \r = 1
    \n = 1
    \0 = 1
       ----
         3

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    15
    \0 is a Null Character Right?

    Is there a way to prevent the \0 from being put in or is that even desirable in the first place?

    PetrolMan

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Every "string literal" ends with a null character. Whether you want that or not depends on how you use CRLF.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There's a multicharacter constant:
    Code:
    #define CRLF '\r\n'
    However that's implementation-defined behaviour, and wont be a direct replacement because then suddently it's not even an array.
    I don't think it makes sense to use sizeof on it. If you'd rather avoid using strlen on it, then just declare another constant with the value 2.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  2. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  3. A newby question on the sizeof operator
    By Evo in forum C++ Programming
    Replies: 2
    Last Post: 07-19-2002, 03:17 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM