YouTip LogoYouTip

C Function Vprintf

[![Image 1: C Standard Library - ](#) C Standard Library - ](#)\n\n## Description\n\nThe C library function **int vprintf(const char *format, va_list arg)** sends formatted output to the standard output stream (stdout) using a variable argument list.\n\n## Declaration\n\nBelow is the declaration for the vprintf() function.\n\n```c\nint vprintf(const char *format, va_list arg)\n\n## Parameters\n\n* **format** -- This is the string that contains the text to be written to stdout. It can optionally contain embedded format tags that are replaced by the values specified in subsequent additional arguments and formatted as requested. The format tag attributes are **%[.precision]specifier**, as explained in the table below:\n\n| specifier (specifier) | Output |\n| --- | --- |\n| c | Character |\n| d or i | Signed decimal integer |\n| e | Scientific notation (uses 'e') |\n| E | Scientific notation (uses 'E') |\n| f | Decimal floating point |\n| g | Uses the shorter of %e or %f |\n| G | Uses the shorter of %E or %f |\n| o | Signed octal |\n| s | String of characters |\n| u | Unsigned decimal integer |\n| x | Unsigned hexadecimal integer |\n| X | Unsigned hexadecimal integer (uppercase) |\n| p | Pointer address |\n| n | Nothing is printed |\n| % | A literal % character |\n\n| flags (flags) | Description |\n| --- | --- |\n| - | Left-justify within the given field width; right justification is the default (see width sub-specifier). |\n| + | Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign. |\n| (space) | If no sign is going to be written, a blank space is inserted before the value. |\n| # | Used with o, x or X specifiers the value is preceded with 0, 0x or 0X respectively for values different than zero. Used with e, E and f, it forces the output to contain a decimal point even if no digits follow. By default, if no digits follow, no decimal point is written. Used with g or G the result is the same as with e or E but trailing zeros are not removed. |\n| 0 | Left-pads the number with zeroes (0) instead of spaces when padding is specified (see width sub-specifier). |\n\n| width (width) | Description |\n| --- | --- |\n| (number) | Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with spaces. The value is not truncated even if the result is larger. |\n| * | The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. |\n\n| .precision (.precision) | Description |\n| --- | --- |\n| .number | For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0. For e, E and f specifiers: this is the number of digits to be printed after the decimal point. For g and G specifiers: This is the maximum number of significant digits to be printed. For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered. For c type: no effect. When no precision is specified, the default is 1. If specified with no explicit value, precision is taken as 0. |\n| .* | The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. |\n\n| length (length) | Description |\n| --- | --- |\n| h | The argument is interpreted as a short int or unsigned short int (only valid for integer specifiers: i, d, o, u, x and X). |\n| l | The argument is interpreted as a long int or unsigned long int for integer specifiers (i, d, o, u, x and X), and as a wide character or wide character string for specifiers c and s. |\n| L | The argument is interpreted as a long double (only valid for floating point specifiers: e, E, f, g and G). |\n\n* **arg** -- A representation of a variable arguments list. This should be initialized by the va_start macro defined in .\n\n## Return Value\n\nIf successful, the total number of characters written is returned, otherwise a negative number is returned.\n\n## Example\n\nThe following example demonstrates the usage of the vprintf() function.\n\n## Example\n\n```c\n#include \n\n#include \n\nvoid WriteFrmtd(char*format, ...)\n\n{\n\nva_list args;\n\nva_start(args, format);\n\nvprintf(format, args);\n\nva_end(args);\n\n}\n\nint main ()\n\n{\n\nWriteFrmtd("%d variable argumentn",1);\n\nWriteFrmtd("%d variable %sn",2,"arguments");\n\nreturn(0);\n\n}\n\nLet us compile and run the above program to produce the following result:\n\n1 variable argument\n2 variable arguments\n\n[![Image 2: C Standard Library - ](#) C Standard Library - ](#)
← Cpp TutorialC Function Rename β†’