2

I want to use symbol in the program written below.

#include <stdio.h>
main(){
    char a='√';
    if (a=='√'){
        printf("Working");
    }
    else{
        printf("Not working");
    }
}

is not ASCII that's why its not working. But I want to know to make it work. Thanks in advance.

Saurabh
  • 43
  • 1
  • 15
  • 1
    Possible duplicate of [c reading non ASCII characters](https://stackoverflow.com/questions/32523898/c-reading-non-ascii-characters) – Salek Mar 16 '19 at 00:50

1 Answers1

7

There are two different things going on here to be aware of:

  1. The source C file itself may not be able to contain this character correctly.
  2. The char type within the semantics of the actual program does not support this character, either.

As to the first issue, it depends on your platform (etc) but being conservative with C source is most portable, which means sticking to ASCII characters only within the code file. That means, e.g., in comments as well as within meaningful code. That said, lots of platforms will allow and support Unicode characters inside the source files.

Regarding the second, a char is old-fashioned for containing characters, and is limited to an octet, which means arbitrary unicode characters with values above 0xFF just don't fit inside of them. I suppose some non-ASCII characters do in a platform dependent way (Windows code pages?) above value 0x7F, but in this case, I would treat this as a string, using a unicode escape sequence for this character: "\u221A".

char * sqrt = "\u221A";
if (strcmp(sqrt, "\u221A") == 0) {
    printf("Working");
} else {
    printf("Not working");
}

Heads-up that C strings (char*) are not really designed around non-ASCII characters either, so in this case you end up embedding the UTF8 encoded representation of the character (which is three bytes long) inside the char string. This works, preserves the value, and the compare works, but if you're going to be working with unicode more generally...

If your platform supports "wide characters" (wchar_t or unichar or similar) that can hold Unicode characters, then you can use those types to hold this character, and do direct equality comparisons like you were doing:

wchar_t sqrt = L'\u221A';
if (sqrt == L'\u221A') {
    ...

(FYI Be a little aware that these wide char types may not be wide enough for arbitrary Unicode code points on your platform thus might work for the square root char, but not, say, an emoji.)


Finally, for the sake of completeness, I feel honor-bound to admit that given a contemporary development environment/toolchain and target platform, you could probably get away with using the explicit character in a widechar literal like so:

wchar_t sqrt = L'√';
if (sqrt == L'√') {
    ....

But I'm old-fashioned, this feels sketchy, and I don't recommend it. :)

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
  • I am using `printf("%s",sqrt);` but it's not printing `√`. So what about printing the value of `sqrt`. – Saurabh Mar 16 '19 at 03:43
  • 1
    @Saurabh: Is this the `char*` string version of sqrt? If so, this format with printf works on the system I'm using and displays the root symbol. What platform are you using? – Ben Zotto Mar 16 '19 at 04:48
  • 1
    I tried it with gcc in arch linux in my brother's laptop and its working. – Saurabh Mar 16 '19 at 05:07
  • OK. It worked for me under clang also. My guess here is that `printf` is likely to behave differently under different platforms when asked to "display" a string with UTF8 byte content inside it. Either the underlying printf implementation is formatting the string differently, or the view of the console that you have on Android is unhappy with the unicode output and isn't displaying it as you want. – Ben Zotto Mar 16 '19 at 05:16
  • If you are working with Unicode strings, and have further questions, please open a new question with the details on platform and expectations and needs. – Ben Zotto Mar 16 '19 at 05:23