Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   printf (http://www.chiefdelphi.com/forums/showthread.php?t=54298)

mormannoob 16-02-2007 18:29

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

thx

BossOfTheGame 16-02-2007 22:09

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

steve d 16-02-2007 22:22

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.

half geek 16-02-2007 22:28

Re: printf
 
And be sure to do an #include "stdio.h"

Dave K. 16-02-2007 22:54

Re: printf
 
Quote:

Originally Posted by mormannoob (Post 579795)
im a noob and need a quick overview on how to call prinfs

thx

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.

BossOfTheGame 17-02-2007 00:01

Re: printf
 
Quote:

Originally Posted by steve d (Post 579929)
"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.

nice catch

TimCraig 17-02-2007 00:41

Re: printf
 
Quote:

Originally Posted by half geek (Post 579931)
And be sure to do an #include "stdio.h"


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

charrisTTI 17-02-2007 23:57

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.

paulcd2000 18-02-2007 08:43

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);

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

et1337 18-02-2007 11:50

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");

Andrew Blair 18-02-2007 13:28

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.

Tureyhall 18-02-2007 16:38

Re: printf
 
Quote:

Originally Posted by Andrew Blair (Post 580907)
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.
\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.

Redneck 18-02-2007 17:51

Re: printf
 
Quote:

Originally Posted by Tureyhall (Post 580990)
%.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.

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:
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);

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.


All times are GMT -5. The time now is 04:49.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi