[Tutorial] About Format and Printf
#1

Hi all, today im making an tutorial about FORMAT and PRINTF
format:
pawn Код:
Formats a string to include variables and other strings inside it.
printf:
pawn Код:
Outputs a formatted string on the console (the server window, not the in-game chat).
Format

Let learn about format, i will show you all a simple code and explain it bit by bit.
pawn Код:
public OnPlayerConnect(playerid)
{
    new name[32], str [64];
    GetPlayerName(playerid,name,sizeof(name));
    format(str,sizeof(str),"%s (%d) Has Joined The Server",name ,playerid);
    SendClientMessageToAllEx(playerid ,COLOR_GREY,str);
    SetPlayerColor(playerid, 0x808080AA);
    return 1;
}
Confused?? let me explain

pawn Код:
public OnPlayerConnect(playerid)
This is an Public. we are using our code under this because we are going to do a connect message
pawn Код:
new name[32], str [64];
We are introducing a string named "name" and "str" in this callback
[32] and [64] are used to tell the program that the size of the cell is 32 and 64, if u cant understand cells, i will explain it . here i used 32 as cell size which means the MAX characters i can use is 32. If the text if more than 32 characters, it will print the character below 32 characters and Leave the other. and also The last character WILL be '\0' So if u do name[32], it will use 31 characters and set the last one as '\0' if the total length of the string in 10, the remaining will be '\0'. '\0' is also know as the NULL TERMINATOR.
pawn Код:
If my text i want to print is "hi all , im making a tutorial which tells us about format and printf", and i use name[32].
it will produce a result: hi all, im making a tutorial wh  . (note: space is also a character)
click me for why we shouldn't use high size of the string
pawn Код:
GetPlayerName(playerid,name,sizeof(name));
GetPlayerName - this function is used to get the player name
playerid - playerid is used in almost all the basic functions. here we are using it to get the name of the player having that id.
name - we are storing the data in the string named "name"
sizeof(name) - finding the size of the string named "name"
pawn Код:
format(str,sizeof(str),"%s (%d) Has Joined The Server",name ,playerid);
format - function named "format" see the staring of the topic for more info.
str - storing the data we get here in a string named "str"
sizeof(str) - finding the size of the string named "str". right now the size is 64 since we already mentioned the size of it in the beginning. (NOTE: if my total characters is 6, the remaining will be \0)
"%s (%d) Has Joined The Server" - the format we want to be shown when a playerConnects
name ,playerid - We are telling the program that what it should display at %s and %d
Here Are some examples of the "%" things in format
More about Place Holders:
  • Adding Limit To Placeholder
%0.5f , %3s, %2.3f |.|.|
%0.5f = it limits %f (float) to a paticular amount of decimal points, in this case i used 0.5, MAX float decimal point = 5. i mean it will show the float not above than 5 decimal points. (EG) Allows: 0.002, Won't Allow: 23.222222, but it will show 23.222222 as 23.22222.
%3s idk how to explain this but i will show u an example. (EG) allow: hi |.|.| Wont Allow : hello || it will print "hel" since hel is the starting 3 letters of "hello".
%2.3f = It limits the float to Max 2 numbers in the left side of the Decimal and Max 3 numbers in the right side of the decimal point.
if the msg = 2563.1235, it will print = "63.123"

================================================== ===============================================
pawn Код:
Format Strings
Placeholder  Meaning
%b   Inserts a number at this position in binary radix
%c   Inserts a single character.
%d   Inserts an integer (whole) number
%f   Inserts a floating point number.
%i   Inserts an integer.
%s   Inserts a string.
%x   Inserts a number in hexadecimal notation.
%%   Inserts the literal '%'
pawn Код:
SendClientMessageToAllEx(playerid ,0xAFAFAFAA ,str);
This is a Function done by Sandra18[NL], which sends the code to all except the one who is the expectional.
str - here the string named "str" will show the result, (the format is stored in str. So, we are using str after format).

pawn Код:
SetPlayerColor(playerid, 0x808080AA);
This is just a small piece of code that just sets the player color to a deep grey.

So, lets see the result of the code we did.
pawn Код:
Secret_Agent (3) Has Joined The Server
it is over for format. (if want another example for format plz comment).

More About Format.

pawn Код:
here we do :
new string [32];
format(string, sizeof(string), "Hi there.");
It will print "Hi there.".
But if i do.
new string [32];
format(string, sizeof(string), "Hi there.");
string [9] = '!';
it replaces the 9th character of the string, which is '.' .
It will print it as : Hi There!
Now
PRINTF


printf is almost same like format but the only difference is that it will show the result in the console.
Pic Of Console :


|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|

Here is an example of printf from wikisamp.
pawn Код:
new number = 42;
printf("The number is %d.",number);  //-> result: The number is 42.
 
new string[]= "simple message";
printf("This is a %s containing the number %d.", string, number); //-> result: This is a simple message containing the number 42.
 
new character = 64; // You can use character = @ (for the same result) (64 is the ASCII code for '@')
printf("I'm %c home",character); //-> result: I'm @ home
Click Me and see the ASCII codes, use hex code and the place holder as '%c' .


But in printf, we dont need to introduce a string for storing data and also no need of an gametext, sendclientmessage,etc. i will explain this if you want.

Difference Between Print and Printf:
Print - Plain, what ever you type will be printed on the console
Printf - Custom format (see above {format})

Credits:

Me:For Making the Tutorial
Alonzo Torres: Console Pic
SA-MP Team: for samp
SAMP WIKI - Useful info
BigETI - Helping me
Other: :@ when i was about to finish the tut, i accidentaly closed this tab :@ and have to do this again
Reply


Messages In This Thread
About Format and Printf - by newbie scripter - 05.11.2013, 12:12
Re: About Format and Printf - by newbie scripter - 06.11.2013, 10:42
Re: About Format and Printf - by CrazyChoco - 06.11.2013, 11:21
AW: About Format and Printf - by BigETI - 06.11.2013, 11:22
Re: About Format and Printf - by newbie scripter - 06.11.2013, 11:30
AW: Re: About Format and Printf - by BigETI - 06.11.2013, 12:52
Re: About Format and Printf - by newbie scripter - 06.11.2013, 13:04
AW: Re: About Format and Printf - by BigETI - 06.11.2013, 13:27
Re: About Format and Printf - by newbie scripter - 06.11.2013, 13:39
AW: About Format and Printf - by BigETI - 06.11.2013, 14:39

Forum Jump:


Users browsing this thread: 1 Guest(s)