[Solved] Why doesn’t the first letter of my string print?


There are multiple issues in the code fragment:

  • The ch = scanf("%c", &ch); serves no purpose and does not even properly read a byte from the file into ch. Note that scanf does store the byte into ch, but it is immediately overwritten as you store the return value into ch as well. Just remove this line.

  • while ((ch = getchar()) != EOF) { printf("%c\n", ch); } is correct for your purpose but the type of ch must be int instead of char to accommodate all values of unsigned char and the special value EOF. As currenly written, your code will fail to stop at end of file on platforms where char is an unsigned type.

3

solved Why doesn’t the first letter of my string print?