Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'y' in the output stand for in C?

Tags:

c

string

I have a problem, I construct a string in a loop and the output of that string to stout displays the string and a character 'y' with two dots above it as the last character.

What is that?

I create the string in this function:

char get_string(char *buf, int ble, FILE *fp, char del)
{
    int i = 0;
    int c;
    char    result;

    memset(buf, 0, BUFLEN);

    do {

        c = fgetc(fp);

        if (c == del) {
            buf[i] = '\0';
            result = c;
            break;
        } else if(c == '\n') {
            buf[i] = '\0';
            result = '\n';
            break;
        } else {
            buf[i] = c;
            i++;
        }

    } while (c != EOF);

    return result;
}

and then use the buf and result as follows in another function:

char    pair[BUFLEN];
char    end;

do {

        end = get_string(pair, BUFLEN, fp, ';');
        printf("Result: %s\n",pair);

} while (pair != NULL);

The last iteration of the above Prints out "Result: y" I have no idea why.

like image 538
goe Avatar asked Nov 15 '09 17:11

goe


2 Answers

You're using a do-while loop, which means that you're executing the loop body before testing for EOF, so you end up putting EOF in your buffer as well. The EOF value of -1 gets translated into the character range where it corresponds to ÿ. I'd recommend you to just switch to a more usual while loop because it handles this condition more naturally.

like image 79
JaakkoK Avatar answered Oct 22 '22 03:10

JaakkoK


ÿ is the glyph for the character which (in Unicode and many ISO-8859-? encodings) has the ordinal value 0xFF. That value, also known in decimal as 255, is also used in some contexts as "the end-of-file character" (aka EOF) -- although there's no standard that defines the character as such (AFAIK), the value -1 is what does get returned in many languages (such as C) when you try to read more from a file that's exhausted ("at end of file").

In practice, therefore, an unexpected ÿ in your output often means that you are erroneously interpreting a byte that's intended to signify "end of something" (a byte encoded with all bits set to one) as if it was part of the text to display.

like image 34
Alex Martelli Avatar answered Oct 22 '22 02:10

Alex Martelli