120

I'm currently writing a little program but I keep getting this error when compiling

error: empty character constant

I realize it's because I'm trying to replace a valid char with empty space c[i]='' but I have not been able to find another way to represent it.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
  • 2
    There is no "empty space" (wrong terminology), although there may be an empty string. Do you want to replace `c[i]` with a *blank space*, or do you want to "eliminate" that character from the array as you would do in an editor when you press backspace (i.e. do you want the chars c[i-1] and c[i+1] to become adjacent after the operation)? – LorenzoDonati4Ukraine-OnStrike Aug 23 '13 at 19:26
  • 1
    If you understands strings then at the time of declaration you can as assign [`char c[5] = "";`](http://stackoverflow.com/questions/17790127/what-is-meant-by-char-temp3/17790207#17790207) empty string but `char c = '';` is not valid as below explained in 6 answers. – Grijesh Chauhan Aug 23 '13 at 19:34

10 Answers10

181

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.

ardent
  • 2,453
  • 1
  • 16
  • 15
  • 13
    Yes, and there is no "empty char". `'\0'` is the *null char* which has nothing to do with `NULL`; the null char is simply a char having 0 as numeric code. – LorenzoDonati4Ukraine-OnStrike Aug 23 '13 at 19:30
  • @LorenzoDonati I referred to it as the null character, the empty char was just to use OP's terminology. I've also clarified that in my edit. As chux notes, NULL isn't very good for non-pointer reference deals so I've made that note as well. – ardent Aug 23 '13 at 19:32
  • 1
    @LorenzoDonati Yeah, I've decided to remove it since it adds nothing to this answer and seems out of context for the discussion at hand. – ardent Aug 23 '13 at 19:40
  • @LorenzoDonati Not `null char` it should be `nul char` single `l`. – Grijesh Chauhan Aug 23 '13 at 20:03
  • 1
    @GrijeshChauhan it is called the null character or null terminating character, however it is abbreviated as `nul`. See https://tools.ietf.org/html/rfc20#section-5.2. Also it is referred to as the null character in the ANSI C standard as well. – ardent Aug 23 '13 at 20:17
  • 1
    @GrijeshChauhan and ardentsonata For a more specific reference, the C99 draft standard (N1256) defines that term in section 5.2.1 (Character sets), paragraph 2: "[...]A byte with all bits set to 0, called the *null character*, shall exist in the basic execution character set; it is used to terminate a character string." C/C++ terminology is always a bit nightmarish ;-) – LorenzoDonati4Ukraine-OnStrike Aug 23 '13 at 21:07
  • 5
    There is no such thing as an empty character. Calling the null char an empty character is simply wrong. – Christoffer Hammarström Jun 25 '15 at 10:01
  • If you are trying to clean invalid characters in a file path and use '\0' as the replacement character, you will still get invalid path exception. Because '\0' is not like empty string. It is a Null character which itself is invalid in file paths. – NthDeveloper Jun 13 '20 at 15:59
  • won't work. will output a hidden character instead. beware. – mjs Dec 05 '20 at 16:34
  • 1
    @mmm worse than that. Most string functions will stop at the first zero character they encounter, interpreting it as the end of the string. So you'll lose every part of the string to the right of it. – Mark Ransom Apr 01 '21 at 19:28
37

You can't store "no character" in a character - it doesn't make sense.

As an alternative you could store a character that has a special meaning to you - e.g. null char '\0' - and treat this specially.

occulus
  • 16,959
  • 6
  • 53
  • 76
  • 5
    I think this is a much better answer than the accepted one, which confuses null char and empty char. As an analogy, it's like saying you can't declare an int and store 'no number' - you must store _some_ value. – simonwo May 13 '15 at 09:42
  • 5
    You can have a null string `""` because a string is a sequence of zero or more chars, but a char is exactly one value - not zero and not more. – simonwo May 13 '15 at 09:42
15

The empty space char would be ' '. If you're looking for null that would be '\0'.

Claudiu
  • 224,032
  • 165
  • 485
  • 680
11

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.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • What if we have some characters after the '\0'. I mean suppose we want to remove a character in the string eg "sample" remove m. Isnt there just a way to replace it by some hypothetical empty character rather than removing it by left shift of character after it. – Himanshu Poddar Jan 28 '19 at 13:37
  • @HimanshuPoddar no it's not possible, because your hypothetical empty character doesn't exist. The closest we have is the Unicode character [U+200B Zero Width Space](https://unicode-table.com/en/200B/) but it can't be represented in an 8 bit char. – Mark Ransom Apr 01 '21 at 19:37
5

There is no such thing as the "empty character" ''.

If you need a space character, that can be represented as a space: c[i] = ' ' or as its ASCII octal equivalent: c[i] = '\040'. If you need a NUL character that's c[i] = '\0'.

verbose
  • 7,827
  • 1
  • 25
  • 40
4

To represent the fact that the value is not present you have two choices:

1) If the whole char range is meaningful and you cannot reserve any value, then use char* instead of char:

char** c = new char*[N];
c[0] = NULL; // no character
*c[1] = ' '; // ordinary character
*c[2] = 'a'; // ordinary character
*c[3] = '\0' // zero-code character

Then you'll have c[i] == NULL for when character is not present and otherwise *c[i] for ordinary characters.

2) If you don't need some values representable in char then reserve one for indicating that value is not present, for example the '\0' character.

char* c = new char[N];
c[0] = '\0'; // no character
c[1] = ' '; // ordinary character
c[2] = 'a'; // ordinary character

Then you'll have c[i] == '\0' for when character is not present and ordinary characters otherwise.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
4

There are two ways to do the same instruction, that is, an empty string. The first way is to allocate an empty string on static memory:

char* my_variable = "";

or, if you want to be explicit:

char my_variable = '\0';

The way posted above is only for a character. And, the second way:

#include <string.h>
char* my_variable = strdup("");

Don't forget to use free() with this one because strdup() use malloc inside.

Nick Cuevas
  • 1,474
  • 15
  • 10
2

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'}
Kyle s
  • 484
  • 5
  • 14
1

This is a case of single quotes and double quotes having different meanings.

"" is translated to (const char[1])"" by the compiler. This lets you use it in initializations of character arrays.

'' is not, and would be an unterminated empty string. Because you can't tell if a string is empty without terminating it, this is not valid code. Hence the error.

You almost certainly wanted to do

c[i] = '\0';

If c was indeed a text string, this sets the string's length to i by terminating it on that character.

If c was not actually intended as a text string, that's still the value you are suppose to use to mean that there's nothing there, because it's false, and any other character is true.

If you actually meant to put a space there, then you wanted

c[i] = ' ';
Andy Sukowski-Bang
  • 1,402
  • 7
  • 20
Mr. Beeblebrox
  • 186
  • 1
  • 5
0

String before = EMPTY_SPACE+TAB+"word"+TAB+EMPTY_SPACE;

Where EMPTY_SPACE = " " (this is String) TAB = '\t' (this is Character) String after = before.replaceAll(" ", "").replace('\t', '\0'); means after = "word"
Stefanidis
  • 61
  • 1
  • 6