Question

#include <stdio.h>
#include <stdlib.h>

int main()

{
    
    char a,b;
    
    printf("enter the firstkeyword \n");
    a = getchar();
    printf("enter your second keyword \n");
    b = getchar();
    
    if(a>b)
    {
    printf("it is '%c' greater than '%c' as i expected \n",a,b);
    }
    else if (b>a)
    {
    printf("here'%c'is greater than '%c' \n",b,a);
    }
    else
    {
    printf("dont type the same character twice in next session \n");
    }
    return(0);
    
}

after compiling the program the o/p is:

enter the first keyword

I entered '$' and used ctrl+z to eof and 'enter' to continue the program. But even without entering the second keyword the compiler printing the output as

enter your second keyword

it is '$' greater than '->' as i expected

Can anyone help with this program?

Sorry if any grammatical or phrase errors.

Was it helpful?

Solution

getchar() is taking extra inputs \n too when you press enter which is still there in buffer.You need to absorb this extra character to let second getchar work.Try calling getchar twice like below-

char just_to_consume;
a = getchar();
just_to_consume = getchar();
printf("enter your second keyword \n");
b = getchar();
just_to_consume = getchar();

other than above option you can use standard function setvbuf to control buffering.One more option is there(personally i do not prefer this to avoid undefined behaviour) is using fflush(stdin)

OTHER TIPS

The problem is that your newline gets buffered and passed onto the next getchar call. You need to deal with the buffered newline in perhaps the following way:

printf("enter the firstkeyword \n");
scanf(" %c", &a);

printf("enter your second keyword \n");
scanf(" %c", &b);

The space before %c is a common idiom that tells scanf to ignore any space before the following character which in our case also includes the newline. It is not necessary in the first instance in this particular case but vital in the second.

You also don't need the stdlib include and you can return without the brackets, like return 0;

Actually, if you feel like experimenting and you are on a Linux terminal, you can set the terminal in raw mode which will remove any buffer and parsing abilities that the terminal would provide for you. To do that run /bin/stty raw in the terminal.

This way there will be no buffering and you won't have to worry about any buffered newlines. The output on the console will look funny though (I've entered here a and b) unless you also regulate that with placing strategically carriage returns (\r):

$ ./a.out 
         enter the firstkeyword 
                               aenter your second keyword 
                                                         bhere'b'is greater than 'a' 

I've used your original code for the above.

To restore it, just run /bin/stty cooked

C takes the '\n' as the second character. What you can do is input both the character in the same line as

$ @

or else modify your program by not using the getchar() function

char a,b;
printf("enter the firstkeyword \n");
scanf(" %c",&a);
printf("enter your second keyword \n");
scanf(" %c",&b);

Notice the white space between " and %c

This does the trick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top