Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > Question about C

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 2 Weeks Ago   #1 (permalink)
Just Joined!
 
Join Date: Nov 2009
Posts: 6
Question about C

how come sometimes I see "int main()", sometimes I see "void main()" or just plain "main()" .. what's the difference?

also, "return (0)" defines the errorlevel when the program finishes. right?

thanks
oospill is offline  


Reply With Quote
Old 2 Weeks Ago   #2 (permalink)
Linux Engineer
 
RobinVossen's Avatar
 
Join Date: Aug 2007
Location: The Netherlands
Posts: 1,319
Send a message via ICQ to RobinVossen Send a message via MSN to RobinVossen
the variable type in front of a function is what kind of variable the program returns.
This should be int since you want to return Exit codes (eg 0 successfull run)
if you do void you always return 0 and therefore this is bad practice.

Hope that helped
__________________
My webpage: codeinject.org
New Users, please read this..
Google first, then ask..
RobinVossen is online now   Reply With Quote
Old 2 Weeks Ago   #3 (permalink)
Linux Guru
 
coopstah13's Avatar
 
Join Date: Nov 2007
Location: NH, USA
Posts: 1,760
don't forget about your parameters

Code:
int main(int argc, char **argv)
coopstah13 is online now   Reply With Quote
Old 2 Weeks Ago   #4 (permalink)
Linux Engineer
 
RobinVossen's Avatar
 
Join Date: Aug 2007
Location: The Netherlands
Posts: 1,319
Send a message via ICQ to RobinVossen Send a message via MSN to RobinVossen
However the int argv, char *argv[] isnt required. its recommended
__________________
My webpage: codeinject.org
New Users, please read this..
Google first, then ask..
RobinVossen is online now   Reply With Quote
Old 2 Weeks Ago   #5 (permalink)
Linux Guru
 
Rubberman's Avatar
 
Join Date: Apr 2009
Location: I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
Posts: 2,662
Back in the "old" days of K&R C, you could define main() as returning void, but that is now deprecated and should be defined to return an int value - the exit code for the application. The following are valid signatures for main() these days:

int main(void);
int main(int argc, char** argv);
int main(int argc, char* argv[]);
int main(int argc, const char** argv);
int main(int argc, const char* argv[]);

The second and third versions of the argument list with char* types is allowed, provided you want to modify the actual contents of the arguments at runtime (not recommended). So, the first and last two versions are preferred - the first only when you don't allow any arguments, and just want to ignore them if passed from a shell.
__________________
Sometimes, real fast is almost as good as real time.
Rubberman is offline   Reply With Quote
Old 2 Weeks Ago   #6 (permalink)
Linux User
 
darkrose0510's Avatar
 
Join Date: Jan 2006
Location: Queensland, the cyclone/flood/drought capital of Australia
Posts: 364
void main() is still ok if you're writting a kernel though, as main should never return in a kernel, or at the very least returning anything is pointless.
__________________
Inventor of '{' and '}' - patent pending

A computer without a Microsoft operating system is like a dog without bricks tied to its head.
darkrose0510 is offline   Reply With Quote
Old 2 Weeks Ago   #7 (permalink)
Just Joined!
 
Join Date: Nov 2009
Posts: 1
Quote:
Originally Posted by oospill View Post
how come sometimes I see "int main()", sometimes I see "void main()" or just plain "main()" .. what's the difference?

also, "return (0)" defines the errorlevel when the program finishes. right?

thanks
It is bcoz in linux OS it takes int main () defaiult as int return type to the OS when main terminates.
Also if you not specify any return type i,e main() only then compiler will explicitly add int data type to the program and compiler......

And also return(0) is very common that main() also a function and it has int return type so, return(0) tells the linux kernel that program is terminated normally.........
pavan45 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 05:35 PM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2