PDA

View Full Version : printf no longer prints text to the console




RossOliver
Jul 5, 2008, 03:34 PM
Hey,

I've managed to bugger my XCode somehow - printf no longer prints anything to the console. I have no idea what I've changed to stop this working - can anyone give me a clue?

I'm sure it's not the project that's bust since I created a new one and the first thing I put in the applciationDidFinishLaunching method was printf( "something" ) and I got nothing in the console...

Thanks for your time,

-Ross



toddburch
Jul 5, 2008, 03:38 PM
What happens when you run it from Terminal?

martintyler
Jul 5, 2008, 03:50 PM
if the string doesn't end with a newline character it probably wont be displayed until a newline is sent to that file descriptor.

So if you really meant "something" and not "something\n" then that is likely to be your problem

RossOliver
Jul 5, 2008, 03:55 PM
Upon opening Terminal I was presented with:

"Could not open a new pseudo-tty."

I guess something was having a fit - a restart seems to have fixed it :o

Cheers

Eraserhead
Jul 5, 2008, 04:14 PM
Why are you using printf with Cocoa rather than NSLog?

Cromulent
Jul 5, 2008, 04:23 PM
if the string doesn't end with a newline character it probably wont be displayed until a newline is sent to that file descriptor.

So if you really meant "something" and not "something\n" then that is likely to be your problem

printf() does not require a newline to print.

RossOliver
Jul 5, 2008, 04:27 PM
Why are you using printf with Cocoa rather than NSLog?

I tried NSLog when printf wasn't working and it didn't work either. I use printf for temporarily checking variable values and to give indications of where in the code execution is currently at (I know I should really use gdb but I find printing simple messages for some debugging tasks is quicker)...

-Ross

lee1210
Jul 5, 2008, 10:43 PM
I tried NSLog when printf wasn't working and it didn't work either. I use printf for temporarily checking variable values and to give indications of where in the code execution is currently at (I know I should really use gdb but I find printing simple messages for some debugging tasks is quicker)...

-Ross

The questions still stands... NSLog is fine for debug statements, so why printf with it's occasionally difficult to decipher format specifiers, etc? I don't know if you can print address pointers using NSLog, but that's a rare thing to need to do, I'd say.

-Lee

RossOliver
Jul 6, 2008, 04:49 AM
The questions still stands... NSLog is fine for debug statements, so why printf with it's occasionally difficult to decipher format specifiers, etc? I don't know if you can print address pointers using NSLog, but that's a rare thing to need to do, I'd say.

-Lee


I guess I'm just used to using printf from programming in C, I haven't really looked at NSLog's capabilities...

Mac Player
Jul 6, 2008, 06:55 AM
printf() does not require a newline to print.

But it forces a flush?

Cromulent
Jul 6, 2008, 07:30 AM
But it forces a flush?

Nope. There is absolutely no need for a \n in any of your printf() statements if you don't want them.

Mac Player
Jul 6, 2008, 07:44 AM
Nope. There is absolutely no need for a \n in any of your printf() statements if you don't want them.

According to this it forces a flush http://www.informit.com/articles/article.aspx?p=350919&seqNum=7. If your program terminates and you don't flush the buffer printf won't/may not print on the screen.

Cromulent
Jul 6, 2008, 08:00 AM
According to this it forces a flush http://www.informit.com/articles/article.aspx?p=350919&seqNum=7. If your program terminates and you don't flush the buffer printf won't/may not print on the screen.

That is the first time I have ever seen anyone claim a \n flushes the buffer for printf(). The man page is pretty detailed and mentions nothing. I'm sceptical but happy to be proved wrong.

printf() is meant for formatted output, requiring a newline to flush the buffer is pretty stupid given it's use.

Plus, have you actually tried it? Works fine for me (and I forget to put \n in all the time in my printf() statements).

Mac Player
Jul 6, 2008, 08:21 AM
Well I experience that behavior a couple of times when i was debugging with printf statements. Some printfs before the segfault code wouldn't print until I added newline characters.

Edit: I found a more credible source here (http://books.google.com/books?id=iRa-3L4jLhEC&pg=PA84&lpg=PA84&dq=printf+buffer+flush+new+line&source=web&ots=dXfybt2e9I&sig=2jZHctY3fcmVz1zI4y9JuE8NeWE&hl=en&sa=X&oi=book_result&resnum=6&ct=result)

lee1210
Jul 6, 2008, 08:25 AM
fprintf() and printf()'s buffering is implementation specific. If you need output flushed use flush() and fflush(). Those will flush.

-Lee

toddburch
Jul 6, 2008, 08:27 AM
I'm not sure you can make an association between a newline character and an output stream getting flushed. I suspect that's why there is (in C) a fflush() function. In C++, you have std::end (newline) and std::endl (newline AND flush). Don't know Obj-C yet, but it stands to reason that Obj-C didn't change the name of the game and cause a \n to flush any buffers.

Todd

Mac Player
Jul 6, 2008, 09:20 AM
Well I'm looking at glibc source trying to figure out how the buffer is handled. Thank god vfprintf is a 1k line function.

Sayer
Jul 6, 2008, 09:42 AM
Copy/paste this into a text file and compile and run it and tell me how a newline character doesn't flush the buffer for stdout:

#include <stdio.h>

main(int argn, char **args) {

printf("Will this be seen?\t");

printf("Or will this be seen first?\n");

printf("Lets use fflush() now.");

fflush(stdout);

printf("\n");

return 0;

}

Cromulent
Jul 6, 2008, 09:47 AM
Copy/paste this into a text file and compile and run it and tell me how a newline character doesn't flush the buffer for stdout:

#include <stdio.h>

main(int argn, char **args) {

printf("Will this be seen?\t");

printf("Or will this be seen first?\n");

printf("Lets use fflush() now.");

fflush(stdout);

printf("\n");

return 0;

}

Seems fine to me. Works fine if you remove all \n characters and the fflush() function as well.

Mac Player
Jul 6, 2008, 10:00 AM
Compile and run this :
#include <stdio.h>
#include <stdarg.h>

int main (int argc, char const *argv[])
{
int* go;
printf("lol");

*go = 1;


return 0;
}


Then complie and run this:

#include <stdio.h>
#include <stdarg.h>

int main (int argc, char const *argv[])
{
int* go;
printf("lol\n");

*go = 1;


return 0;
}

Do you see the difference in the output?

Cromulent
Jul 6, 2008, 10:19 AM
What is the point of the go pointer?

Mac Player
Jul 6, 2008, 10:21 AM
To segfault the program.

Cromulent
Jul 6, 2008, 10:24 AM
To segfault the program.

If your program is segfaulting I think the last thing you will worry about is whether your printf() statements are working correctly. Still, I see your point.

toddburch
Jul 6, 2008, 12:55 PM
Apparently, depending on the type of buffering chosen, a newline can indeed cause a buffer flush, or not.

http://publications.gbdirect.co.uk/c_book/chapter9/input_and_output.html

See sections 9.10.4.3 and 9.10.9.

Todd