void before main ()

Reply

Join Date: Jun 2009
Posts: 28
Reputation: seo2005 is an unknown quantity at this point 
Solved Threads: 0
seo2005 seo2005 is offline Offline
Light Poster

void before main ()

 
-1
  #1
Oct 19th, 2009
I am a beginner in c and would like someone tp help me . My question is why I have to write void before main to execute the c program.

#include<stdio.h>

void main()

clrscr();

{
printf("Hello")

}

This is a very simple program. But I am unable to execute it if I don't write void before main().
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 346
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 44
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Whiz
 
0
  #2
Oct 19th, 2009
Originally Posted by seo2005 View Post
I am a beginner in c and would like someone tp help me . My question is why I have to write void before main to execute the c program.

#include<stdio.h>

void main()

clrscr();

{
printf("Hello")

}

This is a very simple program. But I am unable to execute it if I don't write void before main().
That must be an old text...It should be
  1. int main()
  2. {
  3. printf("Hello");
  4. return 0;
  5. }
Last edited by gerard4143; Oct 19th, 2009 at 1:09 am.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training
 
0
  #3
Oct 19th, 2009
also remeber don't use void main since u might want to check it's return value if it's return sucess or right also main is a specailized function which alawys start the program at like entry point for example . u should see some tutorials on c before doing anything on it.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: seo2005 is an unknown quantity at this point 
Solved Threads: 0
seo2005 seo2005 is offline Offline
Light Poster
 
0
  #4
Oct 19th, 2009
Is it true that some compilers require void before main ?
Also when I declare a variable like this

int a,b,c;

the error shown is : The declaration is not allowed.

Why this error ?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 18
Reputation: SecurExpert is an unknown quantity at this point 
Solved Threads: 1
SecurExpert SecurExpert is offline Offline
Newbie Poster
 
0
  #5
Oct 19th, 2009
Use void to tell the compiler that the function is not expected to return a value. Use int if the function returns a value. By the way its also dependent on the compiler. Some may demand the void like in your case, so use it, that is if the program returns a value.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
1
  #6
Oct 19th, 2009
Is it true that some compilers require void before main ?
No. Any compiler that claims to compile C is required to allow a return value of int from main(). A return of void from main() is an extension that only some compilers allow. These two definitions of main(), and anything equivalent, are the only correct and portable options:
  1. int main()
  2. {
  3. /*...*/
  4. }
  1. int main(int argc, char* argv[])
  2. {
  3. /*...*/
  4. }
Why this error ?
I cannot say without seeing more code. It is probably in the wrong place, judging from the misplacement of clrscr() in your first post.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: Harsh B.C.A. is an unknown quantity at this point 
Solved Threads: 0
Harsh B.C.A. Harsh B.C.A. is offline Offline
Newbie Poster
 
-2
  #7
Oct 19th, 2009
[QUOTE=seo2005;1019063]I am a beginner in c and would like someone tp help me . My question is why I have to write void before main to execute the c program.

#include<stdio.h>

void main()

clrscr();

{
printf("Hello")

}

of course it is a simple program but the void is responsible to return the value.if u dont put void before main() then how should ur program return the desire value.......

but you can also run the program without puting void......
try it.........

#include<stdio.h>
main()
{
clrscr();
printf("hello");
return 0()
}

here return 0() work as void and it returns the vlue of program..

note:-
1) put ; after the end of printf("hello");
===============================================

i hope my answer satisfies you.......
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei
 
0
  #8
Oct 20th, 2009
What a bunch of useless responses
except for Tom's...

Why you don't use void with main() : click here
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: seo2005 is an unknown quantity at this point 
Solved Threads: 0
seo2005 seo2005 is offline Offline
Light Poster
 
0
  #9
Oct 20th, 2009
What does return 0() does ?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei
 
0
  #10
Oct 20th, 2009
Nothing.
But return (0); or return 0; will exit the program and return 0 to the operating system.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC