By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,458 Members | 1,215 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,458 IT Pros & Developers. It's quick & easy.

Indent C preprocessor directives

P: n/a
Can anyone recommend a program for indentation of C preprocessor
directives. My file looks like this:
#ifdef a
#define b
#else
#define c
#endif

int main()
{
return 0;
}
and I want a program able to automatically indent the preprocessor
directives.

I have already tried astyle, but with no success.

Bogdan

May 21 '07 #1
Share this Question
Share on Google+
21 Replies


P: n/a
Bogdan wrote:
Can anyone recommend a program for indentation of C preprocessor
directives. My file looks like this:
#ifdef a
#define b
#else
#define c
#endif

int main()
{
return 0;
}
and I want a program able to automatically indent the preprocessor
directives.

I have already tried astyle, but with no success.
indent(1) and vim both say that the source you provided is already
indented in a good way...

If what you want is something similar to this

#ifdef a
#define b
#else
#define c
#endif

, then I think you've got quite an unusual habit...
Bogdan

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
May 21 '07 #2

P: n/a
On 21 May 2007, Pietro Cerutti <ga**@gahr.chwrote:
Bogdan wrote:
>Can anyone recommend a program for indentation of C preprocessor
directives. My file looks like this:
#ifdef a
#define b
#else
#define c
#endif

int main()
{
return 0;
}
and I want a program able to automatically indent the preprocessor
directives.

I have already tried astyle, but with no success.
indent(1) and vim both say that the source you provided is already
indented in a good way...

If what you want is something similar to this

#ifdef a
#define b
#else
#define c
#endif

, then I think you've got quite an unusual habit...
Maybe unusual, but at least *I* don't feel so lonely anymore ;-)

I think some compilers long ago didn't support preprocessor
directives with the # other than in the first column, but none I've
used in that past few years have that restriction. Seriously, isn't
the indented clause version *way* easier to read? If we indent if
clauses, why shouldn't we indent #if clauses for the same reason?

BTW, I don't use a separate program for this. My editor handles most
of it for me with it's autoindent feature.

Dave

--
D.a.v.i.d T.i.k.t.i.n
t.i.k.t.i.n [at] a.d.v.a.n.c.e.d.r.e.l.a.y [dot] c.o.m
May 21 '07 #3

P: n/a
David Tiktin wrote:
>
On 21 May 2007, Pietro Cerutti <ga**@gahr.chwrote:
Bogdan wrote:
Can anyone recommend a program for indentation of C preprocessor
directives. My file looks like this:
#ifdef a
#define b
#else
#define c
#endif
[...]
and I want a program able to automatically indent the preprocessor
directives.
[...]
If what you want is something similar to this

#ifdef a
#define b
#else
#define c
#endif

, then I think you've got quite an unusual habit...

Maybe unusual, but at least *I* don't feel so lonely anymore ;-)

I think some compilers long ago didn't support preprocessor
directives with the # other than in the first column, but none I've
used in that past few years have that restriction.
I've seen programs keep the '#' in the first column, and insert
whitespace between it and the directive. (Does the Standard say
that this is allowed?) Using the same example:

#ifdef a
# define b
#else
# define c
#endif
Seriously, isn't the indented clause version *way* easier to read?
If we indent if clauses, why shouldn't we indent #if clauses for the
same reason?
When you have complex nested #if/#ifdef's, it is certainly easier
to read for me. However, I've been programming long enough that I've
used compilers that required the '#' in column 1, and no whitespace
following it, so I got in the habit of not indenting such things.
If the Standard guarantees both/either of the above, I just may
retrain myself.

[...]

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

May 21 '07 #4

P: n/a
Kenneth Brody wrote On 05/21/07 16:56,:
David Tiktin wrote:
>>On 21 May 2007, Pietro Cerutti <ga**@gahr.chwrote:

>>>Bogdan wrote:

Can anyone recommend a program for indentation of C preprocessor
directives. My file looks like this:
#ifdef a
#define b
#else
#define c
#endif

[...]
>>>>and I want a program able to automatically indent the preprocessor
directives.

[...]
>>>If what you want is something similar to this

#ifdef a
#define b
#else
#define c
#endif

, then I think you've got quite an unusual habit...

Maybe unusual, but at least *I* don't feel so lonely anymore ;-)

I think some compilers long ago didn't support preprocessor
directives with the # other than in the first column, but none I've
used in that past few years have that restriction.


I've seen programs keep the '#' in the first column, and insert
whitespace between it and the directive. (Does the Standard say
that this is allowed?) Using the same example:

#ifdef a
# define b
#else
# define c
#endif

>>Seriously, isn't the indented clause version *way* easier to read?
If we indent if clauses, why shouldn't we indent #if clauses for the
same reason?


When you have complex nested #if/#ifdef's, it is certainly easier
to read for me. However, I've been programming long enough that I've
used compilers that required the '#' in column 1, and no whitespace
following it, so I got in the habit of not indenting such things.
If the Standard guarantees both/either of the above, I just may
retrain myself.
White space is allowed both before and after the #.
What's more, it can be any species of white space:

/*#ifdef A*/#/*include <stdio.h>*/else/*include "stdio.h"*/

As with most of C, good sense is not enforced.

--
Er*********@sun.com

May 21 '07 #5

P: n/a
On 21 May, 22:37, Eric Sosman <Eric.Sos...@sun.comwrote:
White space is allowed both before and after the #.
What's more, it can be any species of white space:

/*#ifdef A*/#/*include <stdio.h>*/else/*include "stdio.h"*/

As with most of C, good sense is not enforced.
Really?

gcc gives me "`#else' not within a conditional" for that line, with or
without the "-ansi -pedantic" flags.

May 22 '07 #6

P: n/a
ke***@bytebrothers.co.uk wrote:
On 21 May, 22:37, Eric Sosman <Eric.Sos...@sun.comwrote:
> White space is allowed both before and after the #.
What's more, it can be any species of white space:

/*#ifdef A*/#/*include <stdio.h>*/else/*include "stdio.h"*/

As with most of C, good sense is not enforced.

Really?
Really.
gcc gives me "`#else' not within a conditional" for that line, with or
without the "-ansi -pedantic" flags.
That's nice. But that only reinforces Eric's point: the line
above is a #else, but written in a diabolical fashion devoid
of good sense.

[To be fair, most programming languages allow similar exhibitions
of sense without sensibility, since they're blind to the meaning
of names & natural language.]

--
The shortcuts are all full of people using them.

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

May 22 '07 #7

P: n/a
Bogdan wrote:
Can anyone recommend a program for indentation of C preprocessor
directives. My file looks like this:
#ifdef a
#define b
#else
#define c
#endif

int main()
{
return 0;
}
and I want a program able to automatically indent the preprocessor
directives.

I have already tried astyle, but with no success.

$ indent -kr -ppi 3 -st your_file.c
#ifdef a
# define b
#else
# define c
#endif

int main()
{
return 0;
}
--
Tor <torust [at] online [dot] no>

May 22 '07 #8

P: n/a
Tor Rustad wrote:
$ indent -kr -ppi 3 -st your_file.c
Which version of indent are you using?
Mine, on FreeBSD-6.2-STABLE doesn't have any of the options you're using...

Thanks

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
May 22 '07 #9

P: n/a
Pietro Cerutti wrote:
Tor Rustad wrote:
>$ indent -kr -ppi 3 -st your_file.c

Which version of indent are you using?
Mine, on FreeBSD-6.2-STABLE doesn't have any of the options you're
using...
$ indent --version
GNU indent 2.2.9

The laptop is running Ubuntu 7.04 Feisty.

--
Tor <torust [at] online [dot] no>

May 22 '07 #10

P: n/a
Tor Rustad wrote:
Pietro Cerutti wrote:
>Tor Rustad wrote:
>>$ indent -kr -ppi 3 -st your_file.c
Which version of indent are you using?
Mine, on FreeBSD-6.2-STABLE doesn't have any of the options you're
using...

$ indent --version
GNU indent 2.2.9
Uhm, I have gnu indent in the ports... I'll give it a try..
Thank you
--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
May 22 '07 #11

P: n/a
Tor Rustad wrote:
Bogdan wrote:
>Can anyone recommend a program for indentation of C preprocessor
directives. My file looks like this:

#ifdef a
#define b
#else
#define c
#endif

int main()
{
return 0;
}
and I want a program able to automatically indent the preprocessor
directives.

I have already tried astyle, but with no success.

$ indent -kr -ppi 3 -st your_file.c
I can't find the -ppi directive in the manuals anywhere. What is
its description?

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 22 '07 #12

P: n/a
CBFalconer wrote:
Tor Rustad wrote:
>$ indent -kr -ppi 3 -st your_file.c

I can't find the -ppi directive in the manuals anywhere. What is
its description?
The man page of indent say this (see INDENTATION section):
ANSI C allows white space to be placed on preprocessor command lines
between the character ?#? and the command name. By default, indent
removes this space, but specifying the ?-lps? option directs indent to
leave this space unmodified. The option ?-ppi? overrides ?-nlps?
and ?-lps?.

This option can be used to request that preprocessor conditional
statements can be indented by to given number of spaces, for example with
the
option ?-ppi 3?

#if X
#if Y
#define Z 1
#else
#define Z 0
#endif
#endif
becomes
#if X
# if Y
# define Z 1
# else
# define Z 0
# endif
#endif
--
Tor <torust [at] online [dot] no>
May 22 '07 #13

P: n/a
Tor Rustad wrote:
Pietro Cerutti wrote:
>Tor Rustad wrote:
>>$ indent -kr -ppi 3 -st your_file.c

Which version of indent are you using?
Mine, on FreeBSD-6.2-STABLE doesn't have any of the options
you're using...

$ indent --version
GNU indent 2.2.9
I'm using 2.2.9, and the manual never mentions ppi. What does it
do?

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 24 '07 #14

P: n/a
CBFalconer wrote:
Tor Rustad wrote:
>>
$ indent --version
GNU indent 2.2.9

I'm using 2.2.9, and the manual never mentions ppi. What does it
do?
TRINITY: A deja vu is usually a glitch in the Matrix. It happens when they
change something.

--
Tor <torust [at] online [dot] no>

May 25 '07 #15

P: n/a
On Wed, 23 May 2007 20:45:28 -0400, CBFalconer <cb********@yahoo.comwrote:
>Tor Rustad wrote:
>>Which version of indent are you using?
Mine, on FreeBSD-6.2-STABLE doesn't have any of the options
you're using...

$ indent --version
GNU indent 2.2.9

I'm using 2.2.9, and the manual never mentions ppi.
What does it do?
That's odd. Are you sure you are looking at the right manpage?
The manpage of GNU indent 2.2.9 here mentionds -ppi:

: ANSI C allows white space to be placed on preprocessor command
: lines between the character `#' and the command name. By
: default, indent removes this space, but specifying the `-lps'
: option directs indent to leave this space unmodified. The
: option `-ppi' overrides `-nlps' and `-lps'.
:
: This option can be used to request that preprocessor
: conditional state- ments can be indented by to given number of
: spaces, for example with the option `-ppi 3'
:
: #if X
: #if Y
: #define Z 1
: #else
: #define Z 0
: #endif
: #endif
: becomes
: #if X
: # if Y
: # define Z 1
: # else
: # define Z 0
: # endif
: #endif

May 25 '07 #16

P: n/a
David Tiktin writes:
I think some compilers long ago didn't support preprocessor
directives with the # other than in the first column, but none I've
used in that past few years have that restriction.
I've encountered a few. Don't remember how long ago, or if they still
are in use. But they did accept space _after_ the '#', so I write

#ifdef FOO
# define BAR
#endif
Seriously, isn't
the indented clause version *way* easier to read? If we indent if
clauses, why shouldn't we indent #if clauses for the same reason?
In preprocessor-only code that is nice. With #ifdefs in the middle
of other code it gets wrong either way. Well, it's possible but
programs like indent or editors' C mode will mess up code like this:

#ifdef FOO
int hmm()
{
int i;
# ifdef BAR
i = 3;
# else
i = 5;
# endif
...
}
#endif

--
Regards,
Hallvard
May 25 '07 #17

P: n/a
Giorgos Keramidas wrote:
CBFalconer <cb********@yahoo.comwrote:
>Tor Rustad wrote:
.... snip ...
>>
>>$ indent --version
GNU indent 2.2.9

I'm using 2.2.9, and the manual never mentions ppi.
What does it do?

That's odd. Are you sure you are looking at the right manpage?
The manpage of GNU indent 2.2.9 here mentionds -ppi:
No, the man and info pages don't specify the version.
>
>ANSI C allows white space to be placed on preprocessor command
lines between the character `#' and the command name. By
default, indent removes this space, but specifying the `-lps'
option directs indent to leave this space unmodified. The
option `-ppi' overrides `-nlps' and `-lps'.

This option can be used to request that preprocessor
conditional state- ments can be indented by to given number of
spaces, for example with the option `-ppi 3'
Thanks, that's the info I was looking for.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 25 '07 #18

P: n/a
CBFalconer wrote:
Thanks, that's the info I was looking for.
That exact information was posted 3 days ago. :-)

--
Tor <torust [at] online [dot] no>

May 25 '07 #19

P: n/a
Tor Rustad wrote:
CBFalconer wrote:
>Thanks, that's the info I was looking for.

That exact information was posted 3 days ago. :-)
Never seen here before I answered. I think some things don't get
through my news-server. A week ago my posts seemed to take three
days to get posted! I was getting replies two days before my own
post showed up!

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 26 '07 #20

P: n/a
On Fri, 25 May 2007 23:59:20 +0200, Tor Rustad <to****@online.nowrote:
>CBFalconer wrote:
>Thanks, that's the info I was looking for.

That exact information was posted 3 days ago. :-)
You're right, of course. Unfortunately, I saw the 3-days ago post
later, after I had posted mine already :-/

May 26 '07 #21

P: n/a
Giorgos Keramidas wrote:
Tor Rustad <to****@online.nowrote:
>CBFalconer wrote:
>>Thanks, that's the info I was looking for.

That exact information was posted 3 days ago. :-)

You're right, of course. Unfortunately, I saw the 3-days ago post
later, after I had posted mine already :-/
Yours wasn't wasted. The 3-days ago post still hasn't reached
here.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 26 '07 #22

This discussion thread is closed

Replies have been disabled for this discussion.