-2

I would like to know about the getchar() logic in the following codes...

printf("Type up to 25 characters and then press Enter. . . \n") ;  
for (i = 0; i < 25; i++)  
{  
    msg[ i] = getchar() ;   
    if (msg[ i] == ' \n' )  
    {   
            i--;  
            break;  
    }  
}
putchar(' \n' ) ;  
for (; i >= 0; i--)  
{
    putchar(msg[ i] ) ;
}

In the above code if i input a name,say "STACKEXCHANGE" and then hit enter it will display the word properly..but the below code works differently..

printf("What are your two initials?\n") ;  
firstInit = getchar() ;  
lastInit = getchar() ;

In this example, if i type "ST" and then hit enter, 'S' will be stored in firstinit, while "\n" will be stored in lastInit instead of 'T'.

How does this happen? Isn't 'T' the second character input into the buffer. Infact \n is the third character in the buffer. so why does \n get stored. correct me if iam wrong, but the buffer is released when \n is pressed or entered, then why is it again being stored in the next getchar() function.

Why is the first code executed differently then? I mean if i input "stackexchange" and hit enter "t" is stored as the second character as is wanted, but it in the second example if the same "st" is typed and "enter' is input, the "t" is not stored as the second input, but instead "enter' is taken as the second input.

I am sorry if my typing is confusing... basically i want to know the logical flow in both codes, how it happens behind the scene..

smali
  • 4,687
  • 7
  • 38
  • 60
  • 5
    "In this example, if i type "ST" and then hit enter, 'S' will be stored in firstinit, while "\n" will be stored in lastInit instead of 'T'." - this is simply not true. Try it. – John Zwinck Jan 21 '15 at 08:42
  • @Reeza - Are you really entering `ST` together in your second example? – Sadique Jan 21 '15 at 08:45
  • hmm..i am really confused now..actually i lifted both these examples from a c text book...can u explain how the getchar() logic works – ReezaHendricks Jan 21 '15 at 08:47
  • @al-Acme yes..but it seems johnzwinck is right...it does work.. – ReezaHendricks Jan 21 '15 at 08:50
  • Here is that para from the book: You would think that ifthe user typed GT, the G would go in the variable firstInit and the T would go in lastInit, but that’s not what happens. The first getchar() doesn’t finish until the user presses Enter because the G was going to the buffer. Only when the user presses Enter does the G leave the buffer and go to the program—but then the Enter is still on the buffer! Therefore, the second getchar() sends that Enter (actually, the \n that represents Enter) to lastInit. The T is still left for a subsequent getchar() (ifthere is one). – ReezaHendricks Jan 21 '15 at 08:51
  • That's incorrect. What book are you reading? – Evil Dog Pie Jan 21 '15 at 09:03
  • @MikeofSST saw this thing in this book C Programming Absolute Beginner - Perry, Greg – ReezaHendricks Jan 21 '15 at 10:35
  • Ok, it's not 'strictly' correct. Most of my code is in embedded systems that do not use buffered input. In these situations `getchar()` will return immediately when a character is present in the `stdin` stream. On systems that do use a buffered driver, `getchar()` will only return when the buffer is flushed to the stream, usually when the user presses or similar. See [this answer](http://stackoverflow.com/a/3676874/3581917) for more information. – Evil Dog Pie Jan 21 '15 at 11:42
  • possible duplicate of [What does getchar() exactly do?](http://stackoverflow.com/questions/3676796/what-does-getchar-exactly-do) – Evil Dog Pie Jan 21 '15 at 11:44

2 Answers2

0

the only reason for storing \n is when you are entering S the pressing enter and not flushing out the carriage return so it will store \n in your second string.

firstInit = getchar() ; 

will read and store S

lastInit = getchar() ;

will read and store carriage return

getchar() is a standard library function which will read a character from the console.

smali
  • 4,687
  • 7
  • 38
  • 60
0

Check the below code which takes your input ST and stores it in 2 char's a and b.

int main(void){
char a;
char b;

a = getchar();
b= getchar();

printf("a = %c b = %c\n",a,b);
return 0;
}

Input

ST<enter>

Output

a = S b = T

If you are seeing a newline char being saved into b here then the input is like S<enter> not ST<enter>

Gopi
  • 19,784
  • 4
  • 24
  • 36