'\0' evaluates false, "\0" evaluates true

CArraysStringNullKernighan and-Ritchie

C Problem Overview


Inspired by a program described in K&R section 5.5:

void strcpy(char *s, char *t)
{
	while(*s++ = *t++);
}

C program

if ('\0') { printf("\'\\0\' -> true \n"); }
else      { printf("\'\\0\' -> false\n"); }

if ("\0") { printf("\"\\0\" -> true \n"); }
else      { printf("\"\\0\" -> false\n"); }

prints

'\0' -> false
"\0" -> true

Why do '\0' and "\0" evaluate differently in C?

> clang version 3.8.0

C Solutions


Solution 1 - C

Recall how string literals work in C - "\0" is a character array containing two zero bytes (the one you asked for, and the implicit one at the end). When evaluated for the if test, it decays into a pointer to its first character. This pointer is not NULL, so it's considered true when used as a condition.

'\0' is the number zero, equivalent to just 0. It's an integer which is zero, so it's considered false when used as a condition.

Solution 2 - C

First of all, you need to keep in mind that in C,

  • Zero is false and non-zero is true.
  • For pointer types, NULL is false and non-NULL is true.


'\0', as others have said, is the same as the integer literal 0 and hence is false (See first bullet point above to know why).

"\0" is a string literal that contains two \0 characters (One which you have explicitly added and the other, which is implicit and will be added by the compiler). The string literal will be stored somewhere in read-only memory. When you use "\0", it gets converted to a pointer to its first element. This is commonly referred to as "array decay". (This is the reason why stuff like char* str = "string"; works).

So, you are effectively checking the address of the first character of the string literal. Since the address of the string literal will always be non-NULL, the if will always be true (See second bullet point above to know why).


: This "decay" of arrays does not always happen. See Exception to array not decaying into a pointer?

Solution 3 - C

'\0' is a number: 0, so it is evaluated as false (0 = false, !0 = true).

But "\0" is a pointer to a read-only section where the actual string is stored, the pointer is not NULL ergo it's true.

Solution 4 - C

First, looking at the two conditions, '\0' is a constant of type integer, which denotes the null character C, which is the same as 0. While "\0" is a string literal, which contains 2 bytes, the one specified and the null terminator byte implicitly added. Being a string literal, the pointer cannot be NULL.

Second, in C, for the condition of if statement, everything non-zero is evaluated as true, and zero is evaluated as false.

According to this rule, it will be clear that '\0' is false, and "\0" evaluated as true.

Solution 5 - C

First of all, please note that the hexadecimal value of False is 0x00 and True is any other value than 0x00.

"\0" is a string with a character and Null Terminator '\0' at the end. So it is a character pointer, pointing to an array of 2 bytes: ['\0', '\0']. In this array, the first one is the character and the other one is the null terminator.

After compiling (without optimizing), this character pointer is temporarily assigned to an address in the memory pointing to the first byte of these two bytes. This address might be, for example, 0x18A6 in hexadecimal. So the compiler (most of them) actually writes these two values to the memory. Because a string is actually the address of the first byte of that string, our expression is interpreted as 0x18A6 != false . So, it is clear 0x18A6 != 0x00 is True.

'\0' is simply 0x00 in hexadecimal. 0x00 != 0x00 is False.

This answer is written for 8-bit data architecture with 16-bit addressing. I hope that helps.

Solution 6 - C

'\0' is a null character which has the value of 0. It is used to terminate a string of characters. So it's consider false.

"\0" is a null or empty string. The only character in the string is the null character which terminates the string.So it's consider true.

Solution 7 - C

'\0' is a char that is equal to number zero. "\0" is a string and we usually add '\0' at the end of a string. Don't use '\0' or "\0" in a conditional statements because it's quite confusing.

The following usage is suggested:

if (array[0] != 0)
{

}

if (p != 0)
{

}

if (p != NULL)
{

}

Solution 8 - C

Check out this with examples..

#include <stdio.h> 

int main() 
{ 
printf( "string value\n" ); 

//the integer zero 
printf( "0.........%d\n" , 0 ); 

//the char zero, but chars are very small ints, so it is also an int 
//it just has some special syntax and conventions to allow it to seem 
//like a character, it's actual value is 48, this is based on the 
//ASCII standard, which you can look up on Wikipedia 
printf( "'0'.......%d\n" , '0' ); 

//because it is an integer, you can add it together, 
//'0'+'0' is the same as 48+48 , so it's value is 96 
printf( "'0'+'0'...%d\n" , '0'+'0' ); 

//the null terminator, this indicates that it is the end of the string 
//this is one of the conventions strings use, as a string is just an array 
//of characters (in C, at least), it uses this value to know where the array 
//ends, that way you don't have to lug around another variable to track 
//how long your string is. The actual integer value of '\0' is zero. 
printf( "'\\0'......%d\n" , '\0' ); 

//as stated, a string is just an array of characters, and arrays are tracked 
//by the memory location of their first index. This means that a string is 
//actually a pointer to the memory address that stores the first element of 
//the string. We should get some large number, a memory address 
printf( "\"0\".......%d\n" , "0" ); 

//a string is just an array of characters, so lets access the character 
//in position zero of the array. it should be the character zero, which 
//has an integer value of 48 
printf( "\"0\"[0]....%d\n" , "0"[0] ); 

//and the same thing for the empty string 
printf( "\"\\0\"[0]...%d\n" , "\0"[0] ); //equal to '\0' 

//we also said a string is just a pointer, so we should be able to access 
//the value it is pointing to (the first index of the array of characters) 
//by using pointers 
printf( "*\"0\"......%d\n" , *"0" ); 

return 0; 
}

Solution 9 - C

The simple thing is ATLEAST 0 (int) and 0.0 (float or double) have FALSE value in C.

'\0' is integer 0.

"\0" is an array of characters. It does not matter that INSIDE the array how many Characters are there or what are those characters.

So, '\0' evaluates to 0 like 77-77 evaluates to 0. And 0 is false.

int x; x = '\0'; printf("X has a value : %d"); Output:


x has a value : 0

And the code:

if(0){printf("true");}

else{printf("false");}

Output:


false

Solution 10 - C

We can clear above problem in two different concept of C

  1. Working of if( condition ) in C
  2. Difference of Character & String Literals in C

1. Working of if( condition ) in C if( condition )

In C Language, if condition works on 0(Zero) and Non-Zero base.

If the result of the given condition is Zero, then C consider that given condition is false.

If the result of the given condition is Non-Zero then C consider that given condition is true.

2. Difference of Character & String Literals in C

In C, String literals are those which enclosed in Double quotation marks (""), while Character literals are those which enclosed in Single quotation marks ('') and minimum length is one character and max length is two character.

Another important point is that in C, if we convert '\0' (null) to int (Integer), then we will get 0(Zero), while we cannot convert "\0" to int implicitly or explicitly. Because "\0" is string while '\0' is character.

And according to the string IF condition working logic, if condition returns 0 or false, it means condition is false; in case of condition returns non-zero, it means condition is true.

So, according to point 1 and 2 finally we can conclude that

if ('\0') printf("'\0' != false\n"); //condition becomes false

if ("\0") printf(""\0" != false\n"); //condition becomes true

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRahnView Question on Stackoverflow
Solution 1 - Cuser253751View Answer on Stackoverflow
Solution 2 - CSpikatrixView Answer on Stackoverflow
Solution 3 - CFedeWarView Answer on Stackoverflow
Solution 4 - CfluterView Answer on Stackoverflow
Solution 5 - CBoraView Answer on Stackoverflow
Solution 6 - CmscView Answer on Stackoverflow
Solution 7 - CG.MatherView Answer on Stackoverflow
Solution 8 - CKrishnaView Answer on Stackoverflow
Solution 9 - CBhartendu KumarView Answer on Stackoverflow
Solution 10 - CRaviView Answer on Stackoverflow