title: string literals in C terminated with %?
body: <p>I'm in the process of learning C and I ran into this weird problem with strings. I was playing around with strings and the different ways to declare strings and view their contents.</p>
  
  <p>When declaring a string by using a char array and then printing each element of the array, it produced the expected output. It printed each character, finishing with the null character.</p>
  
  <p>When declaring a string literal and printing out the contents of the resulting array, I noticed that a <code>%</code> character was appended on the string after the null byte.</p>
  
  <p>Where is this <code>%</code> coming from?</p>
  
  <p>The code is below. I compiled the code via;
  <code>$ gcc nameStuff.c -o nameStuff</code></p>
  
  <p>I tried this on both my own Mac machine as well as a remote Linux server at school and got the same output both times.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>char myName[] = {72, 97, 121, 100, 101, 110, 0};    // my name in ASCII values
  char *name = "Hayden";                              // my name as string literal
  
  printf("%s, size: %ld\n", myName, sizeof(myName));  // 
  printf("%s, size: %ld\n", name, sizeof(name));
  
  int length1 = sizeof(myName);
  int length2 = sizeof(name);
  int i;
  
  // prints char value of each element in ascii array
  for (i = 0; i &lt; length1; i++) {
      printf("%c,", myName[i]);
  }
  puts("");
  // prints int value of each element in ascii array
  for (i = 0; i &lt; length1; i++) {
      printf("%d,", myName[i]);
  }
  puts("\n");
  
  // prints char value of each element in string literal array
  for (i = 0; i &lt; length2; i++) {
      printf("%c,", name[i]);
  }
  puts("");
  // prints int value of each element in string literal array
  for (i = 0; i &lt; length2; i++) {
      printf("%d,", name[i]);
  }
  puts("");
  </code></pre>
  
  <p>Here's the output:</p>
  
  <pre><code>Hayden, size: 7
  Hayden, size: 8
  H,a,y,d,e,n,,
  72,97,121,100,101,110,0,
  
  H,a,y,d,e,n,,%,
  72,97,121,100,101,110,0,37,
  </code></pre>
  
  <p>I asked my professor about it, and he said it might be an issue with the code, but it's hard to mess up so few lines of simple code. Thanks in advance your consideration.</p>