Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is wcstombs_s in C?

Samia Ishaque

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The wcstombs_s() function converts a wide character data type to a multibyte character. wcstombs_s() is defined in the <stdlib.h> header file, so it is important that you include this library at the beginning of your code as shown below:

#include<stdlib.h>

The <stdlib.h> library defines the wcstombs_s() function as the following:

errno_t wcstombs_s(
   size_t *pReturnValue,
   char *mbstr,
   size_t sizeInBytes,
   const wchar_t *wcstr,
   size_t count
);

Parameter

The wcstombs_s() function has four mandatory parameters:

  1. pReturnValue: the size of the converted string in bytes
  2. mbstr: a pointer to the character array where the resultant string is stored
  3. sizeInBytes: size of the mbstr array in bytes
  4. wcstr: pointer to the string that needs to be converted
  5. count: maximum number of bytes that one can store in mbstr.

Note: wcstombs_s() can usually keep converting the wide character string until it terminates or the size of mbstr is equal to count, but if count is set to TRUNCATE, wcstombs_s() can keep converting until the whole wide character string is converted.

Return value

The return value for wcstombs_s() is errno_t, which indicates whether the program was executed successfully or if an error occurred. If the return value is 00, the program was executed successfully. On the other hand, a non-zero return value indicates that an error occurred.

An error could occur in the following scenarios:

  1. If mbstr is NULL and sizeInBytes is greater than zero.
  2. If wcstr is NULL.
  3. If the count value is too small to store the converted string.

Example

Following is an example of how you can use the wcstombs_s() function:

#include<stdio.h>
#include<stdlib.h>

#define SIZE 100
int main() {

  size_t output_size;
  char* output = (char *)malloc( SIZE );
  wchar_t * str_to_convert = L"Educative is Great";
  wcstombs_s(&output_size, output, (size_t)SIZE,
               str_to_convert, (size_t)SIZE );
    
  //printing the outputs
  printf("Number of characters convertes: %u\n", output_size);
  printf("Multibyte character: %s\n\n",output );
    return 0;
}

Explanation

We define a size of 100 beforehand for the sizeInBytes and count variables. After defining the character pointer that will store the output string and the wide character pointer that stores the string to be converted, we use the wcstombs_s() function to convert to a multibyte character.

The function above is not supported by the GCC compiler, so you will get an implicit declaration of function… error. Use the following variant to get the job done:

size_t wcstombs (char* dest, const wchar_t* src, size_t max);

RELATED TAGS

c

CONTRIBUTOR

Samia Ishaque
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring