|
|
Tutorials

Cpp

|
Topic |
How is "sprintf()" used in C++?
|
|
Explanation |
|
sprintf() is an I/O function that writes the formatted data to an array.This function returns the total number of
characters, but on failure a negative value is returned.
Syntax:
int sprintf ( char * str, const char * format, ... );
The following table lists the type specifier used with sprintf()
| Type |
Description |
| c |
Character |
| d or i |
Signed decimal integer |
| e |
Scientific notation using e character |
| E |
Scientific notation using E character |
| f |
Decimal floating point |
| g |
Use the shorter of %e or %f |
| G |
Use the shorter of %E or %f |
| o |
Signed octal |
| s |
String of characters |
| u |
Unsigned decimal integer |
| x |
Unsigned hexadecimal integer |
| X |
Unsigned hexadecimal integer(capital) |
| p |
Pointer address |
| % |
A % followed by another % character will write % to the stream. |
Example:
#include <stdio.h>
int main ()
{
char arr [50];
int q=4;
sprintf (arr, "decimal %d in the array", q);
printf ("[%s] is print from the array\n",arr);
return 0;
}
|
Result:setbufeg.txt
[decimal 4 in the array] is print from the array
In the above example the "sprintf()" is used print the formatted data
from an array "arr".
|
| Note |
C++ is one of the most used programming languages in the world. Also known as "C with Classes".
Hope you enjoy this tutorial. Do send your feedback or suggestions on this C++ tutorial. This is a copyright content.
|
|
|
|