user62019: I recently decided to learn direct port manipulation instead of builtin arduino routines.
First I tried this piece of code
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("on");
delay(500);                  
digitalWrite(LED_BUILTIN, LOW);   
Serial.println("off");
delay(500);                       
} 

This program on running instantly established connection with the serial monitor on my arduino IDE.
But when I try this code
int main(void)
{
Serial.begin(9600);
DDRB = B11111111;
while(1)
{
PORTB = PORTB | 0x20;
Serial.println("off");
_delay_ms(500);
PORTB = PORTB & 0xDF;
Serial.println("on");
_delay_ms(500);
}
}

But this program is taking some time, about 5-10 seconds to establish serial connection. Also delay() is not working too. When I use delay() instead of _delay_ms(), the entire arduino board freezes like a computer and stops blinking the LED instead LED is fully ON.
I tried is on several programs, while "setup-loop" program connects instantly, AVR C programs does not.
why does it happen?

user4143: By defining your own main() you are circumventing the Arduino startup procedures.
Normally with Arduino code main() first runs an init() function before setup() and (repeatedly) loop().  This init() function will configure, amongst other things, the timer for millis() and delay().
This is the normal main() for a basic AVR-based board:
int main(void)
{
    init();

    initVariant();

#if defined(USBCON)
    USBDevice.attach();
#endif
    
    setup();
    
    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }   
     
    return 0;
}

It first initializes common functions, then board-specific ones (normally nothing), and if applicable, the USB interface (for ATMega32U4 based boards, like the Leonardo).
Only then does it go on to run your sketch.
If you want to use both your own main() and Arduino functions like delay() then you will have to call those initialization routines before running the rest of your code.

62019: <p>I recently decided to learn direct port manipulation instead of builtin arduino routines.</p>
<p>First I tried this piece of code</p>
<pre><code>void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println(&quot;on&quot;);
delay(500);                  
digitalWrite(LED_BUILTIN, LOW);   
Serial.println(&quot;off&quot;);
delay(500);                       
} 
</code></pre>
<p>This program on running instantly established connection with the serial monitor on my arduino IDE.</p>
<p>But when I try this code</p>
<pre><code>int main(void)
{
Serial.begin(9600);
DDRB = B11111111;
while(1)
{
PORTB = PORTB | 0x20;
Serial.println(&quot;off&quot;);
_delay_ms(500);
PORTB = PORTB &amp; 0xDF;
Serial.println(&quot;on&quot;);
_delay_ms(500);
}
}
</code></pre>
<p>But this program is taking some time, about 5-10 seconds to establish serial connection. Also delay() is not working too. When I use delay() instead of _delay_ms(), the entire arduino board freezes like a computer and stops blinking the LED instead LED is fully ON.</p>
<p>I tried is on several programs, while &quot;setup-loop&quot; program connects instantly, AVR C programs does not.</p>
<p>why does it happen?</p>
 4143: <p>By defining your own <code>main()</code> you are circumventing the Arduino startup procedures.</p>
<p>Normally with Arduino code <code>main()</code> first runs an <code>init()</code> function before <code>setup()</code> and (repeatedly) <code>loop()</code>.  This <code>init()</code> function will configure, amongst other things, the timer for <code>millis()</code> and <code>delay()</code>.</p>
<p>This is the normal <code>main()</code> for a basic AVR-based board:</p>
<pre><code>int main(void)
{
    init();

    initVariant();

#if defined(USBCON)
    USBDevice.attach();
#endif
    
    setup();
    
    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }   
     
    return 0;
}
</code></pre>
<p>It first initializes common functions, then board-specific ones (normally nothing), and if applicable, the USB interface (for ATMega32U4 based boards, like the Leonardo).</p>
<p>Only then does it go on to run your sketch.</p>
<p>If you want to use both your own <code>main()</code> and Arduino functions like <code>delay()</code> then you will have to call those initialization routines before running the rest of your code.</p>
