Thread: how to properly indent and format a source code?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87

    how to properly indent and format a source code?

    Hello.

    I want to learn how to indent and format properly my source codes. I have found this online:

    https://en.wikipedia.org/wiki/Indent_style
    GNU Coding Standards: Formatting

    Here is a source code that I wrote where I was criticized that it is not correctly formated:

    why this code is not working? i wanted to multiply a matrix by itself

    Code:
    #include <stdio.h>
      
    int main()
    {
        int i,j;
        int array1[3][3]= {{0,0,0},{0,12,36},{0,32,56}} ;
        int sqr[3][3];
     
        /* multiplication part */
        for(i=0;i<3;i++)
        {
               for(j=0;j<3;j++){
                                  sqr[i][j]=array1[i][0]*array1[0][j]+array1[i][1]*array1[1][j]+array1[i][2]*array1[2][j];
                               }
        }
     
     
     
        /* here is the printing part */
        for(i=0;i<3;i++)
        {
            for(j=0;j<3;j++){
                            printf("%d\t",sqr[i][j]);
                            }
            printf("\n");
        }
        return 0;
    }
    1) What does it mean to properly indent a code? >> I think that this is pretty clearly explained in the wikipedia article and I think that I already do that right, even in the example

    2) What does it mean to properly indent a code? >> Does it mean two empty lines between code? What more?

    Thank you.

    A corrected version:

    Code:
    #include <stdio.h>
      
    int main()
    {
        int i,j;
        int array1[3][3]= {{0,0,0},{0,12,36},{0,32,56}} ;
        int sqr[3][3];
     
    
        /* multiplication part */
        for(i=0;i<3;i++)
        {
            for(j=0;j<3;j++){
                               sqr[i][j]=array1[i][0]*array1[0][j]+array1[i][1]*array1[1][j]+array1[i][2]*array1[2][j];
                            }
        }
    
    
        /* here is the printing part */
        for(i=0;i<3;i++)
        {
            for(j=0;j<3;j++){
                            printf("%d\t",sqr[i][j]);
                            }
            printf("\n");
        }
    
    
        return 0;
    }
    Last edited by nerio; 02-05-2016 at 09:46 AM.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Look at the indent style wiki page you posted.

    Tell me which indent style your code matches up with..
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    I think that the one I used is the Allman style.

    https://en.wikipedia.org/wiki/Indent_style#Allman_style

    I see it now. For the code more on the right I used the Whitesmiths_style..

    https://en.wikipedia.org/wiki/Indent...tesmiths_style

    EDIT: Here it is all just in Allman indent style:

    Code:
    #include <stdio.h>
       
    int main()
    {
        int i,j;
        int array1[3][3]={{0,0,0},{0,12,36},{0,32,56}};
        int sqr[3][3];
      
     
        /* multiplication part */
        for(i=0;i<3;i++)
        {
            for(j=0;j<3;j++)
            {
                sqr[i][j]=array1[i][0]*array1[0][j]+array1[i][1]*array1[1][j]+array1[i][2]*array1[2][j];
            }
        }
     
     
        /* here is the printing part */
        for(i=0;i<3;i++)
        {
            for(j=0;j<3;j++)
            {
                printf("%d\t",sqr[i][j]);
            }
            printf("\n");
        }
     
     
        return 0;
    }
    Is it better to stick to one indent style? I suppose yes.

    How about the formatting?
    Last edited by nerio; 02-05-2016 at 10:02 AM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    Is it better to stick to one indent style?
    It is better to be consistent. Your indentation in your first post is very inconsistent, your inner loop indentation differs from the outer loop indentation.

    Jim

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Your previous post has no style at all.
    Sometimes the starting brace is on a line by itself, sometimes not.
    Sometimes you indent by 4, sometimes by an arbitrary number of spaces.
    It's idiotic.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Post number three of this thread contains a corrected version.

    So the formatting is about number of spaces between a code starts on a line and between spaces between lines of different type of code?
    Last edited by nerio; 02-05-2016 at 10:14 AM.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    So the formatting is about number of spaces between a code starts on a line and between spaces between lines of different type of code?
    Partially yes. There are also differences as to where the braces are located, use of tabs versus spaces, and many other aspects

    As I said the biggest part of "your" indentation style should be consistency. Don't use spaces in some places and tabs in another, don't use 3 spaces in some locations and some other number of spaces in another, don't indent braces in one instance but not another, etc. Also how you use blank lines and whitespace to separate items should be consistent. One of my pet peeves is no/inconsistent spacing within formulas, and excessively long lines. For example:
    Code:
    /////// POOR /////////
            for(j=0;j<3;j++)
            {
                sqr[i][j]=array1[i][0]*array1[0][j]+array1[i][1]*array1[1][j]+array1[i][2]*array1[2][j];
            }
    /////// Better IMO.
            for(j = 0; j < 3; j++)
            {
                sqr[i][j] = array1[i][0] * array1[0][j] + array1[i][1]
                                                  * array1[1][j] + array1[i][2] 
                                                  * array1[2][j];
            }
    And don't forget to use parentheses to insure that your doing the operations in the correct order, instead of relying only on operator precedence.


    Jim

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    You could also make your array initialization look much nicer by using a bit of extra whitespace.

    This:

    Code:
    int array1[3][3]={{0,0,0},{0,12,36},{0,32,56}};
    ... could look something like this:

    Code:
    int array1[3][3] = { { 0,  0,  0 },
                         { 0, 12, 36 },
                         { 0, 32, 56 } };
    Since the position of the numbers on the screen now correlate to their positions within the array, visually finding the value at a particular spot is much easier. This also makes updating the initialization (if you change the size of either/both dimensions) easier.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Thank you all.

    Here is the current version:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int i,j;
        int array1[3][3] = { { 0,  0,  0 },
                             { 0, 12, 36 },
                             { 0, 32, 56 } };
        int sqr[3][3];
    
    
        /* multiplication part */
        for(i=0;i<3;i++)
        {
            for(j=0;j<3;j++)
            {
                sqr[i][j]=array1[i][0]*array1[0][j]
                         +array1[i][1]*array1[1][j]
                         +array1[i][2]*array1[2][j];
            }
        }
    
    
        /* here is the printing part */
        for(i=0;i<3;i++)
        {
            for(j=0;j<3;j++)
            {
                printf("%d\t",sqr[i][j]);
            }
            printf("\n");
        }
    
    
        return 0;
    }
    How to recognize a case for using spaces and for using tabs? In my text editor one tab equals eight spaces.

    I have a better experience of using spaces, because with tabs in different editors the text is just somewhere else (more on the right or left). Maybe that in different editors one tab equals to different number of spaces.
    Last edited by nerio; 02-05-2016 at 11:06 AM.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    How to recognize a case for using spaces and for using tabs? In my text editor one tab equals eight spaces.
    Most text editors can be setup to use a different number of spaces, and to place these spaces in the file instead of the tab character. A lot of people recommend that you configure your editors to change tabs to spaces, to help preserve the formatting no matter what editor you use.

    Don't forget about things like:
    Code:
    // Instead of:
        for(i=0;i<3;i++)
    // Use whitespace to highlight the operators.
        for(i = 0; i < 3; i++)
    After all you wouldn't write a letter without using any whitespace, would you?


    Jim

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,777
    Quote Originally Posted by nerio
    How to recognize a case for using spaces and for using tabs? In my text editor one tab equals eight spaces.
    I prefer to just use spaces as I think it is easier to be consistent that way. If you do want to use tab characters, then make sure that they are used for the basic indentation only, not for pretty alignment of text.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code not working properly.
    By iiwii in forum C Programming
    Replies: 16
    Last Post: 06-18-2015, 10:38 PM
  2. Code not functioning properly
    By mgravier in forum C Programming
    Replies: 5
    Last Post: 03-20-2013, 06:16 PM
  3. how to properly call an executable from C code?
    By remy06 in forum C Programming
    Replies: 3
    Last Post: 05-14-2009, 03:48 AM
  4. how to properly comment on c code
    By agentsmith in forum C Programming
    Replies: 15
    Last Post: 01-30-2008, 01:13 PM
  5. Convert source to HTML format
    By bobk544 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-06-2006, 07:40 PM