一线明星陪夜价位:memcmp.memset.sprintf 的用法 和 意思

来源:百度文库 编辑:中科新闻网 时间:2024/04/27 15:44:19
memcmp.memset.sprintf 的用法 和 意思

memcmp
Compare characters in two buffers.
比较两块内存中的字符

memset
Sets buffers to a specified character
将内存块设置为指定的字符

sprintf
Write formatted data to a string
将格式化的数据写到字符串

memcmp
Compare characters in two buffers.

int memcmp( const void *buf1, const void *buf2, size_t count );

Routine Required Header Compatibility
memcmp <memory.h> or <string.h> ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

The return value indicates the relationship between the buffers.

Return Value Relationship of First count Bytes of buf1 and buf2
< 0 buf1 less than buf2
0 buf1 identical to buf2
> 0 buf1 greater than buf2

Parameters

buf1

First buffer

buf2

Second buffer

count

Number of characters

Remarks

The memcmp function compares the first count bytes of buf1 and buf2 and returns a value indicating their relationship.

Example

/* MEMCMP.C: This program uses memcmp to compare
* the strings named first and second. If the first
* 19 bytes of the strings are equal, the program
* considers the strings to be equal.
*/

#include <string.h>
#include <stdio.h>

void main( void )
{
char first[] = "12345678901234567890";
char second[] = "12345678901234567891";
int result;

printf( "Compare '%.19s' to '%.19s':\n", first, second );
result = memcmp( first, second, 19 );
if( result < 0 )
printf( "First is less than second.\n" );
else if( result == 0 )
printf( "First is equal to second.\n" );
else if( result > 0 )
printf( "First is greater than second.\n" );
printf( "Compare '%.20s' to '%.20s':\n", first, second );
result = memcmp( first, second, 20 );
if( result < 0 )
printf( "First is less than second.\n" );
else if( result == 0 )
printf( "First is equal to second.\n" );
else if( result > 0 )
printf( "First is greater than second.\n" );
}

Output

Compare '1234567890123456789' to '1234567890123456789':
First is equal to second.
Compare '12345678901234567890' to '12345678901234567891':
First is less than second.

Buffer Manipulation Routines

See Also _memccpy, memchr, memcpy, memset, strcmp, strncmp

memset
Sets buffers to a specified character.

void *memset( void *dest, int c, size_t count );

Routine Required Header Compatibility
memset <memory.h> or <string.h> ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

memset returns the value of dest.

Parameters

dest

Pointer to destination

c

Character to set

count

Number of characters

Remarks

The memset function sets the first count bytes of dest to the character c.

Example

/* MEMSET.C: This program uses memset to
* set the first four bytes of buffer to "*".
*/

#include <memory.h>
#include <stdio.h>

void main( void )
{
char buffer[] = "This is a test of the memset function";

printf( "Before: %s\n", buffer );
memset( buffer, '*', 4 );
printf( "After: %s\n", buffer );
}

Output

Before: This is a test of the memset function
After: **** is a test of the memset function

Buffer Manipulation Routines

See Also _memccpy, memchr, memcmp, memcpy, _strnset

sprintf, swprintf
Write formatted data to a string.

int sprintf( char *buffer, const char *format [, argument] ... );

int swprintf( wchar_t *buffer, const wchar_t *format [, argument] ... );

Routine Required Header Compatibility
sprintf <stdio.h> ANSI, Win 95, Win NT
swprintf <stdio.h> or <wchar.h> ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

sprintf returns the number of bytes stored in buffer, not counting the terminating null character. swprintf returns the number of wide characters stored in buffer, not counting the terminating null wide character.

Parameters

buffer

Storage location for output

format

Format-control string

argument

Optional arguments

For more information, see Format Specifications.

Remarks

The sprintf function formats and stores a series of characters and values in buffer. Each argument (if any) is converted and output according to the corresponding format specification in format. The format consists of ordinary characters and has the same form and function as the format argument for printf. A null character is appended after the last character written. If copying occurs between strings that overlap, the behavior is undefined.

swprintf is a wide-character version of sprintf; the pointer arguments to swprintf are wide-character strings. Detection of encoding errors in swprintf may differ from that in sprintf. swprintf and fwprintf behave identically except that swprintf writes output to a string rather than to a destination of type FILE.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_stprintf sprintf sprintf swprintf

Example

/* SPRINTF.C: This program uses sprintf to format various
* data and place them in the string named buffer.
*/

#include <stdio.h>

void main( void )
{
char buffer[200], s[] = "computer", c = 'l';
int i = 35, j;
float fp = 1.7320534f;

/* Format and print various data: */
j = sprintf( buffer, "\tString: %s\n", s );
j += sprintf( buffer + j, "\tCharacter: %c\n", c );
j += sprintf( buffer + j, "\tInteger: %d\n", i );
j += sprintf( buffer + j, "\tReal: %f\n", fp );

printf( "Output:\n%s\ncharacter count = %d\n", buffer, j );
}

Output

Output:
String: computer
Character: l
Integer: 35
Real: 1.732053

character count = 71

Stream I/O Routines

See Also _snprintf, fprintf, printf, scanf, sscanf, vprintf Functions