Proper format/standard of C program?

This is a discussion on Proper format/standard of C program? within the C Programming forums, part of the General Programming Boards category; I've searched all over the internet looking for whats the most preferred format. I know C compilers don't care, but ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    20

    Proper format/standard of C program?

    I've searched all over the internet looking for whats the most preferred format. I know C compilers don't care, but I want to make codes that look nice for others to edit, view over, etc. I guess its better to learn proper format from the beginning. I've learned C briefly about 2 years ago now I want to get back into it again. Please provide some links of known format/styles.

    Here's an example of some of my code. Most of you probably know where this code came from. Notice how I spaced the variable from the equals, and bracket placements.
    Code:
    #include <stdio.h>
    
    /* print Fahrenheit-Celsius table
     * for fahr = 0, 20, ..., 300; floating-point version */
    main()
    {
    	float fahr, celsius;
    	float lower, upper, step;
    
    	lower = 0;	/* lower limit of temperature scale */
    	upper = 300;	/* upper limit */
    	step = 20;	/* step size */
    
    	fahr = lower;
    	while (fahr <= upper) {
    		celsius = (5.0/9.0) * (fahr-32.0);
    		printf("%3.0f %6.2f\n", fahr, celsius);
    		fahr = fahr + step;
    	}
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,555
    There are a number of "coding standards" for C, which really means there is no one standard. Some popular ones, however, are listed here: Indent style - Wikipedia, the free encyclopedia.

    I would say K&R, Allman and BSD KNF are the most popular, probably in that order. The example above is fine in terms of brace placement, spacing around operators, etc. Some people have highly religious views about indentation however. Whether to use tabs vs. spaces, whether you should indent 2, 4 or 8 characters (or some other number). Without sparking a tabs vs. spaces war, I will simply say that I find a 4-character indent most aesthetically pleasing, but 2 or 8 are fine as well.

    The last note, always declare main to return an int, and put in a return statement before finishing main.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    Quote Originally Posted by anduril462 View Post
    There are a number of "coding standards" for C, which really means there is no one standard. Some popular ones, however, are listed here: Indent style - Wikipedia, the free encyclopedia.

    I would say K&R, Allman and BSD KNF are the most popular, probably in that order. The example above is fine in terms of brace placement, spacing around operators, etc. Some people have highly religious views about indentation however. Whether to use tabs vs. spaces, whether you should indent 2, 4 or 8 characters (or some other number). Without sparking a tabs vs. spaces war, I will simply say that I find a 4-character indent most aesthetically pleasing, but 2 or 8 are fine as well.

    The last note, always declare main to return an int, and put in a return statement before finishing main.
    Thank you. I was looking for quite some time, I guess it never occurred to me to look up indent style.

  4. #4
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39

    Lightbulb

    The GNU indent tool works very well for the small C programs I have used it on. There is a windows version, too. It makes the indenting style moot, in my opinion. Just code in whatever style suits best. I prefer indent --line-length 90 -nut -brf -linux -i4
    Code:
    #include <stdio.h>
    /* Print command line arguments and exit. */
    int main (int c, char **v){
        while (c--){
            printf ("Argument %i is \"%s\"\n",c,v[c]);
        }
        return 0;
    }
    I misused flex to write a preprocessor to add missing or fix extra brackets and punctuation. I have no idea how to use flex and bison correctly, but that might be a benefit because my preprocessor does not understand C tokens (It just looks at indenting levels) so it works with other languages and scripts. To test the thing, I usually abuse it and code in a style like this, which is converted to the above style and compiled on the fly. With the tcc compiler, it also serves as sort of a scripting language that compiles C code and runs it in RAM. Though not as fast as code optimized with gcc, it is a lot faster than bash scripting :P:
    Code:
    #!/usr/local/bin/anch -run
    /* This will run as a bash script if TCC and Anchor is installed */
    
    #include <stdio.h>
    /* Print command line arguments and exit. */
    int main  int c, char **v
        while  c--
            printf  "Argument %i is \"%s\"\n",c,v[c]
        return 0
    Last edited by hellork; 11-17-2010 at 12:29 AM. Reason: yacc, bison, whatever

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework Help, simple program, unsure newbie :(
    By Aslan14 in forum C Programming
    Replies: 13
    Last Post: 11-14-2010, 04:07 PM
  2. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  3. Currency Conversion Program
    By kgraw21 in forum C Programming
    Replies: 5
    Last Post: 04-19-2002, 08:39 AM
  4. redirection program help needed??
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2002, 05:50 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21