differences in c programming languages? - Page 1
Forum section: electronics / beginners

author: doctorm
author_rank: Regular Contributor
timestamp: December 24, 2011, 12:30:21 am
content: hi,i had asked in another post about where to find stuff to learn about c programming and i got alot of great answers but some more questions were raised.i would like to ask what are the differences between the different languages of c (C, C++, Obj C, etc)?i just want to start learning about programming and would like to use a language that i can do alot of things with, let is be with creating programs on my windows computer or whatever for electronic projects.some say C++ is better to go with than C and it seems that there is more info out there about C++ than C. any reason why this is? is C++ the better choice?thanks for your help.

author: IanB
author_rank: Super Contributor
timestamp: December 24, 2011, 12:41:48 am
content: C++ is more recent than C and it filled in some of the gaps that made C harder to use. However, C++ also added lots of complicated features of its own that can be challenging for the newcomer. The main reason for using C++ (instead of a managed language) these days is hardware efficiency. If you are not faced with such considerations, then C would be the best language to learn for microcontrollers, and one of the managed languages like C# or Java would be better to learn for programming on desktops.The bigger picture here is what is involved in learning to program. You need to learn two things: one is how the hardware works as a machine, and the other is about algorithms, program structure and design patterns. You could use C and microcontrollers to help get an appreciation for the hardware and C# or Java on the desktop to learn about constructing programs to solve problems.

author: Unknown
author_rank: Unknown
timestamp: December 24, 2011, 12:58:23 am
content: C++ support in some C compilers for micro-controllers is somewhat sketchy and definitely less mature than C support. I've had some issues with the Keil compiler for ARM and virtual methods, for example.I agree that C++ fixes some of the issues with C (eg. no real string support, memory management), but also introduces a lot of complexity. I probably wouldn't recommend either to learn programming since they both have a fair number of non-obvious ways to shoot yourself in the foot (eg. buffer overflows, if (foo=0)). But between C and C++, I think learning C makes more sense if you're primarily interested in programming micros.Quote from: IanB on December 24, 2011, 12:41:48 amI find this sentence a bit unfortunate. C++ is less hardware efficient than C, but still much better than languages like C# or Java. Don't even consider the latter on anything less than a 32-bit micro, and even then they're not well suited for the smaller ones like the ARM Cortex in my opinion, although Microsoft is trying.

author: IanB
author_rank: Super Contributor
timestamp: December 24, 2011, 08:15:29 am
content: Quote from: alm on December 24, 2011, 12:58:23 amYes, poor choice of words perhaps. I meant using C++ in preference to a managed language like Java, since nobody is going to use C for commercial programming on desktops or servers these days. It was not a recommendation to use C++ on micros.

author: joelby
author_rank: Frequent Contributor
timestamp: December 24, 2011, 08:27:41 am
content: QuoteOn Unix systems, and for server/daemon applications in particular, C is still very much the language of choice.

author: Bored@Work
author_rank: Super Contributor
timestamp: December 24, 2011, 11:00:17 am
content: Quote from: doctorm on December 24, 2011, 12:30:21 amWithout going into a detailed elaboration, as a first approximation you can consider C, C++, Objective C, Java and others to be different languages. But with a common ancestor, C.Because of the common ancestor they do share, sometimes a lot, of syntax elements and semantics. But they all do have distinguished features, and also deviations from C, that you can consider them different languages.Even in the areas where they look similar or identical it is often that they are conceptually different. Meaning, for example, that some constructs which are considered good and proper in one of the languages are considered bad and evil in others. Therefore I don't agree to statements that some languages "fix" issues with another of those languages. The languages have different concepts.The good news is, because of the common ancestry it gets easier to learn another of the languages once you know one of them.And it really doesn't matter with which of the languages you start. Programming is much more than just knowing one programming language, and there is no one-size-fits-it-all programming language. Despite what some fanboys like to tell you about their favorite language.BTW: C is neither dead nor not used anymore. The December 2011 TIOBE index of language popularity lists C on position two, almost on par with Java, and before C++, C#, and Objective-C (in that order). And it was on this position one year ago, too. So forget about statements like "nobody" is using C any more.

author: Short Circuit
author_rank: Frequent Contributor
timestamp: December 24, 2011, 01:09:27 pm
content: No idea about the differences, but I do know that plain C is the one and only choice for serious deep embedded (microcontroller) programming.And once that has become your 'native' language, you probably end up using it everywhere else. Personally, I never felt limited by C when programming for Windows, although it's probably a bit more work when it comes to the GUI part.

author: westfw
author_rank: Super Contributor
timestamp: December 27, 2011, 10:08:21 am
content: For desktop programming, one of the "newer" languages like C++ or Objective C will let you reference a library/"class" and start to use "objects" like windows and menus and so on, with a high level of abstraction and without having to worry about all the little bits that go into putting together such GUI things.For embedded programming, that level of "distancing" from the actual mechanisms is frequently less desirable, and plain C (or a restricted subset of one of the more advanced languages) is more typical.

author: Greg J
author_rank: Regular Contributor
timestamp: December 27, 2011, 05:35:43 pm
content: There is no such thing as 'C programming languages'.C is a language on its own (ANSI C), C++ is 95% compatible with C, but the paradigms and the approach is completely different.And Objective C has C in the name, because its C based, but the language itself is completely non-C. It has C in the name, because it used to be set of macros that translated it to straight C. That isn't the case anymore.

author: IanB
author_rank: Super Contributor
timestamp: December 27, 2011, 06:54:36 pm
content: Quote from: BoredAtWork on December 24, 2011, 11:00:17 amThat doesn't make any sense. I can understand using C for embedded programming where it makes perfect sense, but using C for commercial large scale programming makes no sense at all. I know people may use it for financial programming in banks, but fundamentally people who use C today for large complex systems are nuts(Just because something is popular doesn't mean it is sensible--usually the opposite in fact. Following the crowd isn't usually a good way to make sound decisions.)

author: steff
author_rank: Contributor
timestamp: December 27, 2011, 07:55:17 pm
content: Quote from: IanB on December 27, 2011, 06:54:36 pmYou're forgetting that not all software is large or complex. Some of it is smallish, of lesser-to-middling complexity and needs to perform well (and it's helpful if it doesn't cart a bloody great VM around with it). Think of such things as web and mail servers. Routinely written in C. Or small utilities which need to run in consistent amounts of memory. I wrote a small tool a while back which accepts incoming network connections and forwards them to a real and a testing service, discarding the responses from the service under test. Nice to write in C (with libevent2) did the job without needing to be multithreaded to mitigate CPU use and no worrying about GC stalls or any of that nonsense. There are a lot of small, simple bits of infrastructure still being developed and C does a very good job at them, as it always has.

author: Unknown
author_rank: Unknown
timestamp: December 27, 2011, 08:49:30 pm
content: C is also by far the most popular programming language for system programming. I believe all currently popular OS kernels are written in C, and in the case of Windows, so is most of the OS. At least until Windows XP, almost all of the Windows source code was written in C, hardly qualifies as a small, simple system. Just because C doesn't make sense for your applications doesn't mean it doesn't make sense for anyone else.

author: IanB
author_rank: Super Contributor
timestamp: December 27, 2011, 09:15:21 pm
content: The time before Windows XP is ancient history, when dinosaurs still roamed the earth. Today I would use the efficient subset of C++ for systems programming (no virtual methods, polymorphism or overloaded functions etc.). Just as efficient as C, but much more productive.Even the smallish, lesser-to-middling complexity stuff is going to be much faster and more convenient to write with a C++ compiler than a C compiler. It gives you exactly the same performance and control over resource allocation as C (no VM in the picture), but is more convenient to write and maintain.If you have a C++ compiler available you may as well use it. Forcing your C/C++ compiler into C mode is like using a Rigol oscilloscope when you have an Agilent on your desk.Granted there may be companies out there who mandate the use of C, but as I said above following the herd does not always lead to a good destination--and I see no reason to recommend C to newcomers when they are not subject to external constraints.

author: xygor
author_rank: Regular Contributor
timestamp: December 27, 2011, 09:17:30 pm
content: FYI, a new official standard (ISO/IEC 9899:2011) for C has just been published.http://www.h-online.com/open/news/item/ISO-updates-C-standard-1400814.htmlhttp://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=57853

author: Bored@Work
author_rank: Super Contributor
timestamp: December 27, 2011, 10:36:14 pm
content: Quote from: xygor on December 27, 2011, 09:17:30 pmDon't confuse IanB with facts.

author: don.r
author_rank: Frequent Contributor
timestamp: December 27, 2011, 10:37:03 pm
content: Quote from: IanB on December 27, 2011, 06:54:36 pmNot since the early 90s. Since then its been C++, Objective-C (if you were working on NeXTs) and more recently C#.Since C++ is a complete superset of C, I don't see why you wouldn't use it if the compiler was available. It has some very nice enhancements even if you eschew the OO aspect of the language.

author: IanB
author_rank: Super Contributor
timestamp: December 27, 2011, 10:57:25 pm
content: Quote from: don.r on December 27, 2011, 10:37:03 pmPerhaps I described that poorly. I was thinking of automated trading systems where they are aiming for millisecond trades. I am given to understand they program those systems at a low level close to the hardware.QuoteWhich is exactly my point indeed. The very existence of a new C standard recognizes that C can be improved, and the source of much of that improvement is C++.

author: don.r
author_rank: Frequent Contributor
timestamp: December 27, 2011, 11:19:20 pm
content: Quote from: IanB on December 27, 2011, 10:57:25 pmMy first autotrader was built in Excel using macros.I'm sure things have moved on but they are a very small part of programing in finance (perhaps less than 0.1%). Just about everything is done in C++ and C# these days, AFAIK (I've been out of the game for over 7 years now). As you said, just about anything and everything that uses C can use C++ as well with very little efficiency loss. I think its all the legacy C stuff (like most of Unix) that keeps C going these days and perhaps lack of compilers. It's certainly the most pervasive of all languages. If the thing can be programmed, its probably got a C compiler for it.

author: Bored@Work
author_rank: Super Contributor
timestamp: December 27, 2011, 11:21:47 pm
content: Quote from: IanB on December 27, 2011, 10:57:25 pmAnd just a few month ago a new C++ standard was released. Are you aware that you are scrapping the bottom of the barrel for arguments? By your "logic" the new C++ standard indicates that C++ was so broken it needed improvements.However, in reality, the update of a standard simply indicates that there is enough interest in a language that people are willing to put in time and money to get it updated.I have heard the pseudo argument "oh, this stuff is so old, it must be bad" again and again. Whether it was about hardware, ICs, software, languages, tools, IDEs, command line,and whatnot. The thing is, "new" and "improved" as such are of no added value. If it works it works. If it is good enough it is good enough. If it is tried and trusted it is tried and trusted. New, however, just means it is new, pffft.

author: westfw
author_rank: Super Contributor
timestamp: December 28, 2011, 12:31:41 pm
content: Out usual excuse against C++/etc over C was that it made it too easy to accidentally shoot yourself in the foot. It was all too easy to accidentally use some feature that didn't look inefficient or dangerous, but in fact WAS. A small example of this is all the beginners trying to use C++ "Strings" on Arduino... "print( "abd" + "def" + "hij" + mstr) doesn't look so bad, but it's not something that should be done. IMO.

author: Greg J
author_rank: Regular Contributor
timestamp: December 28, 2011, 02:21:12 pm
content: String operations like that are exactly what you should do in C++, but if you have sufficient amount of ram for itEssentially every '+' is an operator, a string class method that will try merging two strings, and returning the new one.

author: ToBeFrank
author_rank: Regular Contributor
timestamp: December 29, 2011, 04:52:34 pm
content: Quote from: Greg J on December 27, 2011, 05:35:43 pmStolen from wikipedia:Quote

author: ToBeFrank
author_rank: Regular Contributor
timestamp: December 29, 2011, 05:24:38 pm
content: Quote from: don.r on December 27, 2011, 10:37:03 pmThis is a little nitpicky, but your statement here is not true. There are programs written in C that are not valid C++.http://public.research.att.com/~bs/bs_faq.html#C-is-subset

author: Mechatrommer
author_rank: Super Contributor
timestamp: December 29, 2011, 05:43:28 pm
content: i'm following this thread... :-|Quote from: ToBeFrank on December 29, 2011, 04:52:34 pmnetcop! haha!

author: don.r
author_rank: Frequent Contributor
timestamp: December 29, 2011, 06:13:10 pm
content: Quote from: ToBeFrank on December 29, 2011, 05:24:38 pmYes... well... let's just say:if you did C programming properly, C++ is a superset of C. Although I'll give you that its not 100% complete and I have personally used void * in the past many times. I would still rather program in C++ than C if the compiler was available.To quote the great Stroustrup againQuote

author: Greg J
author_rank: Regular Contributor
timestamp: December 29, 2011, 06:17:00 pm
content: yes, objective C is super set of C. But trust me, objective C has completely different syntax for ObjC stuff. It's a superset for historical reasons. But you either write one or the other.You can mix them in the code, but ObjC is pretty slow, due to its construction (there are no functions, there are messages, etc, etc).As for C/C++ , this for instance will work in C, but won't in c++:char* foo = "foobar";char bar;strcpy(bar, foo);Or should I say, it will compile in C, but won't in C++. It will crash in C anyway.C is flawed in so many ways, but because its still quite close to assembler - it will be mainly used in embedded stuff.However, I would suggest using C++ish , i.e. - use C++ like C with objects - but only for embedded stuff. Writing bigger programs in such a way is a crime.

author: don.r
author_rank: Frequent Contributor
timestamp: December 29, 2011, 06:24:03 pm
content: Quote from: Greg J on December 29, 2011, 06:17:00 pmYes. If you use the messaging nature of ObjC the dynamic dispatch engine slows things down. Not good for embedded speed critical stuff but great for desktop and large system programming.

author: Bored@Work
author_rank: Super Contributor
timestamp: December 29, 2011, 06:58:05 pm
content: Quote from: don.r on December 29, 2011, 06:13:10 pmDon't you think that guy is a tad biased? I mean, just a tad, a tiny wee bit of a glimmer of a breeze of bias?

author: Mechatrommer
author_rank: Super Contributor
timestamp: December 29, 2011, 07:31:03 pm
content: i can see the confusion between the concept/abstract of "programming languages/syntaxes/structures" vs "compiler making/implementation or specific vendor's compiler or cheapo vs professional compiler". or maybe its me who got confused.

author: gregariz
author_rank: Frequent Contributor
timestamp: December 29, 2011, 07:41:49 pm
content: RE:Quote from: Mechatrommer on December 29, 2011, 07:31:03 pmSince the OP referred to electronics projects I find that there are considerable differences between various ansi C implementations for the various micro's - particularly the vendor supplied/device library bases such that ones code is not quite as portable as one may think. Even the humble printf statement is routed to different places depending on the micro. The implementations of floats vary widely from not at all to a varying number of bits.

author: Greg J
author_rank: Regular Contributor
timestamp: December 29, 2011, 07:47:24 pm
content: If you get vendor specific compiler, and it doesn't adhere to standards (like it uses some custom defined types, like INT8, or whatever, instead of int8_t, etc) - just refuse it, and write to them about it. Change uC , whatever.Both C and c++ are well standardised! For years now !Plus , there are open source compilers that are easily adjustable to a platform that a vendor should use if they don't have their own, or if their own is full of crap code, and struggles to meet standards. To name two, in order of ease to reuse - clang (llvm.org), and gcc (gcc.gnu.org).Don't support uC vendors that supply overpriced, crappy shit IDEs and compilers. It only encourages even worse code, and embedded software engineers make the worse kind of software engineers - because they put up with all that crap they were fed for years.

author: gregariz
author_rank: Frequent Contributor
timestamp: December 29, 2011, 07:54:54 pm
content: Quote from: Greg J on December 29, 2011, 07:47:24 pmElectronic products are about $. The development choices made during project startup usually have little to do with standards adherance.

author: Greg J
author_rank: Regular Contributor
timestamp: December 29, 2011, 08:18:53 pm
content: Trust me, I've been through number of projects in my life - and you end up paying for that in the end great price.

author: don.r
author_rank: Frequent Contributor
timestamp: December 29, 2011, 08:42:05 pm
content: Quote from: BoredAtWork on December 29, 2011, 06:58:05 pmPerhaps just a tadbut I have never seen anything to refute it.

author: steff
author_rank: Contributor
timestamp: December 29, 2011, 09:53:56 pm
content: Quote from: don.r on December 29, 2011, 08:42:05 pm"For want of a compiler" is a hell of a get-out clause. For a long while there was no single compiler that actually implemented all of the C++ standard, and for all I know that may still be the case. The language spec isimmenseand hardly anyone uses more than a small subset of it (but unfortunately never the same subset). Hence Java, and all the fail that brought with it.

author: Mechatrommer
author_rank: Super Contributor
timestamp: December 29, 2011, 11:37:25 pm
content: Quote from: steff on December 29, 2011, 09:53:56 pmand each compiler's vendor like to add they own flavorings and puddings into the compiler, usually in the form of preprocessors and constructs.

author: nasrmiad
author_rank: Contributor
timestamp: December 30, 2011, 11:15:18 pm
content: It is really interesting that most programming languages (such as C++, C#, Obj. C, Java,...) are all built on top of the C programming language. With the main difference that all except C are object oriented. But I am personally more fluent in C as it is the language that is closer to hardware I believe.I just thought to give a personal opinion.

author: bruce273
author_rank: Contributor
timestamp: January 02, 2012, 12:47:13 am
content: C is certainly a very good language to learn but it does require some knowlage of how processors work depending on how far you go with it. It is by far the most efficient language I know of if you use it correctly. C++ adds alot but it doesn't really allow you to do more than C it's just a method of writing code more accuratly for larger projects.Best reason to learn C however is that so many languages use similar syntax that you can learn the basics of other languages such as c# and java in a day.And don't be afraid to start with java if your programming on desktops or android because the same is true of the other way round. I would warn against it if you plan on moving to microcontrollers as java/c# generally don't make you think about memory management that much