|
|
|
#1
|
||||
|
||||
|
printf
im a noob and need a quick overview on how to call prinfs
thx |
|
#2
|
|||
|
|||
|
Re: printf
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 Last edited by BossOfTheGame : 17-02-2007 at 00:03. |
|
#3
|
|||
|
|||
|
Re: printf
"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. |
|
#4
|
||||
|
||||
|
Re: printf
And be sure to do an #include "stdio.h"
|
|
#5
|
|||
|
|||
|
Re: printf
While that will work, technically you should use #include <stdio.h> ![]() |
|
#6
|
|||
|
|||
|
Re: printf
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.
|
|
#7
|
||||
|
||||
|
Re: printf
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:
Code:
printf("Joystick 1 Y-axis: %d\t Joystick 1 Trigger: %d\r", left_joy, p1_sw_trig);
printf("Relay 1 Forward: %d\t Relay 1 Reverse: %d\r", relay1_fwd, relay1_rev);
printf("Left Wheel: %d \tRight Wheel: %d \r", left_motor, right_motor);
|
|
#8
|
||||
|
||||
|
Re: printf
Also, be sure to add "\r\n" 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!\r\n"); |
|
#9
|
|||||
|
|||||
|
Re: printf
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. |
|
#10
|
|||
|
|||
|
Re: printf
Quote:
\n - 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. |
|
#11
|
||||
|
||||
|
Re: printf
Quote:
Code:
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);
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. |
|
#12
|
|||
|
|||
|
Re: printf
Quote:
|
|
#13
|
||||
|
||||
|
Re: printf
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.
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| printf issue | Max Brin | Programming | 6 | 24-01-2006 17:03 |
| Printf | BillyJ | Programming | 9 | 21-01-2006 14:03 |
| printf problem | miketwalker | Programming | 7 | 31-01-2005 13:19 |
| printf isn't printf-ing. Help! | Meandmyself | Programming | 14 | 15-02-2004 16:27 |