This is response to a question asked in a different topic...
For more details than explained here, internet search on printf() and sprintf() for C Programming. Below I explain these functions specifically related to TARGET.
The basic concept is to print a quoted string and include % format specifiers in the string to represent variables you want to print. For example below, we print an integer, vout, using %d. Note how the main text is written out between " ", the %d is included inside the quoted text, and the variable(s) to print are listed after the quoted text as a comma separated list.
int vout = 25;
printf(“Text to be printed: %d\xa”, vout);
Prints the following to the TARGET Console window:
Text to be printed: 25
In normal C, you would use the escape code \n for a newline or \t for a tab over, but in TARGET, we have to insert the ASCII code for a newline, which is 0x0A (hexadecimal, or 10 decimal). Hence we use \xa on the end of the quoted text to do a new line. To tab over we have to use \x9 for the ASCII 9 or TAB character. Go here (
https://www.asciitable.com/) to see the full ASCII table. The only ASCII codes I have used in TARGET are \xa and \x9.
Here are all the % format specifiers I have tested in TARGET. Others may be work, but not all the C printf() % format specifiers are supported in TARGET.
%d – decimal integer
%u – unsigned decimal integer
%x – hexadecimal
%f – floating point (e.g. %0.1f prints xx.x)
%c – char
%s – string
Does not work
%b - binary
sprintf() works the same except that instead of printing to the TARGET console, sprintf() "prints" into a string referenced by an alias. So here is an example of sprintf():
int vout = 25;
char mystring; Dim(&mystring, 256);
sprintf(&mystring, "Text to be printed: %d\xa”, vout);
Sets mystring equal to the following text: "Text to be printed: 25"
Here are several examples:
include "target.tmh"
int main()
{
if(Init(&EventHandle)) return 1;
int var1 = 1;
int var2 = 2;
int var3 = 3;
float float1 = 1.234;
float float2 = 0.1234;
alias string1="This is my string.";
int hexordec = 0x1F; // 1F hex = 16 decimal
printf("Print multiple varaibles: %d, %d, %d\xa", var1, var2, var3);
// prints:
// Print multiple varaibles: 1, 2, 3
printf("This is my float, %0.2f, and this is my integer, %d.\xa", float1, var1);
// prints:
// This is my float, 1.23, and ths is my integer, 1.
printf("This is a percentage: %0.1f%%\xa", float2 * 100);
// prints:
// This is a percentage: 12.3%
// Note in order to print a %, you must excape the % by using %%.
printf("Did you know 1 + 1 = %d? ", 1 + 1);
printf("But 1 / 1 = 1. ");
printf("More importantly, did you notice these 3 printf statements printed on a single line?\xa");
// prints:
// Did you know 1 + 1 = 2? But 1 / 1 = 1. More importantly, did you notice these 3 printf statements printed on a single line?
printf("How do I print a string? Do this: %s\xa", &string1);
// prints:
// How do I print a string? Do this: This is my string.
printf("Hex or Decimal? %x hex = %d deicmal\xa", hexordec, hexordec);
// prints:
// Hex or Decimal? 1f hex = 16 decimal
// And an sprintf example
char buffer; Dim(&buffer, 256); // create a 256 byte buffer for sprintf.
sprintf(&buffer, "A decimal is %d. A float is %0.2f. And I can add a string like this: %s", var1, float1, &string1);
printf("%s\xa", &buffer);
// This is a way you can build a string variable (&buffer) with text and results from variables.
// Then I print it out using a simple printf() and we get this:
// A decimal is 1. A float is 1.23. And I can add a string like this: This is my string.
}
int EventHandle(int type, alias o, int x)
{
DefaultMapping(&o, x);
}