473,397 Members | 2,099 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,397 software developers and data experts.

Can #include use a preprocessing token?

Can #include safely use a preprocessing token, as in

#define HEADERFILE "stdio.h"
#include HEADERFILE
int main(void) {return printf("Hello, world\n")*0;}
TIA,

François Grieu
Nov 14 '05 #1
12 1951
Francois Grieu wrote:
Can #include safely use a preprocessing token, as in

#define HEADERFILE "stdio.h"
#include HEADERFILE
int main(void) {return printf("Hello, world\n")*0;} cat main.c #define HEADERFILE "stdio.h"

#include HEADERFILE

int main(int argc, char* argv[]) {
return printf("Hello, world!\n")*0;
}
gcc -Wall -std=c99 -pedantic -o main main.c
./main

Hello, world!

Why, yes! It appears that you can.
Nov 14 '05 #2
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:
Francois Grieu wrote:
Can #include safely use a preprocessing token, as in

#define HEADERFILE "stdio.h"
#include HEADERFILE
int main(void) {return printf("Hello, world\n")*0;}

> cat main.c

#define HEADERFILE "stdio.h"

#include HEADERFILE

int main(int argc, char* argv[]) {
return printf("Hello, world!\n")*0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main

Hello, world!

Why, yes! It appears that you can.


Well, no. It appears that you can _using GCC_. Given the rather severe
embrace-and-extend habits of the Gnu people, this tells you absolutely
nothing about ISO C.

Of course, you _can_ actually do the above in ISO C, and you don't even
need a C99 compiler for that, as your command line seems to suggest. But
that conclusion can be drawn from the definition of the #include
directive in the ISO C Standard, _not_ from a test using Ganuck.

Richard
Nov 14 '05 #3
Francois Grieu <fg****@francenet.fr> wrote:
Can #include safely use a preprocessing token, as in

#define HEADERFILE "stdio.h"
#include HEADERFILE
int main(void) {return printf("Hello, world\n")*0;}


Yes.

ISO/IEC 9899:1999
6.10.2 Source file inclusion
[...]
4 A preprocessing directive of the form
# include pp-tokens new-line
(that does not match one of the two previous forms) is permitted.
The preprocessing tokens after include in the directive are
processed just as in normal text. (Each identifier currently
defined as a macro name is replaced by its replacement list of
preprocessing tokens.) The directive resulting after all
replacements shall match one of the two previous forms.
[...]

Regards.
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #4
Richard Bos wrote:
E. Robert Tisdale wrote:
Francois Grieu wrote:
Can #include safely use a preprocessing token, as in

#define HEADERFILE "stdio.h"
#include HEADERFILE
int main(void) {return printf("Hello, world\n")*0;}
> cat main.c

#define HEADERFILE "stdio.h"

#include HEADERFILE

int main(int argc, char* argv[]) {
return printf("Hello, world!\n")*0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main

Hello, world!

Why, yes! It appears that you can.


Well, no. It appears that you can _using GCC_.
Given the rather severe
embrace-and-extend habits of the Gnu people,
this tells you absolutely nothing about ISO C.

Of course, you _can_ actually do the above in ISO C,
and you don't even need a C99 compiler for that,
as your command line seems to suggest.


My command line suggests no such thing.
But that conclusion can be drawn
from the definition of the #include directive in the ISO C Standard,
_not_ from a test using Ganuck.


Pardon me Richard,
but I'll trust the GNU compiler developers
over some pretentious ass who "draws conclusions"
from his personal interpretation of the ISO C standard any day.
Nov 14 '05 #5

On Wed, 9 Jun 2004, Richard Bos wrote:

Trollsdale wrote:
Francois Grieu wrote:
Can #include safely use a preprocessing token, as in

#define HEADERFILE "stdio.h"
#include HEADERFILE
int main(void) {return printf("Hello, world\n")*0;}
<snip garbage> Why, yes! It appears that you can.


Well, no. It appears that you can _using GCC_. Given the rather severe
embrace-and-extend habits of the Gnu people, this tells you absolutely
nothing about ISO C.


In fact, AFAICT the OP's code is *not* correct. Since when is

#include "stdio.h"

the appropriate way to get a prototype in scope for 'printf', a function
defined in the header <stdio.h>? The OP's code would be perfectly
correct were he to have written

#define HEADERFILE <stdio.h>
#include HEADERFILE

-Arthur

--
please don't feed the trollsdale, not even if he replies to you
<cough>Christian Bau</cough>
Nov 14 '05 #6
On Wed, 9 Jun 2004 09:36:41 -0400 (EDT), "Arthur J. O'Dwyer"
<aj*@nospam.andrew.cmu.edu> wrote:

On Wed, 9 Jun 2004, Richard Bos wrote:

Trollsdale wrote:
> Francois Grieu wrote:
>
> > Can #include safely use a preprocessing token, as in
> >
> > #define HEADERFILE "stdio.h"
> > #include HEADERFILE
> > int main(void) {return printf("Hello, world\n")*0;}<snip garbage> > Why, yes! It appears that you can.


Well, no. It appears that you can _using GCC_. Given the rather severe
embrace-and-extend habits of the Gnu people, this tells you absolutely
nothing about ISO C.


In fact, AFAICT the OP's code is *not* correct. Since when is

#include "stdio.h"

the appropriate way to get a prototype in scope for 'printf', a function
defined in the header <stdio.h>? The OP's code would be perfectly
correct were he to have written

#define HEADERFILE <stdio.h>
#include HEADERFILE

-Arthur


I was under the impression that "stdio.h" and <stdio.h> could both
reference the same file, but that the <> instructed the preprocessor
to look in a implementation defined location for said file, while ""
is 'look in the current directory, then in any implementation defined
locations'.

So if the pp doesn't find stdio.h in the current directory, it's well
within its rights to look elsewhere.

Am I mistaken?

--
Andrew

Nov 14 '05 #7
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote:
In fact, AFAICT the OP's code is *not* correct. Since when is

#include "stdio.h"

the appropriate way to get a prototype in scope for 'printf', a function
defined in the header <stdio.h>? The OP's code would be perfectly
correct were he to have written

#define HEADERFILE <stdio.h>
#include HEADERFILE


From (a draft of) C89:

# A preprocessing directive of the form
#
# # include "q-char-sequence" new-line
#
#causes the replacement of that directive by the entire contents of the
#source file identified by the specified sequence between the
#delimiters. The named source file is searched for in an
#implementation-defined manner. If this search is not supported, or if
#the search fails, the directive is reprocessed as if it read
#
# # include <h-char-sequence> new-line
#
#with the identical contained sequence (including > characters, if any)
#from the original directive.

Richard
Nov 14 '05 #8

On Wed, 9 Jun 2004, Richard Bos wrote:

"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote:
In fact, AFAICT the OP's code is *not* correct. Since when is

#include "stdio.h"

the appropriate way to get a prototype in scope for 'printf', a function
defined in the header <stdio.h>?


From (a draft of) C89:
#
# A preprocessing directive of the form
#
# # include "q-char-sequence" new-line
#
#causes the replacement of that directive by the entire contents of the
#source file identified by the specified sequence between the
#delimiters. The named source file is searched for in an
#implementation-defined manner. If this search is not supported, or if
#the search fails, the directive is reprocessed as if it read
#
# # include <h-char-sequence> new-line


Okay. So it's implementation-defined whether it's correct (depending
on whether the implementation defines the search for "stdio.h" to
succeed, in which case consult your documentation, or fail, in which
case the code is correct).
Still, the portably correct way is <stdio.h>, which AFAIK is
guaranteed to succeed on any hosted implementation.

-Arthur
Nov 14 '05 #9
E. Robert Tisdale wrote:
Richard Bos wrote:
E. Robert Tisdale wrote:
> gcc -Wall -std=c99 -pedantic -o main main.c
Of course, you _can_ actually do the above in ISO C,
and you don't even need a C99 compiler for that, as your command line
seems to suggest.
My command line suggests no such thing.


The troll ERT used the -std=c99 option intending, it seems, to invoke
gcc in C89 mode.
Nov 14 '05 #10
Martin Ambuhl wrote:
ERT used the -std=c99 option
intending, it seems, to invoke gcc in C89 mode.


That's a lie.

Go away troll.
Nov 14 '05 #11
On Wed, 09 Jun 2004 14:06:37 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote in comp.lang.c:
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote:
In fact, AFAICT the OP's code is *not* correct. Since when is

#include "stdio.h"

the appropriate way to get a prototype in scope for 'printf', a function
defined in the header <stdio.h>? The OP's code would be perfectly
correct were he to have written

#define HEADERFILE <stdio.h>
#include HEADERFILE


From (a draft of) C89:

# A preprocessing directive of the form
#
# # include "q-char-sequence" new-line
#
#causes the replacement of that directive by the entire contents of the
#source file identified by the specified sequence between the
#delimiters. The named source file is searched for in an
#implementation-defined manner. If this search is not supported, or if
#the search fails, the directive is reprocessed as if it read
#
# # include <h-char-sequence> new-line
#
#with the identical contained sequence (including > characters, if any)
#from the original directive.

Richard


Your quotation is basically correct, but your conclusion is not,
according to the standard. It happens to work on most
implementations, because the C standard headers are supplied as text
files for the majority of them.

Here is the relevant text from the C99 standard, including material
above what you quoted:

<begin quote>
6.10.2 Source file inclusion

Constraints
1 A #include directive shall identify a header or source file that can
be processed by the implementation.

Semantics
2 A preprocessing directive of the form
# include <h-char-sequence> new-line
searches a sequence of implementation-defined places for a header
identified uniquely by the specified sequence between the < and >
delimiters, and causes the replacement of that directive by the entire
contents of the header. How the places are specified or the header
identified is implementation-defined.

3 A preprocessing directive of the form
# include "q-char-sequence" new-line
causes the replacement of that directive by the entire contents of the
source file identified by the specified sequence between the "
delimiters. The named source file is searched for in an
implementation-defined manner. If this search is not supported, or if
the search fails, the directive is reprocessed as if it read
# include <h-char-sequence> new-line
with the identical contained sequence (including > characters, if any)
from the original directive.
<end quote>

Note the distinction between "headers" (which need not be files) which
are included with the #include <> directive, and source files which
may be included with the #include "" directive.

There is no guarantee that:

#include "stdio.h"

....will include the standard C header <stdio.h> into the source and
make the corresponding library functions available with proper
prototypes.

Specifically, if the implementation-defined methods of search and file
or file identification happen to come across some random file _not_
provided by the implementation as a standard header, and includes it
as a source file, the behavior is completely undefined.

#include <stdio.h>

....is conforming and guaranteed to properly include the standard
header.

#include "stdio.h"

....is not, and is not required to be accepted by a conforming
implementation.

This is true of all 15, 18, or 24 standard headers for C89/90, 95, and
99 respectively.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #12
Jack Klein <ja*******@spamcop.net> wrote:
On Wed, 09 Jun 2004 14:06:37 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote in comp.lang.c:
From (a draft of) C89:

# A preprocessing directive of the form
#
# # include "q-char-sequence" new-line
#
#causes the replacement of that directive by the entire contents of the
#source file identified by the specified sequence between the
#delimiters. The named source file is searched for in an
#implementation-defined manner. If this search is not supported, or if
#the search fails, the directive is reprocessed as if it read
#
# # include <h-char-sequence> new-line
#
#with the identical contained sequence (including > characters, if any)
#from the original directive.
Your quotation is basically correct,


It is perfectly correct; it is a direct copy and paste. It is not,
however, C99.
Here is the relevant text from the C99 standard, including material
above what you quoted:

<begin quote>
6.10.2 Source file inclusion
[ Snip quote ]
Note the distinction between "headers" (which need not be files) which
are included with the #include <> directive, and source files which
may be included with the #include "" directive. Specifically, if the implementation-defined methods of search and file
or file identification happen to come across some random file _not_
provided by the implementation as a standard header, and includes it
as a source file, the behavior is completely undefined.


Ah, but...

# [#3] If a file with the same name as one of the above < and
# > delimited sequences, not provided as part of the
# implementation, is placed in any of the standard places that
# are searched for included source files, the behavior is
# undefined.

That's from n869, 7.1.2. C99 has the same text, AFAIK.

So if that file is found, _that file_ causes the undefined behaviour.
For an otherwise well-conforming program, i.e. one which does _not_ have
this UB-causing file, #include "stdio.h" is required to fall through to
<stdio.h>.

Richard
Nov 14 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ron | last post by:
The C++ standard ISO/IEC 14882 specifies pp-numbers as follow: pp-number: digit .digit pp-number digit pp-number nondigit pp-number e sign pp-number E sign pp-number .
9
by: bill | last post by:
Forget the exact definition of difference between, #include <foo.h> and #include "bar.h" Normally foo.h is a standard header file, so it's path is not defined in compiler option, but I...
3
by: Frodo Baggins | last post by:
Hi All, I have a piece of code (not written by me) that is failing on compile with the error: pasting "xdr_ndmp_connect_open_request" and "," does not give a valid preprocessing token The...
2
by: Martin Hvidberg | last post by:
Dear list I have found a declaration like this: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <math.h> #include "ectemp.h"
5
by: Chris | last post by:
Hi all We have a strange problem with macros: #define SQLST_MAP_IND_O(Tbl,Fld) i##Tbl##_O=i##Tbl##_##Fld; SQLST_MAP_IND_I(RAGREEJ1,FORMFROMTMSTP); => gcc 3.3.4 gives the following error:...
13
by: Rick Anderson | last post by:
Group, I want to define a #include directive from another macro, if possible... For example, the following doesn't work but is basically what I need to do: #define...
34
by: mr.polik | last post by:
Is it legal to do something like this: #include "x.c" in other source file? And is it good idea? Thank you in advance for your answers, Igor.
3
by: vippstar | last post by:
Is this a valid implementation of stdio.h? -- stdio.h -- #ifndef __STDIO_H #define __STDIO_H /* all the necessary declarations etc */ void __dummy(void); void __dummy(void) { return;...
6
by: Bing | last post by:
Hi folks, Is there a way to define a macro that may contain #include directive in its body. If I just put the "#include", it gives error C2162: "expected macro formal parameter" since here I am...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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.