2

There are many questions about this exercise all over the internet, but I couldn't find any solution (nor any hint) on how to solve this exercise using 'putchar'.

Write a program to print the value of EOF.

I can easily get a working answer to this:

printf("%d", EOF);

I'd like to know if there are any known (or logical) answers using 'putchar' (as I guess that was the whole purpose of the exercise, being located at the end of a paragraph on 'getchar' and 'putchar')

Writing:

putchar(EOF);

or

int c;
c = EOF;
putchar(c);

the program just launches and closes itself without showing any text.

maja
  • 697
  • 5
  • 18
  • 1
    a character is printed here... But I'm not sure to understand what result you are expecting here. The value of EOF is an integer (-1 usually). – Maxime Chéramy Jan 28 '15 at 10:16
  • Perhaps you should use `putchar` to write the *decimal* value of EOF. – Jongware Jan 28 '15 at 10:31
  • _Why_ do you feel the need to "use `putchar`" here? – Lightness Races in Orbit Jan 28 '15 at 10:35
  • 1
    Spoiler: http://stackoverflow.com/q/11975780/1025391 – moooeeeep Jan 28 '15 at 10:36
  • @ Jongware treating EOF as a float just returns some gibberish @Lightness _Races_in_Orbit I asked just to make sure (as I wrote, the exercise is presented in a section about putchar) – maja Jan 28 '15 at 10:54
  • But "numerical" does not automatically imply "float"! (Although you may be doing something wrong. The usual value of EOF, `-1`, is a *perfectly fine value* for a float as well.) – Jongware Jan 28 '15 at 17:21
  • Here are a few examples using different types in printf: `%d` -> `-1`; `%f` -> `1.#NAN0`; `%s` -> [void]; `%x` -> `ffffffff` – maja Jan 29 '15 at 03:28
  • declaring c to be `int`, `double`, `long`, `short`, `float`, `char` and then equationg c to the value of EOF all lead to an empty output – maja Jan 29 '15 at 03:31
  • sorry, equating (`==`) c to `EOF` gives `int` -> `Ç`; `long` -> `Ç`; `short` -> `@`; `float` -> [void]; `char` -> [void] – maja Jan 29 '15 at 03:39
  • (the last two comments meant "declaring c and then calling it with putchar" – maja Jan 29 '15 at 03:48

4 Answers4

2

EOF is not a char which can be printed as you expected. So putchar(EOF) doesn't print any values, as it prints only char.

Hence use printf("%d", EOF) which outputs integer -1.

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
2

putchar converts its argument to unsigned char before it's outputted (and it's a character which is written, not the result of a decimal conversion). As EOF is negative, this conversion isn't value-preserving. Commonly, EOF has the value -1, with 8-bit char, that conversion results in 255, that is, putchar(EOF) is equivalent to putchar('\xff'), which isn't a printable character (assuming an ASCII system). Piping the output to hexdump -C would make the output visible.

Without using printf and friends, a function outputting a decimal number for an int can be written. Basically,

print a '-' if the value is negative
for every decimal digit, starting from the highest-valued:
    convert the digit's value to a character (e.g. 7 should become '7')
    try to print the result (with putchar, for example)
    if that fails, return an error indicator

Hint: The division and modulus operators, / and %, will be useful.

The digits '0' ... '9' are ascending (and contiguous); so for the conversion from the digit's value to a character, adding '0' yields the desired result (3 + '0' is '3', for example).

Care must be taken to avoid integer overflow, even if corner cases like INT_MIN are passed to the function. -INT_MIN may result in an overflow (and in fact does on pretty much every system), a positive number, on the other hand, can always be negated. A value of 0 may need special handling.

(Alternatively, the number can be converted to a string first which then can be outputted. A char array of size 1 + sizeof(int)*CHAR_BIT/3+1 + 1 is big enough to hold that string, including minus sign and 0-terminator.)

If you get stuck, the (non-standard) itoa function does something similar, looking for example implementations may give some ideas.

mafso
  • 5,433
  • 2
  • 19
  • 40
1

As per the man page of putchar(),

int putchar(int c);

putchar(c); is equivalent to putc(c,stdout).

and

putc() is equivalent to fputc() .......fputc() writes the character c, cast to an unsigned char, to stream.

This, putchar() is supposed to output the char representation of the ASCII value supplied as it's argument.

So, if the value of EOF is a non printable character [in ASCII], you won't see anything ["without showing any text"] on stdout.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

putchar(int char) So if you pass a ASCII value to this API you get a corresponding character printed on the screen but EOF evaluates to -1 which is not a printable ASCII character so you don't see anything or might see some junk.

Gopi
  • 19,784
  • 4
  • 24
  • 36