1

"The function returns the character written as an unsigned char cast to an int or EOF on error": see Putchar. In order to do practice, I produced this program:

#include<stdio.h>

int main(void){
  for(putchar('1'); putchar('2'); putchar('3'))
    putchar('4');
  return 0;
}

It results in an infinite loop whit output ...432432432432432.... I cannot get the reason behind such a result. I expected the loop to print just the same number over and over. I am referring to the value of putchar('4'), where the character '4' should be promoted to the int 52. Why such promotion is not behaving as I expected?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Worice
  • 3,847
  • 3
  • 28
  • 49
  • 1
    put...char....what does the name mean? why not `putint()` then? – Sourav Ghosh Jul 13 '17 at 13:54
  • What?? Weird test with a weird expectation. – Eugene Sh. Jul 13 '17 at 13:54
  • put_char writes the ascii character, not the ascii code in decimal. – Malcolm McLean Jul 13 '17 at 13:55
  • @SouravGhosh I agree. I am looking for confirmations of my suspects. – Worice Jul 13 '17 at 13:58
  • 1
    And a simple search for the specification of `putchar` was too complicated to "confirm your suspects"? – too honest for this site Jul 13 '17 at 14:06
  • @Olaf I did, but I did a silly mistake. I forgot that, apart for (putchar('1'), putchar('2'); putchar('3') produce an output for every iteration of the loop. Should I delete the question? – Worice Jul 13 '17 at 14:10
  • @Worice: IMO, yes, you should delete it, because it is not really useful for further readers. As a recommendation: **always** watch out for side-effects. These are not only output, but also changing global variables, object contents, etc. – too honest for this site Jul 13 '17 at 14:15
  • @Olaf ok, thanks a lot for your suggestion. – Worice Jul 13 '17 at 14:17
  • The literal `'4'` has type `int`. It cannot be "promoted" to its own type. The value of `'4'` in the decimal format you're used to, is not guaranteed to be `52` (many computers today are based on ASCII and, in ASCII the value assigned to the character 4 is, in fact, 52). – pmg Jul 13 '17 at 14:18

3 Answers3

4

In your case, as per the loop policy,

  • putchar('1') is executed once.
  • putchar('2') is evaluated, is found TRUTHY, putchar('4'); is executed, and then putchar('3'); is executed.
  • back to previous step unless the putchar('2') is FALSY (which never happens).
  • Hence infinite loop, and every loop iteration includes three putchar statements.

Related to the question "....where the character '4' should be promoted to the int 52" , quoting C11, §7.21.7, (emphasis mine)

The fputc function writes the character specified by c (converted to an unsigned char) to the output stream pointed to by stream, [...]

and

The putchar function is equivalent to putc with the second argument stdout.

So, it's the character representation which will be printed, and the character representation of '4' is, well, 4.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • My error has been thinking that only putchar('4') should print the result. Thanks for such a brilliant explaination of the proceeding of such an unusual loop! – Worice Jul 13 '17 at 14:01
2

It does print 124324324324....because putchar(1) is executed at the beginning of the loop and never again.

Putchar prints char on the standard output, and return the same char casted to int. It does not print the char casted to int to the standard output. If you say: putchar('z'), it will print 'z'

putchar(2) at the beginning of each iteration and return the int value of the char '2' which happens to be non zero. This also means that the loop never ends.

putchar('3') is executed at the end of each iteration.

putchar('4') between the beginning and the end of each iteration. That is why you get that output.

Davide Spataro
  • 7,319
  • 1
  • 24
  • 36
0

Because putchar ('2') is always != 0 and you execute putchar('4') then action in for which is putchar('3) then condition check putchar('2') which always true and you go to the beginning of this loop

0___________
  • 60,014
  • 4
  • 34
  • 74