You are here:

C/return type of main()

Advertisement


Question
I 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;

Answer
Hi 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  

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise


I'm sure that I can solve any doubts in Turbo C ,Graphics Programing ,Mouse, Hardware Programming ,File System ,Interrupts, BIOS handling , TSR Programming , General Concepts in C Language, handling inline Assembly statements

Experience

Research over 6+ Years

Organizations
CG-VAK Softwares and Exports Limited

Education/Credentials
Masters in Computer Applications

©2012 About.com, a part of The New York Times Company. All rights reserved.