[solved] printf/scanf not working...?

  • Hi -

    I was trying to help a beginning C programmer with a small program. I imported it into Qt (my favorite IDE) in a new project (Other Project->Empty Qt Project), and discovered to my surprise that printf doesn't print to the console, and scanf doesn't do anything (in the debugger, it just goes somewhere mysterious.)

    I guess I've never tried printf/scanf within Creator before; do I need to do anything to "hook up" these functions? The program builds just fine.


  • This works for me:

    #include <stdio.h>

    int main(int argc, char *argv[])
    printf("printf: %s %d\n", "this is a string", 23);
    return 0;

    I can see the output in Qt Creator's console tab.

    For the scanf to work, you need another console to provide the input. Could be that you must check the "run in terminal" option in the run settings of your project. It may be that this is a bit problematic on the Mac though, I vaguely remember threads here in the forums dealing with problems with running apps in a terminal.

  • Your program works fine...don't know what the problem with mine is, but at least it's not Qt, so I can drop discussing it here. I didn't realize that about scanf...I thought you could provide input from the console.

    EDIT: I just removed the scanf statements from this program, and the printf statements then worked. Weird...out of curiosity, do you know why the Creator console doesn't work with scanf? Seems like kind of a weak point, even though most users probably aren't going to use it.

    And yes, I too seem to be unable to get the terminal working right with my program. I thought I'd been around this before, but a quick review of my threads doesn't reveal anything about it.

  • Did you tick the "Run in console" setting in Projects->[Target->]Run Settings?

  • By default, stdout is is buffered. It makes a difference whether it is connected to a terminal or another program, though.

    • Terminal
      The output is synced after a certain amont of data has been written (usually 1024 bytes) or after a newline has been written (so called "line buffering")
    • Application
      The output is only synced after the buffer is full (usually a kernel page size, mostly 4096 bytes)

    In both cases you can force the sync by flushing the buffer manually like this:


    You suffered from Qt Creator being an application an thus stdout not being line buffered. If you call the application on Terminal.app, it should work as expected.

    Some more reading on the topic:

  • Hi, guys -

    The solution I used was to set my terminal as "/usr/X11/bin/xterm -e" and to check the box Tobias mentioned.

    Without the pathname, creator can't find the xterm app. Also, trying to use the MacOs's terminal utility doesn't work; not sure why. I seem to recall a better solution than using Xterm's terminal, but I can't yet remember what it is. I'll do some more looking around.

    Volker: interesting observation on Terminal vs. Application. But, cout doesn't suffer the same problem, does it?

  • I would expect cout to suffer from the same problem, as it is only a C++ abstraction above stdout, making the latter a stream. The default buffering usually is set by the operating system. AFAIK there are means to change it from your application, though.

  • You'd think that cout would (not) work the same way, but I've never had a problem with its output appearing in the "Application Output" window. Dunno...

    Anyway, I suppose I should mark this solved.

    EDIT: oops: I answered before I realized what specifically Volker was talking about. Never mind. Still solved, though.

  • You can do setbuf(stdout, NULL); on the start of the program.

Log in to reply