Skip to main content
Commonmark migration
Source Link

There is a bug common to all 5 versions: we should always test whether the read was successful.

  • For scanf(): did it return 1, when we asked for one conversion?

    For scanf(): did it return 1, when we asked for one conversion?

  • For getchar(): did it return EOF?

    For getchar(): did it return EOF?

    Note, in particular, that narrowing the result of getchar() (which is int) to an unsigned char loses the ability to distinguish EOF from a valid character.

Note, in particular, that narrowing the result of getchar() (which is int) to an unsigned char loses the ability to distinguish EOF from a valid character.

Here's a corrected version:

#include <stdio.h>

int main(void)
{
    int d;
    while ((d = getchar()) != EOF) {
        if (d != '\n')
            printf("%d\n", d * d);
    }
}

Finally, a pedantic note: this will only work on ASCII values when run on an ASCII system/locale. In other words, C always uses the environment's character coding, and does not convert input to a fixed representation as done by, say, Java.

There is a bug common to all 5 versions: we should always test whether the read was successful.

  • For scanf(): did it return 1, when we asked for one conversion?
  • For getchar(): did it return EOF?

Note, in particular, that narrowing the result of getchar() (which is int) to an unsigned char loses the ability to distinguish EOF from a valid character.

Here's a corrected version:

#include <stdio.h>

int main(void)
{
    int d;
    while ((d = getchar()) != EOF) {
        if (d != '\n')
            printf("%d\n", d * d);
    }
}

Finally, a pedantic note: this will only work on ASCII values when run on an ASCII system/locale. In other words, C always uses the environment's character coding, and does not convert input to a fixed representation as done by, say, Java.

There is a bug common to all 5 versions: we should always test whether the read was successful.

  • For scanf(): did it return 1, when we asked for one conversion?

  • For getchar(): did it return EOF?

    Note, in particular, that narrowing the result of getchar() (which is int) to an unsigned char loses the ability to distinguish EOF from a valid character.

Here's a corrected version:

#include <stdio.h>

int main(void)
{
    int d;
    while ((d = getchar()) != EOF) {
        if (d != '\n')
            printf("%d\n", d * d);
    }
}

Finally, a pedantic note: this will only work on ASCII values when run on an ASCII system/locale. In other words, C always uses the environment's character coding, and does not convert input to a fixed representation as done by, say, Java.

Passing unsigned char* is okay, apparently.
Source Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308

There are some bugsis a bug common to all 5 versions: we should always test whether the read was successful.

  • It's undefined behaviour to pass a pointer toFor unsigned charscanf() when using: did it return %c1; in practice, you'll usually get away with thiswhen we asked for one. conversion?
  • We should always test whether the read was successful. ForFor getchar(): did it return EOF? For scanf(): did it return 1, when we asked for 1 conversion?

Note, in particular, that narrowing the result of getchar() (which is int) to an unsigned char loses the ability to distinguish EOF from a valid character.

Here's a corrected version:

#include <stdio.h>

int main(void)
{
    int d;
    while ((d = getchar()) != EOF) {
        if (d != '\n')
            printf("%d\n", d * d);
    }
}

Finally, a pedantic note: this will only work on ASCII values when run on an ASCII system/locale. In other words, C always uses the environment's character coding, and does not convert input to a fixed representation as done by, say, Java.

There are some bugs common to all 5 versions:

  • It's undefined behaviour to pass a pointer to unsigned char when using %c; in practice, you'll usually get away with this one.
  • We should always test whether the read was successful. For getchar(): did it return EOF? For scanf(): did it return 1, when we asked for 1 conversion?

Note, in particular, that narrowing the result of getchar() (which is int) to an unsigned char loses the ability to distinguish EOF from a valid character.

Here's a corrected version:

#include <stdio.h>

int main(void)
{
    int d;
    while ((d = getchar()) != EOF) {
        if (d != '\n')
            printf("%d\n", d * d);
    }
}

Finally, a pedantic note: this will only work on ASCII values when run on an ASCII system/locale. In other words, C always uses the environment's character coding, and does not convert input to a fixed representation as done by, say, Java.

There is a bug common to all 5 versions: we should always test whether the read was successful.

  • For scanf(): did it return 1, when we asked for one conversion?
  • For getchar(): did it return EOF?

Note, in particular, that narrowing the result of getchar() (which is int) to an unsigned char loses the ability to distinguish EOF from a valid character.

Here's a corrected version:

#include <stdio.h>

int main(void)
{
    int d;
    while ((d = getchar()) != EOF) {
        if (d != '\n')
            printf("%d\n", d * d);
    }
}

Finally, a pedantic note: this will only work on ASCII values when run on an ASCII system/locale. In other words, C always uses the environment's character coding, and does not convert input to a fixed representation as done by, say, Java.

Source Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308

There are some bugs common to all 5 versions:

  • It's undefined behaviour to pass a pointer to unsigned char when using %c; in practice, you'll usually get away with this one.
  • We should always test whether the read was successful. For getchar(): did it return EOF? For scanf(): did it return 1, when we asked for 1 conversion?

Note, in particular, that narrowing the result of getchar() (which is int) to an unsigned char loses the ability to distinguish EOF from a valid character.

Here's a corrected version:

#include <stdio.h>

int main(void)
{
    int d;
    while ((d = getchar()) != EOF) {
        if (d != '\n')
            printf("%d\n", d * d);
    }
}

Finally, a pedantic note: this will only work on ASCII values when run on an ASCII system/locale. In other words, C always uses the environment's character coding, and does not convert input to a fixed representation as done by, say, Java.