Previous Thread
Next Thread
Print Thread
Rate This Thread
Hop To
#4508687 - 02/27/20 10:38 PM How to use printf() and sprintf() in TARGET  
Joined: Jul 2016
Posts: 61
Drakoz Offline
Junior Member
Drakoz  Offline
Junior Member

Joined: Jul 2016
Posts: 61
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:

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


Last edited by Drakoz; 02/28/20 09:24 PM.
Inline advert (2nd and 3rd post)

#4508747 - 02/28/20 09:35 AM Re: How to use printf() and sprintf() in TARGET [Re: Drakoz]  
Joined: May 2001
Posts: 468
Joao Muas Offline
Member
Joao Muas  Offline
Member

Joined: May 2001
Posts: 468
Portugal
Splendid clarification. Thank you Drakoz!!


Moderated by  RacerGT 

Quick Search
Recent Articles
Support SimHQ

If you shop on Amazon use this Amazon link to support SimHQ
.
Social


Recent Topics
Carnival Cruise Ship Fire....... Again
by F4UDash4. 03/26/24 05:58 PM
Baltimore Bridge Collapse
by F4UDash4. 03/26/24 05:51 PM
The Oldest WWII Veterans
by F4UDash4. 03/24/24 09:21 PM
They got fired after this.
by Wigean. 03/20/24 08:19 PM
Grown ups joke time
by NoFlyBoy. 03/18/24 10:34 PM
Anyone Heard from Nimits?
by F4UDash4. 03/18/24 10:01 PM
RIP Gemini/Apollo astronaut Tom Stafford
by semmern. 03/18/24 02:14 PM
10 years after 3/8/2014
by NoFlyBoy. 03/17/24 10:25 AM
Copyright 1997-2016, SimHQ Inc. All Rights Reserved.

Powered by UBB.threads™ PHP Forum Software 7.6.0