memset()

memset() in C

memset() is used to fill a block of memory with a particular value. The syntax of memset() function is as follows :

// ptr ==> Starting address of memory to be filled
// x   ==> Value to be filled
// n   ==> Number of bytes to be filled starting 
//         from ptr to be filled
void *memset(void *ptr, int x, size_t n);
// C program to demonstrate working of memset()
#include <stdio.h>
#include <string.h>

int main()
{
	char str[50] = "GeeksForGeeks is for programming geeks.";
	printf("\nBefore memset(): %s\n", str);

	// Fill 8 characters starting from str[13] with '.'
	memset(str + 13, '.', 8*sizeof(char));

	printf("After memset(): %s", str);
	return 0;
}
// C program to demonstrate working of memset()
#include <stdio.h>
#include <string.h>

void printArray(int arr[], int n)
{
for (int i=0; i<n; i++)
	printf("%d ", arr[i]);
}

int main()
{
	int n = 10;
	int arr[n];

	// Fill whole array with 0.
	memset(arr, 0, n*sizeof(arr[0]));
	printf("Array after memset()\n");
	printArray(arr, n);

	return 0;
}
// C program to demonstrate working of memset()
#include <stdio.h>
#include <string.h>

void printArray(int arr[], int n)
{
for (int i=0; i<n; i++)
	printf("%d ", arr[i]);
}

int main()
{
	int n = 10;
	int arr[n];

	// Fill whole array with 100.
	memset(arr, 10, n*sizeof(arr[0]));
	printf("Array after memset()\n");
	printArray(arr, n);

	return 0;
}
#include <stdio.h>
#include <string.h>

int main () {
   char str[50];

   strcpy(str,"This is string.h library function");
   puts(str);

   memset(str,'$',7);
   puts(str);
   
   return(0);
}