LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2010, 09:46 AM   #1
portia
Member
 
Registered: Oct 2009
Distribution: Slackware
Posts: 112

Rep: Reputation: 20
the structure of the main function in C


I have started learning C and am following Ivor Horton's "Beginning C - From Novice to Professional".

I've noticed that in the book the structure of the main function is:

int main (void)

however on the internet I found a lot of cases:

void main (void)

Which one is preferred and why?

Thank you
 
Old 09-22-2010, 10:13 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by portia View Post
I have started learning C and am following Ivor Horton's "Beginning C - From Novice to Professional".

I've noticed that in the book the structure of the main function is:

int main (void)

however on the internet I found a lot of cases:

void main (void)

Which one is preferred and why?

Thank you
It doesn't matter, unless you want to look at the return value of your program (from bash, enter "echo $?" as the next command after you run your program). If you use "int main(void)" then what you return from main() will be the return value of your program - if you use "void main(void)" you'll get some random return value.

In general, just use "int main(void)" and return 0 when things go well, 1 when things don't. Programs on *nix generally expect this behaviour.
 
1 members found this post helpful.
Old 09-23-2010, 12:10 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
In future you might also encounter the following
Code:
int main(int argc, char *argv[])
{
...
...
}
argc gives the number of arguments and argv is an array containing those arguments. This way, a program can take command line arguments.

Code:
#include <stdio.h>

int main(int argc, char *argv[])
{
int cnt;
    printf("%d argument%s\n",argc,argc==1?"":"s");

    for(cnt=0; cnt < argc; cnt++)
    {
        printf("%s\n",argv[cnt]);
    }

    return 0;
}
Code:
wim@btd-techweb01:~/forums_linuxquestions/lq833796$ ./a.out hi there
3 arguments
./a.out
hi
there
wim@btd-techweb01:~/forums_linuxquestions/lq833796$ ./a.out "hi there"
2 arguments
./a.out
hi there
wim@btd-techweb01:~/forums_linuxquestions/lq833796$
 
1 members found this post helpful.
Old 09-23-2010, 07:54 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
NEVER use "void main()". It's just plain wrong, but some compilers tolerate it.
 
Old 09-23-2010, 01:46 PM   #5
gozlemci
Member
 
Registered: Mar 2010
Location: Turkey
Posts: 35

Rep: Reputation: 0
MTK358;
While I was learning C, I realized that most books use "int main." However you have strongly not recommend to use "void main." Why?
 
Old 09-23-2010, 01:48 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
http://www.parashift.com/c++-faq-lit....html#faq-29.3

Because it's not valid C. This is can be confirmed by reading the standard.

Last edited by dugan; 09-23-2010 at 01:50 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Assigning Structure object's variable outside main() gives parse error thelink123 Programming 7 09-03-2010 06:41 AM
about main function in C sandeep_raju123 Linux - Newbie 1 12-11-2008 08:35 PM
Why define function pointer in a structure? ArthurHuang Programming 4 08-24-2006 12:32 AM
How to pass structure as function arguments ssg14j Programming 2 08-20-2005 09:59 PM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration