3

Simple question: I have recently started programming in C and have a simple question.

What character set does the C programming language use in its "char" type, e.g ASCII, or does it depend on the software/operating system?

Bongani Dube
  • 78
  • 10
  • Note: There are 2 character sets involved: the set used to code the program and the characters used in I/O. – chux - Reinstate Monica Oct 05 '15 at 02:02
  • C doesn't have a character set. It depends on the libraries/APIs you're working on. The standard C library uses ascii; it doesn't know (much) about encodings. Base C (sans-standard library) doesn't even have a notion of "strings" other than the `"string"` construct, which is syntactic sugar when you really think about it. – Qix - MONICA WAS MISTREATED Oct 05 '15 at 02:57
  • @chux-ReinstateMonica : This question seems to have been duplicated by [that question](https://stackoverflow.com/q/69653883/315052). Since this question was also marked a duplicate, I dup'd that question to the parent, which you apparently disagreed with. Do you want to reopen this question too? – jxh Oct 21 '21 at 02:20

2 Answers2

1

C runs on so many systems that there is no one answer. About all you can count on without knowledge of a particular system is 7-bit ascii, and even that has rare exceptions.

Paul Kienitz
  • 878
  • 6
  • 25
1

char is essentially 1 byte (Mostly on all OS). So by default it can store values from 0 to 255. by default it can represent ASCII set and extended ASCII set.

But if you change the locale something like this in code:

char* locale = setlocale(LC_ALL, "");
    if (locale == NULL) {
        printf("Locale not set\n");
    } else {
        printf("Locale set to %s\n", locale);
    }

You can to handle unicode input or utf8 input.

So by default sequence of chars are treated as extended ascii set, but if locale is changed, sequence of chars can be treated as utf8 string or unicode string depending upon the locale set.

Ritesh
  • 1,809
  • 1
  • 14
  • 16
  • 1
    `Mostly on all OS` No, on **all** OSes. It's part of the standard; `sizeof(char) == 1` on all systems. Whether or not `char` is 8 bits is the question. – Qix - MONICA WAS MISTREATED Oct 05 '15 at 02:56
  • C only requires that a char has at least 8 bits (CHAR_BIT >= 8) and can always store values between 0 and 127, inclusive. It doesn't specify if char is signed or unsigned. – jschultz410 Apr 11 '18 at 17:37