Mysterious character output in C : I had come across this code in K & R:

    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 && c != '\n'; ++len)                          /* Reading a character one by one, till the user enters '\n', and checking for failure of 'getchar' */
        {
            if (len < (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 */
    }

In the K & R, it is known as `getline`, but I made changes, added comments, and thus defined it as `scanline`. To test this, I made a demo program:

    #include <mocl/cancel.h>
    
    int main (int argc, char **argv)
    {
        int len;
        char str [50];
        len = scanline (str, 50);
        printf ("len = %d\n str = %s\n", len, str);
        return 0;
    }
    
The required headers and the function was in my own library, `cancel.h`. 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'):

[![enter image description here][1]][1]

The mysterious character is [![enter image description here][2]][2] which when I copy, gets copied as the letter `m`. Also, when I run my program with different inputs, I get different mysterious outputs:

[![enter image description here][3]][3]

In another case, I get perfect output, just that a blank line is printed:

[![enter image description here][4]][4]

I had also come across [this](http://stackoverflow.com/questions/35651800/what-is-this-mysterious-output-in-c) question, in which the user gets the same symbol. 

------------

**_What have I done till now?_**

I searched a lot, and I could not find any clue about [![enter image description here][5]][5] this character, but if you see carefully, in the second image, I get more characters when I enter "hi this is ashish". One of them is the slash, and one is [![enter image description here][6]][6]. But I get another character [![enter image description here][7]][7]. I got [this](https://rosettacode.org/wiki/Strip_control_codes_and_extended_characters_from_a_string#C) 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 [![enter image description here][8]][8]. Although, even the author of that article could not copy it, and has not posted it. So here's the output:

[![enter image description here][9]][9]

That was the actual output, as that's not clear, here's a cut out version:

[![enter image description here][10]][10]

So basically I got to know that both the characters [![enter image description here][11]][11] and [![enter image description here][12]][12] are extended characters from a string. At that point, I actually figured out what was causing the problem in `scanline`. 

The lines 

    if (c == '\n')                                                                      /* Checking if user has finished inputting the line */
    {
        str [j] = c;                                                                    /* Copying newline into string */
         ++j;
         ++ len;
    }

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. 

---------------

**_My Questions_**

 - How does removing those lines make the program work properly?

 - What are the characters [![enter image description here][13]][13] and [![enter image description here][14]][14]? What are they supposed to do and how did they appear over here?

 - Are there any more characters like this?

 - Why can't those characters be copied? 

 - Is it Undefined Behavior?

**NOTE:** I know this questions lengthy, but I'll make sure and offer a bounty once that "question eligible for bounty in 2 days" gets over. 
        


  [1]: http://i.stack.imgur.com/gJzPb.png
  [2]: http://i.stack.imgur.com/IWP35.png
  [3]: http://i.stack.imgur.com/UKqpI.png
  [4]: http://i.stack.imgur.com/8EKne.png
  [5]: http://i.stack.imgur.com/EbMX9.png
  [6]: http://i.stack.imgur.com/h0yCq.png
  [7]: http://i.stack.imgur.com/qu9oW.png
  [8]: http://i.stack.imgur.com/8XL0F.png
  [9]: http://i.stack.imgur.com/SxxxQ.png
  [10]: http://i.stack.imgur.com/dW876.png
  [11]: http://i.stack.imgur.com/RfafD.png
  [12]: http://i.stack.imgur.com/W5Wam.png
  [13]: http://i.stack.imgur.com/MVrgF.png
  [14]: http://i.stack.imgur.com/ecu9R.png