[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Types in C

jacob navia

5/22/2011 7:29:00 PM

Hi

I am updating my tutorial of C, and I have added a section of type
classification. Please tell me if I forgot something or said
something wrong if possible.

Thanks in advance for your help


A C type can be either a function type, an incomplete type or an object
type. Function types can be either fully specified, i.e. we have a
prototype available, or partially specified with missing arguments but
a known return value. The function type "implicit int function" with
return value and arguments unknown, has been declared obsolete in C99.

Incomplete types are unspecified and it is assumed that they will be
specified elsewhere, except for the void type that is an incomplete
type that can't be further specified.

Object types can be either scalar or composite types. Composite types
are built from the scalar types: structures, unions and arrays. Scalar
types are of two kinds: arithmetic or pointer types. Pointer types can
point to scalar or composite types, to functions or to incomplete types.

Arithmetic types have two kinds: integer types and floating types. The
integer types are either standard integers, bit fields or enumerations.

The standard integer types are boolean, char, short, int, long and long
long, all with signed or unsigned types, except the boolean type
that hasn't any sign but is not an unsigned type.

Floating types are either real or complex, with both of them appearing
in three flavors: float, double and long double.

122 Answers

Keith Thompson

5/22/2011 7:49:00 PM

0

jacob navia <jacob@spamsink.net> writes:
> I am updating my tutorial of C, and I have added a section of type
> classification. Please tell me if I forgot something or said
> something wrong if possible.
>
> Thanks in advance for your help
>
>
> A C type can be either a function type, an incomplete type or an object
> type. Function types can be either fully specified, i.e. we have a
> prototype available, or partially specified with missing arguments but
> a known return value. The function type "implicit int function" with
> return value and arguments unknown, has been declared obsolete in C99.

In C90, "implicit int function" isn't a distinct type.
foo(void);
and
int foo(void);
are merely two different ways of expressing exactly the same thing.
The return type isn't unknown, it's int.

> Incomplete types are unspecified and it is assumed that they will be
> specified elsewhere, except for the void type that is an incomplete
> type that can't be further specified.
>
> Object types can be either scalar or composite types. Composite types
> are built from the scalar types: structures, unions and arrays. Scalar
> types are of two kinds: arithmetic or pointer types. Pointer types can
> point to scalar or composite types, to functions or to incomplete types.

The phrase "composite type" has a very different meaning; see C99 6.2.7.

The standard refers to array and structure types (but not union types)
as "aggregate types". It doesn't seem to have a term that covers
arrays, structures and unions (and I'd be very hesitant in this kind of
tutorial to invent terms that aren't in the standard).

> Arithmetic types have two kinds: integer types and floating types. The
> integer types are either standard integers, bit fields or enumerations.
>
> The standard integer types are boolean, char, short, int, long and long
> long, all with signed or unsigned types, except the boolean type
> that hasn't any sign but is not an unsigned type.

You didn't mention extended integer types. I don't know whether you
want to.

> Floating types are either real or complex, with both of them appearing
> in three flavors: float, double and long double.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Ben Bacarisse

5/22/2011 10:03:00 PM

0

jacob navia <jacob@spamsink.net> writes:

> I am updating my tutorial of C, and I have added a section of type
> classification. Please tell me if I forgot something or said
> something wrong if possible.

I'd add few remarks to what Keith Thompson has said.

> A C type can be either a function type, an incomplete type or an object
> type. Function types can be either fully specified, i.e. we have a
> prototype available, or partially specified with missing arguments but
> a known return value. The function type "implicit int function" with
> return value and arguments unknown, has been declared obsolete in C99.
>
> Incomplete types are unspecified and it is assumed that they will be
> specified elsewhere, except for the void type that is an incomplete
> type that can't be further specified.

Form a style point of view, this listing of terms followed by
definitions (your paragraphs below all start with some category of type)
is not that helpful. Often the definition of a term is the least
interesting thing about it. That's particularly true of incomplete
types. I'd explain them by stating how they often occur:

"It is sometimes helpful to postpone or omit the full specification of a
type. Such a partial definition of an object type gives rise an
incomplete type. The type definition will usually be completed later in
the source file or in another source file where the full details are
finally needed. Until a type is complete, the size of the type is
unknown and objects of the type can't be defined. The type void is
rather special. It is an incomplete type that can never be completed."

> Object types can be either scalar or composite types. Composite types
> are built from the scalar types: structures, unions and arrays. Scalar
> types are of two kinds: arithmetic or pointer types. Pointer types can
> point to scalar or composite types, to functions or to incomplete types.
>
> Arithmetic types have two kinds: integer types and floating types. The
> integer types are either standard integers, bit fields or
> enumerations.

I don't think bit fields are really types. They have a type, but you
can't declare something to have a bit-field type. I'd leave them out
altogether -- I think they are better explained as a special kind of
structure members.

> The standard integer types are boolean, char, short, int, long and long
> long, all with signed or unsigned types, except the boolean type
> that hasn't any sign but is not an unsigned type.

boolean is not a type in C. You could say "bool (the Boolean type)" or
you could use the real type name _Bool, ugly though it is. Also, _Bool
*is* an unsigned type.

Technically, char is not listed as one of the "standard integer types".
"signed char" is one and so is the unsigned type that corresponds to it,
but char is a third type different to these two. It is included in the
group called "integer types". The problem is that the term "standard
integer type" is used to distinguish these types form "extended integer
types". If you dropped the word "standard" from that paragraph, there
would be not possibility of confusion.

> Floating types are either real or complex, with both of them appearing
> in three flavors: float, double and long double.

--
Ben.

jacob navia

5/23/2011 8:35:00 AM

0

Le 23/05/11 00:03, Ben Bacarisse a écrit :

> Form a style point of view, this listing of terms followed by
> definitions (your paragraphs below all start with some category of type)
> is not that helpful. Often the definition of a term is the least
> interesting thing about it. That's particularly true of incomplete
> types. I'd explain them by stating how they often occur:
>

Of course I go later into each type with a lot of detail. This is
just the introduction to types, the "big picgture". There is actually
a picture that shows all the relationships as I described.

>
> I don't think bit fields are really types. They have a type, but you
> can't declare something to have a bit-field type. I'd leave them out
> altogether -- I think they are better explained as a special kind of
> structure members.
>

I have them in because they were in the classification that I used as
base by Plauger and Brody.

>> The standard integer types are boolean, char, short, int, long and long
>> long, all with signed or unsigned types, except the boolean type
>> that hasn't any sign but is not an unsigned type.
>
> boolean is not a type in C. You could say "bool (the Boolean type)" or
> you could use the real type name _Bool, ugly though it is. Also, _Bool
> *is* an unsigned type.
>

Yes, I corrected both counts. bool was added to the unsigned types.

> Technically, char is not listed as one of the "standard integer types".
> "signed char" is one and so is the unsigned type that corresponds to it,
> but char is a third type different to these two. It is included in the
> group called "integer types". The problem is that the term "standard
> integer type" is used to distinguish these types form "extended integer
> types". If you dropped the word "standard" from that paragraph, there
> would be not possibility of confusion.
>

I added:

<quote>
The char type has not only signed and unsigned flavors but in some more
esoteric classifications has a third flavor "plain char", different as a
type from an unsigned or a signed char. We will not go into that
hair splitting here.
<end quote>

I think this gives the reader a glimpse into the underlying complexities
without really getting bogged down...

True "hair splitting" is maybe exaggerated but it conveys my feeling
about that whole issue :-)


Thanks for your help

Keith Thompson

5/23/2011 3:40:00 PM

0

jacob navia <jacob@spamsink.net> writes:
[...]
> I added:
>
> <quote>
> The char type has not only signed and unsigned flavors but in some more
> esoteric classifications has a third flavor "plain char", different as a
> type from an unsigned or a signed char. We will not go into that
> hair splitting here.
> <end quote>
>
> I think this gives the reader a glimpse into the underlying complexities
> without really getting bogged down...
>
> True "hair splitting" is maybe exaggerated but it conveys my feeling
> about that whole issue :-)

Plain char really is a disinct time from both signed char and
unsigned char, just as int and long are distinct types even if they
happen to have exactly the same representation. IMHO you're not
serving your readers well by suggesting that this distinction is
"esoteric" and "hair splitting".

The phrase "in some more esoteric classifications" could lead some
readers to think that they should be using signed char or unsigned
char, and that plain char should be avoided. If I didn't know C,
I might even assume that most compilers don't support plain char.

Yes, it does seem odd that plain char has exactly the same
representation as either signed char or unsigned char, but that
it's a distinct type. But the alternative would be that plain char
is the same type as either signed char or unsigned char -- which
would make some programs either valid or uncompilable, depending
on the implementation. Would you rather be writing a tutorial for
that hypothetical language?

And I think the reason it seems strange is the misperception that
type is *just* about representation. It isn't. C is less strongly
typed than some languages, but it still has the concept tha two types
can be distinct even if one happens to be an exact copy of the other.
That might be something to explore in another part of your tutorial
(probably not the introduction).

Teach the language as it is, not as you wish it were.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Seebs

5/23/2011 5:19:00 PM

0

On 2011-05-23, Keith Thompson <kst-u@mib.org> wrote:
> The phrase "in some more esoteric classifications" could lead some
> readers to think that they should be using signed char or unsigned
> char, and that plain char should be avoided. If I didn't know C,
> I might even assume that most compilers don't support plain char.

Hmm.

This whole thread reminds me: I wrote an article series on the C type
system long ago for IBM developerWorks, and I bet it's got errors that
some of the clever folks here could catch.

http://www.ibm.com/developerworks/power/library/pa-ctypes1/...
http://www.ibm.com/developerworks/power/library/pa-ctypes2/...
http://www.ibm.com/developerworks/power/library/pa-ctypes3/...
http://www.ibm.com/developerworks/power/library/pa-ctypes4/...

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

John Gordon

5/23/2011 5:55:00 PM

0

In <slrnitl4c6.o5q.usenet-nospam@guild.seebs.net> Seebs <usenet-nospam@seebs.net> writes:

> This whole thread reminds me: I wrote an article series on the C type
> system long ago for IBM developerWorks, and I bet it's got errors that
> some of the clever folks here could catch.

Not really an error, but is notation such as /byte/ meant to be italic
text?

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Keith Thompson

5/23/2011 6:33:00 PM

0

John Gordon <gordon@panix.com> writes:
> In <slrnitl4c6.o5q.usenet-nospam@guild.seebs.net> Seebs <usenet-nospam@seebs.net> writes:
>> This whole thread reminds me: I wrote an article series on the C type
>> system long ago for IBM developerWorks, and I bet it's got errors that
>> some of the clever folks here could catch.
>
> Not really an error, but is notation such as /byte/ meant to be italic
> text?

Without having looked at the web pages (yet), I would assume so, because
"byte" is defined by the C standard.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson

5/23/2011 6:45:00 PM

0

Keith Thompson <kst-u@mib.org> writes:
> John Gordon <gordon@panix.com> writes:
>> In <slrnitl4c6.o5q.usenet-nospam@guild.seebs.net> Seebs <usenet-nospam@seebs.net> writes:
>>> This whole thread reminds me: I wrote an article series on the C type
>>> system long ago for IBM developerWorks, and I bet it's got errors that
>>> some of the clever folks here could catch.
>>
>> Not really an error, but is notation such as /byte/ meant to be italic
>> text?
>
> Without having looked at the web pages (yet), I would assume so, because
> "byte" is defined by the C standard.

And now I see that I completely misunderstood your question.

The word "byte" appears on the web page surrounded by '/' characters;
yes, it should be in italics without the '/' characters.

When my newsreader saw "/byte/" in John's article, it helpfully tried to
display it in italics (which my terminal emulator doesn't support) and
dropped the '/' characters. I'll need to figure out how to tell it not
to do that. (Something to do with gnus-emphasis-alist, I think.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

jacob navia

5/23/2011 6:51:00 PM

0

Le 23/05/11 19:19, Seebs a écrit :
> On 2011-05-23, Keith Thompson<kst-u@mib.org> wrote:
>> The phrase "in some more esoteric classifications" could lead some
>> readers to think that they should be using signed char or unsigned
>> char, and that plain char should be avoided. If I didn't know C,
>> I might even assume that most compilers don't support plain char.
>
> Hmm.
>
> This whole thread reminds me: I wrote an article series on the C type
> system long ago for IBM developerWorks, and I bet it's got errors that
> some of the clever folks here could catch.
>
> http://www.ibm.com/developerworks/power/library/pa-ctypes1/...
> http://www.ibm.com/developerworks/power/library/pa-ctypes2/...
> http://www.ibm.com/developerworks/power/library/pa-ctypes3/...
> http://www.ibm.com/developerworks/power/library/pa-ctypes4/...
>
> -s

Interesting. What this discussion is concerned, you define

<quote>
The type of an object in C describes the way in which a chunk of memory
is associated with a value. In many cases, a type describes a computer's
native ways of representing values. For instance, on a typical UNIX®
system, declaring a variable as int will reserve enough storage to hold
a single processor register. This variable will be manipulated by
processor-native instructions, and will have the semantics native to the
processor.
<end quote>

I used a more functional definition of type. The equivalent sentence in
my book is:

<quote>
\subsection{What is a type?}
A first tentative, definition for what a type is, could be ?a type is a
definition of an algorithm for understanding a sequence of storage
bits?. It gives the meaning of the data stored in memory. If we say
that the object a is an int, it means that the bits stored at that
location are to be understood as a natural number that is built by
consecutive additions of powers of two as specified by its bits.
If we say that the type of a is a double, it means that the bits are to
be understood as the IEEE 754 (for instance) standard sequences of bits
representing a double precision floating point value.

A second, more refined definition would encompass the first but add the
notion of "concept" behind a type. For instance in some machines
the type "size_t" has exactly the same bits as an unsigned long, yet,
it is a different type. The difference is that we store sizes in
size_t objects, and not some arbitrary integer. The type is associated
with the concept of size. We use types to convey a concept to the
reader of the program.

The base of C's type hierarchy are the machine types, i.e. the types
that the integrated circuit understands. C has abstracted from the
myriad of machine types some types like 'int' or 'double' that are
almost universally present in all processors.

There are many machine types that C doesn't natively support, for
instance some processors support BCD coded data but that data is
accessible only through special libraries in C.

C makes an abstraction of the many machine types present in many
processors, selecting only some of them and ignoring others.

It can be argued why a type makes its way into the language and why
another doesn't. For instance the most universal type always present in
all binary machines is the boolean type (one or zero). Still, it was
ignored by the language until the C99 standard incorporated it as a
native type.
<end quote>

I think that both paragraphs convey more or less the same thing,
even if I would say you could have expanded a bit the definition part.

Afterwards, most of what you say is similar to what I have written,
but obviously in 4 articles you have much less space than me in a
book with not any hard page limit.

jacob

Seebs

5/23/2011 7:09:00 PM

0

On 2011-05-23, John Gordon <gordon@panix.com> wrote:
> In <slrnitl4c6.o5q.usenet-nospam@guild.seebs.net> Seebs <usenet-nospam@seebs.net> writes:
>> This whole thread reminds me: I wrote an article series on the C type
>> system long ago for IBM developerWorks, and I bet it's got errors that
>> some of the clever folks here could catch.

> Not really an error, but is notation such as /byte/ meant to be italic
> text?

Probably. I thought the editor had caught all of those!

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.