Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have this fucntion , when i use a normal compiler it wors , but when i use keil , it gives me an error that we can use return in void function .
how can i exit from this function after the if condition.
thank you in advance

What I have tried:

#include <stdio.h>
int onevalue(void){
    
    
    return 1;
}



void main(void)
{
    printf("Hello World \n");
    if (onevalue()==1)
    {
        printf ("Error")
      // what is the experssion here to stop this function (main function)  
    }
    for (int i = 0; i<3; i++)
    printf ("here :%d \n" ,  i);


}
Posted
Updated 12-Jan-18 1:28am
Comments
CPallini 12-Jan-18 7:37am    
Do you really want the code exit the main function? As far as I know, Keil compilers cross-compile for microcontrollers...

the correct statement is
C
return;


For instance:
C
if (onevalue()==1)
    {
        printf ("Error");
        // what is the experssion here to stop this function (main function)  
        return; //<-- it's return!
    }
 
Share this answer
 
v3
Comments
BaselAla 12-Jan-18 7:53am    
thank you (:
You can have more than one return in a function. Since this is main(), you may wish, instead, to consider an exit function, instead.

There should be no reason, however, that you cannot use:
if (onevalue()==1)  {
        printf ("Error")
        return;
}


or, wrap the whole thing up as:
void main() {
 if (onevalue()==1)  {
   printf ("Error")
 }
 else {
   for (int i = 0; i<3; i++)
   printf ("here :%d \n" ,  i);
 }
 return;
}
 
Share this answer
 
Comments
BaselAla 12-Jan-18 7:53am    
thank you (:
W Balboos, GHB 12-Jan-18 7:54am    
If either of the replies solves your problem, please mark it as 'accepted' to close the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900