Programming arduino in C

Hello, what I understand about internals of arduino is that it converts the code that we write in the sketch to an equivalent C/C++ code and then compiles it for the microcontroller. Is this correct?

I want to know if it is allowed by the IDE that we write the code in C or C++ rather than the native arduino code? If not is there some way we can code in standard C/C++ using the arduino IDE?

There is no native arduino code. There is C/C++, supplemented with some Arduino specific libraries.

From the FAQ

Can I program the Arduino board in C?

In fact, you already are; the Arduino language is merely a set of C/C++ functions that can be called from your code. Your sketch undergoes minor changes (e.g. automatic generation of function prototypes) and then is passed directly to a C/C++ compiler (avr-g++). All standard C and C++ constructs supported by avr-g++ should work in Arduino. For more details, see the page on the Arduino build process.

You mean I can code in simple C without needing to do with setup(), loo() like arduino specific syntax?

No, you can't do that. Why do you want to?

#include <Arduino.h>

int main(void)
{
	init();

#if defined(USBCON)
	USB.attach();
#endif
	
	setup();  <---- This is the "setup" you write
    
	for (;;) {
		loop(); <---- This is the "loop" you write
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

The reason I am asking this question:

I have been programming my atmega8 with c for a long time, I have that code saved for future use. Now I use arduino and find the IDE much easier to use. SO I was thinking if I could program my boards with the arduino IDE without modifying my already written C codes when I need to use them it would be great.

You mean I can code in simple C without needing to do with setup(), loo() like arduino specific syntax?

Yes, you can override the default behaviour.

Why do you want to?

Code will be fractionally smaller and no call overhead with the 'loop' function ( I think around 6 cycles calling and returning ) compared to cycling just the loop.

  int main(void)
    {
      //Initialise arduino ( so millis, delay work ).
      init(); 
#if defined(USBCON)
    USB.attach();
#endif

      //Setup code here

      //Start loop
      while( true ){

        //loop code here

        if (serialEventRun) serialEventRun();
      }
      return 0;
    }

If you place this in your .ino file ( .cpp equivalent ) it will work.

Not much of a benefit, but you can do it.

Thanks a lot pYro_65, AWOL, Wazzled, dxw00d for all the insight. I will try it for once, may be not much benefit but just for the fun of trying it :)

braindamaged: Thanks a lot pYro_65, AWOL, Wazzled, dxw00d for all the insight. I will try it for once, may be not much benefit but just for the fun of trying it :)

I would encourage its use as it clearly points out that the code is C/C++ rather than an 'Arduino specific' language. When posting code for others, you may want to transpose to the 'setup() loop()' combo or clearly comment that you have omitted them.