View Single Post
  #4   Spotlight this post!  
Unread 18-01-2008, 13:57
gnormhurst's Avatar
gnormhurst gnormhurst is offline
Norm Hurst
AKA: gnorm
#0381 (The Tornadoes)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Trenton, NJ
Posts: 138
gnormhurst will become famous soon enoughgnormhurst will become famous soon enough
Re: printf() syntax and parameters

Maybe this is a holdover from the older version of printf(), but I've always been careful to cast (convert) any "char" types (one byte numbers) to "int" type (two byte numbers on the RC) like this:
Code:
printf( x value: %d,  y value: %d\n\r", (int)XAXIS, (int)YAXIS );
Otherwise you can get some huge, crazy values printing out.

Here's the quick story about printf() on any system:

printf takes a variable number of arguments. The first one must be there and it must be a string. It's called the "format string". That string can have anything in it. You put a '%d' each place where you want it to print a value. For each %d in the format string there must be a matching argument after the format string. They are matched up in the order the %d's appear in the format string.

You don't have to have any %d's. Then you are just printing a message that's always the same:
Code:
printf( "Starting autonomous...\n\r" );
Note the \n and \r. These make it go to a new line. The FRC printf() wants to see \r (I'm not clear on why) but in the rest of the world you would just use a \n ("newline"). Using both makes it work in all cases.

Use %d when you want to print an int type. There are other ones, like %f for "float" types, but if you avoid using floats on the robot controller (and you should avoid using floats) then you won't have a need for %f.
__________________
Trenton Tornadoes 381
2004 Philadelphia Regional Winners
2006 Xerox Creativity Award
---
My corner of the USPTO.
My favorite error message from gcc: main is usually a function
My favorite error message from Windows: There is not enough disk space available to delete this file.