Go to Post Please drill responsibly! - IKE [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 16-02-2007, 18:29
mormannoob's Avatar
mormannoob mormannoob is offline
President and Senior Programmer
AKA: Clinton
FRC #0498 (Cobra Commanders)
Team Role: Programmer
 
Join Date: Jan 2007
Rookie Year: 2006
Location: Glendale,AZ
Posts: 93
mormannoob is on a distinguished road
printf

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

thx
__________________
Why cant code work the first time

Wait I have to lead programmers thats like asking a kid who doesnt know how to ride a bike to lead a group of guys at the tour de france
  #2   Spotlight this post!  
Unread 16-02-2007, 22:09
BossOfTheGame BossOfTheGame is offline
Not the garbage man
FTC #0020 (The Rocketeers)
Team Role: Programmer
 
Join Date: Feb 2007
Rookie Year: 2003
Location: Clifton Park
Posts: 6
BossOfTheGame will become famous soon enough
Send a message via AIM to BossOfTheGame
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   Spotlight this post!  
Unread 16-02-2007, 22:22
steve d steve d is offline
Registered User
FRC #1277
 
Join Date: Jan 2007
Location: Groton, ma
Posts: 38
steve d is an unknown quantity at this point
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   Spotlight this post!  
Unread 16-02-2007, 22:28
half geek's Avatar
half geek half geek is offline
For an extremely large value of 1/2
AKA: Greg R
None #0294 (Beach Cities Robotics)
Team Role: Student
 
Join Date: Feb 2004
Rookie Year: 2003
Location: Manhattan Beach, CA
Posts: 49
half geek will become famous soon enough
Send a message via AIM to half geek
Re: printf

And be sure to do an #include "stdio.h"
__________________
73 de W6DXN k

Parse this:
g r e g a ``n o s p a m" r o b i ``a t" d e v ``d o t" j a v a ``d o t" n e t
  #5   Spotlight this post!  
Unread 16-02-2007, 22:54
Dave K.'s Avatar
Dave K. Dave K. is offline
Engineer/Mentor
FRC #0930
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2005
Location: WI
Posts: 91
Dave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to behold
Re: printf

Quote:
Originally Posted by mormannoob View Post
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.
__________________
--Dave
  #6   Spotlight this post!  
Unread 17-02-2007, 00:01
BossOfTheGame BossOfTheGame is offline
Not the garbage man
FTC #0020 (The Rocketeers)
Team Role: Programmer
 
Join Date: Feb 2007
Rookie Year: 2003
Location: Clifton Park
Posts: 6
BossOfTheGame will become famous soon enough
Send a message via AIM to BossOfTheGame
Re: printf

Quote:
Originally Posted by steve d View Post
"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
  #7   Spotlight this post!  
Unread 17-02-2007, 00:41
TimCraig TimCraig is offline
Registered User
AKA: Tim Craig
no team
 
Join Date: Aug 2004
Rookie Year: 2003
Location: San Jose, CA
Posts: 221
TimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to behold
Re: printf

Quote:
Originally Posted by half geek View Post
And be sure to do an #include "stdio.h"

While that will work, technically you should use #include <stdio.h>
  #8   Spotlight this post!  
Unread 17-02-2007, 23:57
charrisTTI charrisTTI is offline
Ramblin' Wreck
AKA: Charles Harris
FRC #0623
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2003
Location: Vienna, VA
Posts: 106
charrisTTI has a spectacular aura aboutcharrisTTI has a spectacular aura about
Send a message via AIM to charrisTTI
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.
__________________
FRC 623 2003,2004,2005,2006,2007,2008, 2009, 2010, 2011
FRC 1900 2007
FVC 60 and 193 2006
FVC 3271 2007
FTC 226 and 369 2008, 2009, 2010, 2011
FTC 3806 2010
  #9   Spotlight this post!  
Unread 18-02-2007, 08:43
paulcd2000's Avatar
paulcd2000 paulcd2000 is offline
Accidentally speaks in C
AKA: Paul Dagnelie
FRC #1719 (The Umbrella Corp.)
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2006
Location: Baltimore
Posts: 368
paulcd2000 is a jewel in the roughpaulcd2000 is a jewel in the roughpaulcd2000 is a jewel in the rough
Send a message via AIM to paulcd2000
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>
__________________
"People don't say 'It's just a game' when their team is winning!" -- Scott Adams

5.5 students (on average)* $7/h *210 hours/student= $8085 of labor, all volunteered (not counting mentors', who are each that much)

We have blades on our robot?! ***sweeeeeet***

There are 11 types of people in the world. Those who can read binary, those who can't, and those who say this joke is supposed to be, "There are 10 types of people in the world. Those who can read binary and those who have a life."
  #10   Spotlight this post!  
Unread 18-02-2007, 11:50
et1337's Avatar
et1337 et1337 is offline
et1337
AKA: Evan Todd
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2006
Location: Columbus OH
Posts: 42
et1337 is a glorious beacon of lightet1337 is a glorious beacon of lightet1337 is a glorious beacon of lightet1337 is a glorious beacon of lightet1337 is a glorious beacon of light
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");
__________________
There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order.
et1337
  #11   Spotlight this post!  
Unread 18-02-2007, 13:28
Andrew Blair's Avatar
Andrew Blair Andrew Blair is offline
SAE Formula is FIRST with Gasoline.
FRC #0306 (CRT)
Team Role: Alumni
 
Join Date: Feb 2005
Rookie Year: 2004
Location: Corry
Posts: 1,193
Andrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond repute
Send a message via AIM to Andrew Blair Send a message via Yahoo to Andrew Blair
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.
__________________
Reading makes a full man, conference a ready man, and writing an exact man.
-Sir Francis Bacon

"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
-Albert Einstein
  #12   Spotlight this post!  
Unread 18-02-2007, 16:38
Tureyhall Tureyhall is offline
Registered User
AKA: Thomas Hall
FRC #0668 (Apes Of Wrath)
Team Role: Programmer
 
Join Date: Feb 2006
Rookie Year: 2004
Location: San Jose
Posts: 40
Tureyhall will become famous soon enoughTureyhall will become famous soon enough
Re: printf

Quote:
Originally Posted by Andrew Blair View Post
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.
__________________
/*Nerd Camouflage*/

2005 SVR - 668+1072+840 - Semifinalists.
2006 SVR - 668+1280+1693 - Finalists.
2007 SVR - 668+604+624 - Quarter-finalists, Xerox Creativity Award, two highest scores (290-2, 128-0) at SVR.
2007 Nat's - TBA.
  #13   Spotlight this post!  
Unread 18-02-2007, 17:51
Redneck's Avatar
Redneck Redneck is offline
Hacker Hick
AKA: Jamie (2.0) Moran
FRC #0599 (Robodox)
Team Role: Engineer
 
Join Date: Aug 2004
Rookie Year: 2004
Location: California
Posts: 90
Redneck is just really niceRedneck is just really niceRedneck is just really niceRedneck is just really nice
Send a message via AIM to Redneck
Re: printf

Quote:
Originally Posted by Tureyhall View Post
%.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.
__________________


Which badges can you claim?
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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


All times are GMT -5. The time now is 18:43.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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