19

I am trying to use "printf" in my Visual C++ project however it is not working. Using Lazy Foo's tutorial, I set up SDL in my project, but when I play it, printf doesnt do anything.

#include "SDL.h"
#include <stdio.h>

int main( int argc, char* args[] ) {
    printf("Testing");
    return 0;
}

The output looks like this:

The program '[4664] SDL Testing.exe: Native' has exited with code 0 (0x0).

And that's about it. What could be wrong?

19
  • Usually 0 signifies success. Try to take any input (or) keep a break point at return statement.
    – Mahesh
    Jun 17, 2012 at 2:50
  • @Mahesh I tried putting breakpoints during the time I used printf, still no output. :(
    – Qasim
    Jun 17, 2012 at 2:52
  • The output window in Visual Studio != stdout or stderr. Jun 17, 2012 at 3:04
  • @ta.speot.is Is there any way to see the output in Visual Studio?
    – Qasim
    Jun 17, 2012 at 3:06
  • Try putting the 'ugly' getch() before the return statement to see the console output. A less 'ugly' method is to use char c; std::cin>>c; before the return statement. The other (better) way to see the output is by running the executable from the command line.
    – A. K.
    Jun 17, 2012 at 3:07

9 Answers 9

19

Bring up the projects properties, go to linker->system->subsystem and change it to the third option, CONSOLE. That should do it

1
  • AND set the program entry point accordingly in the Linker section.
    – andreszs
    Apr 21, 2023 at 14:18
10

SDL by default redirects stdout to a file, stdout.txt. You should find it in your program's working directory.

1
  • Thank you. This file is created in Visual C++ project Debug folder, containing all the "std::cout" functions I call.
    – Qasim
    Jun 17, 2012 at 14:19
8

In Linker -> System in your project's properties, check that the SubSystem is "Console (/SUBSYSTEM:CONSOLE)".

That causes a separate console window to be brought up when you run your program. If your current entry point isn't main, then you'll need to change it to that if you do this though.

1
  • And to change the entry point in VC++2005, add /ENTRY:"WinMainCRTStartup" to Configuration Properties > Linker > Command Line > Additional options.
    – andreszs
    Apr 21, 2023 at 14:16
6

Everything works, I have even displayed an image to the screen, but I cannot program without having someway to output messages

I assume this means that your have a window available to you, not a console.

If you want to log something to the output window, use OutputDebugString:

Sends a string to the debugger for display.

void WINAPI OutputDebugString(
  __in_opt  LPCTSTR lpOutputString
);

Header WinBase.h (include Windows.h)

1
  • Is there a way to have both a window and a console available? Oct 29, 2023 at 15:51
4

Printf usually needs a newline to update the console. Add a '\n' character to the end and re-run the program.

1
  • Sadly there is still no output! I am wondering what could be causing this. Very absurd.
    – Qasim
    Jun 17, 2012 at 3:03
3

Try defining NO_STDIO_REDIRECT.

#define NO_STDIO_REDIRECT

If that doesn't work try the solution in this link: How can I get console output instead of stdout.txt and stderr.txt?.

1
  • 1
    This is for SDL1.2 only, does not work for SDL2. As you search the source code for SDL2, you will see this symbol is not being used.
    – Bram
    Jan 31, 2015 at 21:12
0

You're probably not seeing the output because you're running the program from within Visual Studio by pressing F5 and the console window closes after the program exits.

You can do one of the 3 things -
Put a breakpoint at the return statement.
Put a getchar() statement before the return statement.
Run the program by pressing Ctrl+F5 instead of F5.

All of the above will cause the console window to remain on the screen.

You could also directly run the EXE from a command prompt (cmd.exe).

6
  • 1
    Putting a breakpoint at the return statement, I don't know where to look for the console. When pressing Ctrl+F5, the Output view remains empty, but when I just use F5 the output shows build logs and debug logs, with it containing the following: pastebin.com/UKWpCKsD Do all the PDB errors play a role in this?
    – Qasim
    Jun 17, 2012 at 3:01
  • The PDB errors that you're seeing don't matter. Check the return value of printf. It returns the number of characters that has been printed. printf outputs characters in a separate console window and not in the output view.
    – Superman
    Jun 17, 2012 at 3:41
  • how do I get access to the seperate console window?
    – Qasim
    Jun 17, 2012 at 3:43
  • When you do Ctrl+F5 there will a console window open. Check your taskbar.
    – Superman
    Jun 17, 2012 at 3:45
  • No console is ever opened. It is just the output window.
    – Qasim
    Jun 17, 2012 at 3:47
0

I just ran into this issue when trying to write a simple console, my fix was to add a fflush(stdout)

printf("Hello world!\n");
fflush(stdout);
-4

there is an output window of visual studio when you are running/debugging your program. You should be able to see the output in that window.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.