Why does the "\0" is interpreted as integer by the compiler?
You are wrong. The string literal "\0"
has array type char[2]
. Arrays used in expressions with rare exceptions are implicitly converted to pointers to their initial elements. From the C23 Standard (6.3.2.1 Lvalues, arrays, and function designators):
3 Except when it is the operand of the sizeof operator, or typeof operators, or the unary & operator, or is a string literal used to initialize an array, an expression that has type "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.
On the other hand, if an object of an integer type used in expressions has a rank less than the rank of the type int
(or unsigned int
) then it is converted to the type int
(or unsigned int
) (the C23 Standard, 6.3.1.1 Boolean, characters, and integers):
2 The following may be used in an expression wherever an int or unsigned int may be used:
— An object or expression with an integer type (other than int or unsigned int) whose integer conversion rank is less than or equal to the rank of int and unsigned int.
...
If the original type is not a bit-precise integer type (6.2.5): if an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; 50) otherwise, it is converted to an unsigned int. These are called the integer promotions. All other types are unchanged by the integer promotions.
So in the expression with the equality operator in this if statement
if (*newChar == "\0")
the value of the left operand *newChar
having type char
is converted to the type int
due to the integer promotions while the right operand having array type is converted to a pointer to its initial element. The equality operator is not defined for such types of its operands. So the compiler issues the message.
As for this statement
printf("%d", "\0");
then it invokes undefined behavior because there is used invalid conversion specifier for an object of the type char *
(again due to the implicit conversion arrays to pointers to their initial elements).
From the C23 Standard (7.23.6.1 The fprintf function):
9 If a conversion specification is invalid, the behavior is undefined.
You may use either the conversion specifier s
if you want to output a string
printf("%s\n", "\0");
or the conversion specifier p
if you want to output the address of a string
printf("%p\n", ( void *)"\0");
In any case your function does not make sense. First of all it should be declared like
size_t stringlen( const char *newChar );
The function shall deal only with strings. Thus introducing the variable flag
and using this if statement
else if (flag == false)
{
return 0;
}
are wrong. Otherwise the function will have undefined behavior if the user will pass not a string.
Also your function in any case has undefined behavior if the user will pass an empty string due to this increment of the pointer
newChar++; // <===
printf("%c", newint);
if (*newChar == "\0")
{
flag = true;
}
printf("%s","\0");
And by "it returns..." do you mean "it outputs"? Please post a Minimal Reproducible Example, the shortest complete code that shows the problem. Aside:"\0"
is the same as""
."\0"
is the same as""
" No, the former has typechar[2]
while the latter has typechar[1]
char*
type.""
ischar [1]
, notchar *
. It will be converted tochar *
in some, but not all uses, but it is an array of 1char
, and that is its type.""
ischar [1]
. That is a fact. It is clearly specified in the C standard. You may want to construe it as equivalent tochar *
because it is often automatically converted to that, but it is not the same thing, and teaching students the wrong thing leads to misconceptions we see play out repeatedly in Stack Overflow.""
is not achar *
, and saying it it is a false statement that harms learning. “And those aren't types” is a false statement;char [2]
andchar [1]
are types. “They are the samechar*
type” is a false statement.