Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. [solved] printf/scanf not working...?

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

Scheduled Pinned Locked Moved Qt Creator and other tools
11 Posts 6 Posters 41.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    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.

    Thanks

    1 Reply Last reply
    1
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      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.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      1
      • mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobias.hunger
          wrote on last edited by
          #4

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

          A B 2 Replies Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            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:

            @
            fflush(stdout);
            @

            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:

            • http://www.pixelbeat.org/programming/stdio_buffering/

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                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.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • mzimmersM Offline
                  mzimmersM Offline
                  mzimmers
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sibislaw2
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • T tobias.hunger

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

                      A Offline
                      A Offline
                      ANASAFOU
                      wrote on last edited by
                      #10

                      @tobias-hunger thank you so much

                      1 Reply Last reply
                      0
                      • T tobias.hunger

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

                        B Offline
                        B Offline
                        bahadir
                        wrote on last edited by
                        #11

                        @tobias-hunger thank you so much awusimusi :******************************

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved