Notices
Results 1 to 12 of 12

Thread: C++ Char difference between c = '\0', c = 0, and c= '0'

  1. #1 C++ Char difference between c = '\0', c = 0, and c= '0' 
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    920
    Are any two the same?
    Whats the difference between them?
    i know '\0' marks the end of an array, but is it valid to use in a single variable to represent null?
    im finding a char variable to be null, so it will print white spaces into the screen when i cout<<c;
    i need it to cout a white space in a tic tac toe box.

    differences between these 3 anyone?


    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  2.  
     

  3. #2  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,670
    c = '\0' and c = 0 are equivalent.

    The only difference is typing. '\0' is a char while 0 is an int.

    This means that if c is a char, then c = 0 will involve an implicit type cast. And if c is an int, then c = '\0' will involve an implicit type cast. In practice, this rarely matters. But there may be situations where it does matter.

    c = '0' is different as this assigns the character code for '0' to c (which is 48, decimal).

    If you want a white space, then isn't ' ' (space) good enough?


    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  4. #3  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    920
    is \0 in the ascii table?
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  5. #4  
    Forum Freshman
    Join Date
    May 2013
    Posts
    13
    Quote Originally Posted by RamenNoodles View Post
    is \0 in the ascii table?
    yes ,it is part of the control-code range of ascii, its called NULL .. integer 0.

    At your level the only control code you need is \n , and its actually WRONG for you to use \0 NULL
    Reply With Quote  
     

  6. #5  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    920
    so, where does the c-string actually stop, since all elements are null('\0')?

    does giving a null('\0') to index array[i] shorten a c-string to size array[i]?

    or is the full c-string size still reserved in the memory?
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  7. #6  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,670
    Quote Originally Posted by RamenNoodles View Post
    so, where does the c-string actually stop, since all elements are null('\0')?
    I'm not quite sure what you are asking, but if you initialise all the elements of a char array to zero, then you have a zero length string.

    does giving a null('\0') to index array[i] shorten a c-string to size array[i]?

    or is the full c-string size still reserved in the memory?
    The storage for an array is fixed, regardless of its contents.
    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  8. #7  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    920
    Quote Originally Posted by Strange View Post
    The storage for an array is fixed, regardless of its contents.
    does it also apply for strings?

    what would happen if i do "char i[5]={0,0,0,0,0};" then convert i into C++ string?


    since c++string ends with a '\0' or null, or 0, how would string behave when each character in this c++ string are nulls??
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  9. #8  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,670
    Quote Originally Posted by RamenNoodles View Post
    does it also apply for strings?

    what would happen if i do "char i[5]={0,0,0,0,0};" then convert i into C++ string?

    since c++string ends with a '\0' or null, or 0, how would string behave when each character in this c++ string are nulls??
    Ah, I see. I didn't realise you meant the string type. I am a but rusty, but my understanding is that creating a string type reserves some (implementation defined) amount of storage capacity. This will be automatically increased when more characters are added to the string. Probably with some "padding" for efficiency; e.g. so that the string capacity doesn't have to be increased every time you add one character (increasing the capacity requires the string to be copied so that it can remain compatible with a traditional char-array). I don't think the capacity of string type will ever be reduced (unless you do it explicitly). But that could be an implementation dependent.

    If you convert your char-array to a string then I'm not sure if it is guaranteed to preserve any but the first '\0'. You would have to check the C++ standard to be sure. If you are serious about C++ programming, you should get used to reading the standard.

    In any case, any '\0' characters after the first are irrelevant as they are not part of the string representation. So, for example, strcpy() will ignore them. And if you use the shrink_to_fit() method, then the storage of the string may be reduced to include only the first '\0'. (Although, again, some extra padding may be included. Or the capacity may not be changed at all.)

    In summary, anything after the '\0' is not considered to be part of the string.

    More here: string - C++ Reference (as always).
    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  10. #9  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    920
    Quote Originally Posted by Strange View Post
    In any case, any '\0' characters after the first are irrelevant as they are not part of the string representation.
    but in this code
    Code:
    char hi[3]={'\0','\0','\0'}
    does it shorten hi[]?

    does it apply to c-string? or only c++'s string type?
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  11. #10  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,670
    Quote Originally Posted by RamenNoodles View Post
    Quote Originally Posted by Strange View Post
    In any case, any '\0' characters after the first are irrelevant as they are not part of the string representation.
    but in this code
    Code:
    char hi[3]={'\0','\0','\0'}
    does it shorten hi[]?

    does it apply to c-string? or only c++'s string type?
    This is a 3 element array. It will always be a three element array. It contains a zero length string.

    If you do this with the string type, then it will be implemented as an array with 3 or more elements.
    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  12. #11  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    If you're already using C++, why are you using C-style strings anyway? The string class was made specifically to get around all these problems. There's nothing you can do with a C-string that you can't do with a C++-string barring some really weird direct memory twiddling which is rarely worth anything at all.
    Reply With Quote  
     

  13. #12  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,670
    Quote Originally Posted by MagiMaster View Post
    If you're already using C++, why are you using C-style strings anyway? The string class was made specifically to get around all these problems. There's nothing you can do with a C-string that you can't do with a C++-string barring some really weird direct memory twiddling which is rarely worth anything at all.
    Good point. I tend to use C-style strings because of my age but there is no reason for anyone else to
    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

Similar Threads

  1. difference
    By icu in forum Physics
    Replies: 4
    Last Post: May 10th, 2009, 12:01 AM
  2. IS THERE A DIFFERENCE . . .
    By BARCUD in forum Biology
    Replies: 17
    Last Post: October 28th, 2008, 09:53 PM
  3. Difference!!
    By pulasthi in forum Biology
    Replies: 26
    Last Post: July 27th, 2007, 08:48 AM
  4. What´s the difference?
    By carlosgabriel2 in forum General Discussion
    Replies: 5
    Last Post: May 17th, 2007, 01:37 AM
  5. difference
    By anand_kapadia in forum Chemistry
    Replies: 2
    Last Post: February 4th, 2007, 08:59 AM
Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •