Showing posts with label memset function in c. Show all posts
Showing posts with label memset function in c. Show all posts

Sunday, December 6, 2020

, ,

Example Program for memset function in c

 memset() function in C use to take a memory location and taken a VOID* pointer and this function copies the first n byte in memory location.

After update the memory location it return that location with the help of pointer.

Example Program for memset function in c
memset c

 

Syntax :  -

void* memset(void* mem_loc, int c, size_t n);

mem_loc is a memory location

c is an unsigned character in that upper syntax


Header File for memset function in c

Here this function deal with the characters so we have to use header file in the program

Now the complete import for memset function

#include <string.h>
 
void* memset(void* mem_loc, int c, size_t n);

Program Example for memset c

#include <stdio.h>
#include <string.h>
 
int main() {
    char a[] = {"Hello from functioninc"};
 
    printf("a = %s\n", a);
     
    printf("Filling the first 5 characters a with 'P' using memset\n");
     
    memset(a, 'P', 5 * sizeof(char));
     
    printf("After memset, a = %s\n", a);
 
    return 0;
}

Then the 5 character of the given string is filled by the 'P'

a = Hello from Functioninc
Filling the first 5 characters with 'P' using memset function in c
After memset c, a = PPPPP from Functioninc

Conclusion of memset c

Here we learn about the memset function in c and how to allocate the memory location.