Arduino Forum

Using Arduino => Programming Questions => Topic started by: uelman on Nov 18, 2013, 06:53 pm

Title: syntax command Arduino IDE vs. C-language, same?
Post by: uelman on Nov 18, 2013, 06:53 pm
>>> are the syntax commands (for C-programming) in Arduino IDE different from "pure" C-Language commands?
>>> if they have a slight difference, then I should focus on Arduino's frequently used commands, right?
>>> Is it just that the Compiler makes the difference, since either "other" Compiler or Arduino simply needs the HEX file to transfer the code to the  uC like Atmega ?
Title: Re: syntax command Arduino IDE vs. C-language, same?
Post by: wildbill on Nov 18, 2013, 06:58 pm
The compiler that deploys with the IDE is gcc, so the Arduino is programmed in plain old C/C++. There are also some Arduino specific libraries with helpful functions like digitalWrite. Finally, the IDE transforms your code a little to add a main function and prototypes. In consequence, you can use any C++ constructs you desire.
Title: Re: syntax command Arduino IDE vs. C-language, same?
Post by: graynomad on Nov 19, 2013, 04:36 am
At the basic level the Arduino "language" is exactly the same as C/C++ because that's all it is.

There are however many library functions such as digitalWrite() that may appear to be part of the language but really are just functions written to make life easier. These will not directly port to another environment. The reverse is also true, functions you may be used to say on a PC using Visual C may not work on an Arduino, for example anything to do with the file system.

One other gotcha is as wildbill says, the IDE rearranges your code a bit to help beginners, for example you can easily declare a function after it's referenced and not have a prototype before the reference because the IDE sticks the prototype in for you. That practice will cause the code to fail the compile in other environments.

______
Rob
Title: Re: syntax command Arduino IDE vs. C-language, same?
Post by: GoForSmoke on Nov 19, 2013, 05:16 am
What's under the Arduino easier-to-use IDE custom framework "hood":

http://www.nongnu.org/avr-libc/user-manual/index.html

http://www.nongnu.org/avr-libc/user-manual/modules.html

There is also an Arduino version of the free Code::Blocks IDE, but I haven't needed it yet. I have used Code::Blocks for PC code and it's got better debugging though I haven't found an auto-format like the Arduino IDE has.

http://sourceforge.net/projects/arduinodev/?source=dlp

edit:add

Please be aware that Arduino's like UNO, even MEGA have limited RAM. UNO has 2k for heap and stack and 32k flash ROM for program and constant data (usually tables and text). It is not a good idea to use dynamic objects in such small RAM space even if that's how you do it on a PC. Learn/Use string.h for text manipulation or just evaluate chars with your own routines and you will be able to pack much more into your code.
Title: Re: syntax command Arduino IDE vs. C-language, same?
Post by: pico on Nov 19, 2013, 05:20 am

One other gotcha is as wildbill says, the IDE rearranges your code a bit to help beginners, for example you can easily declare a function after it's referenced and not have a prototype before the reference because the IDE sticks the prototype in for you. That practice will cause the code to fail the compile in other environments.


The other "other" gotcha with the IDE is that it breaks compatibility with the standard C/C++ pre-processor in a number of important ways. So your #include directives won't work as expected, and a referring to a standard C/C++ pre-processor reference will be confusing.

So not exactly C++, at least according to the published standards, but close, for the main part.

FWIW, I disagree that the IDE rearranging your code in this way helps beginners, since a) it leads to confusion when referring to standard references on C/C++, and b) declaring prototypes is no big deal anyway (if you can write a function, you can cut and paste the prototype from the function declaration in about three keystrokes.)

But that's a debate for another time, I suspect.

Title: Re: syntax command Arduino IDE vs. C-language, same?
Post by: pico on Nov 19, 2013, 05:25 am

I haven't found an auto-format like the Arduino IDE has.


Just use the "external editor" option (in "preferences") with any syntax aware programmers' editor. I use emacs, which even has an "Arduino mode" (which is just a slightly tweaked C++ mode).

But just about any decent programmers' editor will be C/C++ syntax aware and able to do auto-format, brace checking, etc.