All C-style code, C, C++, JavaScript, etc. must conform to industry standard
formatting guidelines. You may select from either K&R or Allman style,
while also making sure to conform to universal standards (listed first).
Applies to all code regardless of the specific style you choose.
if (x > 0) { is correct, while if(x>0){ is not.
The two specific styles below are both acceptable and are the most common in industry. You may choose either one for your code. The most important thing is to be consistent. Do not mix styles within the same project or file.
This is the most common style in C and C++ code, and is also used in JavaScript. It is named after Brian Kernighan and Dennis Ritchie, who popularized it in their seminal book on C programming. In this style, the opening brace of a block is placed on the same line as the control statement, and the closing brace is aligned with the control statement.
if (condition) {
do_something();
} else {
do_other();
}
This style is named after Eric Allman, who used it in the original Sendmail code. Also known as "BSD style," it is commonly used in Windows programming and some open-source projects. In this style, the opening brace of a block is placed on a new line, and the closing brace is aligned with the control statement.
if (condition)
{
do_something();
} else
{
do_other();
}