943,931 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 927
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 19th, 2009
-1

void before main ()

Expand 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().
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
seo2005 is offline Offline
28 posts
since Jun 2009
Oct 19th, 2009
0
Re: void before main ()
Click to Expand / Collapse  Quote originally posted by seo2005 ...
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.
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is offline Offline
2,196 posts
since Jan 2008
Oct 19th, 2009
0
Re: void before main ()
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.
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Oct 19th, 2009
0
Re: void before main ()
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 ?
Reputation Points: 10
Solved Threads: 0
Light Poster
seo2005 is offline Offline
28 posts
since Jun 2009
Oct 19th, 2009
0
Re: void before main ()
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
SecurExpert is offline Offline
24 posts
since Sep 2009
Oct 19th, 2009
1
Re: void before main ()
Quote ...
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. }
Quote ...
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Oct 19th, 2009
-2
Re: void before main ()
[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.......
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Harsh B.C.A. is offline Offline
1 posts
since Oct 2009
Oct 20th, 2009
0
Re: void before main ()
What a bunch of useless responses
except for Tom's...

Why you don't use void with main() : click here
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,738 posts
since May 2006
Oct 20th, 2009
0
Re: void before main ()
What does return 0() does ?
Reputation Points: 10
Solved Threads: 0
Light Poster
seo2005 is offline Offline
28 posts
since Jun 2009
Oct 20th, 2009
0
Re: void before main ()
Nothing.
But return (0); or return 0; will exit the program and return 0 to the operating system.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,738 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Scheduling CPu algorithms
Next Thread in C Forum Timeline: Array of Pointers to structures...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC