473,394 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Indent C preprocessor directives

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
21 7590
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Trying_Harder | last post by:
Is it possible to redefine a macro with global scope after undefining it in a function? If yes, could someone explain how? /If/ my question above isn't very clear you can refer to the...
4
by: Jim Ford | last post by:
I have a single C file with the following code: int f2() { /* Blah-blah */ } int f1() { /* Blah-blah */
13
by: seemanta dutta | last post by:
Greetings C gurus, I have used preprocessor directives since a very long time. But whenever I see some professional piece of C code, the linux kernel for example, I get literally confused by the...
6
by: Urs Thuermann | last post by:
Does a tool exist to apply C preprocessor expansion to a C source only partially, i.e. replace only some directives? I want to replace some #if's by their expansions, e.g. all #ifdef SOME_SYMBOL,...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
14
by: lagman | last post by:
All, I'm looking for a tool that is able to take my code base (which is full of preprocessor directives) and spit out source code that is "clean" of any preprocessor directives based on the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.