Originally Posted by
Hodor
since when can you have/name a function like double √(double); ?
Since C99. Implementations can choose which extended characters they accept as identifiers. U+221A, √, is not one that are required to be supported as an identifier, though.
I tend to use GCC, and although -fextended-identifiers allows \uHHHH character constants in identifiers, it strictly restricts the identifiers to the minimum set the C99 standard specifies. There is an one line patch to allow non-ASCII characters "raw" in the identifiers. So, GCC does not allow √ in identifiers. It could (per C99 standard), but it chooses not to. Other compilers might.
In general, using non-ASCII (rather, non-basic characters as the C standard defines it) characters in the source is a bit problematic, because unless you use UTF-8 everywhere, you have to worry about the source character set, especially when copying sources from one system to another. It's too much hassle for too little gain.
As I explained in the post linked to above, using Unicode characters in strings and as character constants is no problem, however. We can even do it very portably, relying on C99 alone. Here's the on-topic example:
Code:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <locale.h>
#include <wchar.h>
#include <math.h>
int main(int argc, char *argv[])
{
double value;
char dummy;
int arg;
setlocale(LC_ALL, "");
fwide(stdout, 1);
if (argc < 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
fprintf(stderr, "\n");
fprintf(stderr, "Usage: %s [ -h | --help ]\n", argv[0]);
fprintf(stderr, " %s NUMBER [ NUMBER ... ]\n", argv[0]);
fprintf(stderr, "\n");
fprintf(stderr, "This program calculates the square root of each NUMBER.\n");
fprintf(stderr, "\n");
return EXIT_FAILURE;
}
for (arg = 1; arg < argc; arg++)
if (sscanf(argv[arg], " %lf %c", &value, &dummy) == 1) {
fwprintf(stdout, L"\u221a(%s) = %f\n", argv[arg], sqrt(value));
fflush(stdout);
} else {
fprintf(stderr, "%s: Not a number!\n", argv[arg]);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Note that only standard output is wide, here, unlike in my example in the other thread.
The code is locale-aware. If you use a locale that uses different characters for numbers (digits), the code should Just Work.
This code does not check the character sets used; it assumes it is some Unicode encoding. C99 did not standardize character set conversion utilities, but POSIX.1-2001 and POSIX.1-2008 did; the iconv() interface is very easy to use, and GNU libiconv (used in Linux) is available for non-POSIX systems too, including Windows.
The code does not use a message cataloging library (like gettext) to provide the output in different languages. C99 does not standardize one, so I omitted it -- although GNU Gettext is ported to just about all OSes, and adding the support would have only added two or three lines to the code above.