| ID | CS000897 | Creation date | April 17, 2008 |
| Platform | S60 3rd Edition, FP1 | Tested on devices | Nokia N93 |
| Category | Open C | Subcategory | Files/Data |
| Keywords (APIs, classes, methods, functions): sprintf(), snprintf() |
sprintf() and snprintf() functions can be used in Open C to convert numeric values into formatted numeral strings. Both of these functions return the number of characters printed.
Note: In order to use this code, you need to install Open C plug-in.
This snippet can be self-signed.
The following libraries are required:
LIBRARY libc.lib
#include <stdio.h> // sprintf(), snprintf()
#define MAX_STR_LEN 8
int main(void)
{
int printed = 0;
char numeral_string[MAX_STR_LEN];
/* - sprintf() - */
printed = sprintf(numeral_string, "%d", 123);
printf("123 in decimal is %s\n", numeral_string);
printf("printed characters: %d\n", printed);
printed = sprintf(numeral_string, "%x", 123);
printf("123 in hexadecimal is %s\n", numeral_string);
printf("printed characters: %d\n", printed);
/* - snprintf() - */
printed = snprintf(numeral_string, 7, "%o", 123);
printf("123 in octal is %s\n", numeral_string);
printf("printed characters: %d\n", printed);
/* only first three characters + trailing \0 is written to the buffer */
printed = snprintf(numeral_string, 4, "%d", 1234567890);
printf("123|4567890 in decimal is %s\n", numeral_string);
printf("printed characters: %d\n", printed);
return 0;
}
Four different number to string conversions are executed and displayed to the standard output.
No related wiki articles found