Thread: Reading Blank Char

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Reading Blank Char

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    main()
    {
          char a,b;
          printf ("Enter the first character: ");
          scanf ("%c",&a);
          printf ("Enter the second character: ");
          scanf ("%c",&b);
          printf ("\n");
          if (a>b)
             printf ("a is larger than b");
          getch();
    }
    Hey,
    {Java-programmer, new to C, read tutorials, Googled, FAQed)

    Problem: Run the code, and the software automatically assigns blank character to b {i think ASCII = 10}.
    It doesn't even give me the chance to insert a value to b!
    Why?
    Thank for helping.
    Last edited by godingly; 03-03-2010 at 10:02 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    add this:

    getchar();

    right after your first scanf(). That will remove the newline from the keyboard buffer, and allow scanf()'s second call, to go as you want.

    And welcome!

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can also add a space in front of the %c for the second scanf to tell it to ignore leading whitespace.
    Code:
    printf ("Enter the second character: ");
    scanf (" %c",&b);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    Thanks both of you!
    {fully answered}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. Replies: 6
    Last Post: 06-30-2005, 08:03 AM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM