0

I am not getting ASCII code of white-space. Please fix and explain it too...

#include <stdio.h>
void main()
{
    char choice,ch;
    printf ("Press 'a' to find ASCII code of character: ");
    scanf ("%c",&choice);
   switch(choice)
   {
        case 'a':
            printf ("Enter the character: ");
            scanf (" %c",&ch);
            printf ("The ASCII code of character %c is %d",ch,ch);
        break;
        default:
            printf ("Please enter right key");
        break;
    }
}
willsir
  • 19
  • 1
  • 7
  • 1
    `scanf` eats whitespace characters. Don't use `scanf`, like, ever. – zwol Feb 09 '18 at 15:23
  • void main() is not standard C. – machine_1 Feb 09 '18 at 15:23
  • @machine_1 That's true but it also has nothing to do with OP's problem. For all we know they are using a compiler where that is an officially supported alternative declaration for `main`. – zwol Feb 09 '18 at 15:24
  • 1
    @zwol scanf eats whitespace characters?? I don't understand that...please explain – willsir Feb 09 '18 at 15:29
  • 2
    @willsir Almost all `scanf` formats discard leading whitespace from the input. %c is an exception, but you have `scanf(" %c")` which explicitly asks for whitespace to be skipped. See the linked Q&A for what you should be doing instead (namely using `fgets`). – zwol Feb 09 '18 at 15:42

0 Answers0