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

Akkana Peck akkana at shallowsky.com
Tue Mar 13 17:56:03 UTC 2012


Jacinta Richardson writes:
> 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).

There are also people who set their editors to display tab stops as
4 spaces (or even 3) instead of 8, then use tabs for indentation.

Please don't do this yourself. It makes it really hard for anyone
else to collaborate with you.  If you set your tab stops to a
non-standard width (anything other than 8), anyone trying to read
your code will be completely confused, because some lines will be
indented inconsistently. Also, if you print your code on a printer
it will usually print out with 8 space tab stops, so your
indentation will be all messed up.

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

In case there are any emacs users about, there are all sorts of
useful things you can do in emacs regarding C indentation.
First, some equivalents to Jacinta's vim suggestions:

(setq-default indent-tabs-mode nil)
(setq tabify nil)
(set-variable "default-tab-width" 8)
(setq c-indent-level 4)

That last one is because emacs knows all about C syntax, and will
automatically indent lines by the appropriate amount. I tell it to
do this automatically whenever I hit return:

(global-set-key "\C-m" 'newline-and-indent)

I can re-indent a single line by going to the beginning of the line
and hitting TAB, or re-indent a whole block with C-c TAB (bound to
the "indent-region" command).

This does more than just save you from having to hit
space-space-space-spce a lot: it also tells you when you've made a
typo like forgetting a semicolon or brace, because you hit return
and find the cursor somewhere other than where you expected.

There are a bunch of other variables you can set in c-mode depending
on which C style you prefer, e.g.

(setq c-continued-statement-offset 4)
(setq c-brace-offset -4)
(setq c-brace-imaginary-offset 0)
(setq c-argdecl-indent 0)
(setq c-label-offset -2)

... and lots more.

And sometimes you have to change your settings when you're working
on other people's code. For instance, the Linux kernel uses tabs,
not spaces (with standard 8-space tab stops), so when I was trying
to learn how to hack kernel code, I would run a function:

(defun linux-c-mode ()
    "C mode with adjusted defaults for use with the Linux kernel."
   (interactive)
   (c-mode)
   (c-set-style "K&R")
   (setq c-basic-offset 8)
   (setq indent-tabs-mode t)
)

Finally, you can put something called a "local variables line" as
the first line of your C file, and anyone who edits it with emacs
will use the indentation settings you specify, even if they normally
use different settings.

/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

	...Akkana


More information about the Courses mailing list