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
 

martintyler

macrumors newbie
Feb 9, 2005
27
0
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

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

RossOliver

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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

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

Cromulent

macrumors 604
Oct 2, 2006
6,816
1,100
The Land of Hope and Glory
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

macrumors regular
Jan 19, 2006
225
0
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
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
fprintf() and printf()'s buffering is implementation specific. If you need output flushed use flush() and fflush(). Those will flush.

-Lee
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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

macrumors regular
Jan 19, 2006
225
0
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

macrumors 6502a
Jan 4, 2002
981
0
Austin, TX
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;

}
 

Cromulent

macrumors 604
Oct 2, 2006
6,816
1,100
The Land of Hope and Glory
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.
 

Mac Player

macrumors regular
Jan 19, 2006
225
0
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.