How does one represent the empty char?

C

C Problem Overview


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.

C Solutions


Solution 1 - C

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.

Solution 2 - C

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.

Solution 3 - C

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

Solution 4 - C

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.

Solution 5 - C

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'.

Solution 6 - C

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.

Solution 7 - C

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.

Solution 8 - C

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'}

Solution 9 - C

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"

Solution 10 - C

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] = ' ';

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
Questionuser2494770View Question on Stackoverflow
Solution 1 - CardentView Answer on Stackoverflow
Solution 2 - CocculusView Answer on Stackoverflow
Solution 3 - CClaudiuView Answer on Stackoverflow
Solution 4 - CGrijesh ChauhanView Answer on Stackoverflow
Solution 5 - CverboseView Answer on Stackoverflow
Solution 6 - CBartoszKPView Answer on Stackoverflow
Solution 7 - CNick CuevasView Answer on Stackoverflow
Solution 8 - CKyle sView Answer on Stackoverflow
Solution 9 - CStefanidisView Answer on Stackoverflow
Solution 10 - CMr. BeeblebroxView Answer on Stackoverflow