A
asm
Hi All,
Like typdef, does C have further support for portability?
Thanks,
ASM
Like typdef, does C have further support for portability?
Thanks,
ASM
asm said:Hi All,
Like typdef, does C have further support for portability?
"typedef" is sort of a support for portability, in that it is often possibleasm said:Like typdef, does C have further support for portability?
Yes, C has 'support' for portablility, in that it
is by definition a portable, platform-independent language.
However, one must follow the language rules in order to achieve said
portability.
No real world programmers deliberately follow any "rules" that would
make a C program completely portable.
People may do so inadvertently
and usually if their program is completely trivial.
... Anything else you try to do with the language will, by
itself, usually lead you away from portability (just using floating
point on x86, where "double"s can have intermediate calculations of
either 64 or 80 bits depending on the compiler *version*),
and just ordinary "expected usage" (<isspace(0x80)> is UB, <int digit =
(char)cdig - '0';> is not portable)
You are confusing "trivial" with "doesn't manipulate hardware". A largePaul Hsieh said:No real world programmers deliberately follow any "rules" that would
make a C program completely portable. People may do so inadvertently
and usually if their program is completely trivial.
Likewise, if "cdig" is a value in '0' through '9', the line:
int digit = (char)cdig - '0';
is guaranteed to set digit to 0 through 9 correspondingly.
"typedef" is just a way of naming an arbitrary abstract data type. It
has nothing to do with portability per se (except that all C compilers
support it). You may be thinking of "#ifdef" which you misheard from
some conversation.
Excuse me, but C is *NOT* a platform-independent language. It
specifically states in the ANSI standard that certain operations such
as the right shift of signed integers, or the signedness of char are
platform specific. By the same token C is not a portable language.
Virtually every C compiler exposes important platform specific
functionality *by neccessity* and the vast majority of C programs
specifically leverage these extensions which makes them non-portable.
Compare this with Java, where the only way to make a non-portable Java
program is to chew through insane amounts of memory (different
platforms will fault at different times), or have a race condition
(different timings will cause the race condition to manifest in
different ways).
<snip>No real world programmers deliberately follow any "rules" that would
make a C program completely portable. People may do so inadvertently
and usually if their program is completely trivial. Remember that the
real purpose of the C language is for making UNIX-style command line
utilities.
I think (int) would be a better cast, if a cast is needed.
Are you being deliberately obtuse? Whether it has anything to do with
portability "per se", it's widely used to aid portability.
[...] The
current C standard even explicitly uses it for that purpose:
7.18.1.1 Exact-width integer types
1 The typedef name intN_t designates a signed integer type with
width N. Thus, int8_t denotes a signed integer type with a
width of exactly 8 bits.
2 The typedef name uintN_t designates an unsigned integer type
with width N. Thus, uint24_t denotes an unsigned integer type
with a width of exactly 24 bits.
What's your point? C can be used to write portable programs that can
run on anything from the largest supercomputers to tiny
microcontrollers. It can also be used to write hideously unportable
programs that break every time the compiler vendor issues a point
release.
To me, C is *much* more portable than Java. There are many, many
platforms out there that do not have anything even remotely
approximating a Java runtime, but have a competent C compiler.
<snip>
Now you're just being silly. You follow the portability rules as much
as possible, and isolate platform specific code as much as possible.
Avoid undefined behavior, and isolate implementation defined behavior.
I suppose you'll find some reason that does not qualify as a "rule".
Malcolm said:You are confusing "trivial" with "doesn't manipulate hardware". A large
number of programs use a GUI, or network, or similar, however not all,
particularly if you are writing scientific or similar programs.
[...] Often the
only "non-portable" feature needed is directory support.
Also, a lot of programs have inherently portable components. For instance a
spell-checker is most useful when integrated with a wordprocessor, but the
routine itself neden't have any platform dependencies.
Chris Torek said:But good real-world C programmers will often follow many "rules" that
will make their C programs far more portable than if they fail to
follow them.
Sensible people do so deliberately.
"Complete" portability will indeed only result for a quite limited
subset of "all possible C programs", but not all such programs
should be labeled "trivial", in my opinion. (For instance, yacc/bison
and lex/flex are not "trivial", yet can be written entirely in
portable C.)
True enough -- but the same problem occurs in other languages as
well. The x86 in particular is especially troublesome, because
"fast" code invariably leaves values inside the FPU, where they
have "too much" precision, even if you use precision-control code
to fiddle with the FPU control word. (In particular, the exponent
is always held in 80-bit format, even when the mantissa is rounded,
so that single and double precision values do not overflow to
infinity correctly.)
Paul said:"typedef" is just a way of naming an arbitrary abstract data type.
E. Robert Tisdale said:If you are *really* concerned about portability,
none of the built-in types should appear in the body of your code.
Instead, you should substitute your own type definitions
for all of the built-in types -- in a header file:
typedef int myInteger;
typedef float mySingle;
typedef double myDouble;
typedef size_t myExtent;
// etc.
#define myIntegerMax INT_MAX
// etc.
That is sadly the case. If C99 was widely implemented we could use int32_tPaul Hsieh said:What the hell has this got to do with portability? First of all its
in reference to a C standard that is not widely adopted at all. So in
fact use of intN_t will specifically destroy any chance of
portability. Secondly, integers in C don't have a predescribed
implementation (like 2s complement, for example), so the int16_t on
one platform doesn't have to behave the same way as it might on any
other platform (except maybe having the same "sizeof").
Of course. The stdin / stdout model isn't acceptable to many users, who willThe point is that there is no clear dividing line between these two
classes of programs in C. And the vast majority of the functionality
of your platform will mostly be in these non-portable extensions.
The question is, by "portability" do we mean bit-for-bit correspondenceNo, my claim is that such rules are just not followed. Who the hell
rigorously test for signedness independence of "char"? C specifically
says that char can be either signed or unsigned. And who keeps track
of the difference between POSIX and ANSI? And if you are doing heavy
floating point, you can't do anything to isolate such platform
specific behaviors.
Paul said:.... snip ...
But my point is that they *fail*. The reason is simple -- the
code continues to work and is correct for their platform even as
they drift to non-portability. So there is no feedback mechanism
by which a programmer can realize that what they are doing is
non-portabile. Compare this to Java, where there is no choice
about portability -- its just is.
(e-mail address removed) (Mark F. Haigh) wrote:
You are confusing availability with portability. By this reasoning
assembly/machine language is even more portable since you have to
start there to bootstrap any C compiler.
To the best of my knowledge, in fact, there are no two C compilers in
existence which are portable with each other (Intel C++ and MSVC are
close since they actually share libraries and include files, but each
contains different semantics and extensions). By contrast there are
no two Java compilers/implementation which are *not* portable (modulo
bugs).
No, my claim is that such rules are just not followed. Who the hell
rigorously test for signedness independence of "char"? C specifically
says that char can be either signed or unsigned.
And who keeps track
of the difference between POSIX and ANSI?
And if you are doing heavy
floating point, you can't do anything to isolate such platform
specific behaviors.
Malcolm said:That is sadly the case. If C99 was widely implemented we could use int32_t
in the rare cases we actually require 32 bits, safe in the knowledge that
any platform that didn't support it was so weird that it wouldn't be worth
trying to run on it anyway.
Of course. The stdin / stdout model isn't acceptable to many users, who will
demand a graphical interface. For a lot of applications, like games, this is
required.
ANSI could have done the development community a big favour by declaring a
struct POINT, so that 3d libraries all use the same structure for
co-ordinates, and a struct COLOUR (I suppose that would have to be COLOR) so
that images all use the same system of colour naming.
It then could have declared a few very simple functions, like OpenWindow()
to allow for management of graphics. Third parties would soon build
comprehensive libraries on the top of drawpixel(), querymouse(), and so on.
It still wouldn't be easy to implement a fully-functional GUI app, but
something like a Mandelbrot could be very simply written.
In said:The "typedef double myDouble;", for example, is not useful unless
there's a possibility that myDouble could be defined as something
other than double (and if there is, myDouble is a poor choice of
name).
Not necessarily. Imagine an implementation where float and double share
the same representation and you need long double to get the additional
precision implied by the (English) word "double".
E.g. on a system with 48-bit words, one machine word would match the
C requirements for both float and double, so the implementor chooses
to provide more than that (a two word floating point type) only for
long double, because this type is a lot slower (no hardware support).
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.