printf

im a noob and need a quick overview on how to call prinfs

thx

you type in printf(“Hello World”); and that should do it.

other examples

int i = 120;

printf(“Hello %d World”,i);

Output: Hello 120 World

"you type in printf("Hello World); and that should do it.

That won’t do it, you forgot closing quotes.
printf(“this gets printed: first number = %d, second number here= %d\r”,first_number, second_number);

When it sees the %, it prints the first_number, then the words agian till it sees the next %,… The %d is a format descriptor, there is %s for strings,… google on printf format.

And be sure to do an #include “stdio.h”

If you used the default installation paths, then look in “C:\MCC18\docs” and you’ll find four PDF files. The ‘MPLAB-C18-Libraries.pdf’ file contains documentation on the included libraries. Section 4.7 covers the character output functions.

nice catch

While that will work, technically you should use #include <stdio.h> :cool:

confusion my come from the fact that ifi code contains a printf as well. see printf_lib.h and .c files. stdio.h implies using the printf routine in clib. use one or the other but not both.

printf is fun! It allows a lot of feedback, and i know we’d be stuck without them. Heres a few samples, straight from our code:


	printf("Joystick 1 Y-axis: %d	 Joystick 1 Trigger: %d\r", left_joy, p1_sw_trig);
 	printf("Relay 1 Forward: %d	 Relay 1 Reverse: %d\r", relay1_fwd, relay1_rev);
	printf("Left Wheel: %d 	Right Wheel: %d \r", left_motor, right_motor);

printfs can really help you do anything. just be sure to # include <stdio.h>

Also, be sure to add "
" at the end of each printf. Otherwise, the terminal fills with unreadable text and you have to disable it to read it.

Like so:

printf("Hello World!
");

On the topic, can someone describe to me what all the calls mean? /r, /n, d%, %3d, etc?

Always been a mystery to me… I just copy pasta typically.

\r - return character, moves cursor to beginning of current line.

  • newline character, moves cursor down by one.

%d is for ints. You can search online for other variable types. This will display the entire variable.

%3d means display a int three digits in length.

%.3f means display a float with as many digits before the decimal as it needs, but limit it to three digits after the decimal.

%3.2f means display a float with three digits before the decimal, and two digits after the decimal.

All this is off of the top of my head, and may not be correct.

P.S. I like copying pasta as well. Yummy pasta.

These won’t work in FRC code. The PIC18 chip used on the RC doesn’t support floating point operations natively, so it’s all done in software. Unfortunately, that software doesn’t include the %f place holder. If you want to print a float, your only choice is to do something like this:


float variable = .... //Whatever float you're trying to print
int variabledeci;  // Integer to hold the numbers after the decimal point
variabledeci = variable - (int)variable;
variabledeci*=1000;
printf("variable = %d.%03d", (int)variable, (int)variabledeci);

So, say that variable = 3.141
variabledeci = 3.141 - (int)3.141 = 3.141 - 3 = .141
variabledeci *= 1000 = 141

If you want more decimal places, make the 1000 into 10000, 100000, etc.