Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RossOliver

macrumors regular
Original poster
Nov 6, 2006
157
0
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
 
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
 
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 :eek:

Cheers
 
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
 
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
 
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...
 
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).
 
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
 
fprintf() and printf()'s buffering is implementation specific. If you need output flushed use flush() and fflush(). Those will flush.

-Lee
 
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
 
Well I'm looking at glibc source trying to figure out how the buffer is handled. Thank god vfprintf is a 1k line function.
 
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:

Code:
#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;

}
 
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:

Code:
#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.
 
Compile and run this :
Code:
#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:

Code:
#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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.