Are these two statements not logical, semantic as well as binary clones?

This is a discussion on Are these two statements not logical, semantic as well as binary clones? within the C Programming forums, part of the General Programming Boards category; Are these two statements not logical, semantic as well as binary clones? char aggregation_key[250] = ""; char aggregation_key[250] = {0};...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    7

    Question Are these two statements not logical, semantic as well as binary clones?

    Are these two statements not logical, semantic as well as binary clones?

    char aggregation_key[250] = "";
    char aggregation_key[250] = {0};

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes they are the same thing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Are these two statements not logical, semantic as well as binary clones?
    From a theoretical point of view, they aren't, as '\0' doesn't need to equal 0. But it does in both ASCII and EBCDIC, so it's probably safe to assume that the two statements have the same effect. But beware: this may change as soon as we start communicating with an extraterrestrial intelligence on a regular basis.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    Ah, in that case the two below are the same in any respect:

    char aggregation_key[250] = "";
    char aggregation_key[250] = {'\0'};

  5. #5
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Exactly.
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > From a theoretical point of view, they aren't, as '\0' doesn't need to equal 0
    Yes it does.

    The character encoding comes into it when you say 'A'. Whether that gets you 65 or 193 or whatever depends on your character encoding.

    But if you write '\x41', then that is what you end up with. Maybe it's interpreted as 'A', and maybe it isn't. But look in memory, and that will be it's bit pattern for sure.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    Salem,

    Then why do we write '\0' instead of 0?

    I know their internal representation is the same across UNIX and Windows, but is it written specifically so in the C standard anywhere that NULL terminator has to be 0?

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by PoorLuzer View Post
    Then why do we write '\0' instead of 0?
    Do we? You don't have to. But it does make it easier to recognize, the same way in ascii
    Code:
    char x='a';
    char x=97;
    are the same thing, but most people would use the first one.

    Just don't confuse '\0' (which is 0) with '0' (which is 48).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Oops, Salem is right (though I wouldn't recommend looking in memory to determine the behaviour of some mechanism).

    '\<num>' simply interprets <num> as an octal number. I was completely mistaken about this one, but I could have lived with it. What's more humiliating is this snippet from the C standard, 5.2.1, node 2:

    "A byte with all bits set to 0, called the null character, shall exist in the basic execution character set; it is used to terminate a character string."

    Salem: 1
    Snafuist: 0

    0 commies killed.

    Insert next question.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Quote Originally Posted by PoorLuzer View Post
    Then why do we write '\0' instead of 0?
    For type correctness. Other languages such as Pascal wont let you get away without assigning the right type.
    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"

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by PoorLuzer View Post
    Salem,

    Then why do we write '\0' instead of 0?

    I know their internal representation is the same across UNIX and Windows, but is it written specifically so in the C standard anywhere that NULL terminator has to be 0?
    The null terminator cannot be anything but zero. This is because C is intended to be portable across encodings. If we declared that on such-and-such a platform, '\0' is (char)122, then that platform cannot support any encoding with a character at index 122.

    On the other hand, nearly all encodings do not represent a character at index 0. It seems like a chicken-and-egg problem, but in reality C has influenced the charater encodings just as much as the encodings have influenced C.

    I write '\0' not for the \0 part but for the single quotes, which indicate that I'm using a character literal. In a complex expression where a 0 appears, it makes it easier to figure out what data types are involved.

    EDIT: Also, compare with the semantics of NULL. The actual hardware value of NULL is implementation-defined. However, at the source code level, we can always refer to NULL as (void *)0, even if the actual bit pattern of NULL is not all zero bits.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    Quote Originally Posted by iMalc
    For type correctness.
    I think that in C it really is more of readability than type correctness since character constants are of type int.
    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

  13. #13
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Could someone please explain "semantically equivalent"? My understanding of that statements makes me think that those two are not semantically equivalent, as the first example is clearly per usage of a string, whereas the second is a character array. What am I forgetting about semantics?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by neandrake View Post
    Could someone please explain "semantically equivalent"? My understanding of that statements makes me think that those two are not semantically equivalent, as the first example is clearly per usage of a string, whereas the second is a character array. What am I forgetting about semantics?
    I suppose it depends on what you think "per usage of a string" means. My view is that the first one declares an array and initializes it to all zeroes (by specifying one zero character and using the "whatever I didn't specify is 0" rule), while the second one declares an array and initializes it to all zeroes (by specifying one zero character and using the "whatever I didn't specify is 0" rule).

    Edit: I suppose your point might be that the string has the \0 terminator built-in, while it has to be specified in the second example?
    Last edited by tabstop; 02-19-2009 at 07:29 PM.

  15. #15
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I think my point was somewhere around that. That with a string there will be an implied ending null-terminator (not enforced), but the second one doesn't imply that at all, just an array of char's.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 10:07 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 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