Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Style guides for C and/or C++

1 view
Skip to first unread message

Vicent Giner-Bosch

unread,
Jan 19, 2010, 3:45:10 AM1/19/10
to
I've been searching at this forum and at the Internet for style
guides for C and/or C++, and I've found these ones:

http://www.psgd.org/paul/docs/cstyle/cstyle.htm
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml

Also, I've read some threads about C style matters. I think there is
really a lot of stuff!

I just would like to ask you for a quick answer to this question:
which style guide or style rules do you recommend me to use when
writing C/C++ code? It would be great if many of you would answer, so
we all could have here a thread with a good summary about this
subject.

Thank you for the feed-back!! :-)


PS: I've also posted this theme at comp.lang.c, so I apologize for
cross-posting.

Alf P. Steinbach

unread,
Jan 19, 2010, 3:52:25 AM1/19/10
to
* Vicent Giner-Bosch:

> I've been searching at this forum and at the Internet for style
> guides for C and/or C++, and I've found these ones:
>
> http://www.psgd.org/paul/docs/cstyle/cstyle.htm
> http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
>
> Also, I've read some threads about C style matters. I think there is
> really a lot of stuff!
>
> I just would like to ask you for a quick answer to this question:
> which style guide or style rules do you recommend me to use when
> writing C/C++ code?

Different ones. C is not C++. C++ is not C.

For C++ consider the Alexandrescu and Sutter guide (it's a book).


> It would be great if many of you would answer, so
> we all could have here a thread with a good summary about this
> subject.
>
> Thank you for the feed-back!! :-)
>
>
> PS: I've also posted this theme at comp.lang.c, so I apologize for
> cross-posting.

You didn't cross-post. You may perhaps have multi-posted.


Cheers & hth.,

- Alf

Timothy Madden

unread,
Jan 19, 2010, 5:43:42 AM1/19/10
to
Vicent Giner-Bosch wrote:
[...]

> I just would like to ask you for a quick answer to this question:
> which style guide or style rules do you recommend me to use when
> writing C/C++ code? It would be great if many of you would answer, so
> we all could have here a thread with a good summary about this
> subject.

I write C++ and I use the following indentation which I care about:

template< typename charT, typename traits=char_traits<charT> >
class charConvertor
{
public:
static
std::basic_string<charT> strMessage;

template<typename ExternalCharType>
static
inline
void Transcode(ExternalCharType const *szMessage)
{
strMessage = szMessage;
}
}
convertor;

int main()
{
int iResult = 0;

if
(
iResult <= 10
&&
iResult >= -2
&&
iResult == 0
)
{
cout << "Right result";
cout << endl;

return iResult;
}
else
::CreateWindow
(
szClassName,
szTitle,
dwStyle,
xPos,
yPos,
cxWidth,
cxHeight,
hwndParent
);

return -1;
}


It looks better with syntax highlighting though ... :)

My tab characters are 8 columns wide, although some editors made it a
habit to use tab stops at 4 columns...

I really do not understand the style some people use for blocks:

int ProcessIndex(int iIndex) {
return iIndex * 3 / 4;
}


It is so ugly and un-intuitive ...

Timothy Madden

Richard McBeef

unread,
Jan 19, 2010, 1:13:54 PM1/19/10
to
Vicent Giner-Bosch wrote:
> I've been searching at this forum and at the Internet for style
> guides for C and/or C++, and I've found these ones:
>
> http://www.psgd.org/paul/docs/cstyle/cstyle.htm
> http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
>
> Also, I've read some threads about C style matters. I think there is
> really a lot of stuff!
>
> I just would like to ask you for a quick answer to this question:
> which style guide or style rules do you recommend me to use when
> writing C/C++ code?
Here is my style "rule" :
In netbeans right mouse click in an open code
window. Select "Format".
Hope That Helps :)

Marcelo Pinto

unread,
Jan 19, 2010, 2:33:48 PM1/19/10
to
On 19 jan, 06:45, Vicent Giner-Bosch <vgi...@gmail.com> wrote:
[snip]

> I just would like to ask you for a quick answer to this question:
> which style guide or style rules do you recommend me to use when
> writing C/C++ code? It would be great if many of you would answer, so
> we all could have here a thread with a good summary about this
> subject.
[snip]

My 2 cents:

- Don't sweat on small stuff (chapter 1 of C++ Coding Standards by
Sutter & Alexandrescu)
- Don't use Hungarian notation
- Use a tool to enforce formatting (such as AStyle or ident)
- Be consistent

HTH,

Marcelo Pinto

Richard

unread,
Jan 19, 2010, 3:25:31 PM1/19/10
to
[Please do not mail me a copy of your followup]

"Alf P. Steinbach" <al...@start.no> spake the secret code
<hj3roe$na7$1...@news.eternal-september.org> thusly:

>For C++ consider the Alexandrescu and Sutter guide (it's a book).

I'll second that.

There's another earlier one by Plum & Saks that I have found useful,
but its out of print and some of its advice is dated. Alexandrescu
and Sutter is a good choice.
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>

Legalize Adulthood! <http://legalizeadulthood.wordpress.com>

Sousuke

unread,
Jan 20, 2010, 3:11:03 PM1/20/10
to
On Jan 19, 3:45 am, Vicent Giner-Bosch <vgi...@gmail.com> wrote:
> I've been searching at this forum and at the Internet for style
> guides  for C and/or C++, and I've found these ones:
>
> http://www.psgd.org/paul/docs/cstyle/cstyle.htmhttp://google-styleguide.googlecode.com/svn/trunk/cppguide.xml

>
> Also, I've read some threads about C style matters. I think there is
> really a lot of stuff!
>
> I just would like to ask you for a quick answer to this question:
> which style guide or style rules do you recommend me to use when
> writing C/C++ code? It would be great if many of you would answer, so
> we all could have here a thread with a good summary about this
> subject.

It's not a good idea to take advice from both C style guides *and* C++
style guides, because they will contradict each other on some matters.
And the reason is that some practices that might be considered good
style in C are actually bad style in C++, because in C++ there are
better alternatives.

To take the two style guides you suggested as an example, here is one
such contradiction: on defining names for constants,
http://www.psgd.org/paul/docs/cstyle/cstyle12.htm says
"The #define feature of the C preprocessor should be used to give
constants meaningful names."
while http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Preprocessor_Macros#Preprocessor_Macros
says
"Instead of using a macro to store a constant, use a const variable."

There are similar arguments for parameterized macros vs. inline
functions, pointers vs. smart pointers, raw arrays vs. array classes
(like std::vector), etc.

Vladimir Jovic

unread,
Jan 26, 2010, 8:05:39 AM1/26/10
to

I see you haven't been bit by this bug-type yet.

I would write this code snippet like this:

Jorgen Grahn

unread,
Feb 6, 2010, 6:26:51 PM2/6/10
to
On Tue, 2010-01-19, Vicent Giner-Bosch wrote:
> I've been searching at this forum and at the Internet for style
> guides for C and/or C++, and I've found these ones:
>
> http://www.psgd.org/paul/docs/cstyle/cstyle.htm
> http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
>
> Also, I've read some threads about C style matters. I think there is
> really a lot of stuff!
>
> I just would like to ask you for a quick answer to this question:
> which style guide or style rules do you recommend me to use when
> writing C/C++ code? It would be great if many of you would answer, so
> we all could have here a thread with a good summary about this
> subject.

You'll get no consensus or summary, that's for sure. Too many
different styles and opinions.

> PS: I've also posted this theme at comp.lang.c, so I apologize for
> cross-posting.

Thanks for mentioning it. Note that C and C++ are different
languages, and you cannot just mindlessly use the same style in both.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Jorgen Grahn

unread,
Feb 6, 2010, 6:42:55 PM2/6/10
to

Since you brought it up, I find *your* style extremely ugly and hard
to read. I've seen nothing like it before.

The style you say is ugly and used by "some people" is very common,
and used by people like Stroustrup. (Maybe not for function
definitions like in your example, but for most other blocks.) If you
have problems reading it, learn to. Otherwise you'll have problems
working with just about any other C++ programmer.

For reference, I would have written your code like this:

int main()
{
const int i = 0;

if(i<=10 && i>=-2 && i==0) {
std::cout << "Right result\n";
return 0;
}

::CreateWindow(name, title, style,
x, y,
width, height,
parent);

return 1;
}

(I don't believe in returning -1 from main().)

Robert Fendt

unread,
Feb 7, 2010, 3:56:57 AM2/7/10
to
And thus spake Jorgen Grahn <grahn...@snipabacken.se>
6 Feb 2010 23:26:51 GMT:

> You'll get no consensus or summary, that's for sure. Too many
> different styles and opinions.

Maybe one should quote from Marshall Cline's (excellent) C++ FAQ here:

http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.1

"27.1 What are some good C++ coding standard?

[...] Obviously anyone who asks this question wants to be trained so they don't run off on their own ignorance, but nonetheless posting a question such as this one to comp.lang.c++ tends to generate more heat than light. [...]"

Which is very much to the point, actually. The question of which style exactly is 'superior' is moot. Just choose one and stick with it. If you are working with other people, just choose one (together!) and then stick with it. When reading code written by other people, I (like most even semi-experienced C++ programmers) usually do not really care about subtle differences in indentation and such, as long as (1) there *is* some level of indentation to make block levels clear, (2) it is readable and (3) *consistent*.

Consistency is key. To that end, source code formatters like astyle and the like can help you. But of course they do not support every formatting style on the planet, just what their authors deemed popular and/or sensible. So using e.g. astyle limits your choices, which can also mean that your code formatting style implicitely gets less 'individual' thus making it easier for others to read. Which can be a positive effect as well.

Regards,
Robert

Krice

unread,
Feb 7, 2010, 7:41:28 AM2/7/10
to
On 19 tammi, 21:33, Marcelo Pinto <mpint...@gmail.com> wrote:
> - Don't use Hungarian notation

HN could be good sometimes. I'm using HN in enums and
sometimes in classes, like G_Screen (G=graphics) to make it
easier to find/list them in class browser.

Jonathan Lee

unread,
Feb 7, 2010, 11:21:50 AM2/7/10
to
On Feb 7, 7:41 am, Krice <pau...@mbnet.fi> wrote:
> HN could be good sometimes. I'm using HN in enums and
> sometimes in classes, like G_Screen (G=graphics) to make it
> easier to find/list them in class browser.

Something I prefer is to put enums in a namespace or
class. So "G_Screen" becomes "Graphics::Screen".

--Jonathan

0 new messages