The sprintf () function in C

The int sprintf function (char * str, const char * format, ...) in the standard C library sends the formatted output to a string str.

The int sprintf function (char * str, const char * format, .) in the standard C library sends the formatted output to a string str .

Declare the sprintf () function in C

Below is the declaration for the sprintf () function in C:

 int sprintf ( char * str , const char * format , .) 

Parameters

str - This is a pointer to an array of char elements where the result string is stored.

format - This is the string containing the text to be written to the buffer. 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

 

flags Description - Align left into the given field width. The alignment must be the default + Forced to precede the result with a plus or minus (+ or -) even with positive numbers. By default, only negative numbers are preceded by a - (space). If no symbols are written, then a space will be inserted before the value # Used with the specifier o, x or X. The value is preceded by 0, 0x or 0X corresponding to values ​​other than 0. Using with e, E and f, it forces the recorded output to acquire a decimal pointer even if not any number followed. By default, if no digit follows, no decimal pointer is written. Use with g or G, the result is the same as e or E but the zeroes at the end do not have to be removed from the left 0 pad (left-pad) of the number with the 0s instead of the spaces

 

width Description (number) The 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 cut even if the result is too large. The width is not specified in the format format string, but as an additional integer value parameter preceding that parameter must be formatted

 

.precision Description.number For Integer Specifiers (d, i, o, u, x, X) - then Precision specifies the minimum number of digits to be written. If the recorded value is shorter than this number, the result is padded with 0 at the beginning. Value is not cut even if the result is longer. A precision of 0 means that no character is written to the value 0. For e, E and f specifier: this is the number of digits to be printed after the decimal point. With g and G specifier: this is the maximum number of digits to be printed. With s specifier: this is the minimum number of characters to be printed. By default, all characters are printed until the last null character is encountered. With type c: it has no effect. When no precision is specified, the default is 1. If the period is specified without a clear precision value, 0 is assumed. * Precision is not specified in the format format string, but as an additional raw value parameter precedes that parameter that must be formatted

 

length Description The parameter is interpreted as a short int or unsigned short int (applied only to integer specifiers: i, d, o, u, x and X) l Parameters are interpreted as a long int or unsigned long int for integer specifier (i, d, o, u, x and X), and as a wide char or wide char string for specifiers are c and s L The parameter is interpreted as a long double (applied only to floating point specifier: e, E, f, g and G)

Additional parameters - Depending on the format format string, this function may have an additional parameter array, each containing a value to be inserted instead of each% -tag specified in the format parameter, if. This number of parameters should be the same number as% -tags that expect a value.

Returns the value

If successful, the total number of characters recorded will be returned to exclude null characters appended at the end of the string. If it fails, return a negative number.

For example

The following program C illustrates the usage of the sprintf () function in C:

 #include #include int main () { char str [ 80 ]; sprintf ( str , "Gia tri cua Pi = %f" , M_PI ); puts ( str ); return ( 0 ); } 

Compiling and running the above C program will result:

Picture 1 of The sprintf () function in C

According to Tutorialspoint

Previous lesson: Function fprintf () in C

Next lesson: Function vfprintf () in C

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile