2
#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.

Community
  • 1
  • 1

3 Answers3

5

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)

Dayal rai
  • 6,548
  • 22
  • 29
  • thanks alot for dayal rai... if you dont mind can you explain it in more brief??? what can we do if there more than 2 number input keywords, is there any shortcut form to reduce the coding length?? actually i'm learning c from "c from dummies" in that book they mentioned that using eof like ctrl + z (page no.169 in c for dummies) can run the program but it didn't work. can you tell me the reason bro?? – InstanceDeveloper Aug 06 '13 at 08:23
  • Perhaps [setvbuf](http://www.gnu.org/software/libc/manual/html_node/Controlling-Buffering.html) and [what does getchar actually do](http://stackoverflow.com/questions/3676796/what-does-getchar-exactly-do) will help.Reducing coding length depends on Practice, Selection of definition,optimization and ofcourse on experience.No need to be in hurry.`ctrl+z` is for EOF but i can't see any use of that in your code.please have a look [here](http://stackoverflow.com/questions/16132971/how-to-send-ctrlz-in-c) – Dayal rai Aug 06 '13 at 08:34
2

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

Nobilis
  • 7,310
  • 1
  • 33
  • 67
  • @chintusrikanth What didn't work? In my case the new line isn't getting passed to the next call and I'm able to get the following output: `enter the firstkeyword b enter your second keyword a it is 'b' greater than 'a' as i expected ` – Nobilis Aug 06 '13 at 08:34
  • sorry @Nobilis i'm using orwell devc++.if you can tell me for dev c++ in windows thanks in advance – InstanceDeveloper Aug 06 '13 at 08:40
  • @chintusrikanth the compiler or platform doesn't matter in the case of the `scanf` statements, please tell me what doesn't work? – Nobilis Aug 06 '13 at 08:42
  • @Nobilis i think you need to give some focus towards the `space` used with specifier.I guess OP is not following that `space`. – Dayal rai Aug 06 '13 at 08:46
  • 1
    @Dayalrai I've added an explanation for why space is necessary there, hope that clarifies things for the OP. – Nobilis Aug 06 '13 at 08:54
0

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.

nikoo28
  • 2,961
  • 1
  • 29
  • 39