简体   繁体   中英

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

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.

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.

ÿ 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.

'y' with two dots over it is character 0xFF (in latin-1 - the default codepage for the console).

0xFF as a 8-bit signed value is -1.

Look for places where you're printing -1 as a char (or using -1 as a char and then printing it).

When you "construct your string in a loop", do you remember to properly terminate it with a '\\0' ?

If the loop assigns characters to a character array, the last array item should be '\\0' .

Ok, after seeing the code, you are terminating the string.

EDIT :

Looks like you are including the EOF character in your string. This is one case where the string is not terminated properly. You should check for an EOF in your if-else structure and handle it properly.

One other thing I noticed:

You are assigning an int c to a char result when returning from your function. The compiler should have warned you, that you are trying to put a larger data type into a smaller data type. Depending on what the purpose of the return value is, I would think about changing the return data type to int .

Your if -statement, which ends with an else putting the character into the buffer has two flaws:

  1. It does not filter out the EOF special "character", which signifies the end of the stream
  2. It does not check for buffer overrun by comparing i with the BUFLEN value.

The first problem is the cause of your ÿ character, when the stream ends, you add the EOF character to the buffer, then the loop terminates.

The fix is to put a clause into your if-else statement to filter that away, like this:

} else if (c != EOF) {
    buf[i] = c;
    i++;
}

The second problem you need to decide how to handle before fixing, but it should be fixed.

You do not properly null-terminate your string. If reading from fp never returns "del" or "\\n" and you reach EOF, there will be no null-terminator. You need to fix your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM