1

I am a java programmer and started learning C recently. I was going through headfirst C and wrote one of the example programs in Eclipse (with CDT). Here is the program

#include <stdlib.h>
#include <stdio.h>
int main()
{
    char card_name[3];
    puts("Enter the card_name: ");
    scanf("%2s",card_name);
    int val = 0;
    if(card_name[0] == 'K'){
        val = 10;
    }else if (card_name[0] == 'Q'){
        val = 10;
    }else if(card_name[0] == 'J'){
        val = 10;
    }else if(card_name[0] == 'A'){
        val = 11;
    }else{
        val = atoi(card_name);
    }

    printf("The card value is: %i\n",val);
    return 0;
}

When I run it on eclipse, the line containing puts should be executed and then the user should enter in eclipse console and then the scanf line should execute.

But it doesn't happen that way, when I run it, it just expects the user to enter first on the eclipse console, then execute the puts line and finally the scanf line. I couldn't understand this behavior. Can someone help me on this ?

userx
  • 3,713
  • 5
  • 32
  • 38
  • What happens if you try from a normal commandlnie/shell? – Yunnosch Aug 05 '18 at 06:15
  • What happens if you append a `"\n"` at the end of the text to output via `puts()`? – Yunnosch Aug 05 '18 at 06:17
  • @Yunnosch - doesn't help, the sequence still remains the same. just that a new line now happens between puts and printf. puts("Enter the card_name: \n"); – userx Aug 05 '18 at 06:21
  • What about my first question? – Yunnosch Aug 05 '18 at 06:23
  • How did you figure out which line is executed first? They are always executed in proper order for me. Also which version of Eclipse and on which OS are you using? – user7860670 Aug 05 '18 at 06:24
  • 1
    What happens if you explicitly flush after puts(): `fflush(stdout);` ? – Yunnosch Aug 05 '18 at 06:26
  • @VTT- I am using Oxygen, Windows 7. The way I figured out is because the program just doesn't execute and stagnates unless I input say QJ, then it prints the puts and finally it scans and program logic executes. – userx Aug 05 '18 at 06:28
  • @Yunnosch - fflush(stdout) worked for me but I didn't understand the reason. How did it solved the problem ? – userx Aug 05 '18 at 06:30
  • Wait, you are just saying that "Enter the card_name: " appears after `scanf`? This does not mean that `puts` executed after `scanf` it simply means that `puts` does not recognize output as an interactive terminal and does not flush the output. You should put a breakpoint to figure out how those lines are actually executed. – user7860670 Aug 05 '18 at 06:34
  • It might also be helpful to try the program from a normal shell/commandline prompt, rather than from within the Eclipse console. That might be the relevant factor. What happens if you execute the program you created from a normal prompt? – Yunnosch Aug 05 '18 at 06:35
  • I would think using a `switch` statement would make the code easier to read. – Ed Heal Aug 05 '18 at 06:36
  • [Lots of duplicates](https://stackoverflow.com/questions/linked/16877264?lq=1) – Spikatrix Aug 05 '18 at 06:45

1 Answers1

2

Depending on how the buffering of the output is configured, you might nee to be very explicit about when to actually make the output.
Usually a newline at the end of text is sufficient, i.e. there must be something special in your environment, I suspect the Eclipse console, but that is just guessing.
Especially when attempting to do prompt and reading within one line, it is wise to use a

/* ... */
puts("Enter the card_name: ");
fflush(stdout);
/* ... */

at exactly the point where you need the output you have already send.

In your case the buffering causes this sequence:

  • put the prompt in output buffer
  • environment is configured not to automatically output immediatly
  • (this is where I propose to flush in order to get output)
  • scanf is executed
  • user input
  • your program does not do anythign visible, though executing completly
  • put the final message into output
  • ending the program causes final output of anything buffered

I.e. the scanf is not executed before the first puts, it is only the visible effect which gets delayed.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54