Aleksandar Makragić
Aleksandar Makragić

Reputation: 1997

Indenting C code, Unix

I searched documentation for indent, but I gave up eventually, I want to indent code like this:

int main(int argc, char **argv){
    some code;
}

I know indent -kr gives you braces like this, but -kr style also includes

int
main(int argc, char **argv){
    some code;
}

and this int in line before main gives me creeps.

Can anyone please tell me option for this?

Upvotes: 2

Views: 989

Answers (2)

David C. Rankin
David C. Rankin

Reputation: 84521

You are likely looking for the -npsl option. The -psl (--procnames-start-lines) option causes the type of a procedure being defined to be placed on the line before the name of the procedure. Thus if you specify that option, all function declarations will be changed from:

int main(int argc, char **argv){
    some code;
}

to

int
main(int argc, char **argv){
    some code;
}

You can check whether you are including -psl in the common type -kr and remove it, or if not included, you can specify -npsl (--dont-break-procedure-type) and the type will not be placed on a separate line.

There are trade-off with all options. I like braces on the same line as int main() {, but for function definitions I like braces on the next line. e.g.:

type function
{
    ...
}

So if you have mutually exclusive preferences such as that, you simply choose the one you want a majority of the code to incorporate and tweak the rest. You might give the following invocation a try:

indent -i 4 -lp -ts 8 -lp -lps -br -brs -blf -ce -cdw -pcs -bs -nbc -npsl -saf -sai -saw -nut

It is somewhat of a balanced indent scheme.

Upvotes: 0

Thomas Dickey
Thomas Dickey

Reputation: 54465

The particular options that you are interested in are

-npsl
--dont-break-procedure-type
Put the type of a procedure on the same line as its name.

-brf
--braces-on-func-def-line
Put braces on function definition line.

As suggested, the GNU indent manual describes the various options.

Here is a quick script to illustrate the effect of those options on the basic predefined styles:

#!/bin/sh
for opt in gnu linux orig kr
do
    echo "** $opt"
    indent -st -$opt -npsl -brf hello.c
done

and the input file:

#include <stdio.h>

int main(int argc, char **argv) { int n; for (n = 0; n < argc; ++n) printf("arg%d=%s\n", n, argv[n]); return 0; }

and corresponding output:

** gnu
#include <stdio.h>

int main(int argc, char **argv) {
    int n;
    for (n = 0; n < argc; ++n)
        printf("arg%d=%s\n", n, argv[n]);
    return 0;
}
** linux
#include <stdio.h>

int main(int argc, char **argv) {
    int n;
    for (n = 0; n < argc; ++n)
        printf("arg%d=%s\n", n, argv[n]);
    return 0;
}
** orig
#include <stdio.h>

int main(int argc, char **argv) {
    int             n;
    for (n = 0; n < argc; ++n)
        printf("arg%d=%s\n", n, argv[n]);
    return 0;
}
** kr
#include <stdio.h>

int main(int argc, char **argv) {
    int n;
    for (n = 0; n < argc; ++n)
        printf("arg%d=%s\n", n, argv[n]);
    return 0;
}

I do not see an option to suppress the space before the { character.

Upvotes: 1

Related Questions