C/return type of main()
Expert: Prince M. Premnath - 12/5/2007
QuestionI have a simple program in c:
It is as follows:
#include <stdio.h>
main(){
printf("Hi There");
}
After compiling it shows an error saying "Function should return a value" whereas if i simply run it -- it displays the desired message;
Also please tell me how to clean the messages in the output box as they keep on accumulating;
AnswerHi dear Ajay !
The default return type of functions in C is int
eg
returnnothing(); the default return type of this function is "int" , if your function didn't return any values then you have to specify "void" explicitly as follows
void returnnothing()
And same rules will apply for main() function since main() is also a user defined function in C so by default main() too return an integer !
How to clean the messages?
Use one of these two approaches ( just modify the code as follows )
#include <stdio.h>
void main(){ /* specifies no return values for main() */
printf("Hi There");
}
2.
#include <stdio.h>
main(){ /* by default assumed integer return type */
printf("Hi There");
return 0; /* Integer value should be returned */
}
the best approach is to use as follows
int main()
{
return 0;
}
Note : As you are a novice in C i skipped the detailed discussion of return values regarding main() function.. , if you wish just post a follow up !
Thanks and Regards !
Prince M. Premnath