0

Excuse me if this might be painfully obvious, but I've not found an answer to this so far. I just started learning some C basics after mostly working with Java so far.

I've tried to follow this tutorial and am currently very confused with how printf and scanf behave in the Eclipse console.

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

int main(void) {
    char character;
    printf("Enter a character: ");

    scanf("%c", &character);

    printf("Character: %c",character);
    return 0;
}

What I'm expecting to happen, based on the linked tutorial:

Enter a character: _

I'm expecting this line to be printed to the console and then wait for my input where I put _ as a placeholder, before then printing Character: _. This behaviour is how I'd expect it in Java.

What actually happens:

On running my code, the console stays completely empty. At first I spent 2 hours thinking the program was simply not executing. Luckily I then noticed that the empty console was simply my prompt to give my user input. Once I input a character and press enter, the printf's are written into the console like so:

a
Enter a character: 
Character: a

Is this behaviour normal and my expectations for the behaviour are just plain wrong? If so, how would I go about realizing my desired behaviour of first being presented with the prompt to input a character?

yur
  • 380
  • 2
  • 15
  • 2
    Why are you expecting the input cursor to remain on the same line after printing a newline character (`\n`)? Perhaps the answers to [this question](https://stackoverflow.com/q/4327942/1679849) will help. – r3mainer Jul 02 '20 at 11:38
  • @r3mainer Oh my bad, I added the newline character while trying things out, I wasn't expecting it to ignore the newline character. Thank you anyway for the resource! – yur Jul 02 '20 at 11:45
  • @user3121023 Thanks, that did it! So you're saying this is an IDE specific behaviour? Feel free to post your comment again as an answer so I can accept it! – yur Jul 02 '20 at 11:46
  • 1
    No, it's not so much IDE specific. The reason is the output buffering of the terminal, many IDEs are line-buffered. – the busybee Jul 02 '20 at 13:30
  • @thebusybee Alright thanks for the reply. – yur Jul 07 '20 at 05:49

0 Answers0