Thread: Using c with other languages.

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    7

    Using c with other languages.

    Hi
    I'm trying to print some texts in other languages. But It's not possible to print those characters. Do you know why can we use it like this? Actually, if I press directly then it works as well ü ö Ö Ü but I wonder why that doesn't work as the book explains.

    Code:
    #include <stdio.h>
    int main () {
    printf("\x81 \x94 \x99 \x9A \n");
    return 0;
    }
    Last edited by whynot; 01-24-2022 at 04:11 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well the representation of non ASCII characters depends on
    - your OS
    - your compiler
    - your console/terminal (not all are capable of rendering extended characters)
    - your fonts (not all have glyphs at the code points of interest)

    How your generated code outputs glyphs to your chosen console/terminal affects how the terminal will respond.

    > Actually, if I press directly then it works as well ü ö Ö Ü but I wonder why that doesn't work as the book explains.
    You need to show some examples of your "working" code as well.

    Nor do we have any idea what "the book" is that you're referring to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2021
    Posts
    138
    Quote Originally Posted by Salem View Post
    Well the representation of non ASCII characters depends on
    - your compiler
    How the compiler plays a role? I'm talking about compilers for the same language? Aren't they produced the same code when it comes to data types?

  4. #4
    Registered User
    Join Date
    Oct 2021
    Posts
    138
    Quote Originally Posted by whynot View Post
    Hi
    I'm trying to print some texts in other languages. But It's not possible to print those characters. Do you know why can we use it like this? Actually, if I press directly then it works as well ü ö Ö Ü but I wonder why that doesn't work as the book explains.

    Code:
    #include <stdio.h>
    int main () {
    printf("\x81 \x94 \x99 \x9A \n");
    return 0;
    }

    Because it doesn't work like that. UTF-8 unicode (which is the most common) uses multiple codepoints for each character (other than the 128 ascii ones, hence why you can do something like this: `printf("\x2e\n");`. To print unicode characters, you either copy paste them (like you said) or you print it's codepoints in order like that:

    Emoji used: 😅 - Smiling Face with Open Mouth and Cold Sweat Emoji: U+1F605 - Unicode Character Table

    ```
    printf("\xf0\x9f\x98\x85\n");
    ```

    At least, that's my understanding of it. There may be other ways to do it but I'm not sure.

    Also, I would like to see "the book" you are saying so I can see the example.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    One mistake is to think C only deals with ASCII or single byte charsets... The standard is clear: C deals with MULTIBYTE CHARSETS. Specifically UNICODE (since it was created, too, by Ken Thompson)... You can print the smiley face like this:

    printf( "😅\n" );

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    If your OS isn't using UTF-8 this will print unexpected chars, of course.

  7. #7
    Registered User
    Join Date
    Jan 2021
    Posts
    7
    hmm
    I got it. It's a command-line setup Asci or UTF-8. Noting to do with c compiler.

    Output is like this
    Code:
    #include <stdio.h>
    int main () {
    printf("\x81 \x94 \x99 \x9A\n");
    printf("ü ö Ö Ü\n");
    printf("\x2e\n");
    return 0;
    }
    Code:
    � � � �
    ü ö Ö Ü
    .
    Thank you all.
    Last edited by whynot; 01-25-2022 at 08:23 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This is what it produces as a byte stream.
    Code:
    $ cat foo.c
    #include <stdio.h>
    int main () {
      printf("\x81 \x94 \x99 \x9A\n");
      printf("ü ö Ö Ü\n");
      printf("\x2e\n");
      return 0;
    }
    $ gcc foo.c
    $ ./a.out | hd
    00000000  81 20 94 20 99 20 9a 0a  c3 bc 20 c3 b6 20 c3 96  |. . . .... .. ..|
    00000010  20 c3 9c 0a 2e 0a                                 | .....|
    00000016
    $
    All those c3 xx pairs are your UTF-8 encoded diacritic characters.

    u - U with two Dots: U+00FC uuml - Unicode Character Table
    Scroll down to the UTF-8 encoding.

    Not sure what you expected to see with your \x81 line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why c++ and which other languages
    By l2u in forum C++ Programming
    Replies: 9
    Last Post: 08-02-2008, 11:59 AM
  2. Web Languages
    By Vicious in forum Tech Board
    Replies: 21
    Last Post: 09-07-2004, 08:27 PM
  3. what languages are fading ? what languages remain ?
    By dot_rain in forum Tech Board
    Replies: 32
    Last Post: 03-04-2004, 09:25 AM
  4. Rap in different languages
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 06-23-2003, 07:09 AM
  5. Languages
    By KrAzY CrAb in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 02-18-2003, 12:23 PM

Tags for this Thread