C - Print an array filled with Unicode-Symbols












0















I want to print an array(/string), which is filled with unicode (and normal) symbols , for example squares.



Here's my code:



#include <stdio.h>

int main()
{
char array[5];
for (int i = 0; i < 4; i++){
array[i]='u25A1';
}
array[4]='A';
printf("%s", array);
return 0;
}


It just prints " ííííA◊".
Why doesn't it print the squares, and how to fix it?



According to fileformat.info the square's



C/C++/Java source code is "u25A0"



i also tried the square's



UTF-8 (hex), which is "0xE2 0x96 0xA0 (e296a0)"



Neither work.










share|improve this question



























    0















    I want to print an array(/string), which is filled with unicode (and normal) symbols , for example squares.



    Here's my code:



    #include <stdio.h>

    int main()
    {
    char array[5];
    for (int i = 0; i < 4; i++){
    array[i]='u25A1';
    }
    array[4]='A';
    printf("%s", array);
    return 0;
    }


    It just prints " ííííA◊".
    Why doesn't it print the squares, and how to fix it?



    According to fileformat.info the square's



    C/C++/Java source code is "u25A0"



    i also tried the square's



    UTF-8 (hex), which is "0xE2 0x96 0xA0 (e296a0)"



    Neither work.










    share|improve this question

























      0












      0








      0








      I want to print an array(/string), which is filled with unicode (and normal) symbols , for example squares.



      Here's my code:



      #include <stdio.h>

      int main()
      {
      char array[5];
      for (int i = 0; i < 4; i++){
      array[i]='u25A1';
      }
      array[4]='A';
      printf("%s", array);
      return 0;
      }


      It just prints " ííííA◊".
      Why doesn't it print the squares, and how to fix it?



      According to fileformat.info the square's



      C/C++/Java source code is "u25A0"



      i also tried the square's



      UTF-8 (hex), which is "0xE2 0x96 0xA0 (e296a0)"



      Neither work.










      share|improve this question














      I want to print an array(/string), which is filled with unicode (and normal) symbols , for example squares.



      Here's my code:



      #include <stdio.h>

      int main()
      {
      char array[5];
      for (int i = 0; i < 4; i++){
      array[i]='u25A1';
      }
      array[4]='A';
      printf("%s", array);
      return 0;
      }


      It just prints " ííííA◊".
      Why doesn't it print the squares, and how to fix it?



      According to fileformat.info the square's



      C/C++/Java source code is "u25A0"



      i also tried the square's



      UTF-8 (hex), which is "0xE2 0x96 0xA0 (e296a0)"



      Neither work.







      c arrays unicode char printf






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 0:24









      flogg1flogg1

      1




      1
























          1 Answer
          1






          active

          oldest

          votes


















          2














          array[i] = 'u25A1' will not compile correctly in C. You should at least get a compiler warning.



          'u25A1' is of type char16_t (two bytes per character), it is not relevant here.



          u can be used as a escape sequence in a string literal, to represent Unicode code points below 0x10000. Example:



          strcpy(array, u8"u25A0");
          printf(array);



          Output: ■



          Note that u8"u25A0" is stored as 4 bytes (0xE2, 0x96, 0xA0 + null-character) based on UTF8 conversion. It can also be printed as follow (if the console supports UTF8 output):



          strcpy(array, "xE2x96xA0");
          printf(array);



          Output: ■



          Moreover the string should be null-terminated, the last character in the string should be zero.



          To store UTF8 in bytes, you can assign values as follows:



          array[0] = 0xE2;
          array[1] = 0x96;
          array[2] = 0xA0;
          array[3] = '';


          If your development environment supports it you can also declare



          char array = u8"■";





          share|improve this answer


























          • Thanks @chux. I cleaned up the answer a bit.

            – Barmak Shemirani
            Nov 23 '18 at 8:08






          • 1





            Other minor: "u8"u25A0" is turned in to 3 bytes" --> 4 bytes (3 + ) as string literals always append a null character.

            – chux
            Nov 23 '18 at 8:13













          • it just prints "öüä" for everything you said :/

            – flogg1
            Nov 23 '18 at 15:10











          • It depends on your operating system, the version of your operating system, and if the console supports UTF8. I don't know anything about your environment you are using.

            – Barmak Shemirani
            Nov 23 '18 at 17:13













          • @flogg1: maybe you should specify your OS in your question. The Windows console, for example, may need some forced coercing before it does something as wildly far-fetched as defaulting to UTF8...

            – usr2564301
            Nov 25 '18 at 11:00











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439353%2fc-print-an-array-filled-with-unicode-symbols%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          array[i] = 'u25A1' will not compile correctly in C. You should at least get a compiler warning.



          'u25A1' is of type char16_t (two bytes per character), it is not relevant here.



          u can be used as a escape sequence in a string literal, to represent Unicode code points below 0x10000. Example:



          strcpy(array, u8"u25A0");
          printf(array);



          Output: ■



          Note that u8"u25A0" is stored as 4 bytes (0xE2, 0x96, 0xA0 + null-character) based on UTF8 conversion. It can also be printed as follow (if the console supports UTF8 output):



          strcpy(array, "xE2x96xA0");
          printf(array);



          Output: ■



          Moreover the string should be null-terminated, the last character in the string should be zero.



          To store UTF8 in bytes, you can assign values as follows:



          array[0] = 0xE2;
          array[1] = 0x96;
          array[2] = 0xA0;
          array[3] = '';


          If your development environment supports it you can also declare



          char array = u8"■";





          share|improve this answer


























          • Thanks @chux. I cleaned up the answer a bit.

            – Barmak Shemirani
            Nov 23 '18 at 8:08






          • 1





            Other minor: "u8"u25A0" is turned in to 3 bytes" --> 4 bytes (3 + ) as string literals always append a null character.

            – chux
            Nov 23 '18 at 8:13













          • it just prints "öüä" for everything you said :/

            – flogg1
            Nov 23 '18 at 15:10











          • It depends on your operating system, the version of your operating system, and if the console supports UTF8. I don't know anything about your environment you are using.

            – Barmak Shemirani
            Nov 23 '18 at 17:13













          • @flogg1: maybe you should specify your OS in your question. The Windows console, for example, may need some forced coercing before it does something as wildly far-fetched as defaulting to UTF8...

            – usr2564301
            Nov 25 '18 at 11:00
















          2














          array[i] = 'u25A1' will not compile correctly in C. You should at least get a compiler warning.



          'u25A1' is of type char16_t (two bytes per character), it is not relevant here.



          u can be used as a escape sequence in a string literal, to represent Unicode code points below 0x10000. Example:



          strcpy(array, u8"u25A0");
          printf(array);



          Output: ■



          Note that u8"u25A0" is stored as 4 bytes (0xE2, 0x96, 0xA0 + null-character) based on UTF8 conversion. It can also be printed as follow (if the console supports UTF8 output):



          strcpy(array, "xE2x96xA0");
          printf(array);



          Output: ■



          Moreover the string should be null-terminated, the last character in the string should be zero.



          To store UTF8 in bytes, you can assign values as follows:



          array[0] = 0xE2;
          array[1] = 0x96;
          array[2] = 0xA0;
          array[3] = '';


          If your development environment supports it you can also declare



          char array = u8"■";





          share|improve this answer


























          • Thanks @chux. I cleaned up the answer a bit.

            – Barmak Shemirani
            Nov 23 '18 at 8:08






          • 1





            Other minor: "u8"u25A0" is turned in to 3 bytes" --> 4 bytes (3 + ) as string literals always append a null character.

            – chux
            Nov 23 '18 at 8:13













          • it just prints "öüä" for everything you said :/

            – flogg1
            Nov 23 '18 at 15:10











          • It depends on your operating system, the version of your operating system, and if the console supports UTF8. I don't know anything about your environment you are using.

            – Barmak Shemirani
            Nov 23 '18 at 17:13













          • @flogg1: maybe you should specify your OS in your question. The Windows console, for example, may need some forced coercing before it does something as wildly far-fetched as defaulting to UTF8...

            – usr2564301
            Nov 25 '18 at 11:00














          2












          2








          2







          array[i] = 'u25A1' will not compile correctly in C. You should at least get a compiler warning.



          'u25A1' is of type char16_t (two bytes per character), it is not relevant here.



          u can be used as a escape sequence in a string literal, to represent Unicode code points below 0x10000. Example:



          strcpy(array, u8"u25A0");
          printf(array);



          Output: ■



          Note that u8"u25A0" is stored as 4 bytes (0xE2, 0x96, 0xA0 + null-character) based on UTF8 conversion. It can also be printed as follow (if the console supports UTF8 output):



          strcpy(array, "xE2x96xA0");
          printf(array);



          Output: ■



          Moreover the string should be null-terminated, the last character in the string should be zero.



          To store UTF8 in bytes, you can assign values as follows:



          array[0] = 0xE2;
          array[1] = 0x96;
          array[2] = 0xA0;
          array[3] = '';


          If your development environment supports it you can also declare



          char array = u8"■";





          share|improve this answer















          array[i] = 'u25A1' will not compile correctly in C. You should at least get a compiler warning.



          'u25A1' is of type char16_t (two bytes per character), it is not relevant here.



          u can be used as a escape sequence in a string literal, to represent Unicode code points below 0x10000. Example:



          strcpy(array, u8"u25A0");
          printf(array);



          Output: ■



          Note that u8"u25A0" is stored as 4 bytes (0xE2, 0x96, 0xA0 + null-character) based on UTF8 conversion. It can also be printed as follow (if the console supports UTF8 output):



          strcpy(array, "xE2x96xA0");
          printf(array);



          Output: ■



          Moreover the string should be null-terminated, the last character in the string should be zero.



          To store UTF8 in bytes, you can assign values as follows:



          array[0] = 0xE2;
          array[1] = 0x96;
          array[2] = 0xA0;
          array[3] = '';


          If your development environment supports it you can also declare



          char array = u8"■";






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 8:34

























          answered Nov 23 '18 at 0:56









          Barmak ShemiraniBarmak Shemirani

          21.1k42145




          21.1k42145













          • Thanks @chux. I cleaned up the answer a bit.

            – Barmak Shemirani
            Nov 23 '18 at 8:08






          • 1





            Other minor: "u8"u25A0" is turned in to 3 bytes" --> 4 bytes (3 + ) as string literals always append a null character.

            – chux
            Nov 23 '18 at 8:13













          • it just prints "öüä" for everything you said :/

            – flogg1
            Nov 23 '18 at 15:10











          • It depends on your operating system, the version of your operating system, and if the console supports UTF8. I don't know anything about your environment you are using.

            – Barmak Shemirani
            Nov 23 '18 at 17:13













          • @flogg1: maybe you should specify your OS in your question. The Windows console, for example, may need some forced coercing before it does something as wildly far-fetched as defaulting to UTF8...

            – usr2564301
            Nov 25 '18 at 11:00



















          • Thanks @chux. I cleaned up the answer a bit.

            – Barmak Shemirani
            Nov 23 '18 at 8:08






          • 1





            Other minor: "u8"u25A0" is turned in to 3 bytes" --> 4 bytes (3 + ) as string literals always append a null character.

            – chux
            Nov 23 '18 at 8:13













          • it just prints "öüä" for everything you said :/

            – flogg1
            Nov 23 '18 at 15:10











          • It depends on your operating system, the version of your operating system, and if the console supports UTF8. I don't know anything about your environment you are using.

            – Barmak Shemirani
            Nov 23 '18 at 17:13













          • @flogg1: maybe you should specify your OS in your question. The Windows console, for example, may need some forced coercing before it does something as wildly far-fetched as defaulting to UTF8...

            – usr2564301
            Nov 25 '18 at 11:00

















          Thanks @chux. I cleaned up the answer a bit.

          – Barmak Shemirani
          Nov 23 '18 at 8:08





          Thanks @chux. I cleaned up the answer a bit.

          – Barmak Shemirani
          Nov 23 '18 at 8:08




          1




          1





          Other minor: "u8"u25A0" is turned in to 3 bytes" --> 4 bytes (3 + ) as string literals always append a null character.

          – chux
          Nov 23 '18 at 8:13







          Other minor: "u8"u25A0" is turned in to 3 bytes" --> 4 bytes (3 + ) as string literals always append a null character.

          – chux
          Nov 23 '18 at 8:13















          it just prints "öüä" for everything you said :/

          – flogg1
          Nov 23 '18 at 15:10





          it just prints "öüä" for everything you said :/

          – flogg1
          Nov 23 '18 at 15:10













          It depends on your operating system, the version of your operating system, and if the console supports UTF8. I don't know anything about your environment you are using.

          – Barmak Shemirani
          Nov 23 '18 at 17:13







          It depends on your operating system, the version of your operating system, and if the console supports UTF8. I don't know anything about your environment you are using.

          – Barmak Shemirani
          Nov 23 '18 at 17:13















          @flogg1: maybe you should specify your OS in your question. The Windows console, for example, may need some forced coercing before it does something as wildly far-fetched as defaulting to UTF8...

          – usr2564301
          Nov 25 '18 at 11:00





          @flogg1: maybe you should specify your OS in your question. The Windows console, for example, may need some forced coercing before it does something as wildly far-fetched as defaulting to UTF8...

          – usr2564301
          Nov 25 '18 at 11:00


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439353%2fc-print-an-array-filled-with-unicode-symbols%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'