title: Garbage characters in C
body: <h1>Edited question</h1>
  <p>I understood my mistake in the code I had given in the original question, and the characters I was getting are garbage characters. Although, I still have a few questions about garbage characters in C:</p>
  <ul>
  <li><p>Why can't the character <a href="https://i.stack.imgur.com/Q8uKM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Q8uKM.png" alt="enter image description here" /></a> be copied?</p>
  </li>
  <li><p>Do garbage characters have some pattern? Meaning that can you predict for an empty string what character can come, for an empty integer what will come, and so on.</p>
  </li>
  <li><p>When a variable is declared, why does it have a garbage character instead of being blank? Is there a specific reason of storing it with a garbage character?</p>
  </li>
  <li><p>For a string which is not null-terminated, will the same garbage character be printed on every OS? If yes, which one?</p>
  </li>
  <li><p>Are there the same garbage characters on every OS? Or are they different?</p>
  </li>
  <li><p>Is there a way to print these characters on the <code>stdout</code> buffer in C / C++?</p>
  </li>
  <li><p>If you see carefully in the character <a href="https://i.stack.imgur.com/5Y6ge.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5Y6ge.png" alt="enter image description here" /></a>, there are some characters and numbers in it. Do they represent something?</p>
  </li>
  <li><p>Is there a list of garbage characters which can be printed in C / C++?</p>
  </li>
  </ul>
  <hr />
  <hr />
  <h1>Original Question</h1>
  <blockquote>
  <p><strong>Title of original question:</strong> Mysterious character output in C</p>
  </blockquote>
  <p>I had come across this code in K &amp; R:</p>
  <pre><code>int scanline (char str [], int lim)                                                     /* Line will be read in 'str []', while lim is the maximum characters to be read */
  {
      int c, len, j;                                                                      /* 'len' will have the length of the read string */
  
      j = 0;                                                                              /* Initializing 'j' */
      for (len = 0; (c = getchar ()) != EOF &amp;&amp; c != '\n'; ++len)                          /* Reading a character one by one, till the user enters '\n', and checking for failure of 'getchar' */
      {
          if (len &lt; (lim -2))                                                             /* Checking that string entered has not gone beyond it's boundaries. '-2' for '\n' and '\0' */
          {
             str [j] = c;                                                                 /* Copying read character into 'string [j]' */
             ++ j;                                                                        /* Incrementing 'j' by 1 */
          }
      }
      if (c == '\n')                                                                      /* Checking if user has finished inputting the line */
      {
          str [j] = c;                                                                    /* Copying newline into string */
          ++j;
          ++ len;
      }
  
      return len;                                                                         /* Returning number of characters read */
  }
  </code></pre>
  <p>In the K &amp; R, it is known as <code>getline</code>, but I made changes, added comments, and thus defined it as <code>scanline</code>. To test this, I made a demo program:</p>
  <pre><code>#include &lt;mocl/cancel.h&gt;
  
  int main (int argc, char **argv)
  {
      int len;
      char str [50];
      len = scanline (str, 50);
      printf (&quot;len = %d\n str = %s\n&quot;, len, str);
      return 0;
  }
  </code></pre>
  <p>The required headers and the function was in my own library, <code>cancel.h</code>. Then when I compiled my program, it was successful. Although, when I ran the executable, I got unexpected output (I cannot type it as I get a character which when I copy, it just gets pasted as 'm'):</p>
  <p><a href="https://i.stack.imgur.com/gJzPb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gJzPb.png" alt="enter image description here" /></a></p>
  <p>The mysterious character is <a href="https://i.stack.imgur.com/IWP35.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IWP35.png" alt="enter image description here" /></a> which when I copy, gets copied as the letter <code>m</code>. Also, when I run my program with different inputs, I get different mysterious outputs:</p>
  <p><a href="https://i.stack.imgur.com/UKqpI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UKqpI.png" alt="enter image description here" /></a></p>
  <p>In another case, I get perfect output, just that a blank line is printed:</p>
  <p><a href="https://i.stack.imgur.com/8EKne.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8EKne.png" alt="enter image description here" /></a></p>
  <p>I had also come across <a href="https://stackoverflow.com/questions/35651800/what-is-this-mysterious-output-in-c">this</a> question, in which the user gets the same symbol.</p>
  <hr />
  <p><strong><em>What have I done till now?</em></strong></p>
  <p>I searched a lot, and I could not find any clue about <a href="https://i.stack.imgur.com/EbMX9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EbMX9.png" alt="enter image description here" /></a> this character, but if you see carefully, in the second image, I get more characters when I enter &quot;hi this is ashish&quot;. One of them is the slash, and one is <a href="https://i.stack.imgur.com/h0yCq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/h0yCq.png" alt="enter image description here" /></a>. But I get another character <a href="https://i.stack.imgur.com/qu9oW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qu9oW.png" alt="enter image description here" /></a>. I got <a href="https://rosettacode.org/wiki/Strip_control_codes_and_extended_characters_from_a_string#C" rel="nofollow noreferrer">this</a> link which was showed how to reproduce it, and explained it, although I could not understand. When you run the code given there, you get a lot of characters, and one of them is <a href="https://i.stack.imgur.com/8XL0F.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8XL0F.png" alt="enter image description here" /></a>. Although, even the author of that article could not copy it, and has not posted it. So here's the output:</p>
  <p><a href="https://i.stack.imgur.com/SxxxQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SxxxQ.png" alt="enter image description here" /></a></p>
  <p>That was the actual output, as that's not clear, here's a cut out version:</p>
  <p><a href="https://i.stack.imgur.com/dW876.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dW876.png" alt="enter image description here" /></a></p>
  <p>So basically I got to know that both the characters <a href="https://i.stack.imgur.com/RfafD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RfafD.png" alt="enter image description here" /></a> and <a href="https://i.stack.imgur.com/W5Wam.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/W5Wam.png" alt="enter image description here" /></a> are extended characters from a string. At that point, I actually figured out what was causing the problem in <code>scanline</code>.</p>
  <p>The lines</p>
  <pre><code>if (c == '\n')                                                                      /* Checking if user has finished inputting the line */
  {
      str [j] = c;                                                                    /* Copying newline into string */
       ++j;
       ++ len;
  }
  </code></pre>
  <p>were causing the problems as you were copying a newline into the string. It worked, but I'm not sure why, as doing that was just a guess. I searched but still could not find the reason.</p>
  <hr />
  <p><strong><em>My Questions</em></strong></p>
  <ul>
  <li><p>How does removing those lines make the program work properly?</p>
  </li>
  <li><p>What are the characters <a href="https://i.stack.imgur.com/MVrgF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MVrgF.png" alt="enter image description here" /></a> and <a href="https://i.stack.imgur.com/ecu9R.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ecu9R.png" alt="enter image description here" /></a>? What are they supposed to do and how did they appear over here?</p>
  </li>
  <li><p>Are there any more characters like this?</p>
  </li>
  <li><p>Why can't those characters be copied?</p>
  </li>
  <li><p>Is it Undefined Behavior?</p>
  </li>
  </ul>