Skip to main content
Associate II
February 26, 2024
Question

Should main return 0 when there is a while(1) ?

  • February 26, 2024
  • 11 replies
  • 3485 views

Hi all,

I'm programming an embedded software.

According to C standards, I write : 

 

 

int main(void)
{
 // Init
 initMethod();

 /* Main loop */
 while (1)
 {
 execute();
 }

 // This return is never reached
 return 0;
}

 

 


But when my code is analyzed by SonarQube, I have a bug report: 'return' will never be executed.

To fix it, I would write the following:

 

 

int main(void)
{
 // Init
 initMethod();

 /* Main loop */
 while (1)
 {
 execute();
 }
}

 

 

But I'm annoyed with the fact that the main method doesn't have the "good" return type.
What is the good pratice in this case ?

Best regards.

    11 replies

    Associate III
    February 26, 2024

    In C, the return statement should always match the return type. What you did is good and that warning is not so important. I dont know where it come from, because its not a warning-case of gcc. Maybe it come from another parser.

    Associate III
    February 26, 2024

    Or maybe its come from gcc with compilation flags ?

    Do you know how your project is compiled ?

    Remi99Author
    Associate II
    February 26, 2024

    For more information, here is the SonarQube rules : 

    Remi99_0-1708964189860.png

    Remi99_2-1708964267704.png

     

     

    mƎALLEm
    Technical Moderator
    February 26, 2024

    Hello,

    Indeed in your case the return will never reached because of the while(1) loop. But, normally, it should be treated as a warning not an error.

    To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
    Associate III
    February 26, 2024

    is gcc is your compiler ?

    Associate III
    February 26, 2024

    There are severals "norms" about compiling C code around the world and epoch.
    Yours seems to unaccept unreachable code. This is just a detail.
    Later in your project, you will probably add some conditions to exit this while loop, and so you will have to add a return statement in your function.

    Remi99Author
    Associate II
    February 26, 2024

    I'm talking about quality. Not about compilation error. 
    The program compiles well and runs. But SonarQube (quality tool) return that the first code is not well written.

    Associate III
    February 26, 2024

    Here, its not about code quality, but about follow the norms of your compiler.

    Pavel A.
    Super User
    February 26, 2024

    Since C99, int main() does NOT have to return anything. Now you can understand why they made this improvement ))

    Tesla DeLorean
    Guru
    February 26, 2024

    The bigger question is WHERE it would return too? Not even sure the GNU startup would get you to exit()

    Can you define it as not returning a value, and likely never leaving?

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Pavel A.
    Super User
    February 26, 2024

    Not even sure the GNU startup would get you to exit()

    At least there's the return instruction. In standalone environment aka "bare metal"  exit() is not warranted.

    https://github.com/STMicroelectronics/cmsis_device_f4/blob/2e1724e592931693ce4dfc43e2fd6b3bc375d6a0/Source/Templates/gcc/startup_stm32f401xc.s#L101

     

    Pavel A.
    Super User
    February 26, 2024

    But I'm annoyed with the fact that the main method doesn't have the "good" return type.

    Int is the perfect return type for main(). The good practice? Before taking on a project, verify which language version and coding guidelines should be used. Refresh your C. Read documentation. Repeat every so often.