Conversion specification of %c

This is a discussion on Conversion specification of %c within the C Programming forums, part of the General Programming Boards category; Hi guys... I'm doing a simple program which can input 2 single character. Unfortunately, every time i run the program, ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    13

    Conversion specification of %c

    Hi guys...

    I'm doing a simple program which can input 2 single character. Unfortunately, every time i run the program, the second character cannot be input.

    code:
    Code:
    #include<conio.h>
    #include<stdio.h>
    
    int main()
    {  
        char character1;
        char character2;
        
        printf(" input first character : ");
        scanf("%c",&character1);
        printf("input second character: ");
        scanf("%c",&character2);              // the program cannot read this statement
    
        printf("exit");
        getch();
        return 0;
    }

    Anybody can explain to me, why these problem happen.....

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Because unlike nearly all other conversion formats, %c will READ the newline character.

    Instead of
    a<enter>
    b
    <enter>

    Try
    ab<enter>

    Generally, I prefer fgets() to read a whole line, then get what I need from the resulting buffer when it is in memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NTFS Specification
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-18-2009, 12:36 PM
  2. Expression parsing specification?
    By Mole42 in forum C Programming
    Replies: 4
    Last Post: 07-04-2008, 02:27 PM
  3. about exception specification
    By George2 in forum C++ Programming
    Replies: 1
    Last Post: 01-27-2008, 03:25 AM
  4. Incomplete type specification
    By New++ in forum C++ Programming
    Replies: 14
    Last Post: 12-20-2004, 07:51 AM
  5. Replies: 3
    Last Post: 08-03-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21