Formatted Output Functions (The GNU C Library) (2024)

Next: Dynamically Allocating Formatted Output, Previous: Other Output Conversions, Up: Formatted Output [Contents][Index]

12.12.7 Formatted Output Functions

This section describes how to call printf and related functions.Prototypes for these functions are in the header file stdio.h.Because these functions take a variable number of arguments, youmust declare prototypes for them before using them. Of course,the easiest way to make sure you have all the right prototypes is tojust include stdio.h.

Function: int printf (const char *template, …)

Preliminary:| MT-Safe locale| AS-Unsafe corrupt heap| AC-Unsafe mem lock corrupt| See POSIX Safety Concepts.

The printf function prints the optional arguments under thecontrol of the template string template to the streamstdout. It returns the number of characters printed, or anegative value if there was an output error.

Function: int wprintf (const wchar_t *template, …)

Preliminary:| MT-Safe locale| AS-Unsafe corrupt heap| AC-Unsafe mem lock corrupt| See POSIX Safety Concepts.

The wprintf function prints the optional arguments under thecontrol of the wide template string template to the streamstdout. It returns the number of wide characters printed, or anegative value if there was an output error.

Function: int fprintf (FILE *stream, const char *template, …)

Preliminary:| MT-Safe locale| AS-Unsafe corrupt heap| AC-Unsafe mem lock corrupt| See POSIX Safety Concepts.

This function is just like printf, except that the output iswritten to the stream stream instead of stdout.

Function: int fwprintf (FILE *stream, const wchar_t *template, …)

Preliminary:| MT-Safe locale| AS-Unsafe corrupt heap| AC-Unsafe mem lock corrupt| See POSIX Safety Concepts.

This function is just like wprintf, except that the output iswritten to the stream stream instead of stdout.

Function: int sprintf (char *s, const char *template, …)

Preliminary:| MT-Safe locale| AS-Unsafe heap| AC-Unsafe mem| See POSIX Safety Concepts.

This is like printf, except that the output is stored in the characterarray s instead of written to a stream. A null character is writtento mark the end of the string.

The sprintf function returns the number of characters stored inthe array s, not including the terminating null character.

The behavior of this function is undefined if copying takes placebetween objects that overlap—for example, if s is also givenas an argument to be printed under control of the ‘%s’ conversion.See Copying Strings and Arrays.

Warning: The sprintf function can be dangerousbecause it can potentially output more characters than can fit in theallocation size of the string s. Remember that the field widthgiven in a conversion specification is only a minimum value.

To avoid this problem, you can use snprintf or asprintf,described below.

Function: int swprintf (wchar_t *ws, size_t size, const wchar_t *template, …)

Preliminary:| MT-Safe locale| AS-Unsafe heap| AC-Unsafe mem| See POSIX Safety Concepts.

This is like wprintf, except that the output is stored in thewide character array ws instead of written to a stream. A nullwide character is written to mark the end of the string. The sizeargument specifies the maximum number of characters to produce. Thetrailing null character is counted towards this limit, so you shouldallocate at least size wide characters for the string ws.

The return value is the number of characters generated for the giveninput, excluding the trailing null. If not all output fits into theprovided buffer a negative value is returned, and errno is set toE2BIG. (The setting of errno is a GNU extension.) Youshould try again with a bigger output string. Note: this isdifferent from how snprintf handles this situation.

Note that the corresponding narrow stream function takes fewerparameters. swprintf in fact corresponds to the snprintffunction. Since the sprintf function can be dangerous and shouldbe avoided the ISOC committee refused to make the same mistakeagain and decided to not define a function exactly corresponding tosprintf.

Function: int snprintf (char *s, size_t size, const char *template, …)

Preliminary:| MT-Safe locale| AS-Unsafe heap| AC-Unsafe mem| See POSIX Safety Concepts.

The snprintf function is similar to sprintf, except thatthe size argument specifies the maximum number of characters toproduce. The trailing null character is counted towards this limit, soyou should allocate at least size characters for the string s.If size is zero, nothing, not even the null byte, shall be written ands may be a null pointer.

The return value is the number of characters which would be generatedfor the given input, excluding the trailing null. If this value isgreater than or equal to size, not all characters from the result havebeen stored in s. If this happens, you should be wary of usingthe truncated result as that could lead to security, encoding, orother bugs in your program (see Truncating Strings while Copying).Instead, you should try again with a bigger outputstring. Here is an example of doing this:

/* Construct a message describing the value of a variable whose name is name and whose value is value. */char *make_message (char *name, char *value){ /* Guess we need no more than 100 bytes of space. */ size_t size = 100; char *buffer = xmalloc (size);
 /* Try to print in the allocated space. */ int buflen = snprintf (buffer, size, "value of %s is %s", name, value); if (! (0 <= buflen && buflen < SIZE_MAX)) fatal ("integer overflow");
 if (buflen >= size) { /* Reallocate buffer now that we know how much space is needed. */ size = buflen; size++; buffer = xrealloc (buffer, size); /* Try again. */ snprintf (buffer, size, "value of %s is %s",name, value); } /* The last call worked, return the string. */ return buffer;}

In practice, it is often easier just to use asprintf, below.

Attention: In versions of the GNU C Library prior to 2.1 thereturn value is the number of characters stored, not including theterminating null; unless there was not enough space in s tostore the result in which case -1 is returned. This waschanged in order to comply with the ISOC99 standard.

Formatted Output Functions (The GNU C Library) (2024)

References

Top Articles
Latest Posts
Article information

Author: Tish Haag

Last Updated:

Views: 6021

Rating: 4.7 / 5 (47 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Tish Haag

Birthday: 1999-11-18

Address: 30256 Tara Expressway, Kutchburgh, VT 92892-0078

Phone: +4215847628708

Job: Internal Consulting Engineer

Hobby: Roller skating, Roller skating, Kayaking, Flying, Graffiti, Ghost hunting, scrapbook

Introduction: My name is Tish Haag, I am a excited, delightful, curious, beautiful, agreeable, enchanting, fancy person who loves writing and wants to share my knowledge and understanding with you.