From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.ada Subject: Re: What is a byte? Date: Sat, 02 Aug 2014 14:01:34 -0700 Organization: None to speak of Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx05.eternal-september.org; posting-host="9db00645988affa5077391f450a92002"; logging-data="15488"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/4GQs4QtAry7V6U8JMgHTX" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:HtN7jnfd+nrIDQrgQE55/5CcI6I= sha1:hcQGvLM0dsietIlvjhoW12wbwUU= Xref: news.eternal-september.org comp.lang.ada:21415 Date: 2014-08-02T14:01:34-07:00 List-Id: Jeffrey Carter writes: > On 07/28/2014 12:09 PM, Victor Porton wrote: >> When I need to pass a byte to a C function, which Ada type should I use? > > "Byte" isn't a C concept. Generally what others call a byte is called > "char" in C. So you should use whatever type in Interfaces.C(.*) > corresponds to the C type used by the C function. Yes, it is. C has no type called "byte" (unless you define it yourself), but the C standard defines a "byte" as an "addressable unit of data storage large enough to hold any member of the basic character set of the execution environment". Other wording in the C standard says that the types char, unsigned char, and signed char are exactly 1 byte in size. A byte is exactly CHAR_BIT bits, where CHAR_BIT is a macro defined in , and is required to be at least 8. (POSIX further requires CHAR_BIT==8, but of course not all implementations conform to POSIX.) Interfaces.C.CHAR_BIT should have the same value as C's CHAR_BIT, and Interfaces.C.unsigned_char is probably the best type to use to hold a single byte (in the sense that C uses the term). Looking at C.Interfaces in the RM, it's odd that it defines CHAR_BIT, SCHAR_MIN, SCHAR_MAX, and UCHAR_MAX (all defined in C's ), but doesn't define any of the other constants from that header. For example, it has type signed_char is range SCHAR_MIN .. SCHAR_MAX; but type int is *mplementation-defined*; rather than INT_MIN : constant := *implementation-defined*; INT_MAX : constant := *implementation-defined*; type int is range INT_MIN .. INT_MAX; and so on. (Of course you can just use C.Interfaces.int'First rather than INT_MAX, but a bit more consistency would be good.) Also, in C char, signed char, and unsigned char are all distinct types (with char having the same range and representation as one of the others), but C.Interfaces.plain_char is an implementation-defined subtype. It's also odd that Interfaces.C doesn't mention long long (an integer type at least 64 bits wide, added to the C standard in 1999), even though the RM refers to the 1999 ISO C standard as a normative reference. (I'm not sure I'm looking at the most current Ada standard; a newer C standard was published in 2011.) The most current draft of the 1999 ISO C standard, with the three Technical Corrigenda merged into it, is: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf The most current public draft of the 2011 ISO C standard (from shortly before the publication of the final standard) is: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf THe actual C standard is available from, for example, ansi.org, but it costs money. The free drafts are nearly as good (or, in the case of n1256, probably better). -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"