Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is wcslen in C?

Talha Ashar

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 wcslen function in C returns the number of characters in a wide string, excluding the null terminating character.

The process is illustrated below:

Note: A wide string is comprised of characters from the Unicode character set.

To use the wcslen function, you will need to include the <wchar.h> library in the program, as shown below:

#include <wchar.h>

The prototype of the wcslen function is shown below:

size_t wcslen(const wchar_t *str);

Parameters

The wcslen function takes a single parameter, i.e., a pointer to a null-terminated wide string.

Return Value

The wcslen function returns the length of the given wide string, excluding the null-terminating character.

Example

The code below shows how the wcslen function works in C:

#include <stdio.h>
#include <wchar.h>
int main() {
// initializing wide string
wchar_t str1[] = L"Hello World";
wchar_t str2[] = L"Learn wcslen in C";
// calculate lengths
size_t len1 = wcslen(str1);
size_t len2 = wcslen(str2);
// output results
printf("\'%ls\' contains %d characters.\n", str1, len1);
printf("\'%ls\' contains %d characters.\n", str2, len2);
return 0;
}

Explanation

First, the code initializes two wide strings. The ‘L’ identifier in lines 7 and 8 informs the compiler that the Unicode character set is being used.

The wcslen function proceeds to find the length of both of these strings in lines 11 and 12, respectively. These lengths are then written to stdout.

RELATED TAGS

c

CONTRIBUTOR

Talha Ashar
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