Hi, I am learning/practising C using the book of <<C Primer Plus>>. Got stuck at the exercise 702:
Write a program that reads input until encountering #. Have the program print eachinput character and its ASCII decimal code. Print eight character-code pairs per line.Suggestion: Use a character count and the modulus operator (%) to print a newlinecharacter for every eight cycles of the loop.
And this is my code:
Code:
#include <stdio.h>
#define SPACE ' '
#define NL '\n'
int main(void)
{
char ch;
int m;
m = 0;
printf("\nEnter your input and end with a "#":\n");
while ((ch = getchar()) != '#')
{
if (ch == NL)
continue;
if (ch == SPACE)
continue;
printf("%c:%d ", ch, ch);
m++;
if (m % 8 == 0)
printf("\n");
}
printf("\n\n");
return 0;
}
Compiled and ran. But whenever I hit the Return key during the input test even before I gave the # character to end the input, I got output shown on the screen. Baffled about it, anyone can help me to explain and give some hints? Many thanks.