Reads input until encountering # and print each input character and its ASCII decimal code. - C String

C examples for String:String Console Input

Introduction

Print eight character-code pairs per line.

Demo Code

#include <stdio.h>
#define STOP '#'/*from  w ww . j a  va  2 s . com*/

#define SPACE ' '
#define TAB '\t'
#define NEWLINE '\n'
#define BACKSPACE '\b'

int main(void)
{
  unsigned int count = 0;
  char ch;

  printf("Enter input (%c to stop):\n", STOP);

  while ((ch = getchar()) != STOP){
    switch (ch){
      case SPACE :
          printf("' ': %3d ", ch);
          break;
      case TAB :
          printf("'\\t': %3d ", ch);
          break;
      case NEWLINE :
          printf("'\\n': %3d ", ch);
          break;
      case BACKSPACE :
          printf("'\\b': %3d ", ch);
          break;
      default:
          printf(" %c : %3d ", ch, ch);
    }
    count++;
    if (count % 8 == 0)
      printf("\n");
  }
  return 0;
}

Result


Related Tutorials