memset() — Set buffer to value

Standards

Standards / Extensions C or C++ Dependencies

ISO C
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3

both  

Format

#include <string.h>

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

General description

The memset() built-in function sets the first count bytes of dest to the value c converted to an unsigned int.

Returned value

memset() returns the value of dest.

Example

CELEBM15
⁄* CELEBM15                                      

   This example sets 10 bytes of the buffer to "A" and                          
   the next 10 bytes to "B".                                                    
                                                                                
 *⁄                                                                             
#include <string.h>                                                             
#include <stdio.h>                                                              
#define  BUF_SIZE     20                                                        
#define  HALF_BUF_SIZE   BUF_SIZE⁄2                                             
                                                                                
int main(void)                                                                  
{                                                                               
   char buffer[BUF_SIZE + 1];                                                   
   char *string;                                                                
                                                                                
   memset(buffer, 0, sizeof(buffer));                                           
   string = (char *)memset(buffer,'A', HALF_BUF_SIZE);                          
   printf("\nBuffer contents: %s\n", string);                                   
   memset(buffer+HALF_BUF_SIZE, 'B', HALF_BUF_SIZE);                            
   printf("\nBuffer contents: %s\n", buffer);                                   
}                                                                               
Output
Buffer contents: AAAAAAAAAA

Buffer contents: AAAAAAAAAABBBBBBBBBB

Related information