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
C.

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.

Zoltan

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

.



Relevant Pages

  • C language now truly universal
    ... C language now truly universal ... a C compiler for z/VSE (an ... IBM mainframe operating system), and would like to ... Has a documented programming API. ...
    (comp.lang.c)
  • Re: more on linux hardening
    ... > is running only binaries/libs that are built from a trusted compiler, ... is involved in producing an operating system. ... as trojans in hardware are a lot less likely to work along ... then re-write the compiler in a higher-level language ...
    (Focus-Linux)
  • Re: Squeezing bytes out of a struct
    ... I have some systems that were developed with a 15 year old C compiler. ... I am a fan of weighing up the pros and cons of language and tool choices, to get a reasonable balance and the right tool for the job. ... For example, if a good choice of name (for the macro, function, type, whatever) can make the code clear, then it is better than using a poorer name choice and a comment. ... I don't want a warning here, because I like my code to compile without warnings - but I don't want to disable the warnings for unused variables in general. ...
    (comp.arch.embedded)
  • Re: Spirit rover OS problems ( a reliable language )
    ... > problems than any C compiler I used 20 years ago, ... >>What occurs in real life is that you get massive amounts of warnings. ... that does not exist in the C language. ... I don't doubt that your lint program is very impressive to you. ...
    (comp.arch.embedded)
  • Re: Interfaces.Shift_Left
    ... is a tough sell until you've had the compiler find errors for you. ... warnings when translating the definition of function by cases. ... the makers of static analysis tools might even ... So each of the vendors can define their own informal language tacked onto the ...
    (comp.lang.ada)