How does one represent the empty char?

前端 未结 9 1355
闹比i
闹比i 2020-12-22 18:06

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

error: empty character constant

I realize i

9条回答
  •  醉酒成梦
    2020-12-22 18:36

    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.

提交回复
热议问题