Re: Delay Routine: Fully-portable C89 if possible
- From: David Brown <david@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 10 Oct 2007 09:09:43 +0200
John Devereux wrote:
Martin Wells <warint@xxxxxxxxxx> writes:
David:
Most"long unsigned int" is a part of C89.
importantly, it gives you "stdint.h" and types like "uint32_t" so that
you can avoid unspecific non-standardised types like "long unsigned"
(which should always be written "long unsigned int").
Perhaps you were on about "long long unsigned int"? (which is a part
of C89 but not C99)
As far as any compliant C89 compiler is concerned, "long unsigned" and
"long unsigned int" are the same thing. If the compiler doesn't accept
it, then it isn't a C89 compiler.
Even if it were worth switching to C99 (which I don't think it is), I
still wouldn't because it's so poorly implemented today.
*All* C standards are implemented to varying degrees, and *all* embedded compilers add their own extensions. Take advantage of what you get (such as <stdint.h>, inline, and // comments), and leave out the parts that are poorly or inconsistently implemented (such as variable length arrays). Even nominally C89 compilers frequently support such features.
No, he is saying that he prefers being able to use uint32_t, instead
of, for example, long unsigned.
Actually, I was saying two things (without making that very clear).
First the simple part - omitting the "int" part of declarations and definitions is an abomination brought about by the terrible keyboards K&R had to work with when developing C. The type in question is called "long unsigned int" - "long" and "unsigned" are type qualifiers. The language standards say that if a type is expected, but you haven't given one, then an "int" is assumed, and any C compiler will accept that. But the compiler will accept all sorts of hideous code and generate working results - you have to impose a certain quality of code standards if you are going to write good code. One of these rules is that if you mean a type should be "int" (optionally qualified), then you write "int". If you don't want to write out "long unsigned int", then use a typedef.
Secondly, I was suggesting that if you want portable code, you have to use size-specific integer types. Using <stdint.h> is an easy way to get that - otherwise, a common format header file that is adapted for the compiler/target in question is a useful method. It doesn't really matter whether you use "uint32_t" from <stdint.h>, or have a "typedef unsigned long int uint32_t" in a common header file - nor does it matter if you give the type your own name. But it *does* matter that you have such types available in your code.
Certainly many of the situations where size specifics are important are in hardware dependant and non-portable - and thus the only issue is that the code in question is clear.
But there are many cases where you need a minimum range which may not be satisfied by "int" on every platform, and also where you want the fastest implementation. If you have a function delayMicrosecs(unsigned int n), then the useful range is wildly different on a 32-bit target and a 16-bit (or 8-bit, with 16-bit int) target. On the other hand, if it is declared with "uint32_t n", it is portable from 8-bit to 64-bit processors. Since the OP was asking for portable code in an embedded newsgroup, there's no way he can make assumptions about the size of "int".
mvh.,
David
I personally don't agree though. At least in my own work I have not.
found a real-life situation where these types are an
improvement. Basically, if you are worried about the exact widths of
types, then that part of the program is likely non-portable anyway, so
the new types don't help much.
For example, in C99 I could define a 32 bit hardware register like
this:
#include <stdint.h>
#define PORT (*(volatile uint32_t *)(0x1FFF0000))
But in fact this code will likely be useless on some hypothetical
other CPU anyway. I can just as easily rely on ints being 32 bit on my
platform, and do
#define PORT (*(volatile unsigned long *)(0x1FFF0000))
- Follow-Ups:
- Re: Delay Routine: Fully-portable C89 if possible
- From: Martin Wells
- Re: Delay Routine: Fully-portable C89 if possible
- From: CBFalconer
- Re: Delay Routine: Fully-portable C89 if possible
- From: rob windgassen
- Re: Delay Routine: Fully-portable C89 if possible
- From: John Devereux
- Re: Delay Routine: Fully-portable C89 if possible
- References:
- Delay Routine: Fully-portable C89 if possible
- From: Martin Wells
- Re: Delay Routine: Fully-portable C89 if possible
- From: David Brown
- Re: Delay Routine: Fully-portable C89 if possible
- From: Martin Wells
- Re: Delay Routine: Fully-portable C89 if possible
- From: John Devereux
- Delay Routine: Fully-portable C89 if possible
- Prev by Date: Re: All USB & only USB
- Next by Date: Re: how is the thread/process awakened after it slept?
- Previous by thread: Re: Delay Routine: Fully-portable C89 if possible
- Next by thread: Re: Delay Routine: Fully-portable C89 if possible
- Index(es):
Relevant Pages
|