473,699 Members | 2,431 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Types

Hi people
I have been working again in my tutorial, and I have finished the
"types" chapter. If you feel like "jacob bashing" this is the
occasion! I am asking for criticisms, or for things I may have said
wrong. Keep in mind that this is a tutorial, and I donot want to
overwhelm the reader with little details.

-------------------------------------------------------------------------*

Types

A machine has no concept of type, everything is just a sequence of bits,
and any operation with those sequences can be done, even if it is not
meaningful at all, for example adding two addresses, or multiplying two
character strings.

A high level programming language however, enforces the concept of types
of data. Operations are allowed between compatible types and not between
any data whatsoever. It is possible to add two integers, or an integer
and a floating point number, and even an integer and a complex number.
It is not possible to add an integer to a function or to a character
string, the operation has no meaning for those types.

An operation implies always compatible types netween the operands.
In C, all data must be associated with a specific type before it can be
used. All variables must be declared to be of a known type before any
operation with them is attempted since to be able to generate code the
compiler must know the type of each operand.

C allows the programmer to define new types based on the previously
dfined ones. This means that the type system in C is static, i.e. known
at compile time, but extensible since you can add new types.

This is in contrast to dynamic typing, where no declarations are needed
since the language associates types and data during the run time.
Dynamic typing is much more flexible, but this flexibility has a price:
the run time system must constantly check the types of the operands for
each operation to see if they are compatible, what slows down the
program considerably.

In C there is absolutely no run time checking in most operations, since
the compiler is able to check everything during the compilation, what
accelerates execution of the program, and allows the compiler to
discover a lot of errors during the compilation instead of crashing at
run time when an operation with incompatible types is attempted.
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. If we say that the type of a is a double, it
means that the bits are to be understood as the IEEE 754 standard
sequences of bits representing a double precision floating point value.
Functions have a type too. The type of a function is determined by the
type of its return value, and all its arguments. The type of a function
is its interface with the outside world: its inputs (arguments) and its
outputs (return value).

Types in C can be incomplete, i.e. they can exist as types but nothing
is known about them, neither their size nor their bit-layout. They are
useful for encapsulating data into entities that are known only to
certain parts of the program.

Each type can have an associated pointer type: for int we have int
pointer, for double we have double pointer, etc. We can have also
pointers that point to an unspecified object. They are written as void
*, i.e. pointers to void. Some types exist only as pointers: the
function pointer type has no object counterpart since a function object
doesn’t exist in C, only pointers to functions exist.

Types classification

This type classification is based on the classification published by
Plauger and Brody, slightly modified.
Types
1. Function types
1.1 Fully qualified function types. (Functions whose full prototype is
visible)
1.2 Assumed function types (Functions whose prototype is partly or not
visible)
1.2.1 Functions whose return value is visible but not their arguments
1.2.2 Functions where the return value and their arguments are unknown.
2. Incomplete types (Types not completely specified)
2.1 void
2.2 Incomplete struct types
2.3 Incomplete array types
2.4 Incomplete union types
3. Object types
3.1 Scalar types
3.1.1 Arithmetic types
3.1.1.1 Integer types
3.1.1.1.1 Specific integer types
3.1.1.1.1.1 char (signed/unsigned)
3.1.1.1.1.2 short (signed unsigned)
3.1.1.1.1.3 int (signed/unsigned)
3.1.1.1.1.4 long (signed/unsigned)
3.1.1.1.1.5 long long (signed/unsigned)
3.1.1.1.2 Bitfields (signed/unsigned)
3.1.1.1.3 Enumeration types
3.1.1.2 Floating types
3.1.1.2.1 float
3.1.1.2.2 double
3.1.1.2.3 long double
3.1.2 Pointer types
3.1.2.1 Pointer to function
3.1.2.2 Pointer to object types
3.1.2.3 Pointer to incomplete types
3.2 Non -scalar types
3.2.1 Struct types
3.2.2 Union types
3.2.3 Array types

Integer types

The language doesn’t specify exactly how big each integer type must be,
but it has some requirements as to the minimum size of the integer
types. The char type must be at least 8 bits, the int type must be at
least 16 bits, and the long type must be at least 32 bits. How big each
integer type actually is, is defined in the standard header limits.h.

Floating types

Floating types are discussed in more detail later. Here we will just
retain that they can represent integer and non integer quantities, and
in general, their dynamic range is bigger that integers of the same
size. They have two parts: a mantissa and an exponent.
As a result, there are some values that can’t be expressed in floating
point, for instance 1/3 or 1/10. This comes as a surprise for many
people, so it is better to underscore this fact here. More explanations
for this later on.
Floating point arithmetic is approximative, and many mathematical laws
that we take for granted like a+b is equal to b+a do not apply in many
cases to floating point math.

Compatible types

There are types that share the same underlying representation. For
instance, in lcc-win32 for the Intel platform, in 32 bits, long and int
are the same. They are compatible types for that version of lcc. In the
version of lcc-linux for 64 bits however, long is 64 bits and int is 32
bits, they are no longer compatible types, but long is now compatible
with the long long type.
Plauger and Brody give the following definition for when two types are
compatible types:
Both types are the same.
Both are pointer types, with the same type qualifiers, that point to
compatible types.
Both are array types whose elements have compatible types. If both
specify repetition counts, the repetition counts are equal.
Both are function types whose return types are compatible. If both
specify types for their parameters, both declare the same number of
parameters (including ellipses) and the types of corresponding
parameters are compatible. Otherwise, at least one does not specify
types for its parameters. If the other specifies types for its
parameters, it specifies only a fixed number of parameters and does not
specify parameters of type float or of any integer types that change
when promoted.
Both are structure, union, or enumeration types that are declared in
different translation units with the same member names. Structure
members are declared in the same order. Structure and union members
whose names match are declared with compatible types. Enumeration
constants whose names match have the same values.

Incomplete types

An incomplete type is missing some part of the declaration. For instance

struct SomeType;

We know now that “SomeType” is a struct, but since the contents aren’t
specified, we can’t use directly that type. The use of this is precisely
to avoid using the type: encapsulation. Many times you want to publish
some interface but you do not want people using the structure,
allocating a structure, or doing anything else but pass those structure
to your functions. In those situations, an opaque type is a good thing
to have.

Casting

The programmer can at any time change the type associated with a piece
of data by making a “cast” operation. For instance if you have:
float f = 67.8f;
you can do
double d = (double)f;
The “(double)” means that the type of data in f should be converted into
an equvalent data using the double representation. We will come back to
types when we speak again about casts later.
Dec 10 '06
58 3451
jacob navia <ja***@jacob.re mcomp.frwrites:
Harald van Dijk wrote:
>jacob navia wrote:
[...]
>>>I do not know if this classification in function types is correct, or it
would be better to add this to the "incomplete types" section.
A function type cannot ever be an incomplete type.

Incomplete in the sense that the number and types of the function
arguments are unknown, as in an incomplete declaration the number and
types of the structure/union members are unknown.

There are two kinds:

1) Function declarations that are not prototypes, to use your terminology.
2) Completely implicit declaration where the compiler assumes:

int fn();
But it's unwise to use the term "incomplete type" to mean something
other than what the standard defines it to be.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 11 '06 #21
jacob navia wrote:
Harald van Dijk wrote:
Ian Collins wrote:
>jacob navia wrote:

Chris Torek wrote:

(This glosses over some stuff, e.g., many "high level" languages *do*
allow you to add an integer to a character string.)
Adding an integer to a character string? What would be the result of THAT?

Or maybe you mean
"9" + "1" --"91"
More likely "string"+1, which is legal in several languages.

The meaning of "string"+1 is inconsistent between languages, though. In
Perl, its result is 1. In JavaScript, the result is "string1". It's
also legal C, and equivalent to "tring".

But this is not adding 1 to a character string!

It means (in C) adding 1 to the ADDRESS of the start of the string,
what is completely another matter!
Yes, the comment about C was just a side note. In the other two
languages, it /is/ adding 1 to a character string.

Dec 11 '06 #22
jacob navia wrote:
Hi people
I have been working again in my tutorial, and I have finished the
"types" chapter. If you feel like "jacob bashing" this is the
occasion!
Jacob, your attitude verges on paranoia. And if there's
one thing I hate above all else, it's the way you paranoids
are always going out of your way to irritate ME!
I am asking for criticisms, or for things I may have said
wrong. Keep in mind that this is a tutorial, and I donot want to
overwhelm the reader with little details.
[... thoughtful essay ...]
There are a lot of minor nit-picks, but I'll ignore them
and try to look at a couple larger issues.

First, I think the notion of "type" in the essay is at odds
with C's use of the term. The essay promotes the idea that type
is a creature of the compiler, something imposed by the language
upon an undifferentiate d mass of bits. While I agree that the
bits themselves may not carry type information (they do on some
systems but not on all), I'd dispute the notion that "type" is
absent from the machine language. For example, most machines
will have two (or more) ADD instructions that operate differently
on bit-for-bit identical operands: One will interpret the operands
as integers and generate an integer sum, the other will interpret
them as floating-point values and generate a different sum. This
seems to me a perfect example of "type."

Second, there seems to be a rather narrow focus on "type" as
a property of data. That's only part of the story, because in C
the expressions have types. True, those types do in some cases
derive from the declared types of variables that appear in them,
but it could be argued that the only reason for assigning a type
to a variable is to allow the compiler to do the bookkeeping to
derive the type of the expression. (This point of view leads to
a fairly straightforward explanation of why one can view a data
object in different ways by accessing it with expressions of
different types: it's not that the object itself somehow morphs
between `double' and `unsigned char[sizeof(double)]', but that the
object just sits there while expressions of different types work
on it. This approach also handles the question of the "type" of a
freshly-mallocated memory area: the memory has no "type" of its
own, but the expressions that operate on it do.) You might not wish
to take the notion quite as far as I'm suggesting, but I think it
is a mistake to limit "type" to the data objects and ignore the
expressions.

Third and finally, you write that you do not want to "overwhelm
the reader with little details," and that's fine. What, then, is
the purpose of the long list of C's types? If you are trying to
make the point that there are many types built in to C, I think it
could be made more clearly and more briefly than by exhibiting a
sterile (and incomplete!) catalog.

Looking forward to version two-dot-oh.

--
Eric Sosman
es*****@acm-dot-org.invalid

Dec 11 '06 #23
On Mon, 11 Dec 2006 10:03:45 +0100, in comp.lang.c , jacob navia
<ja***@jacob.re mcomp.frwrote:
>Chris Torek wrote:
>(This glosses over some stuff, e.g., many "high level" languages *do*
allow you to add an integer to a character string.)

Adding an integer to a character string? What would be the result of THAT?

Or maybe you mean
"9" + "1" --"91"
Precisely.
"foo" + 31 = "foo31"
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Dec 11 '06 #24
On Mon, 11 Dec 2006 10:48:13 +0100, in comp.lang.c , jacob navia
<ja***@jacob.re mcomp.frwrote:
>
But this is not adding 1 to a character string!
It is, but just not according to the definition you're thinking of.

Bear in mind that several languages define string addition and most
provide default conversion operators. It'd be fairly trivial to
implement such an "addition" operator.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Dec 11 '06 #25
Mark McIntyre a écrit :
On Mon, 11 Dec 2006 10:03:45 +0100, in comp.lang.c , jacob navia
<ja***@jacob.re mcomp.frwrote:

>>Chris Torek wrote:
>>>(This glosses over some stuff, e.g., many "high level" languages *do*
allow you to add an integer to a character string.)

Adding an integer to a character string? What would be the result of THAT?

Or maybe you mean
"9" + "1" --"91"


Precisely.
"foo" + 31 = "foo31"

Ahhh how CLEVER!!!
Gosh, I hope I do not come across it sometime
:-)
Dec 11 '06 #26
Mark McIntyre <ma**********@s pamcop.netwrite s:
On Mon, 11 Dec 2006 10:48:13 +0100, in comp.lang.c , jacob navia
<ja***@jacob.re mcomp.frwrote:
>>But this is not adding 1 to a character string!

It is, but just not according to the definition you're thinking of.

Bear in mind that several languages define string addition and most
provide default conversion operators. It'd be fairly trivial to
implement such an "addition" operator.
It would? What is the lifetime for the object that "a" + "b"
would yield?
--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz
Dec 11 '06 #27
Ben Pfaff a écrit :
Mark McIntyre <ma**********@s pamcop.netwrite s:

>>On Mon, 11 Dec 2006 10:48:13 +0100, in comp.lang.c , jacob navia
<ja***@jacob. remcomp.frwrote :

>>>But this is not adding 1 to a character string!

It is, but just not according to the definition you're thinking of.

Bear in mind that several languages define string addition and most
provide default conversion operators. It'd be fairly trivial to
implement such an "addition" operator.


It would? What is the lifetime for the object that "a" + "b"
would yield?
Even if lcc-win32 implements operator overloading, I have warned
in the docs and in the tutorial against such "applicatio ns".

"a"+"b" != "b"+"a"

Addition is no longer commutative, and there is always the
horrible
"1"+"1" --"11"

:-(

Dec 11 '06 #28
Ben Pfaff <bl*@cs.stanfor d.eduwrites:
Mark McIntyre <ma**********@s pamcop.netwrite s:
>On Mon, 11 Dec 2006 10:48:13 +0100, in comp.lang.c , jacob navia
<ja***@jacob.r emcomp.frwrote:
>>>But this is not adding 1 to a character string!

It is, but just not according to the definition you're thinking of.

Bear in mind that several languages define string addition and most
provide default conversion operators. It'd be fairly trivial to
implement such an "addition" operator.

It would? What is the lifetime for the object that "a" + "b"
would yield?
It depends on how the language in question deals with the concept of
"lifetime". In some languages, it will simply live until the
implementation can prove that there are no more references to it. And
you're assuming it's an object rather than just a value; there are
plenty of languages in which strings are treated almost like simple
scalar values.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 11 '06 #29
Keith Thompson <ks***@mib.orgw rites:
Ben Pfaff <bl*@cs.stanfor d.eduwrites:
>Mark McIntyre <ma**********@s pamcop.netwrite s:
>>On Mon, 11 Dec 2006 10:48:13 +0100, in comp.lang.c , jacob navia
<ja***@jacob. remcomp.frwrote :
>>>>But this is not adding 1 to a character string!

It is, but just not according to the definition you're thinking of.

Bear in mind that several languages define string addition and most
provide default conversion operators. It'd be fairly trivial to
implement such an "addition" operator.

It would? What is the lifetime for the object that "a" + "b"
would yield?

It depends on how the language in question deals with the concept of
"lifetime". In some languages, it will simply live until the
implementation can prove that there are no more references to it. And
you're assuming it's an object rather than just a value; there are
plenty of languages in which strings are treated almost like simple
scalar values.
Let me expand on my comment.

It is my impression that Mark McIntyre was saying that it'd be
fairly trivial to implement a string addition operator in C. In
that case, you need to pick one of the existing C forms of
lifetime, or define a new one. I don't think any of the existing
forms are suitable. I also think it'd be nontrivial to define a
new kind of lifetime. Therefore, I don't think it'd be trivial
to implement a string addition operator in C.
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Dec 11 '06 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4538
by: John Dibling | last post by:
Could somebody please direct me to a location in the standard where POD types are defined? That is, where in the standard is it defined what attributes a POD type has that a non-POD hasn't? Also, what does the acronym stand for? Thanks, </dib> John Dibling Witty banter omitted for your protection
5
3448
by: Andy Skypeck | last post by:
I am looking for some validation against a dubious coding practice that prevails where I work. C types defined in types.h (Linux) or stdint.h (Windows, C99?) are used as if they belong to the C++ standard namespace. std::uint8_t instead of uint8_t std::uint32_t instead of uint32_t .... I don't think the use of std:: is correct. Nowhere are these types
8
2336
by: Shailesh | last post by:
One problem I've been wrestling with for a long time is how to use the C++ integral data types, vis-a-vis their size. The C++ rules guarantee that a char is at least 1 bytes, a short and int at least 2 bytes, and a long at least 4 bytes. The rules also define a size precedence to the types. In Stroustrup's book, it says that all type sizes are multiples of char, "so by definition the size of a char is 1." According to the rules, that...
188
17355
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
5
2097
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that object, is still created on the stack, I would say, so I don't understand this inline business. Apart from the fact that it is my understanding that "inline" as it exists in C++ doesn't exist in C#. Could someone please shed some light on this...
14
2302
by: Lane Straatman | last post by:
I would like to write a 'struct'. I have a library that is all but completely inappropriate for this task. So I'm looking for C code that fills in the gaps between: #undef whateverkeithsaysfor99compliance #define whateverkeithsays for99compliance int main(void { struct foo
159
6256
by: Bob Timpkinson | last post by:
Hi, I have a 32-bit machine... Is there anyway I can get gcc to use the following integer sizes? char: 8 bits short: 16 bits int: 32 bits long: 64 bits long long: 128 bits
3
2383
by: sophia.agnes | last post by:
Dear all, what are the major expression types in c? i have seen the following types of expressions 1) constant expressions 2) integral expressions 3) float expressions 4) pointer expressions
55
3973
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable built-in-like behavior for objects of class type. That leads to the "necessity" for the exception machinery so that errors from constructors can be handled. Is all that complexity worth it just to get built-in-like behavior from class objects? Maybe a...
0
8687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9034
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2347
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2009
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.