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().

Recommended Answers

All 10 Replies

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

int main()
{
printf("Hello");
return 0;
}

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.

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 ?

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.

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:

int main()
{
    /*...*/
}
int main(int argc, char* argv[])
{
    /*...*/
}

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.

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.......

:icon_rolleyes: What a bunch of useless responses :icon_rolleyes:
except for Tom's...

Why you don't use void with main() : click here

What does return 0() does ?

Nothing.
But return (0); or return 0; will exit the program and return 0 to the operating system.

Small addition:

Nothing.
But return (0); or return 0; will exit the program and return 0 to the operating system.

(if the return statement was placed inside the main function).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.