Home > Software engineering >  C wclen in MSCV
C wclen in MSCV

Time:04-11

I using wcslen to determine the length of null-terminated wide string (wchar_t*), but I have some problems with this function in MSVC compiler

Code example:

#include <iostream>
#include <cstring>
#include <cwchar>

int main()
{
    auto sc  = "The good and bad";
    auto wsc = L"Уставший лесник";

    auto ws = std::wstring(wsc);

    std::cout << "sc len:" << std::strlen(sc) << std::endl;
    
    std::cout << "wsc len:" << std::wcslen(wsc) << std::endl;

    std::cout << "ws len:" << ws.length() << std::endl;

}

MSVC (amd64 16.8.2 x64) output:

sc len:16
wsc len:29
ws len:29

Clang (10.0.0 (GNU CLI) for MSVC 16.8.30717.126) output:

sc len:16
wsc len:15
ws len:15

Is it a problem of MSVC compiler, some undefined behaivor or nuances of MSVC implementation?

CodePudding user response:

You need to save your file as either UTF-16 or UTF-8 with BOM. MSVC doesn't seem to be able to handle a UTF-8 file without a BOM (which is understandable as the character encoding of such a file is a matter of interpretation).

Some editors (I am using Notepad2) call this 'UTF-8 with signature'.

  • Related