Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 23:11 29 Apr 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Why C?

     Page 1 of 2    
Author Message
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9063
Posted: 09:40pm 30 Apr 2015
Copy link to clipboard 
Print this post

Just a curiosity of mine - WHY are so many things coded in C?

Obviously, this language has good support for various things, and I am GUESSING that it is a standardised language, meaning that to port from one device running the C code, to some other device running essentially the same C code is relatively painless.

No need for a page-one re-write or anything.

Case in point: The ARMmite port.(from PIC32 architecture, to ARM architecture, but both MCU families run C)

It's not just MCU's - just about any serious PC/MAC/Linux app is written in a C-based language, so how come this particular language has become dominant?

I don't care that it has, I would just like to know why it has.

Thanks to all who henceforth attempt to enlighten me!
Smoke makes things work. When the smoke gets out, it stops!
 
G8JCF

Guru

Joined: 15/05/2014
Location: United Kingdom
Posts: 676
Posted: 11:24pm 30 Apr 2015
Copy link to clipboard 
Print this post

Hi G

C is a High Level Assembly Language !! Seriously it is.

Unlike Assembly Language, C is strongly standardised and has been so for over 25 years.

Porting the C compiler from one target CPU to a different target CPU is "relatively" simple (it's certainly not for the faint hearted, but for those who do this kind of thing it is much simpler than writing a compiler for a language from scratch), because the compiler is mostly written in C itself, so once one gets the bootstrap compiler working with the new target CPU's assembler, then one can build a proper C compiler for the target, and then with the new C compiler recompile/build all the rest of the GNU utils/tools so that in relatively short time, one has a complete toolchain available.

C has all the stuff required to get at the very lowest parts of a CPU/Memory/I/O etc, and yet one can write reasonably high level stuff with it (although anybody who wanted to write business type applications in C would need to have their head examined IMHO).

C++ which although it has the letter C in its name, is a vastly different huge and complex beast built on top of C, and well capable of creating any and all types of applications of any sort including Operating Systems, but it tends to also generate less efficient code, so where space/performance really matter, eg MCUs, OS'es, drivers, DSP etc, C is still dominant - one can still do all that low-level stuff in C++ as well, but most people who do "bit-fiddling" tend to also be closer to the hardware and C suits that constituency better. Having said that, Arduino is C++ based, so perhaps C++ compilers have gotten much better at producing tight fast code.

(Incidentally which is why MMBasic+CFunctions is special and interesting because it allows a very high level approach/view to be taken, ie BASIC, combined with a very low level view, eg I2C/LCD/SetPin etc, and then with CFunctions a bare-metal view - I can't think of another language which covers that range so comprehensively).

That's my take on it, I'm sure there are far more knowledgeable people out there who can enlighten us much more.

Right I'll "duck and cover" now !

Peter
The only Konstant is Change
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3659
Posted: 11:48pm 30 Apr 2015
Copy link to clipboard 
Print this post

I've seen that wrong claim about C before. As C is a higher level language than Pascal the claim doesn't make much sense. It's a kind of urban myth.

I think it's passed around by C beginners and non-C programmers, especially those who know one or more other languages.

They seem to think it must be true because C can often be used where assembler once was (or still is), but that fact doesn't make the myth true.

So, why C? It's expressive, normally results in fast code, does what you say (not necessarily what you meant). The latter hints at a detail: C can be tough for beginners and really it's best if you use it a lot and often. I think similar comments apply to designing hardware, including analogue stuff. (I used to see op amp circuits and knew I should stick to programming.)

Another reason C did well is that some great (readable, short, understandable) books were written about it.

It got standardised (ANSI & ISO), too, partly because it had turned out so useful.

So, used well, C is great. Used badly and it's terrible. And thereby hangs a tale as they say. I think a lot of people dabble or start in C because they see how much C is all over the place. Really, many of the things would be better written in a language that would protect them - by managing strings automatically, by having no pointers, checking array subscripts (indices/indexes, if you prefer) and so on.

C++ is a much bigger more complex language, vaguely related to C, much harder to write well. Not for the occasional programmer, I'd say.

Part of the reason C gets used is that a large amount of lowish-level code was openly and freely available (Linux, GNU, etc - in the older days UNIX etc). And yes fairly portable - some of it very portable. It can get less readable in the process, due to the way CPUs are so different and inflict that onto the code, sadly. More readable and fairly portable languages exist but tend to need largish support subsystems (e.g. JRE). Horses for courses I suppose. Writing lowish-level code in a higher level language tends to be hard work as you fight its deliberate protection features all the time. Yes people have written OSes in BASIC and FORTRAN. Yes they were horrible and now dead (AFAIK).

I've sometimes (not often) wondered: why TTL? Or why 7400 series? I mean, they're so clumsy and low level. Ah, but they do (did?) get the job done. And were so much better than what went before them? I suspect so.

JohnEdited by JohnS 2015-05-02
 
G8JCF

Guru

Joined: 15/05/2014
Location: United Kingdom
Posts: 676
Posted: 12:12am 01 May 2015
Copy link to clipboard 
Print this post

As I said "duck and cover"

  Quote   As C is a higher level language than Pascal


ISO 7185 in 1982 cf ANSI C89 ?

Really ? Edited by G8JCF 2015-05-02
The only Konstant is Change
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3659
Posted: 12:30am 01 May 2015
Copy link to clipboard 
Print this post

Explain the relevance of the dates as to which is higher level.

I'm sure you know that Pascal is infamous for being hardly portable at all, especially if sticking to the standard.

It's not bad as a simple teaching language. Then move on to something better.

JohnEdited by JohnS 2015-05-02
 
hitsware
Guru

Joined: 23/11/2012
Location: United States
Posts: 535
Posted: 12:56am 01 May 2015
Copy link to clipboard 
Print this post

Pascal sorta 'appears' higher level
***********************************************************************************

/* Hello World program */

#include<stdio.h>

main()
{
printf("Hello World");


}

***********************************************************************************

program HelloWorld;

begin
writeln('Hello World');
end.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9063
Posted: 01:33am 01 May 2015
Copy link to clipboard 
Print this post

Interesting......
Smoke makes things work. When the smoke gets out, it stops!
 
Dylan
Regular Member

Joined: 17/06/2013
Location: Netherlands
Posts: 81
Posted: 04:47am 01 May 2015
Copy link to clipboard 
Print this post

  JohnS said   I've seen that wrong claim about C before. As C is a higher level language than Pascal the claim doesn't make much sense. It's a kind of urban myth.

I think it's passed around by C beginners and non-C programmers, especially those who know one or more other languages.

They seem to think it must be true because C can often be used where assembler once was (or still is), but that fact doesn't make the myth true.


C is definitely not "higher level" than Pascal; both are what we used to call 3GL back in the time that the first two generations were binary and assembler.

Some C features were probably influenced by assembler - IIRC the "op=" group and all the more so the increments - where Pascal for example instead had a function Inc(). This is all before my time, so I am not going to pretend any real authority either.

But remember that C was written primarily as a language to write an operating system (and its tools) in. That means it was DESIGNED to be "close to the metal". No myth.

It also happened to be designed sufficiently well to be successful for general programming. In an era where computer time was more expensive than programmer time, efficiency was just as important as now.

For the longest time, microcontrollers were by and large programmed in assembler, because every bit counted, and so did every joule, in the case of digital watches. Nowadays we take it for granted that low power PIC/Atmel/etc chips are available and programmable in C, but for a long time MCUs had so little memory that C was NOT the default.

Let me try to rephrase, in terms of both the OP and this "myth" comment. Pascal was designed as a learning language. function Inc(B: Byte) would be expected to raise an error on 255. C was designed to do what it was told: char b = 255; b++ and b would be expected to be 0 (and yes, the single-byte integer value is called char in C, the constant ' ' is the same as 32.

Note that I am not by any means an expert in C, nor Unix/Linux. People use C for all kinds of horrible reasons, as well as some very valid ones.

To return solely to the OP: C was originally anything BUT standardised. The whole point was to be close to the machine, remember? But C perhaps became SO popular because it allowed porting of other programs to new architectures. For example, big- and little-endian architectures are not all that different, and C had to run/compile on both (similarly 16/32/64-bit and 12/14/18/36-bit too of course.). One way of dealing with this kind of thing which is part of C but not part of the C language is the C pre-processor. Combined with constant folding, this allows certain features to be implemented "closer to the metal" than in say Pascal.

Wiffle waffle :)
 
G8JCF

Guru

Joined: 15/05/2014
Location: United Kingdom
Posts: 676
Posted: 05:49am 01 May 2015
Copy link to clipboard 
Print this post

@G

See what you've started !!

Light blue touch paper, and stand well back.

Peter
The only Konstant is Change
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3659
Posted: 06:31am 01 May 2015
Copy link to clipboard 
Print this post

According to Dennis Ritchie, op= came from Algol68. Now that's a LOT higher level language than Pascal. Maybe you never used A68?

op= came via Steve Bourne (maybe you know the UNIX/Linux Bourne shell), who was at Cambridge (England) and brough A68C with him.

If you use both Pascal and C for any length of time you find that there are lots of things (not just low level) that are very painful in Pascal but easier in C. Not surprising as it has more operators, better type handling, far better formatted I/O and so on. It is easy to justify why it's higher level than Pascal - though not a particularly high level language.

C was actually very standard and portable long before an official standards body got around to formalising it yet more. There were test suites early on and various rather well known compiler experts put a lot of effort into making things work the same as each other.

An unofficial test suite was UNIX and its large number of tools written in C (some hundreds) and in due course GNU tools etc.

Not much Pascal was ever ported, partly because it was so hard (people didn't stick to the standard because it included so little and lacked so much that was needed in real programs) but I think mainly because not that much was ever written.

John

Edited by JohnS 2015-05-02
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3165
Posted: 06:41am 01 May 2015
Copy link to clipboard 
Print this post

  JohnS said   I've seen that wrong claim about C before. As C is a higher level language than Pascal the claim doesn't make much sense. It's a kind of urban myth.

That statement is just plain wrong.

I use C a lot just because it does allow you to manipulate registers and bits as efficiently as assembler but at a much higher level. I think that when Peter called it "a High Level Assembly Language" he was just trying illustrate one of its main features - i.e. a high level language that can replace assembly language.

C is very good at this. Some time ago I was writing a program in C that had a short loop which was critical to the performance of the overall program. So I decided to rewrite the whole loop in assembler. As a check I also examined the disassembled output of the C compiler for the same loop and was amazed to find that it had generated more efficient code than my hand crafted assembler. In fact I learnt a trick or two from examining its output.

A good optimising C compiler is a wonderful thing to behold. The MIPS architecture (used by the PIC32) is designed to work with an optimising compiler so the two go hand in hand.

Geoff
Geoff Graham - http://geoffg.net
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3659
Posted: 06:45am 01 May 2015
Copy link to clipboard 
Print this post

You're in effect saying C can be used to create good code, given a really good optimising compiler. Yes.

That doesn't make it a lower level language than Pascal.

The language itself is higher level than Pascal for the reasons I've given and more I've no doubt skipped.

It's possible to use C to do lower level things than are possible in standard Pascal. That statement does NOT mean C is a lower level language because that is something else entirely. People often confuse "can be used to do low level things" and "is a low (or lower than language XXX) level language". Very distinct meanings.

The level of a language is a reflection of the features in the language, such as recursion (or not), data structures, types, and so on.

C compilers tend to be good - sometimes x=excellent, such as gcc which is the one you're using. (Pascal compilers tend not to be because no-one cares any more and to a fair extent never did. Pascal was not about writing complex or fast programs.)

JohnEdited by JohnS 2015-05-02
 
Dylan
Regular Member

Joined: 17/06/2013
Location: Netherlands
Posts: 81
Posted: 07:12am 01 May 2015
Copy link to clipboard 
Print this post

  JohnS said  (Pascal compilers tend not to be because no-one cares any more and to a fair extent never did. Pascal was not about writing complex or fast programs.)


Put up or shut up.
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3165
Posted: 07:20am 01 May 2015
Copy link to clipboard 
Print this post

Where on earth did this silly Pascal vs C thing come from? No one (except you) is talking about Pascal... who cares if you think one is higher than the other.

As matherp said, stop carping.
Geoff Graham - http://geoffg.net
 
G8JCF

Guru

Joined: 15/05/2014
Location: United Kingdom
Posts: 676
Posted: 07:52am 01 May 2015
Copy link to clipboard 
Print this post

Thank you Geoff, yes indeed that was the point.

A high level language to most people means needing fewer statements/less effort to get something done, and the higher the level of the language the less work the programmer has to do and the more quickly the solution to the problem is ready for Prime Time.

Peter
The only Konstant is Change
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3659
Posted: 08:05am 01 May 2015
Copy link to clipboard 
Print this post

I did put up - I wrote in detail about Why C.

I put a one line mention of Pascal which G8JCF decided to argue about so I justified my position. No-one forced him to argue on nor indeed anyone to go on reading.

John
 
G8JCF

Guru

Joined: 15/05/2014
Location: United Kingdom
Posts: 676
Posted: 08:57am 01 May 2015
Copy link to clipboard 
Print this post

  Quote  I've seen that wrong claim about C before. As C is a higher level language than Pascal the claim doesn't make much sense. It's a kind of urban myth.


All I said was look at the standards which defined each language and then could one objectively really state that C was really a Higher Level language than C as most people would understand the term ?

I don't think anyone agrees with you that C is a Higher Level language than Pascal, and indeed Pascal devotees would probably be mightily offended, but that's language snobbery for you; but why did you bring Pascal into the discussion in the first place, red herrings, distraction stage right ?

You stated that C was Higher Level language than Pascal, and used that as the fundamental basis for your point of view. Since that statement is incorrect, and you built everything else on top of that position then everything that followed must by definition be irrelevant.

  Quote  I think it's passed around by C beginners and non-C programmers, especially those who know one or more other languages.

What a snob - Only True Believers need apply then ?

  Quote  
They seem to think it must be true because C can often be used where assembler once was (or still is), but that fact doesn't make the myth true.

Illogical

@Grogster, have you gotten the answers yet ?

@JohnS, I hate engaging in this type of public disagreement, especially with someone who I respect for their skill and knowledge, but please learn some manners, and treat the rest of us with courtesy and respect.

  William Horman 1440-1535 said  "Manners Maketh Man"


PeterEdited by G8JCF 2015-05-02
The only Konstant is Change
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3659
Posted: 09:03am 01 May 2015
Copy link to clipboard 
Print this post

I did not use it as a fundamental part at all. If you read it that way then I'm sorry you did. The separate paragraphs were just that - separate. Had I made them part of the Pascal statement then they would have been meant non-separate.

It seems you have been arguing about something I didn't say or intend to say.

JohnEdited by JohnS 2015-05-02
 
G8JCF

Guru

Joined: 15/05/2014
Location: United Kingdom
Posts: 676
Posted: 09:41am 01 May 2015
Copy link to clipboard 
Print this post

@JohnS

  JohnS said  I've seen that wrong claim about C before. As C is a higher level language than Pascal the claim doesn't make much sense. It's a kind of urban myth.

I think it's passed around by C beginners and non-C programmers, especially those who know one or more other languages.

They seem to think it must be true because C can often be used where assembler once was (or still is), but that fact doesn't make the myth true.


Just what was the point of these statements in response to @Grogster's question, except to intimidate, and/or prove your superiority over the rest of us - are you really that insecure that you need go on the attack every time ?

When you insult people, make erroneous statements up front, the rest of what you say is lost in the noise.

The rest of your points were very valid, and had you just made those points, rather than opening up with insults, and an attempt to intimidate, then what you said would have been taken as a valuable contribution. As it is all any of us will remember is the bitter taste of your discourtesy and disrespect.

Let's close this off right now, enough's enough, life's too short

Peter

@Gizmo, if you come across this thread, I vote to have it expunged from the Back Shed - this is not how we should be conducting ourselves on TBS - I apologise if I have contributed to the mess.
The only Konstant is Change
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3659
Posted: 09:47am 01 May 2015
Copy link to clipboard 
Print this post

I don't know why you choose to see those as attacks.

John
 
     Page 1 of 2    
Print this page
© JAQ Software 2024