C Tab Sizes

Status
Not open for further replies.

Suraj143

Active Member
Can somebody tell me whats the TAB size you all using when doing a code in C?

Also I have a problem where to place the braces is it on the same line or on the next line after a function or a loop?

I know the compiler will detect automatically but I need a correct writing technic.
 
Last edited:
I always use 4 spaces as I find that to be neater.

As for braces, there is no right way, but I prefer them on the same line. I.E.
Code:
    [COLOR="green"]if((IntCount==100)&&(CountFlag==0)){[/COLOR]
        IntCount=0;
        Seconds++;
        [COLOR="blue"]if(Seconds==(Interval*15)){[/COLOR]
            BeepCount=4;
            Seconds=0;
        [COLOR="blue"]}[/COLOR]
    [COLOR="green"]}[/COLOR]
Doing it this way means I can see more code on screen at the same time. However, I do put the closing brace on its own line so I can easily see where the code block ends.

Mike.
 
And i prefer this way... to each his own.

Code:
   if ($_ =~/NET,Network/)
      {
         @adapter_defs = split(/,/); 
         $numAdapters  = (($#adapter_defs -2) / 2);
         $index=2;
      }
 
I don't know of any rule, but I do it like gabeNC does, but I'm not certain why he tabs before and after the braces. I've never seen that before. Based on the code I've seen, I would say at least 70% or more coders do what Pommie does and puts the opening brace after the opening loop or conditional statement.

I've been using 2 spaces lately.

Code:
  if( ( IntCount==100) && ( CountFlag==0))
  {
    IntCount = 0;
    Seconds++;
    if( Seconds == ( Interval * 15))
    {
      BeepCount = 4;
      Seconds = 0;
    }
  }
 
Last edited:
There are religious wars about tab sizes. Just use what you feel comfortable with, and if you work with people then you need to agree a standard to be used across the team.
Interestingly in MikroC I use 2 spaces, and in Visual Studio I use TABs with the tab width set to 4 characters. When using browsers without autoindent I also use 2 spaces.

Location of braces changes, personally I prefer
Code:
if (a==b)
{
  printf("Hello");
}
(the old argument about wasted screen space never convinced me, although I could see the point when we were all on 80x25, and I've not heard it recently with vast super high resolution LCD panels that we all have these days) but this is also quite common
Code:
if (a==b) {
  printf("Hello");
}
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…