[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
#2

Any reviews??
Reply
#3

Wait, why do you bump this topic?
Reply
#4

Some (or should I say "a lot of"?) explanations are wrong, lacks explanation of both functions, not giving credits to the wiki topic OP, and the picture above does not exactly match to the example below it.

Before you attempt to correct this tutorial, I'll give you some questions to learn and answer it all by yourself:
  • What does AMX cell size mean?
  • What is the maximum string/array size GetPlayerName()'s first argument requires?
  • What are the other possibilities to use sizeof, and what is its purpose?
  • What kind of placeholders does format() else support?
  • Is there any better and easier way to write a single character instead of a number?
Reply
#5

ANSWERS:
1. Cell size means the max number of characters allowed , or like limit.
2. can't understand what you are asking
3. u can use sizeof in OnPlayerText, etc. its purpose is to get the size of the text written
4. % placeholders?? cant understand.
5. %c?
or
5.
pawn Код:
new array[5];
array[0] = 1,
array[1] = 2,
array[2] = 3,
array[3] = 4,
array[4] = 5
idk whether these are right

Quote:
Originally Posted by CrazyChoco
Посмотреть сообщение
Wait, why do you bump this topic?
This is my 2nd tut and i need reviews, any mistake, etc
Reply
#6

It's lovely to see, how much effort you just gave to re-search for those answers. *sarcastic*

Quote:
Originally Posted by newbie scripter
Посмотреть сообщение
ANSWERS:
1. Cell size means the max number of characters allowed , or like limit.
2. can't understand what you are asking
3. u can use sizeof in OnPlayerText, etc. its purpose is to get the size of the text written
4. % placeholders?? cant understand.
5. %c?
or
5.
pawn Код:
new array[5];
array[0] = 1,
array[1] = 2,
array[2] = 3,
array[3] = 4,
array[4] = 5
  1. Almost correct. A cell can actually store 4 bytes, hence 32 bit. But you just wrote
    Quote:

    [32] and [64] are used to tell the program that the size of the cell is 32bit and 64bit

    which is completly wrong!
  2. My bad, I was asking for the second argument.
    Код:
    GetPlayerName(first argument, second argument, third argument)
    The second argument obviously stores a string. Hint: See SA:MP limits and there is a well know definition used in many scripts.
  3. Not asked that way, neither OnPlayerText() needs sizeof at all.
    I was asking about its syntax and its functionality.
  4. Read the whole format() wiki page, before you copy some of its content.
  5. Neither asked that way. This question refers to
    Quote:
    pawn Код:
    new character = 64;
    printf("I'm %c home",character); //-> result: I'm @ home
    where you did
    pawn Код:
    new character = 64;
    where you just represented a single character as a number.
    Do you think there is a much easier way to represent a single character, by just replacing the number
    pawn Код:
    64
    with something else?
Reply
#7

2. the second argument is used to store the data we get from the function to the string we mention
3. idk much of it xD. let me try . it is used to get the size of (or) the No. of characters used in that string??
5. '@' ?
EDIT: (i was reading the wiki page) 4. eg = %0.5f , %3s. %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 it in this lang. (EG) allow: hi .. Wont Allow : hello || it may print (i am not sure) "hel"

"This forum requires that you wait 240 seconds between posts. Please try again in 6 seconds."
Reply
#8

Quote:
Originally Posted by newbie scripter
Посмотреть сообщение
2. the second argument is used to store the data we get from the function to the string we mention
3. idk much of it xD. let me try . it is used to get the size of (or) the No. of characters used in that string??
5. '@' ?

"This forum requires that you wait 240 seconds between posts. Please try again in 6 seconds."
About 2.:
I've already mentioned that... I was talking about the limits...
You just did
pawn Код:
new name[32], str [64];
GetPlayerName(playerid,name,sizeof(name));
pawn Код:
name[32]
Do you think, that this can be optimized?
Quote:

Hint: See SA:MP limits and there is a well know definition used in many scripts.

About 3.:
sizeof can be used that way aswell:
pawn Код:
sizeof something
pawn Код:
(sizeof something)
pawn Код:
sizeof (something)
pawn Код:
(sizeof(something))
so you can't say, that sizeof only relies on brackets.

During pre-compilation sizeof simply returns the amount of cells an array has been defined.

pawn Код:
new something[16];
print(" \"sizeof something\" equals "#sizeof something".");
Prints
Код:
 "sizeof something" equals 16.
About 5.:
This is correct.


Edit:
About 4.:
It would be very useful to explain this in the main post for other people.
Reply
#9

i will edit my post

EDIT: about4: is the one i explained is right??
Reply
#10

Don't skip major mistakes while improving this tutorial!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)