Printf prints only the first character


I’ve been using printf to print out a menu to the terminal, so that I can interact with the program at run time. It has been working like a charm…until today.

I have this function that has been working great all along. Until I tried it today.

This is the function:

void PIM_Int_PrintPIDMenu(void) {
	//Print out the menu.
	printf("Steering PID:
	printf("P: %d / %d, I: %d / %d, D: %d / %d, Max I: %d
	printf("For P term:
	printf("[a] Reset Num to 0 and Denom to 1
	printf("**/[c] (Inc/Dec)rement Num
	printf("[d]/[e] (Inc/Dec)rement Denom
	printf("[f] Double Num and Denom
	printf("For I term:
	printf("[g] Reset Num to 0 and Denom to 1
	printf("[h]/* (Inc/Dec)rement Num
	printf("[j]/[k] (Inc/Dec)rement Denom
	printf("[l] Double Num and Denom
	printf("For D term:
	printf("[m] Reset Num to 0 and Denom to 1
	printf("[n]/[o] (Inc/Dec)rement Num
	printf("[p]/[q] (Inc/Dec)rement Denom
	printf("[r] Double Num and Denom
	printf("[s] Refresh this menu (ALWAYS do before reading numbers!!!)
	printf("[t]/ (Inc/Dec)rement I max PWM
	printf("[v] Reset I max PWM to 0
	printf("[w] Toggle anti-windup (currently %d)

I should be getting each one’s output in my terminal…but I get

Steering PID:


Has anyone seen this before? I’m seriously considering writing a homebrewn function to print each character independently…but I’d like to stay away from that route if at all possible.

Please help…

Have you added recent code that generates a lot of interrupts? You could be missing some now. Or maybe…

Your output is going crazy when you try to print your first variable. Is there something wrong with your variables? Is the linker giving you any warnings?


I don’t have many interrupts firing at all, and I haven’t added any for quite a few builds. It looks like it is the variables making it stop working…and i’ve seen some pretty strange stuff involving my EEPROM_Read2Byte function (it was CRASHING the COMPILER…:ahh: :ahh: :ahh: ) so maybe…

unsigned int EEPROM_Read2Byte(unsigned int arg_address) {
	//Return the parameter.
	return (EEPROM_Read(arg_address) * 0x100) + EEPROM_Read(arg_address+1);

See anything wrong? It’s Kevin Watson’s EEPROM code with my own function added for 2-byte variables…

Now, why don’t we have a “beating head on brick wall” smiley?


Your last two parameters in the third printf() are single bytes. The %d specification wants to see two-byte integers. You are thus causing it to try to read more data than you’ve provided, which results in really really weird printing.

If those last two things are really single bytes, make the printf() happy by saying (int)EEPROM_Read(…). Otherwise, correct them to say EEPROM_Read2Byte(…).

The last two calls are to routines that return single bytes… but the compiler automatically passes these as ints to the printf - for readability you can and the (int) or (unsigned int) case, but it won’t change the code generated.


  RCALL EEPROM_Read                      
  MOVF POSTDEC1, F, ACCESS   ; pop the arg off  stack        
  MOVF POSTDEC1, F, ACCESS   ; pop 2nd byte of arg off stack
  MOVWF POSTINC1, ACCESS    ; push returned char onto stack
  CLRF POSTINC1, ACCESS        ; push 2nd byte of zeros onto stack

Try commenting out the printf with the 7 arguments. If that works, add back in arguments one at a time, i.e.

printf( "P: %d / %d

Could be running up against variable stack limit (shouldn’t be) or formated printf buffer limit…

Actually, I’ll take that back… could be a variable stack limit depending on how the printf -> vprintf -> vfprintf -> … is done vis-a-vis copying stack arguments.

Debug steps are the same, reduce the printf. Could change this into a series of printfs and leave off the
until the last one in the series too.

OK, new strangeness…

I used to see this glitch only on lines that had variables in them. I now am getting single-character output from a line with no variables.

It always prints the first token; if I have a %d first thing, the number is printed, but often the rest of the string is absent.

Wrist goal: 2

That’s my output from this snippet:

	int loc_temp;
	printf("Shoulder goal: ");
	printf("Wrist goal: ");

It seems to think it’s done with the string. There are only Kevin Watson’s ADC interrupts firing, and I have it set at 200Hz, so that should not be my problem. It also seems to be value-dependent. When I have negative values for the shoulder goal, it does print them, but the line before it gets truncated to first character.

This is getting stranger by the second.


Try things one at a time. You can’t easily deduce which line is causing the problem. The output is buffered so sometimes its a bit difficult to correlate the cause of the problem with what actually makes it out the serial port.

Are you sure you are not reading from places in memory that are not in the EPROM? Try printing out the parameters to the EEPROM_Read2ByteSigned function.

Maybe the serial buffers are overflowing, are you checking the return values of printf?


I’ve been doing things slowly, one at a time, but I haven’t documented every step because I’ve been kinda busy…:slight_smile:

Aah…good point. I’ll check that exhaustively today. They appear to read/write and retain their value fine, though, suggesting that they are indeed EEPROM.

Maybe the serial buffers are overflowing, are you checking the return values of printf?

I didn’t even know printf returned anything…:o
What is the expected return value? I would assume 0, and nonzero is failure…but I never thought about it at all.

Thanks so much for continuing to check this thread. I’ve had another printf bug thread die quickly with not many replies…


" On success the number of input fields converted and stored are returned. If an input failure occurred, then EOF is returned."


try not calling frunctions from the printf i have had trouble with that in the past…

I just started having this same problem last night as well. It seems to be a series of printfs that are in a single if block. I can comment out any combination of them and get the same result. The one thing I didn’t check was passing a byte as an argument although Ithink I’ve done this in the past without problems.

Subsequent printfs don’t seem to be a problem. One symptom is that I have multiple lines in single printfs (ie. printf(“x=%d
\r”, x, y):wink: I get the first character of each line not just the first character of the printf.

This is smelling like a stack issue.