A beginner's Q about style in vanilla C

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
User avatar
cobaka
Posts: 521
Joined: Thu Jul 16, 2020 6:04 am
Location: Central Coast, NSW - au
Has thanked: 87 times
Been thanked: 49 times

A beginner's Q about style in vanilla C

Post by cobaka »

A hearty woof to all!

This question is about style, from a beginner.
(1) In C, a block of function "calls" are denoted using curly braces {block}

(2) Good style suggests that the opening brace is seen on a line, by itself.
(3) The function block inside the braces is indented.

Code: Select all

    {
     function call;
     function call;
     etc;
     }
     

OR

Code: Select all

    {  # or it can be indented one (or more tabs) beyond the curly bracket, thus:
    		function call;
    		function call;
    		function call ;  (etc)
    }

By now you can see my question is a simple beginner's question.
I understand that the compiler 'dumps' white space so the question is about style alone.

Thanks, in anticipation

собака

собака --> это Русский --> an old dog
"so-baka" (not "co", as in coast or crib).

User avatar
Jafadmin
Posts: 377
Joined: Tue Aug 04, 2020 4:51 pm
Has thanked: 68 times
Been thanked: 84 times

Re: A beginner's Q about style in vanilla C

Post by Jafadmin »

The following are common styles:

Code: Select all


int main(void){
	DoSomeStuff(); // comment your code
	DoMoreStuff(var1, var2);
}

int main (void)
{
	DoSomeStuff(); // still commenting because I'm cool like that
	DoMoreStuff(var1, var2);
}

geo_c
Posts: 2509
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 1804 times
Been thanked: 710 times

Re: A beginner's Q about style in vanilla C

Post by geo_c »

I don't do real code, maybe some basic scripts, but I do lilypond files which are music scoring files I think based on lua script. They end up with a lot brackets within brackets, and these << >> inside these << >>

So I prefer method two when I have to debug the script. A section might like the following and everything is lined up according to how it's grouped:

Code: Select all

{
  \unfoldRepeats 
   {
    \context ChordNames = "PartPOneVoiceOneChords" { \PartPOneVoiceOneChords}
    \new Staff
     <<
      \set Staff.instrumentName = "Piano"

      \context Staff 
      <<
        \mergeDifferentlyDottedOn\mergeDifferentlyHeadedOn
        \context Voice = "PartPOneVoiceOne" {  \voiceOne \PartPOneVoiceOne }
        \context Voice = "PartPOneVoiceTwo" {  \voiceTwo \PartPOneVoiceTwo }
      >>
     >>
   }
  \midi {\tempo 4 = 100 }
}

geo_c
Old School Hipster, and Such

User avatar
6502coder
Posts: 89
Joined: Mon Jul 13, 2020 6:21 pm
Location: Western US
Has thanked: 3 times
Been thanked: 20 times

Re: A beginner's Q about style in vanilla C

Post by 6502coder »

As you say, this is purely a matter of style.

Kernighan and Ritchie used the style with the opening brace at the end of the line where the block starts and the closing brace aligned with the start of that same line. Since K&R wrote THE definitive book on C, this became a standard.

Then, later a fellow named Pike (I think) popularized the style with the opening brace on a separate line.

The main argument for the Pike style is that the opening and closing braces line up.

The main argument for the K&R style is that it is more compact and visually less cluttered: note that in the Pike style, there are THREE elements that have to align: a keyword and then the two braces, whereas the K&R style only needs TWO elements, the keyword and the closing brace.

Also, when you are rapidly looking through a printout, it is often hard to distinguish an opening brace from a closing brace (especially with my old eyes). In the K&R style, you know that any brace at the current indent level is almost certainly a closing brace, whereas in the Pike style it might be either an opening brace or a closing brace.

As you can guess, I prefer K&R. But then I learned C back in the late 70's so of course I take the K&R book as gospel.

Post Reply

Return to “Programming”