Table of Contents
This guide covers the function vsprintf ()() in c with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
Declare the Function Vsprintf ()() in C
Below is the declaration for vsprintf () function in C:
int vsprintf ( char * str , const char * format , va_list arg )
Parameters
Str - This is the array of char elements where the result string is stored.
Format - This is the string containing text to be written to str. It may optionally contain formatting tags that can be embedded that are replaced by values defined in subsequent additional parameters and formatted as required. Prototype tags of the format % [flags] [width] [. Precision] [length] specifier , explained as follows:
Character specifier Character d or i Decimal integer with the sign e Scientific symbol (mantissa / exponent) uses the character e E Scientific notation (mantissa / exponent) using characters E f Actual floating point number Decimal g Short use of% e or% f G Compact use of% E or% fo Octal number signed s Character string u Unsigned decimal number x Hexadecimal integers mark X Unsigned hexadecimal integers (uppercase letters) p Pointer address n Do not print anything% characters
Arg - An object representing the variable parameter list. It should be initialized by the va_start macro defined in.
Returns the value
If successful, the total number of characters recorded will be returned, otherwise a negative number will be returned.
For example
The following program C illustrates the usage of the vsprintf () function in C:
#include #include char buffer [ 80 ]; int vspfunc ( char * format , .) { va_list aptr ; int ret ; va_start ( aptr , format ); ret = vsprintf ( buffer , format , aptr ); va_end ( aptr ); return ( ret ); } int main () { int i = 5 ; float f = 27.0 ; char str [ 50 ] = "QTM xin chao cac ban !!!" ; vspfunc ( "%d %f %s" , i , f , str ); printf ( "%sn" , buffer ); return ( 0 ); }
Compile and run the above C program to see the results.
According to Tutorialspoint
Previous lesson: Function vfprintf () in C
Next lesson: Function fscanf () in C
Frequently Asked Questions
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Was this article helpful?
Your feedback helps us improve.
Reader Comments 0
Sign in with email or Google to join the discussion.