C language  - Page 1
Forum section: electronics / beginners

author: Muhammad Nasar
author_rank: Contributor
timestamp: November 29, 2010, 10:32:44 am
content: I am new to electronics and also want to learn C language as it can be help full in programming a Microcontroller. My question is that with which Compiler of C language should i being to learn it. Also plz tell me the difference between C and C++, Can we use C++ to program microcontrollers.

author: GeoffS
author_rank: Unknown
timestamp: November 29, 2010, 10:52:04 am
content: Check out the website of the microcontroller manufacturer - which ever one you choose.They generally have a free compiler to get started.

author: joelby
author_rank: Frequent Contributor
timestamp: November 29, 2010, 10:54:55 am
content: Depends on what device you want to target. The language itself is a standard, so it should theoretically be the same in all compilers. IO register names and interrupt methods will differ between compilers and architectures.If you're very new to programming it might be worth learning basic C on a desktop computer first, using a free compiler such as GCC or MS Visual Studio Express. There are plenty of good books that will teach you about the language constructs and algorithms. C++ is an object orientated superset of the C language. It can be used to program microcontrollers, though it would typically be used on devices with more memory.

author: Murphy
author_rank: Regular Contributor
timestamp: November 29, 2010, 11:01:52 am
content: When I learned I read the guide on howstuffworks.com and found it quite good. (http://www.howstuffworks.com/c.htm)Learning with gcc in linux is great because its free, completely standard, and just works. Type program in text file, gcc program.c -o program.bin, ./program.bin ... no fuss.

author: Chris_R
author_rank: Contributor
timestamp: November 29, 2010, 12:01:01 pm
content: As far as I know a lot of microcontrollers don't support c++ because it's not quite as efficient as C. If you're planing on learning to use C for programming microcontrollers then I would basically ignrore the differences between C and C++ because there are plenty, but the language is similar enough to confuse you if you try learning both.As for learning C I recommend you find yourself a good book and start learning on a computer first, you'll save yourself a lot of headaches by learning the basics of C on a PC before adding microcontrollers to make things more confusing. If you google search for best beginners C books I'm sure you'll find plenty of recommendations.

author: Mechatrommer
author_rank: Super Contributor
timestamp: November 29, 2010, 01:37:22 pm
content: buy a book/ebook, sit infront of computer 18hours a day playing c for the duration of at least 5 years if you want to be a master. c++ is an extension of c, some keywords specific to c++ such as class, template, virtual etc. so c++ is OOP version of c. OOP is meant for a very complex program hierarchy thats not applicable in mcu domain. afaik.

author: Unknown
author_rank: Unknown
timestamp: November 29, 2010, 10:01:07 pm
content: Any language can run on any turing-complete platform (assuming it can support the runtime), the MCU doesn't have to support C++, you just need a C++ compiler that supports your MCU. Higher languages like C++ tend to have a higher overhead, so the smaller the micro, the more likely it is to be an issue.I believe the Arduino uses (some) C++ for some libs, and the mbed is also based on C++. So C++ is used in the MCU space, although C is definitely more popular for small micros, and I don't think C++ is a very good choice if you're short on flash.

author: RayJones
author_rank: Frequent Contributor
timestamp: November 30, 2010, 06:57:23 pm
content: Biggest problem with C++ is it loves to use the stack, which is usually a limited resource in micros.There are however some "C" compilers that inherit some of the ideas of C++ such as "pass by reference".The CCS complier for PIC is one that comes to mind.C++ is also heavily pointer orientated which is always a struggle for new comers to grapple with.

author: tyblu
author_rank: Frequent Contributor
timestamp: November 30, 2010, 08:08:41 pm
content: You can find dozens of articles describing the differences between C and C++ in their full glory.With respect to microcontrollers, the debate continues. Those on the "C" side claim that C++ is inefficient (sounds like the C vs. assembly argument!); those on the "C++" side claim that any inefficiencies can be dealt with by the programmer while reaping the benefits of a higher level language such as OOP, overloading, etc.. There are advantages to each -- to assembler as well -- and a good understanding of the benefits and costs is required to make an informed decision. Start with C or assembler, move up the C++ curve as quickly as possible, then make decisions when complex projects merit the research.

author: Unknown
author_rank: Unknown
timestamp: November 30, 2010, 10:49:42 pm
content: One difference between MCU (at least the small 8/16-bit stuff) and PC programming is that a program for a micro is limited in complexity by a few k flash, so I consider the C++ advantages less important than for a huge business application. But I can see advantages for both sides, if you can make good use of the extra C++ features and the extra flash/memory/performance overhead is acceptable, go for it, programmer time is often much more expensive than hardware, especially for small series.I think C might be a better place to start since you basically have to know C to learn C++, and it's closer to the hardware (although you can do almost anything in C++ that you can do in C).

author: Lance
author_rank: Frequent Contributor
timestamp: December 01, 2010, 07:21:05 pm
content: If this is your first language, I'd recommend staying away from MCUs until you pick up the language. Just write programs in either C or C++ that run on your computer. That way you can focus on actually learning the language, instead of having to debug the hardware as well. Once you have a decent grip on it on the language and gain an understanding into how these machines work, then move to MCUs. It'll be easier to debug any issues you run into once you get there, because you already know the language.

author: scrat
author_rank: Frequent Contributor
timestamp: December 01, 2010, 09:56:59 pm
content: Quote from: Lance on December 01, 2010, 07:21:05 pmI agree. I'll start with C (not C++, although you can use a C++ compiler, which must work for pure C code too) on a PC, making simple console programs (there is plenty of tutorials).A compiler like gcc can be the right way. After a little training with an editor, compiler and linker, an Integrated Development Environment like Dev-Cpp can be useful (it also comes with gcc compiler). When you feel confident with C, you can face C++.Maybe a bit of assembly would be useful, since it forces you to learn how the processor works.

author: Lance
author_rank: Frequent Contributor
timestamp: December 01, 2010, 11:28:25 pm
content: Quote from: scrat on December 01, 2010, 09:56:59 pmIf you're gonna do that just skip assembly and use machine code. Doing C first is a good idea. C++ has features that C doesn't, and it's easy to get hooked on them. If you're in linux try Eclipse out if you want an IDE. It natively supports Java, but you can get C and C++ packages for it:http://www.eclipse.org/cdt/Remember, always look for ways to improve your program. There's a difference between doing something, and doing something well.

author: Mechatrommer
author_rank: Super Contributor
timestamp: December 02, 2010, 01:13:53 am
content: Quote from: Lance on December 01, 2010, 11:28:25 pmassembly is a "directly translated machine code" each opcode with specific machine codeQuote from: Lance on December 01, 2010, 11:28:25 pmnot for me. of course u'll get a habit into programming style, but as long as you know the difference between c and c++ and application you are developing, you can easily change that. as general rule... c is only the basic arithmetic, for loop while if etc, but c++ is more complex and more generalization of classing, OOPing, templating etc. if i'm not mistaken, pointers are included (comes from) in c, not c++ specific, gotta reopen the bookas comment to learn c first before digging into mcu... that can be true. but learning both c and mcu at the same time could be pain in the arse, but the payback is probably you can learn much quicker.. albeit, painful. patience is the key to success.

author: Unknown
author_rank: Unknown
timestamp: December 02, 2010, 07:43:50 pm
content: String manipulation in C is probably going to feel pretty painful after being used to the C++ string class, that would be a practical difference in MCU programming. If you're using C++ in a real object-oriented fashion, it will also be a significant change to move to C. There are also some more subtle differences like the treatment of const and call by reference.

author: Muhammad Nasar
author_rank: Contributor
timestamp: December 03, 2010, 06:49:41 am
content: Can i start with "TurboC" as it's book which is written by "Robert Lafore" is easily avaiable in Pakistan. And thanks for the guidence.

author: GeoffS
author_rank: Unknown
timestamp: December 03, 2010, 07:19:05 am
content: TurboC was a development environment from Borland - it's pretty old.The classic c language text is 'The C Programming Language' (otherwise known as K&R) by Brian Kernighan and Dennis Ritchie. Published by Prentice-Hall ISBN 0-13-110163-3I don't think it's been out of print in the last 30 years.It's available as a PDF from a number of sources online.I'd also do a Google search for 'c language tutorials' and you'll find lots of excellent introductions.

author: Lance
author_rank: Frequent Contributor
timestamp: December 03, 2010, 07:55:35 am
content: There's lots of good online resources too:http://www.cprogramming.com/

author: Mechatrommer
author_rank: Super Contributor
timestamp: December 03, 2010, 03:36:25 pm
content: the easiest, is try to program console/text based c in pc, dont mess yourself with things like windows c programming or highly dependent on non standard libraries, stick just to the basic <stdio.h> <stdtype.h> std etc etc... if your only aim is to program a mcu. just a 2cnts.

author: Jimmy
author_rank: Regular Contributor
timestamp: December 05, 2010, 12:28:10 am
content: If you just want to program microcontrollers the buy an arduino starter kit and you can have stuff working within min's

author: vaualbus
author_rank: Frequent Contributor
timestamp: April 09, 2014, 01:51:21 pm
content: Old post but I will expalin why you can't use C++ if the microprocessor have not a DMA (Direct Memory acess).The C++ need 5 runtime function iplementation that are menaged by the OS, or in this case by the pic.They are the new keyword, the delete, the dynamic_cast and the try/catch and typeid.For the first three you need to menage memory allocation and deallocationand heap status. For the try you need to have an exeception handler.dynamic_cast and typeid are used to retrive runtime (C++ have a little runtime!) informationModern PICs (32, 24) support C++.

author: David Hess
author_rank: Super Contributor
timestamp: April 09, 2014, 11:20:08 pm
content: This is my favorite C book for beginners:http://www.amazon.com/On-C-Patrick-Henry-Winston/dp/020158042XThere is a version for C++ as well.

author: enz
author_rank: Regular Contributor
timestamp: April 10, 2014, 08:34:36 am
content: Quote from: vaualbus on April 09, 2014, 01:51:21 pmSorry, but this post is totally wrong.C++ does not depend on DMA functionality (or any other microcontroller peripheral) nor are the mentioned functions necessary for C++. You can choose to not use it. But even if you use it, these functions do not rely on DMA or any other memory managment unit (a DMA-Controller is NOT a memory managment unit).C++ does not make more demands on a CPU than plain old C does.If it's possible to write a C-Compiler for a CPU (for example hardware stack available), than you can also write a C++-Compiler for it.Martin

author: miguelvp
author_rank: Super Contributor
timestamp: April 10, 2014, 08:37:39 am
content: True that, first C++ compilers did output C so the C compiler would take care of business as usual.DMA is a hardware thing, driven by software I guess but no compiler dependent.

author: amyk
author_rank: Super Contributor
timestamp: April 10, 2014, 09:07:30 am
content: Agreed, C++ started out as a set of extensions to C that make it easier to do OOP and other "large systems programming" things, and C++ can still be translated to C. E.g. instead of having to define structures and functions that manipulate them, define a class and let the compiler generate all the "this" references automatically. The only real difference I've seen is that C++ programmers tend to make use of dynamic allocation alotmore, probably because of how easy the language makes it. That's not entirely a bad thing, although for working with memory-constrained, latency-constrained embedded systems it's something you have to be very aware of.

author: miguelvp
author_rank: Super Contributor
timestamp: April 10, 2014, 09:09:58 am
content: I don't agree with that, overloaded new and delete to manage memory is better than vanilla C++ can do.

author: con-f-use
author_rank: Unknown
timestamp: April 10, 2014, 11:22:19 am
content: Just to put it out thereKim N. King, C Programming: A Modern ApproachIts expensive but most university libraries have it. I like it very much.

author: vaualbus
author_rank: Frequent Contributor
timestamp: April 12, 2014, 11:07:52 pm
content: I soeey but as you pointed out you I was totally totally wrong.I try to said that to have the c++ feature you need a memory menager system that allocated and deallocate memory so some runtime code.Of course you can compile C++ without C++ features.

author: tiborsimon
author_rank: Contributor
timestamp: April 14, 2014, 06:39:14 am
content: I prefer C for small embedded projects. C++ has more power because the OO concept, but for a beginner it may be a bit confusing at the first glance.If you want to play with C, there are a lot of online compilers (without debug of course):http://www.compileonline.com/compile_c_online.phphttp://codepad.orgA small example with codepad:http://codepad.org/OtxQ3Wa2I usually test out algorithms and ideas using these tools. Hope it helps

author: zapta
author_rank: Super Contributor
timestamp: April 14, 2014, 07:02:19 am
content: Quote from: Jimmy on December 05, 2010, 12:28:10 am+1It comes with a first class and free C/C++ compiler, tons of examples, easy to install and use development environment (free of course), community and support forums, supports all mainstream operating systems and and gazillions of optional peripherals on ebay, all very reasonable priced.Here is one random examplehttp://www.ebay.com/itm/UNO-R3-ATmega328P-ATmega16U2-2012-Version-Board-Free-USB-Cable-For-Arduino-/300948763268?pt=LH_DefaultDomain_0&hash=item4611f1b284

author: retrolefty
author_rank: Super Contributor
timestamp: April 21, 2014, 03:53:49 pm
content: Quote from: zapta on April 14, 2014, 07:02:19 am+2The Arduino IDE uses a version of the popular open source gcc compiler, versions that can be obtained to run on a PC. As C++ is a superset of C you can choose to just use bog standard C statements in your arduino sketches (programs) without having to understand anything about C++ until you are ready to master them. Most all the arduino supplied standard functions and libraries are C++ based but they work transparently without you having to understand of C++ classes work, etc.One has to be careful with trying to utilize too many of the C++ features (classes, OPP, etc) as most ucontrollers are very much SRAM limited so heap and stack space is at a premium. String functions are an example of being taxing on many 8 bit ucontrollers, and it's better to deal with them as arrays of characters using the simpler C functions.