... there are still people who need to write portable C
code, and it is well for them to write C properly and with an eye
to portability.
Portability is exactly the reason I decided to use C to implement
Seed7. When I began to implement the predecessor of Seed7 I used
Pascal but I had much more portability problems with it.
When C compilers use high warning levels it is almost impossible to
write code that never (with no C compiler) gets a warning:
- On some compilers you get warnings complaining about things in
standard include files. How do you get rid of such warnings?
- Historically C had the philosophy that actual float parameters
are automatically converted to double when a function is called.
On some compilers you get warnings when your prototyp requests
a formal float parameter and the actual parameter is a float
also. So you get a warning for something that is 100% OK and
I don't want to cast every float argument to float just to
avoid this warning.
- On some compilers you get warnings when 'unsigned char *' strings
are used for functions like 'strlen' or 'strcpy' (which request
a signed 'char *' pointer). Naturally you can assume that
'strlen' works independent of the sign of the character, but the
compiler warnings do not care about that.
- C has rules how to deal with operations when integers of
different sizes or signedness are involved. One operator argument
is implicitly casted to the type of the other. Naturally such
casts can involve loss of data or loss of the sign. This warnings
are helpful to find possible sources of errors. But to silence
such warnings it is necessary to use explicit casts. The problem
is that with and without explicit casts the same things are done.
The cast just tells the compiler: I know what I mean, just do it.
This way you will not get warnings when new typedefs or other
declarations cause the expression to become dangerous.
- Warnings about 'variablename' may be used uninitialized:
This can be false complaints. Interestingly gcc is not able to
recognize when the states of two variables are connected.
Such as a global fail_flag variable and a local condition
variable (cond). The connection is: As long as fail_flag is
FALSE the cond variable is initialised. When the fail_flag
is TRUE the cond variable is not used and therefore it could
be in an uninitialized state. At several places I use such
connected variable states which are not recognized by the
gcc optimizer and are therefore flagged with a warning. I
accept such warnings in performance critical paths. I am not
willing to do "unnecessary" initialisations in performance
critical paths of the program. At places that are not
performance critical I do some of this "unnecessary"
initialisations just to avoid such warnings.
I appreciate every help to reduce the number of warnings when
compiling Seed7. It would be nice to recive the warnings generated
by various C compilers.
Greetings Thomas Mertes
Seed7 Homepage:
http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.