3

C99 and C11 support wchar_t and multibyte functions .But I am not sure about ANSI C (1989).

Is it correct that wchar_t and multibyte functions (mblen, mbstowcs, mbtowc, wcstombs, wctomb) are part of ANSI C?

I don't find these functions in Kernighan and Ritchie book (C Programming Language (2nd Edition)).

http://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137

3 Answers3

3

The name wchar_t is in the C89 (and C99) standard(s), but it is not a langauge-supported type. It is a typedef for some integer type capable of holding the requisite number of bits. C89 7.1.6 [Standard Definitions ] says:

wchar_t
which is an integral type whose range of values can represent distinct codes for all members of the largest extended character set specified among the supported locales; the null character shall have the code value zero and each member of the basic character set defined in 5.2.1 shall have a code value equal to its value when used as the lone character in an integer character constant.

This means that someone can define wchar_t to be whatever they want in C89 so long as <stddef.h> has not been #included.

In C++, this is illegal; wchar_t is a keyword in that language.


As for the multibyte functions you referenced, they appear to be part of C89. Section 7.10.7 [Multibyte character functions] defines mblen, mbtowc, wctomb, and 7.10.8 [Multibyte string functions] defines mbstowcs, and wcstombs (all in <stdlib.h>). Note of course that because C89 does not have const that the const-qualified versions of these functions are not available.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • "This means that someone can define `wchar_t` to be whatever they want in C89 so long as `` has not been `#include`d. " Technically yes, but then the behavior is undefined. C89, 4.1.2 says "All external identifiers declared in any of the [standard library] headers are reserved, whether or not the associated header is included. [...] If the program defines an external identifier with the same name as a reserved external identifier, even in a semantically equivalent form, the behavior is undefined." – real-or-random Oct 15 '21 at 09:52
2

wchar_t is part of ANSI C (1989).

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
1

wchar_t is perfectly C89 standard. In my edition, wchar_t is mentioned at section § A2.5.2 in Kernighan and Ritchie book.

md5
  • 23,373
  • 3
  • 44
  • 93
  • I could not find multibyte functions. – Amir Saniyan Apr 15 '13 at 17:19
  • 1
    There's a note in A2.5.2 that makes me believe this (`wchar_t`) was introduced in the 2nd edition of the book: "... the main intent in adding `wchar_t` was to accomodate Asian languages." *Note: the 1st edition was published in 1978.* – pmg Apr 15 '13 at 17:26
  • @pmg: If you believe it so much then find a standard reference to support it. – Billy ONeal Apr 15 '13 at 17:33
  • @BillyONeal: I'd like very much to have a 1st edition of the book. If I find one I'll try and remember to come here and update this question. – pmg Apr 15 '13 at 17:35
  • @pmg: The book doesn't matter; the standard does. – Billy ONeal Apr 15 '13 at 17:38