9

I am trying to understand following piece of code, but I am confused between "\0" and '\0'.I know its silly but kindly help me out

   #define MAX_HISTORY 20

   char *pStr = "\0";
   for(x=0;x<MAX_HISTORY;x++){
        str_temp = (char *)malloc((strlen(pStr)+1)*sizeof(char));
        if (str_temp=='\0'){
            return 1;
    }
    memset(str_temp, '\0', strlen(pStr) );
    strcpy(str_temp, pStr);
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Fermi
  • 115
  • 1
  • 6
  • `char *pStr = "\0";` I guess this is not what you think it is. You declare a pointer and write something in it. That would be the address it points to. Do a `malloc` there and afterwards fill the allocated memory. Or you could just use an char array there. – Kami Kaze Oct 19 '16 at 10:55
  • 2
    @KamiKaze I guess `char *pStr = "\0";` is not what you think. It declares a pointer whose value is the **address of the string literal** that contains two nul bytes. No need to malloc to be correct. – Jean-Baptiste Yunès Oct 19 '16 at 11:02
  • @Jean-BaptisteYunès Mhh you got me thinking there. What is the lifetime of the memory in which the two nul bytes reside? – Kami Kaze Oct 19 '16 at 11:05
  • 1
    @KamiKaze String literals have static storage duration (see standard 6.4.5/6). – Jean-Baptiste Yunès Oct 19 '16 at 11:12

3 Answers3

12

They are different.

"\0" is a string literal which has two consecutive 0's and is roughly equivalent to:

const char a[2] = { '\0', '\0' };

'\0' is an int with value 0. You can always 0 wherever you need to use '\0'.

P.P
  • 117,907
  • 20
  • 175
  • 238
10

Double quotes create string literals. So "\0" is a string literal holding the single character '\0', plus a second one as the terminator. It's a silly way to write an empty string ("" is the idiomatic way).

Single quotes are for character literals, so '\0' is an int-sized value representing the character with the encoding value of 0.

Nits in the code:

  • Don't cast the return value of malloc() in C.
  • Don't scale allocations by sizeof (char), that's always 1 so it adds no value.
  • Pointers are not integers, you should compare against NULL typically.
  • The entire structure of the code makes no sense, there's an allocation in a loop but the pointer is thrown away, leaking lots of memory.
unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    Don't forget to nit about the memory leak (overwrites reference to allocated memory in loop). – Klas Lindbäck Oct 19 '16 at 11:03
  • 1
    To not use `sizeof(char)` in malloc is a very subjective style opinion, I don't understand why people keep making that remark. People might be typing out the sizeof(char) explicitly so because they want to have a consistent coding style no matter the type and always call malloc as `malloc(n * sizeof(type))`. This adds the value of self-documenting code. To argue against that it's just like saying that we shouldn't write `char* ptr = malloc(n*sizeof(*ptr))` but rewrite that as `char* ptr = malloc(n)`, but only if the type is char. – Lundin Oct 19 '16 at 11:16
  • It is not a silly way to create an empty string, as it just not create an empty string. It just creates a string literal that is not a string, see 6.4.5/6 note 78 – Jean-Baptiste Yunès Oct 19 '16 at 11:19
  • 1
    @Lundin To be fair, many if not most C programmers do not know that `sizeof(char)` is *defined* to be one. – Andrew Henle Oct 19 '16 at 11:19
  • @Lundin Yeah, that's maybe a fair point. It's hard to know if snippets posted here are parts of large codebases with consistent coding styles. My assumption tends to be that most of the time, they're not. I think it's valuable to "preach" about this since it's still a bit unknown, and I dislike cargo culting. – unwind Oct 19 '16 at 11:21
  • I typed `strlen("\0")` but got `0`; does it suggest `"\0"` is an empty string? – Chris Tang Mar 16 '20 at 06:21
  • 1
    @ChrisTang Yes, since `'\0'` acts as the terminator, it is not counted. In memory `""` will be a single `'\0'`, when you include it explicitly it will be doubled but that doesn't matter from `strlen()`'s point of view. – unwind Mar 17 '20 at 09:22
6

\0 is the null terminator character.

"\0" is the same as {'\0', '\0'}. It is a string written by a confused programmer who doesn't understand that string literals are always null terminated automatically. Correctly written code would have been "".

The line if (str_temp=='\0') is nonsense, it should have been if (str_temp==NULL). Now as it happens, \0 is equivalent to 0, which is a null pointer constant, so the code works, by luck.

Taking strlen of a string where \0 is the first character isn't very meaningful. You will get string length zero.

Lundin
  • 195,001
  • 40
  • 254
  • 396