Re: I am learning C: a little problem with a simple source code

On Fri, 27 Jan 2012 20:28:14 +0100
Fritz Wuehler <fritz@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

C is a shitty unsafe language designed to be easy to implement. It's been

C is a language originally designed to replace assembly as the primary
language for writing operating systems. Therefore, it allows you to get down
to the bare metal and deal with things what assembly gives you access to,
such as addresses, words, bits and the like. C has achieved that goal and is a
brilliant language when you have to write code for an embedded system where
you have a processor and a bucketload of peripherals but what you do not have
is gigabytes of virtual memory, clock cycles to waste and, most importantly, a
hardware budget to whack in a processor which is $10 dearer and add an other
$10 worth of memory. The fact that one can use C to write application
programs on a full-blown operating system environment is an added bonus.

Programming languages are tools and C is a very pointy and sharp tool, but in
a craftsman's hand it can create beautiful things. If you need type safety
and objects and abstract concepts and all the other stuff, you should use some
other language; there are plenty around.

Typical Linux apps already compile with hundreds or thousands of warnings
and everybody already ignores them. Very few people are inclined to write
correct, safe code and of those people only a small percentage of them use

I am one of them. I turn on all warnings on the compiler and make all
warnings errors. The thing is, most of the warnings you eliminate that way
are warnings which are the result of lazyness. You assign a pointer to a
pointer of different kind because you know that that's OK. The compiler
screams. All you have to do is to put a cast there and it keeps its mouth
shut. The result is better code because whoever reads it (including myself, a
few years down the track) can see the intention to change the pointer from
type X to type Y. There's hopefully a comment nearby explaining why that's
happening. The rest of the warnings are usually an indication of real
programming dangers (i.e. bugs which will byte you later).

[...] inside a burning house like gcc. I realize there are plenty of C
compilers around but unfortunately gcc is the most popular and so are forks
of it.

The problem with gcc is that it is not just a C compiler. It is a C compiler
and a C++ compiler and an Ada compiler and a Fortran compiler and a java
compiler and a whatnotelse compiler, which should run on any machine
whatsoever, compiling for any processor and operating system combination.

In that regards, gcc achieves a helluva lot, despite all its quirks. Yes, it
has a few extremely stupid things, yes, it sometimes generates inefficient
code (and on, admittedly very rare, occasions even incorrect code), but it
has some advantages, especially when you have to deal with multiple targets.

In addition, gcc has a lot of historical baggage which is very hard to clean
up unless you are willing to start from scratch. However, there are attempts
to rectify gcc's problems, the most prominent being LLVM + clang; time will
tell if that will succeed.


Zoltán Kócsi
Bendor Research Pty. Ltd.