C - Limit the string length
<p>(Sorry for my bad english !)</p>

<p>I wrote a program that asks you to type a password no longer than a certain number, eight characters in this case. The characters that pass the limit will be cut out from the array:</p>

<pre><code>#include &lt;stdio.h&gt;
#define MAXCHAR 8

main()
{
    char password[MAXCHAR];
    int i;
    char c;

    printf("Insert password: MAX 8 CHARS!\n\n");
    for(i = 0; i &lt;= MAXCHAR; i++){
        c = getchar();

        if(i == MAXCHAR){
            break;
        }
        else{
            password[i] = c;
        }
    }

    printf("%s\n", password);
}
</code></pre>

<p>So the program works BUT there is a "strange" problem. If the limit IS EIGHT and I type a password longer than eight characters 
(Example: P455w0rds98) 
the output will be like this:</p>

<pre><code>P455w0rd☺
</code></pre>

<p>So it puts a smiley at the end and I don't know why. It happens only if a the limit is established at eight. </p>