tags:

views:

66

answers:

4
#include <stdio.h>

int main()
{
    char TurHare; 

    while(1)
    {
        scanf("%c",TurHare);
     printf("\nCharacter :%c", TurHare);
 }
    return 0;
}

When I compile and then run the program the output is like :

w
Character : w
Character : 

where w is the input from console.

It should appear like:

w
Character : w

How to do it?

+2  A: 

You missed &.

retry with

int main()
 {
char TurHare;   

    while(1)
    {
    scanf("%c",&TurHare);
    printf("\nCharacter :%c", TurHare);
    }

return 0;
} 

I recommend getch,getche,getchar to use in case of character ,scanf will lead you to some buffering problem

org.life.java
D.J.
try getch(),getchar(),getche() scanf shows some issues with string and characters. char c = getchar();
org.life.java
yes when you press enter it queue up the enter key in stdin and next time it fetches in from stdin thus it doesn't work as it should
org.life.java
+1  A: 

Ok so Its because of return key which you enter after entering w. so once it reads w and other time it read the end of line character.

Arjit
A: 

Your program does what you tell it to do, it outputs the characters you type.

Now when you input w , look at what you're doing. You're hitting 2 keys. the w key , and Enter . That's the output you get, w and a newline(from the enter key). If you don't want that, do e.g.

char TurHare; 

while(1)
{
    if(scanf("%c",&TurHare) != 1) { //always check for errors
       break; //or some other error handling
    }

    if(c != '\n') { //or perhaps if(!isspace(c)) from <ctype.h>
      printf("\nCharacter :%c", TurHare);
      fflush(stdout);
    }
 }
nos
+1  A: 

There is an buffering-problem with scanf("%c"). Many people use fflush(stdin) to solve, but isnt ANSI.

void fflushstdin()
{
    int c;
    while( (c=getchar())=='\n' );
    if( c!=EOF )
        ungetc(c,stdin);
}

main()
{
    int TurHare;
    while( fflushstdin(), !feof(stdin)&&1==scanf("%c",&TurHare) )
    {
        printf("\nCharacter :%c", TurHare);
    }
    return 0;
}

Break the loop with ^Z on Windows and ^D with Unix/Linux.