[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


snprintf

Format a character string array.

Compatibility:

This function is compatible with the following targets:

ANSI

BeOS

EMB/RTOS

Mac OS

Palm OS

Win32


Prototype:
#include <stdio.h>
int snprintf(char * s, size_t n, 
  const char * format, ...);
Parameters:

Parameters for this facility are:

s  
char *  
A string to write to  
n  
size_t  
Max number of chars to be written to s  
format  
const char *  
The format string  

Remarks:

The snprintf() function works identically to fprintf() except that the output is written into the array s instead of to a stream. If n is zero nothing is written; otherwise, any characters beyond the n-1st are discarded rather than being written to the array and a null character is appended at the end.

For specifications concerning the output control string and conversion specifiers please see: "Output Control String and Conversion Specifiers."

Return:

Snprintf() returns the number of characters that would have been assigned to s, had n been sufficiently large, not including the null character or a negative value if an encoding error occurred. Thus, the null-terminated output will have been completely written if and only if the returned value is nonnegative and less than n.

Example Of Snprintf() Usage:
#include <stdio.h>

int main()
{	
	int i = 1;
	static char s[] = "Metrowerks";
	char dest[50];
	int retval;

	retval = snprintf(dest, 5, "%s is number %d!", s, i);
	printf("n too small, dest = |%s|, retval = %i\n", dest, retval);
	retval = snprintf(dest, retval, "%s is number %d!", s, i);
	printf("n right size, dest = |%s|, retval = %i\n", dest, retval);

	return 0;
}

Output:
n too small, dest = |Metr|, retval = 23
n right size, dest = |Metrowerks is number 1|, retval = 23


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 16, 2000