Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
C++ Tutorial
C++ Language Tutorial - Object Oriented Programing - OOPs History of C++
C++ Language Tutorial - Object Oriented Programing - OOPs Structure of C++
C++ Language Tutorial - Object Oriented Programing - OOPs C++ Datatypes
C++ Language Tutorial - Object Oriented Programing - OOPs Variables
C++ Language Tutorial - Object Oriented Programing - OOPs Constants
C++ Language Tutorial - Object Oriented Programing - OOPs Operators
C++ Language Tutorial - Object Oriented Programing - OOPs Control Structures
C++ Language Tutorial - Object Oriented Programing - OOPs Arrays
C++ Language Tutorial - Object Oriented Programing - OOPs Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Class
C++ Language Tutorial - Object Oriented Programing - OOPs Predefined Functions
C++ Language Tutorial - Object Oriented Programing - OOPs I/O Functions
C++ Language Tutorial - Object Oriented Programing - OOPs String, Character Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Mathematical Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Time Date Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Dynamic Allocation
C++ Language Tutorial - Object Oriented Programing - OOPs Utility Functions
C++ Language Tutorial - Object Oriented Programing - OOPs OOP's Concept
C++ Language Tutorial - Object Oriented Programing - OOPs Special Topics
C++ Language Tutorial - Object Oriented Programing - OOPs Type casting
C++ Language Tutorial - Object Oriented Programing - OOPs Feedback
C++ Language Tutorial - Object Oriented Programing - OOPs Ask Your Doubts
 




vsprintf() - I/O Function


Tutorials Cpp

Topic

How is "vsprintf()" used in C++?



Explanation

vsprintf() is an I/O function is used to write the contents of the format string to the stream, with a pointer to a list of arguments replacing the argument list. This function returns the totalnumber of characters, but on failure a negative value is returned.

Syntax:
    int vsprintf (char * str, const char * format, va_list arg );

The following table lists the type specifier used with vsprintf()

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 <stdarg.h>
   #include <stdio.h>
   void vsform (char *string, char *format, ...);
   char arr [] = "%s  %s  %s\n";
   int main(void)
     {
      char string[100];
      vsform(string, arr , "A", "B", "C");
      printf("The strings are:  %s\n", string);
     }
   void vsform(char *string, char *fmt, ...)
     {
      va_list arg_ptr;
      va_start(arg_ptr, fmt);
      vsprintf(string, fmt, arg_ptr);
      va_end(arg_ptr);
     }

Result:setbufeg.txt
    The strings are:  A B C

In the above example the array "arr" is used to write the content to get the formatted output with multiple arguments.










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.

Other Links

web hosting