title: What is the best way to represent characters in C?
body: <p>I know that a <code>char</code> is allowed to be signed or unsigned depending on the implementation. This doesn't really bother me if all I want to do is manipulate bytes. (In fact, I don't think of the <code>char</code> datatype as a character, but a byte).</p>
  
  <p>But, if I understand, string literals are <code>signed char</code>s (actually they're not, but see the update below), and the function fgetc() returns <code>unsigned char</code>s casted into <code>int</code>. So if I want to manipulate characters, is it preferred style to use signed, unsigned, or ambiguous characters? Why does reading characters from a file have a different convention than literals?</p>
  
  <p>I ask because I have some code in c that does string comparison between string literals and the contents of files, but having a <code>signed char *</code> vs <code>unsigned char *</code> might really make my code error prone.</p>
  
  <p><strong>Update 1</strong></p>
  
  <p>Ok as a few people pointed out (in answers and comments) string literals are in fact <code>char</code> arrays, not <code>signed char</code> arrays. That means I really <em>should</em> use <code>char *</code> for string literals, and not think about whether they are signed or unsigned. This makes me perfectly happy (until I have to start making conversion/comparisons with unsigned chars).</p>
  
  <p>However the important question remains, how do I read characters from a file, and compare them to a string literal. The crux of which is the conversion from the <code>int</code> read using fgetc(), which explicitly reads an <code>unsigned char</code> from the file, to the <code>char</code> type, which is allowed to be either signed or unsigned.</p>
  
  <p>Allow me to provide a more detailed example.</p>
  
  <pre><code>int main(void)
  {
      FILE *someFile = fopen("ThePathToSomeRealFile.html", "r");
      assert(someFile);
  
      char substringFromFile[25];
      memset((void*)substringFromFile,0,sizeof(substringFromFile));
  
      //Alright, the real example is to read the first few characters from the file
      //And then compare them to the string I expect
      const char *expectedString = "&lt;!DOCTYPE";
  
      for( int counter = 0; counter &lt; sizeof(expectedString)/sizeof(*expectedString); ++counter )
      {
          //Read it as an integer, because the function returns an `int`
          const int oneCharacter = fgetc(someFile);
          if( ferror(someFile) )
              return EXIT_FAILURE;
          if( int == EOF || feof(someFile) )
              break;
  
          assert(counter &lt; sizeof(substringFromFile)/sizeof(*substringFromFile));
  
          //HERE IS THE PROBLEM:
          //I know the data contained in oneCharacter must be an unsigned char
          //Therefore, this is valid
          const unsigned char uChar = (const unsigned char)oneCharacter;
          //But then how do I assign it to the char?
          substringFromFile[counter] = (char)oneCharacter;
      }
  
      //and ultimately here's my goal
      int headerIsCorrect = strncmp(substringFromFile, expectedString, 9);
  
      if(headerIsCorrect != 0)
          return EXIT_SUCCESS;
      //else
      return EXIT_FAILURE;
  }
  </code></pre>
  
  <p>Essentially, I know my fgetc() function is returning something that (after some error checking) is code-able as an <code>unsigned char</code>. I know that <code>char</code> may or may not be an <code>unsigned char</code>. That means, depending on the implementation of the c standard, doing a cast to <code>char</code> will involve <em>no</em> reinterpretation. However, in the case that the system is implemented with a signed <code>char</code>, I have to worry about values that can be coded by an <code>unsigned char</code> that aren't code-able by <code>char</code> (i.e. those values between (INT8_MAX UINT8_MAX]). </p>
  
  <p>tl;dr</p>
  
  <p>The question is this, should I (1) copy their underlying data read by fgetc() (by casting pointers - don't worry, I know how to do that), or (2) cast down from <code>unsigned char</code> to <code>char</code> (which is only safe if I know that the values can't exceed INT8_MAX, or those values can be ignored for whatever reason)?</p>