Thread: Why can’t you initialize a char to empty?

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    2

    Why can’t you initialize a char to empty?

    I simply wanted to do this:

    char y = “ “;

    It wouldn’t compile using clang and c++14. I got the error ‘cannot initialize a variable of type ‘char’ with an Ivalue of type ‘const char’ ‘

    1. What’s the default value of a char? Is it random just like int?

    2. How should you initialize a char?

    3. Can you please explain what an Ivalue is?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Double quotes make a string. Single quotes make a char. Remember that distinction, it's important.

    1) C++ has no default values for the fundamental types. Only exception: When global, they are initialized to numerical zero.

    2) However you wish, provided that it suits your needs and that it doesn't break your algorithms.

    3) "lvalue" and "rvalue" are C++ jargon that describe the operands of an assignment. "lvalue" stands for "left value", meaning the operand on the left side of the assignment. "lvalues" must not be constant (otherwise, how could you assign to them). "rvalue" stands for "right value", meaning the operand on the right side. "rvalues" can be anything from constants, to variables. An rvalue is usually associated with the temporary result of an operation, such as:
    Code:
    a = b*3 + 2;
    in this example, a is an "lvalue" while "b*3 + 2" is an "rvalue".
    Devoted my life to programming...

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by GReaper View Post
    1) C++ has no default values for the fundamental types. Only exception: When global, they are initialized to numerical zero.
    When static they are initialised to 0 also

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    2) All of the following initialize a char variable to 0:
    Code:
    char a{};
    char b = '\0';
    char c = 0;
    char d = char();
    char e(0);
    char f('\0');
    char g{0};
    char h{'\0'};
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Apr 2017
    Posts
    2
    Thanks

  6. #6
    Registered User
    Join Date
    Feb 2018
    Posts
    4
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initialize char array with string
    By monik in forum C++ Programming
    Replies: 7
    Last Post: 04-15-2016, 09:35 PM
  2. Initialize 2 dimensional char array
    By skiabox in forum C Programming
    Replies: 4
    Last Post: 11-01-2010, 06:59 AM
  3. help with simple initialize 2-d char array
    By LightYear in forum C Programming
    Replies: 12
    Last Post: 04-26-2010, 09:59 PM
  4. how to initialize char *
    By vignesh in forum C Programming
    Replies: 3
    Last Post: 03-30-2009, 01:17 AM
  5. how to initialize a char matrix with empty elements?
    By kobra_swe in forum C Programming
    Replies: 3
    Last Post: 08-22-2006, 05:28 AM

Tags for this Thread