title: Why does my C program print "(null)" during execution of tolower conversion of characters?
body: <p>I wrote this program to try to understand C a little better. It works, but for some reason, (null) is printed before the correct output. My code:</p>
  
  <pre><code>/* This program will take a string input input from the keyboard, convert the
  string into all lowercase characters, then print the result.*/
  
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;ctype.h&gt;
  
  int main()
  {
      int i = 0;
      /* A nice long string */
      char str[1024];                               
      char c;
      printf("Please enter a string you wish to convert to all lowercase: ");
  
      /* notice stdin being passed in for keyboard input*/
      fgets(str, 1024, stdin);  //beware: fgets will add a '\n' character if there is room       
  
      printf("\nYour entered string: \n\n%s\n", str);
      printf("\nString converted to lowercase: \n\n%s\n");
      while(str[i]) { //iterate str and print converted char to screen
          c = str[i];
          putchar(tolower(c));
          i++;
      }
      putchar('\n'); //empty line to look clean
      return 0;
  }
  </code></pre>
  
  <p>Incidentally, I noticed that when if my string variable is added to the last printf function, the problem goes away.</p>
  
  <p>Replace:</p>
  
  <pre><code>printf("\nString converted to lowercase: \n\n%s\n"); 
  </code></pre>
  
  <p>with</p>
  
  <pre><code>printf("\nString converted to lowercase: \n\n%s\n, str");
  </code></pre>
  
  <p>Here is a sample output showing the issue:</p>
  
  <pre><code>Please enter a string you wish to convert to all lowercase: THE QUICK BROWN FOX
  JUMPED OVER THE LAZY DOG.
  
  Your entered string:
  
  THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.
  
  
  String converted to lowercase:
  
  (null)
  the quick brown fox jumped over the lazy dog.
  
  Press any key to continue . . .
  </code></pre>