Sprintf

From Game Editor

Revision as of 07:48, 11 October 2011 by Skydereign (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search
int sprintf(char *s, const char *format, ...);

sprintf is a function in C that allows you to format text and write it into a string. The formatting allows you to display the contents of variables, making string handling, and debugging much easier. In gameEditor, sprintf is commonly used to set actors' text variable. sprintf returns the number of characters written (not including the ending '\0').


sprintf Input

s

  • This input variable is the string that sprintf will write to. It converts the variables into a string using the format, and ultimately writes it into s. sprintf terminates the string with a '\0'.

format

  • The format string is essentially a template of what the text you are writing is going to look like. The characters in this string can be broken into two types. The first are just ordinary characters, which will be copied directly into the string s. The second type of characters are used for conversions. These allow you to write subsequent variables into string s, and even allow you to format them. Every conversion starts with the % character, and ends with the conversion type.


For string variables write this:

strcpy(text, (char *)YourStringVar);

or

sprintf(text, "%s", YourStringVar);

%s is for string.

For integer and real variables the same as above, %d for an integer variable and %f for real variables.

example:

sprintf(text, "A string: %s\nAn integer: %d\nA float value: %f", "Hello!", 7, 3.4);

That changes your text to:

A string: Hello!

An integer: 7

A float value: 3.4

("\n" means new line)

Avaible escape sequences

Escape sequences are things like \n or \s.

specifer output example
c character a
d or i signed decimal integer 392
f decimal floating point 392.65
s string of characters sample
x unsigned hexadecimal integer 7fa
X unsigned hexadecimal integer (capital letters) 7FA
n new line
% puts out a per cent symbol %