C in embedded fastest/overstayed?

Discussion in 'Embedded Systems and Microcontrollers' started by aamirali, Sep 22, 2014.

  1. aamirali

    Thread Starter Member

    Feb 2, 2012
    1. Almost everyone says C is very lossely bound there are many chances of error, whereas other languages C++/Java are much more error proof.

    2. Then why did C stayed or it is overstayed?

    3. Some people say C is fastest language, why is it so? Why can't same can't be done by C++.
    Why would a compiler generate larger/slow code for C++ rather than C.

    4. Is C language is here to stay,particularly for embedded or we will see shift in another language for embedded in near future.
  2. sirch2

    Well-Known Member

    Jan 21, 2013
    Not sure what you mean by "loosely bound" but C and C++ are very similar in that they allow the same mistakes, indeed one of the objectives of C++ was to be able to compile C code and this lead to some unfortunate (in my view) compromises.

    One of the issues with C is that you have to do your own memory management and so it is easy to get memory leaks unless you are careful. Languages like Java and the .NET based languages (C#, etc) take care of memory management for you however the approach used means that to some extent it is harder to predict things like execution time because you don't know exactly when the garbage collector will run.

    C has stayed because it is a good language for the task. It was developed at a time when computers didn't have much processing power or memory and so a language which was fairly close to the hardware without much overhead was a good thing. For small embedded processors this is still useful.

    Any program is only "fast" if the person writing it know what they are doing. It is more likely that an inexperienced programmer will write a slow, buggy program in C than in something like Java. However if you understand what you are doing at a detailed level you can make your C program faster. The reason why compiled code may be slower are things like type checking, where the code tracks the types of variables so that bad assignments (assigning a double to a string for example) raise an error rather than just allowing you to write all over the memory and cause a nasty crash, or even worse a security hole.

    More than likely C is here to stay. It is interesting in a way that languages like ADA haven't got wider acceptance in embedded since they are used in industry, particularly for safety critical systems where pointer based languages are not allowed.
  3. Papabravo


    Feb 24, 2006
    A memorable quote from a SIGPLAN conference in 1972 on high-level languages for systems programming:

    "There is no substitute for the right algorithm"

    Sorry, I can't remember who said it, but he said it before there was a C language.
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    Is there a question here?

    Who is this "almost everyone"? Did they take a pole and not ask me?

    "C is very lossely bound" is completely incorrect. There are no losses.

    Alexander Pope said it best" “Whatever is, is right.”

    C++ is object based, while C is procedurally based. There are both code and memory overheads required to support the object abstraction. Additionally C++ requires runtime dynamic memory management, and there is a limit as to how small a processor can be and still run a C++ program; C has no such limit.

    The Magic 8 Ball said "It is decidedly so."
  5. KL7AJ

    Senior Member

    Nov 4, 2008
    I wouldn't say C in inherently faster than C++. I think it's just that you can be a sloppier programmer with C++ and that generally results in slower code. Any language that forces you to program "lean and mean" is going to generally run faster, all other things being equal
  6. nsaspook

    AAC Fanatic!

    Aug 27, 2009
  7. MrChips


    Oct 2, 2009
    You can write sloppy, inefficient code in any language.
  8. NorthGuy

    Active Member

    Jun 28, 2014
    Dynamic memory allocation is a problem for embedded, because they run forever and don't have much memory. This severely limits what you can do with C, and makes C++ all but impossible to use.

    Of course, with new chips, with lots of memory and fast processors, it's different - C programs can go bold and C++ is totally acceptable.

    I never could understand why C is so popular. Languages like Pascal are definitely better for both programmers and compilers. But it is what it is.
  9. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    C is simple and natural (I don't mean like in nature) for embedded programming tasks. Naturalness is something that's hard to perfectly describe but you can feel it when it's right. If you interact with a machine like a bike or car that somehow has the right lines or structure it almost drives itself around a curve at high speed and when you're in the groove you don't feel the machine anymore. Now, this is also about the time you go flying over the curve if you don't have the experience or training to think several steps ahead of the road so care is needed. I think C has 'naturalness' when programming machines because it was designed to be the language of Unix and Unix was the natural outcome of research into how machines should interface with machines at the systems level. This doesn't mean it's perfect for everything but its efficiency of translating ideas between human and computer at the bit-bang level is just not something most people who design languages put a lot of effort into improving today.
  10. MrChips


    Oct 2, 2009
    I cut my teeth on Fortran, BASIC, Algol, Pascal and C.
    Frankly, I don't see that much difference.
  11. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    I've had the misfortune to write device drivers in Fortran long ago. You can make it work but you can tell the language wasn't designed to easily translate bit register level abstractions into concise bit sized chunks like C was. I've worked with at few vendors from France that used Pascal for a embedded PIC18 controller project. I asked the applications engineer while he was on site debugging a problem with the controller that caused unstable beams why they used Pascal, he laughed and said the chief scientist made them use it because it was French and elegant.:D Algol based languages like C are very similar in basic design but you can tell most were designed by a committee instead of guys who just needed something that worked in a 'natural' way without a lot of fluff.


    Last edited: Sep 25, 2014