LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-03-2007, 06:53 PM   #1
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Rep: Reputation: 30
More questions on C++ style


Hello,

I am really interested in learning the best style to create C++ programs. So I have a couple more questions on style.

How do you guys write your main() function?

Code:
main ()

int main ()

void main ()

Do you place a return 0; at the end of your main()?


So far the way I have been shown to program in class is:

Code:
main ()
{
    statement;
    .
    .
    .
}
But the book shows:

Code:
int main()
{
    statement;
    .
    .
    .

    return 0;
}

Which way to you write your general function header line and curly braces?

Code:
return-type function-name(arguments){
    statements;
}

return-type function-name(arguments)
{
    statements;
}

And the last question I have right now is how do you write your function return?

Code:
return expression;

return (expression);

Thanks in advance.
 
Old 10-03-2007, 08:47 PM   #2
_john_i_
Member
 
Registered: Aug 2003
Location: Austin, TX
Distribution: Linux from Scratch
Posts: 52

Rep: Reputation: 15
Really I think the best style is whatever style you are comfortable with.

I always do my main like this:
Code:
int main(int argc, char *argv[])
{

        return 0;
}

I really hate the opening curly brace on the same line as the function, etc. I find it much more readable when they are aligned in the same column. I also prefer 8-space indentation - also better readability.

You'll find most people feel strongly about their personal style.


I usually avoid the parenthesis around the return value.
 
Old 10-03-2007, 09:27 PM   #3
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
The C++ standard requires "int main()". For your other questions, the answer is "whatever the style guide at your workplace/school says". (My current employer requires vertically-aligned braces and parentheses around return values. My college comp sci courses had preferred them the other way. I learned to adapt.)

If you don't have an official style guide, then just use your personal preference. If you haven't decided yet, just flip a coin: Heads for K&R brace style and tails for vertically-aligned ones.
 
Old 10-03-2007, 09:34 PM   #4
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Always put an "int" before main, and return and integer, some compilers expect it. It's also good form, so someone using your program through a script can check to see its exit status. As for curly braces, I follow the kernel coding conventions, and use separate lines for functions, same line for other forms (unless it makes it less readable).
 
Old 10-04-2007, 12:50 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by JMJ_coder View Post
How do you guys write your main() function?

Code:
main ()

int main ()

void main ()
This has been discussed countless times all over the web. Basically, in C++, the only second form is acceptable (the others are WRONG!). In modern C, both the first and second forms are acceptable (since a missing return type will default to int), but the second is preferred. The third may have been acceptable a long time ago in a dialect of C where a missing return type defaulted to void. To be portable, use only the second form when writing C or C++.

An additional note is the arguments. Both C and C++ assure that a conforming implementation will accept
Code:
int main ()
as well as
Code:
int main (int argc, char *argv[])
(or their equivalents). Any other arguments may be accepted, depending on the implementation. For example, on most Unix systems, you can use
Code:
int main (int argc, char *argv[], char *envp[])
Another note about C and C++ arguments. When you leave the arguments empty in C++, it means the function takes no arguments. When you leave the arguments empty in C, it means the number of arguments is unspecified. So to tell a C compiler that the function takes no arguments, you put void in the parentheses. Sometimes, with code designed to work in either C or C++, you’ll see this practice.
Quote:
Originally Posted by JMJ_coder View Post
Do you place a return 0; at the end of your main()?
The standard leaves this up to style. For any other value-returning function, flowing off the end of the function without a return statement results in undefined behavior. Since main() is special, flowing off the end of main() is equivalent to returning 0 as the last statement. I think most people would agree that it’s good style to have an explicit return statement for main().
Quote:
Originally Posted by JMJ_coder View Post
Which way to you write your general function header line and curly braces?

Code:
return-type function-name(arguments){
    statements;
}

return-type function-name(arguments)
{
    statements;
}
I think you’ll find that the answer depends on whether the programmer is more used to C or C++ programming. For C programming, many people (including myself) prefer the K&C bracing style (used in the linux kernel), where the opening brace is on the same line for control statements (e.g., for, switch, if, etc.), but on a separate line for functions. The oft-cited reasoning for this apparent inconsistency is that in C, functions are always outside everything else and cannot be nested (i.e., if you use that style, you can guarantee that the opening brace will be in column 1, and you can guarantee that the function name will begin in column 1 on the line above). All other control statements are always inside either a function or another control statement, so it does not apply to them.

Of course, in C++, functions are not always outside everything else. They can be inside classes or namespaces. Additionally, classes and namespaces may be nested or be local to other functions. Therefore, the idea of putting the opening brace on column 1 of the next line is not always helpful. So for C++, some people put the opening brace on the next line only for “outside” functions (those which are not part of any class and which are in the global namespace). For everything else, the brace is on the end of the first line. Others just use the brace at the end of the line for all functions (and everything else).

Of course, those are not the only two bracing styles.
Quote:
Originally Posted by JMJ_coder View Post
And the last question I have right now is how do you write your function return?

Code:
return expression;

return (expression);
Again, both are semantically equivalent. I don’t use parentheses, since their use creates clutter and makes it look like return is a function (which it isn’t). Often, you have a similar dilemma with sizeof. In that case, sometimes the different forms are not semantically equivalent (since sizeof is an operator).
 
Old 10-07-2007, 08:38 AM   #6
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Original Poster
Rep: Reputation: 30
Hello,

Quote:
Originally Posted by Dan04 View Post
The C++ standard requires "int main()".
Quote:
Originally Posted by 95se View Post
Always put an "int" before main, and return and integer, some compilers expect it. It's also good form, so someone using your program through a script can check to see its exit status.
Quote:
Originally Posted by osor View Post
This has been discussed countless times all over the web. Basically, in C++, the only second form is acceptable (the others are WRONG!). In modern C, both the first and second forms are acceptable (since a missing return type will default to int), but the second is preferred. The third may have been acceptable a long time ago in a dialect of C where a missing return type defaulted to void. To be portable, use only the second form when writing C or C++.

An additional note is the arguments. Both C and C++ assure that a conforming implementation will accept
Code:
int main ()
as well as
Code:
int main (int argc, char *argv[])
(or their equivalents). Any other arguments may be accepted, depending on the implementation. For example, on most Unix systems, you can use
Code:
int main (int argc, char *argv[], char *envp[])
Another note about C and C++ arguments. When you leave the arguments empty in C++, it means the function takes no arguments. When you leave the arguments empty in C, it means the number of arguments is unspecified. So to tell a C compiler that the function takes no arguments, you put void in the parentheses. Sometimes, with code designed to work in either C or C++, you’ll see this practice.
Wow! Really!! In my class we have been taught to use only main (). That is how all of the professor's programs are written and that is how he has instructed us to write our programs. I don't know what he would say if I showed up with a lab beginning int main() and a return 0. Maybe I'll show up with my next lab with that. As a side, the programs compile both on the system at school (Solaris) and on my home computer (Slackware).

I don't recall where I got the void main ()? I thought it was some book or tutorial that did it that way when writing the Hello World! program - but I may have been mistaken.


Also, what do the int argc, char *argv[], char *envp[] mean?

Last edited by JMJ_coder; 10-07-2007 at 08:50 AM.
 
Old 10-07-2007, 08:48 AM   #7
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Original Poster
Rep: Reputation: 30
Hello,

Quote:
Originally Posted by Dan04 View Post
For your other questions, the answer is "whatever the style guide at your workplace/school says". (My current employer requires vertically-aligned braces and parentheses around return values. My college comp sci courses had preferred them the other way. I learned to adapt.)

If you don't have an official style guide, then just use your personal preference. If you haven't decided yet, just flip a coin: Heads for K&R brace style and tails for vertically-aligned ones.
Yeah, in my current programming course the professor has a set of requirements on programming style - several of which I don't like (i.e., a space between each and every element, every element of an iostream on a seperate line)

Code:
cout << "This is not the way"
     << '\n'
     << "That I would choose"
     << '\n'
     << "To style my programs!"
     << "\n\n"
     << "Where MAXCHARS is some"
     << "'\n'
     << "int or float variable"
     << '\n'
     << "already declared and assigned"
     << "\n\n"
     << "The maximum characters is: "
     << setw(5)
     << int(MAXCHARS)
     << '\n';
My hope is to start my own company, so I won't have any employer enforced guidelines.
 
Old 10-07-2007, 08:53 AM   #8
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Original Poster
Rep: Reputation: 30
Hello,

Quote:
Originally Posted by osor View Post
I think you’ll find that the answer depends on whether the programmer is more used to C or C++ programming. For C programming, many people (including myself) prefer the K&C bracing style (used in the linux kernel), where the opening brace is on the same line for control statements (e.g., for, switch, if, etc.), but on a separate line for functions. The oft-cited reasoning for this apparent inconsistency is that in C, functions are always outside everything else and cannot be nested (i.e., if you use that style, you can guarantee that the opening brace will be in column 1, and you can guarantee that the function name will begin in column 1 on the line above). All other control statements are always inside either a function or another control statement, so it does not apply to them.

Of course, in C++, functions are not always outside everything else. They can be inside classes or namespaces. Additionally, classes and namespaces may be nested or be local to other functions. Therefore, the idea of putting the opening brace on column 1 of the next line is not always helpful. So for C++, some people put the opening brace on the next line only for “outside” functions (those which are not part of any class and which are in the global namespace). For everything else, the brace is on the end of the first line. Others just use the brace at the end of the line for all functions (and everything else).

Of course, those are not the only two bracing styles.
WOW!! Thanks for the links. And those links have other links that lead to a lot of useful information!!! THANKS!
 
Old 10-07-2007, 12:11 PM   #9
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by JMJ_coder View Post
Also, what do the int argc, char *argv[], char *envp[] mean?
argc is the argument count, argv is the argument vector (a pointer to strings which represent the arguments, with argv[0] representing the name under which the executable was invoked), and envp is a string vector holding the environment variables. The functionality of envp is redundant to using char **environ directly. Of course, the preferred method of using environment variables is through the function getenv().

You can read more about how these arguments should work and the historical rationale behind them in the POSIX specification.
 
Old 10-07-2007, 12:17 PM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
There are so many variations of coding style, it's a good idea to be a little flexible. For a given project it is a good idea to formally define a style and then value consistency highly.
 
  


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
Questions on C++ style JMJ_coder Programming 21 09-18-2007 01:19 PM
How to know if I am using mbox-style or maildir-style? Niceman2005 Linux - General 1 09-23-2005 12:08 PM
what's up with the cartoony style :D darkleaf General 12 01-03-2005 05:27 AM
VIM-style wrapping to OpenOffice style schmmd Linux - Software 1 12-21-2004 06:50 PM
how to change mandrake style menu to kde style menu msalimane Mandriva 1 12-07-2004 01:16 PM

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

All times are GMT -5. The time now is 02:55 PM.

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