Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7880709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:12:30+00:00 2026-06-03T04:12:30+00:00

Why does this code: char a[10]; wchar_t w[10] = Lä; // German a Umlaut

  • 0

Why does this code:

char a[10]; 
wchar_t w[10] = L"ä"; // German a Umlaut
int e = wcstombs(a, w, 10);

return e == -1?

I am using Oracle Solaris Studio 10 on Solaris 11. The locale is Latin-1, which contains the German Umlauts. All docs I have found indicate (to me) that the conversion should succeed.

If I do this:

char a[10] = "ä"; // German a Umlaut
wchar_t w[10];
int e = mbstowcs(w, a, 10);
e = wcstombs(a, w, 10);

there is no error, but the result is wrong. (Some variant of upper A.)

I also tried wstostr with similar result.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:12:31+00:00Added an answer on June 3, 2026 at 4:12 am

    1) verify that the correct value is getting into the wchar_t. The compiler producing the wide character string literal has to convert L"ä" from the source code encoding to the wide execution charset.

    2) verify that the program’s locale is correct. You can do this with printf("%s\n", setlocale(LC_ALL, NULL));

    I suspect that the problem is 1) because for me even if the program’s locale isn’t set correctly I still get the expected output. To avoid problems with the source code encoding you can escape non-ascii characters like L"\x00E4".

     1  #include <iostream>
     2  #include <clocale>
     3
     4  int main () {
     5    std::printf("%s\n", std::setlocale(LC_ALL, NULL));   // prints "C"
     6
     7    char a[10];
     8    wchar_t w[10] = L"\x00E4"; // German a Umlaut
     9    std::printf("0x%04x\n", (unsigned)w[0]);             // prints "0x00e4"
    10
    11    std::setlocale(LC_ALL, "");
    12    printf("%s\n", std::setlocale(LC_ALL, NULL));        // print something that indicates the encoding is ISO 8859-1
    13    int e = std::wcstombs(a, w, 10);
    14    std::printf("%i 0x%02x\n", e, (unsigned char)a[0]);  // print "1 0xe4"
    15  }
    16
    


    Character Sets in C and C++ Programs

    In your source code you can use any character from the ‘source character set’, which is a superset of the ‘basic source character set’. The compiler will convert characters in string and character literals from the source character set into the execution character set (or wide execution character set for wide string and character literals).

    The issue is that the source character set is implementation dependent. Typically the compiler simply has to know what encoding you use for the source code and then it will accept any characters from that encoding. GCC has command line arguments for setting the source encoding, Visual Studio will assume that the source is in the user’s codepage unless it detects one of the so-called Unicode signatures for UTF-8 or UTF-16, and Clang currently always uses UTF-8.

    Once the compiler is using the right source character set for your code it will then produce string and character literals in the ‘execution character set’. The execution character set is another superset of the basic source character set, and is also implementation dependent. GCC takes a command line argument to set the execution character set, VS uses the user’s locale, and Clang uses UTF-8.

    Because the source character set is implementation dependent, the portable way to write characters outside the basic set is to either use hex encoding to directly specify the numeric values to be used in execution, or (if you’re not using C89/90) to use universal character names (UCNs), which are converted to the execution character set (or wide execution character set when used in wide string and character literals). UCNs look like \uNNNN or \UNNNNNNNN and specify the character from the Unicode character set with the code point value NNNN or NNNNNNNN. (Note that C99 and C++11 prohibit you from using surrogate code points, if you want a character from outside the BMP just directly write the character’s value using \U.)

    The source and execution character sets are determined at compile time and do not change based on the locale of the system running the program. That is, the program locale uses another encoding not necessarily matching the execution character set. The wide execution character set should correspond to the wide character encoding used by supported locales, however.


    Solaris Studio’s behavior

    Oracle’s compiler for Solaris has very simple behavior. For narrow string and character literals no particular source encoding is specified, bytes from the source code are simply used directly as the execution literal. This effectively means that the execution character set is the same as the encoding of the source files. For wide character literals the source bytes are converted using the system locale. This means that you have to save the source file using the locale encoding in order to get correct wide literals.

    I suspect that your source code is being saved in an encoding other than the one specified by the locale, so your compiler was failing to produce the correct wide string literal from L"ä". Your editor might be using UTF-8. You can check using the following program.

     1  #include <iostream>
     2  #include <clocale>
     3
     4  int main () {
     5    wchar_t w[10] = L"ä"; // German a Umlaut
     6    std::printf("0x%04x 0x%04x\n", (unsigned)w[0], (unsigned)w[1]);
     7  }
     8
    

    Since wcstombs can correctly convert the wide character 0x00E4 to the latin-1 encoding of ‘ä’ you want the above to display 0x00E4 0x0000. If the source code encoding is UTF-8 then you should see 0x00C3 0x00A4.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does this code int (*g)(int); int (*h)(char); h = g; In C, give
Why does this code: #include <stdio.h> int main(int argc, char** argv) { printf(%lld\n, 4294967296LL);
I'm not really getting how this code does what it does: char shellcode[] =
Does this code cause a memory leak: int main(){ int * a = new
Why does this code return a cell? static NSString *cellIdentifier = @Cell; UITableViewCell *cell
Why does this code always return 0? var possibleMatches = new Array(); $.getJSON('getInformation.php', function(data)
why does this code crash? is using strcat illegal on character pointers? #include <stdio.h>
Does this code have defined behaviour? char *str = NULL; printf(%s\n,str); In context of
Why does this code (unassigned temporary variable constructed from a const char* variable): class
Look at the following code: int main(int argc, char* argv[]) { // This works:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.