[Courses] C Programming For Absolute Beginners, Lesson 5: All About Functions, part 1

Jacinta Richardson jarich at perltraining.com.au
Tue Mar 13 06:22:36 UTC 2012


On 13/03/12 15:28, Carla Schroder wrote:
> int timex (int a, int b)
> {
>    return a * b;
> }

Having gotten on my high horse already about indentation, I thought I'd make a 
note about this too.  As a general most programming communities have decided 
that 4 space indentation is best.  Some people still use tabs (8 spaces if you 
expand them).  The main reason for this is that 1 and 2 space indentation can be 
very hard to see once your code starts getting complicated. (I've already 
demonstrated that C doesn't actually care that much about whitespace).

If we look at Leslie's code with 2 character indentation:

int main()
{
   int a = 0;
   int total = 0;

   puts ("\n\n"
     "       *******************************************\n"
     "       *                                         *\n"
     "       *         Two-column Table                *\n"
     "       *             by Leslie                   *\n"
     "       *                                         *\n"
     "       *                                         *\n"
     "       *******************************************\n\n");


Then just with that little bit of space to show that the string is inside puts, 
she's already at another full indentation level. I strongly recommend against 
using such thin indentation.

If you're using vim, you might find it useful to add the following to your 
~/.vimrc file

    :syn on
    :set sm
    :set ts=8
    :set sts=4
    :set shiftwidth=4
    :set backspace=indent,eol,start
    :set ai
    :set et
    :set shiftround
    :set mouse=a

 From the top:

syn on		turn syntax highlighting on.

sm		(showmatching) highlight the matching parentheses, brace or bracket
		in your code when your cursor is on the other.

ts=8		(tabstop) specifies a tab (in the document) should be seen as 8 characters wide

sts=4		(softtabstop) specifies that when we press tab, we want to only indent by 4 spaces

shiftwidth=4	when we manually indent or outdent code using<  or>, it should be moved by 4 spaces

ai		(autoindent) inserts sufficient whitespace (tabs or newlines) to start your next line
		in the same column as your previous line.

backspace=... 	we can use backspace in insert mode, to delete past a newline, or back one indentation level.

et		(expandtab)  When we press the tab key, expand that to spaces not tabs. Likewise for
		anything else that might add tabs.

shiftround	indent/outdent to nearest (soft) tab stops

mouse=a		"a" = all, and there are other options.  This will allow you to use your mouse to move your
		cursor, select text, scroll through your document etc.


> Perhaps some our ace gurus could discuss the best places to put your function
> prototypes. This is not a big deal in our tiny example programs, but in large
> complex programs what are some best practices?

Now, back on topic...

I was taught, and most code I've seen follows the idea that if you're not 
putting your function prototypes in a header file, then they belong between the 
#defines/#includes and the start of your main function.

So, to use an example from earlier:

    //    using flush_i() to clear input stream buffer

    #include<stdio.h>

    /* our prototype */

    void flush_i(void);

    /* Now our main function */

    int main() {

            int a, b, c;
            printf("Please enter any number up to three digits:" );
            scanf("%d", &a);

            flush_i();

            printf("You entered %d. Now enter another number up to three "
                     "digits:\n", a );
            scanf("%d", &b);

            c = a + b;
            printf("%d + %d = %d\n", a, b, c);

            return 0;
    }

    void flush_i(void){
            int c;
            while (c != '\n' && c != EOF){
                    c = getchar();
           }
    }

All the best,

     Jacinta


More information about the Courses mailing list