I\'m currently writing a little program but I keep getting this error when compiling
error: empty character constant
I realize i
You can use c[i]= '\0'
or simply c[i] = (char) 0
.
The null/empty char is simply a value of zero, but can also be represented as a character with an escaped zero.
Yes, c[i]=''
is not a valid code. We parenthesis character constant between '
'
, e.g. c[i] = 'A';
char A
. but you don't write any char in between ''
.
Empty space is nothing but suppose if you wants to assigned space then do:
c[i] = ' ';
// ^ space
if wants to assigned nul char
then do:
c[i] = '\0';
// ^ null symbol
Example: Suppose if c[]
a string (nul \0
terminated char array) if you having a string. for example:
char c[10] = {'a', '2', 'c', '\0'};
And you replace second char with space:
c[1] = ' ';
and if you print it using printf as follows:
printf("\n c: %s", c);
then output would be:
c: a c
// ^ space printed
And you replace second char with '\0':
c[1] = '\0';
then output would be:
c: a
because string terminated with \0
.
It might be useful to assign a null in a string rather than explicitly making some index the null char '\0'
. I've used this for testing functions that handle strings ensuring they stay within their appropriate bounds.
With:
char test_src[] = "fuu\0foo";
This creates an array of size 8 with values:
{'f', 'u', 'u', '\0', 'f', 'o', 'o', '\0'}