# Advice for code indentation style

Thread Starter

#### Pushkar1

Joined Apr 5, 2021
40
Hello guys!

I hope code indenting would be very important for professional programmer. I need some advice from experts on how to indent code properly . I want to indent C code properly so that someone can understand easily.

There are different style, When writing program which style should be followed

This style puts the brace associated with a control statement on the next line,
C:
while (x == y)
{
Dosomething();
Dosomethingelse();
}
This style puts the opening brace at the end of the line
C:
while (x == y) {
Dosomething();
Dosomethingelse();
}
Which style do you follow @MrChips ?

#### ericgibbs

Joined Jan 29, 2010
12,974
hi P1,
I prefer to use this, by considering it as a 'then' command.E also keeps the on screen listing shorter.
while (x == y) {

#### MrChips

Joined Oct 2, 2009
23,570
I prefer the first style. It keeps the braces paired.
When entering code I always type both opening and closing braces as a pair before I type the code in between the braces.

Thread Starter

#### Pushkar1

Joined Apr 5, 2021
40
Control statement style

C:
#define   X    5

int main (void)
{
if (X)
{
printf(" Yes ");
}
else
{
printf(" No ");
}
return 0;
}
I prefer the below style
C:
#define   X    5

int main (void)
{
if ( X == 5)
{
printf(" Yes ");
}
else
{
printf(" No ");
}
return 0;
}

Last edited:

#### Yaakov

Joined Jan 27, 2019
3,190
Different programming communities use different conventions from cases like Python where whitespace is significant to perl where it could be written nearly any way but isn't among perl programmers.

Sometimes there is no convention to fall back on and so choosing something functional and readable for you but where the is a community convention, sticking to it even if you don't like it is really important.

As far as choices, one thing I think is best is keeping tabs to 2 spaces to keep the code from shifting too far.

#### click_here

Joined Sep 22, 2020
217
You should always follow your companies style or the style of existing code on the project.

If you don't code professionally, or you are a solo trader, you need to choose one based on what you usually see.

For example, we do a lot of C# at work which is uses "Allman style", so we use the same for our C code. It is really good for commenting out lines (for debugging) and conditional compilation.
Code:
// from wikipedia
// while (x == y)
{
something();
somethingelse();
}

/*** OR ***/

int c;
#ifdef HAS_GETCH
while ((c = getch()) != EOF)
#else
while ((c = getchar()) != EOF)
#endif
{
do_something(c);
}
"K&R style" (aka Egyptian brackets) is the other popular one, you see it (or variants of it) everywhere (like linux and Arduino sketches) - It reduces the amount of lines used

Code:
// from wikipedia
void checknegative(x) {
if (x < 0) {
puts("Negative");
} else {
nonnegative(x);
}
}
But in the end it's just what is more readable to you and the people who will read your code - Long story short: choose a style and be consistant

#### BobaMosfet

Joined Jul 1, 2009
1,780
Hello guys!

I hope code indenting would be very important for professional programmer. I need some advice from experts on how to indent code properly . I want to indent C code properly so that someone can understand easily.

There are different style, When writing program which style should be followed

This style puts the brace associated with a control statement on the next line,
C:
while (x == y)
{
Dosomething();
Dosomethingelse();
}
This style puts the opening brace at the end of the line
C:
while (x == y) {
Dosomething();
Dosomethingelse();
}
Which style do you follow @MrChips ?
I use this style (this is original, standard 'C' style):

Code:
while(x==6)
{
dosomething();
dosomethingelse();
};
The reason I do this, is because the braces line up vertically, and in a listing across several printed pages, it is easier to find the end of a statement grouping.

The other style you mentioned, came later- this was seen a lot by Pascal programmers, and then began wider adoption. I think it looks sloppier, but that really is just my opinion- compiler doesn't care.

People think somehow they save space vertically but not putting braces on their own lines, but this is ridiculous. The compiler doesn't care which way you do it. I prefer the method I use for legibility, as stated above. But then again, I tend to comment copiously and have comment blocks above every function, and at the head of every file, detailing the logic of what the file does, replete with tables, character-drawings, etc. This go into a printed folder as well, along with flow-charts for any complex logic or processes.

Thread Starter

#### Pushkar1

Joined Apr 5, 2021
40
I apricate any advice to improve code indenting style

I write indent code like this.

C:
/*
* Program:   Find largest number in List
* Author:    Pushkar
* Created on May 26, 2021, 6:31 AM
*/

/*Include the contents of the standard header stdio.h */
#include<stdio.h>

#define     Size          5              // Maixmum size of numbers in List

int main (void)  //Strat main
{
int I = 0;

int List[Size] = {5, 4, 9, 2, 3};   // Given numbers in list

int Largest = List[0];              // Assume first number is largest number in list

// Loop strat from 0 to 5
for ( I = 1; I < Size; I++ )
{
if ( Largest < List[I] )        // Is largest number smaller than list number ?
{
Largest = List[I];          // Yes -> Update largest number with list number
}
}// Exit Loop

// Show largest number in list
printf (" Largest Numer = %d", Largest );

return 0;
} // End Main

Last edited:

#### andrewmm

Joined Feb 25, 2011
1,497
Im old, so excuse me

my things on code style

a) don't use tabs, use white spaces and a fixed point font,
b) Comments, dont put "this is an adder" , put why you want an adder .
c) pick a style and stick to it, till company you work for has a different standard, then copy it,

#### MrChips

Joined Oct 2, 2009
23,570
I notice that you are placing comments on the closing brace.
I put the same comment on both opening and closing brace.
When your code structure gets huge and you start editing and modifying your code you will quickly lose track of which braces are paired.

#### click_here

Joined Sep 22, 2020
217
The style you are doing is "Allman Style", that's all good

Those comments are not good - As Andrew said, never say things like "//loop between foo and fah", the code already does that. Never comment "//end loop" "//Start main", the code/indentation tells you that

If it is starting to get too long, you should be thinking about using functions

#### Young2

Joined Dec 7, 2020
35
I like the first style, I think it looks more comfortable visually.

#### MrChips

Joined Oct 2, 2009
23,570
I do it this way:
C:
while(x==6)
{
dosomething();
dosomethingelse();
};

#### click_here

Joined Sep 22, 2020
217
I do it this way:
C:
while(x==6)
{
dosomething();
dosomethingelse();
};
Did you accidently put a ";" after the control loop? Or is that a style thing that you like to do?

Thread Starter

#### Pushkar1

Joined Apr 5, 2021
40
Did you accidently put a ";" after the control loop? Or is that a style thing that you like to do?
MrChip is talking about style

Both are same things

C:
While (condition);
C:
While(condition)
{
}

#### click_here

Joined Sep 22, 2020
217
MrChip is talking about style

Both are same things

C:
While (condition);
C:
While(condition)
{
}
Did you miss the ; at the end of the { } in his example?

That is usually a typo

#### MrChips

Joined Oct 2, 2009
23,570
I had copied the code that TS posted.
The semicolon is not required.

#### BobTPH

Joined Jun 5, 2013
3,333
I prefer this style:

Code:
while x = 6 loop
do_something;
end loop;

and

if x < 5 or x > 10 then
do_one_thing;
else
do_another_thing;
end if;
Bob

#### click_here

Joined Sep 22, 2020
217
I prefer this style:

Code:
while x = 6 loop
do_something;
end loop;

and

if x < 5 or x > 10 then
do_one_thing;
else
do_another_thing;
end if;
Bob
What style in C is that

Similar threads