Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Preprocessor Directives

Reply
Thread Tools

Preprocessor Directives

 
 
Water Cooler v2
Guest
Posts: n/a
 
      07-04-2005
Sorry if this is a foolish question, but here it is.

I see things like

#ifndef STDC

...blah
#endif

#ifdef WIN32
...blah
#endif

in open source projects but I do not see where they actually define
these symbols WIN32 and STDC. I know what the #ifdef and #ifndef
preprocessor directives mean. I want to know how these symbols like
STDC and WIN32 are chosen. Are they some pre-defined symbols that
individual compilers expect? Or are they included when building the
project? What gives?

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      07-04-2005
Water Cooler v2 wrote:
> Sorry if this is a foolish question, but here it is.
>
> I see things like
>
> #ifndef STDC
>
> ...blah
> #endif
>
> #ifdef WIN32
> ...blah
> #endif
>
> in open source projects but I do not see where they actually define
> these symbols WIN32 and STDC. I know what the #ifdef and #ifndef
> preprocessor directives mean. I want to know how these symbols like
> STDC and WIN32 are chosen. Are they some pre-defined symbols that
> individual compilers expect? Or are they included when building the
> project? What gives?


A few preprocessor symbols like __STDC__ (not STDC) are
defined by all Standard-conforming C compilers. Others may
be defined by particular compilers, but the only place to
learn about them is from the compilers' documentation. Many
compilers allow preprocessor symbols to be defined at the time
the compiler is invoked (often by means of command-line options);
you'll need to check the build scripts or Makefiles or whatever
other machinery the project uses.

As it happens, a compiler that predefines WIN32 (without
being provoked by a command-line option or something) is not
Standard-conforming. The identifier WIN32 should be available
for any use the programmer cares to make of it. The following
is a strictly conforming program, and a compiler whose predefined
names clash with anything here is non-conforming:

#include <stdio.h>
#include <stdlib.h>
#define SPARC EXIT_SUCCESS
#define I386 EXIT_FAILURE
int main(int WIN32, char **STDC) {
char UNIX[] = "Hello, world!";
int VAX = puts(UNIX);
return VAX == EOF ? SPARC : I386;
}

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid


 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      07-04-2005

"Water Cooler v2" <(E-Mail Removed)> wrote
> #ifndef STDC
>
> ...blah
> #endif
>
> #ifdef WIN32
> ...blah
> #endif
>
> in open source projects but I do not see where they actually define
> these symbols WIN32 and STDC. I know what the #ifdef and #ifndef
> preprocessor directives mean. I want to know how these symbols like
> STDC and WIN32 are chosen. Are they some pre-defined symbols that
> individual compilers expect? Or are they included when building the
> project? What gives?
>

Compilers are allowed to define symbols to the preprocessor before they
start compilation. Usually there is also an option to define your own
symbols.

WIN32, for instance, looks like a symbol that is defined on 32-bit MS
Windows systems. A program might want to take advantage of this, for
instnace to replace a generic function with a faster windows-specific call.


 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      07-05-2005
On Mon, 04 Jul 2005 15:52:19 -0400, Eric Sosman wrote:

....

> As it happens, a compiler that predefines WIN32 (without
> being provoked by a command-line option or something) is not
> Standard-conforming.


It creates a non-conforming implementation whether WIN32 is built in or
specified in the command line. The conformance of a compiler is highly
dependent on the command line options given to it, including macro
predefinitions.

> The identifier WIN32 should be available
> for any use the programmer cares to make of it. The following
> is a strictly conforming program, and a compiler whose predefined
> names clash with anything here is non-conforming:
>
> #include <stdio.h>
> #include <stdlib.h>
> #define SPARC EXIT_SUCCESS
> #define I386 EXIT_FAILURE
> int main(int WIN32, char **STDC) {
> char UNIX[] = "Hello, world!";
> int VAX = puts(UNIX);
> return VAX == EOF ? SPARC : I386;
> }


Right, if any command line options break that code they have broken the
conformance of the implementation.

The program does curiously return EXIT_SUCCESS when puts() fails.

Lawrence


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      07-05-2005
Lawrence Kirby wrote:
> On Mon, 04 Jul 2005 15:52:19 -0400, Eric Sosman wrote:
>

.... snip ...
>
>> The identifier WIN32 should be available
>> for any use the programmer cares to make of it. The following
>> is a strictly conforming program, and a compiler whose predefined
>> names clash with anything here is non-conforming:
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>> #define SPARC EXIT_SUCCESS
>> #define I386 EXIT_FAILURE
>> int main(int WIN32, char **STDC) {
>> char UNIX[] = "Hello, world!";
>> int VAX = puts(UNIX);
>> return VAX == EOF ? SPARC : I386;
>> }

>
> Right, if any command line options break that code they have broken
> the conformance of the implementation.
>
> The program does curiously return EXIT_SUCCESS when puts() fails.


Precisely as it has been told to. It normally returns
EXIT_FAILURE. However, how do you make puts fail here for testing
purposes?

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      07-05-2005
On Tue, 05 Jul 2005 15:25:43 +0000, CBFalconer wrote:

> Lawrence Kirby wrote:
>> On Mon, 04 Jul 2005 15:52:19 -0400, Eric Sosman wrote:
>>

> ... snip ...
>>
>>> The identifier WIN32 should be available
>>> for any use the programmer cares to make of it. The following
>>> is a strictly conforming program, and a compiler whose predefined
>>> names clash with anything here is non-conforming:
>>>
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>> #define SPARC EXIT_SUCCESS
>>> #define I386 EXIT_FAILURE
>>> int main(int WIN32, char **STDC) {
>>> char UNIX[] = "Hello, world!";
>>> int VAX = puts(UNIX);
>>> return VAX == EOF ? SPARC : I386;
>>> }

>>
>> Right, if any command line options break that code they have broken
>> the conformance of the implementation.
>>
>> The program does curiously return EXIT_SUCCESS when puts() fails.

>
> Precisely as it has been told to.


That is what the source specifies, yes. It is unusual behaviour for
e Hello world program however.

> It normally returns EXIT_FAILURE.


Well, I said it is curious not that it is wrong.

> However, how do you make puts fail here for testing
> purposes?


You need control of the environment in some way e.g. how the program is
invoked so it will be platform specific. stdout needs to be attached to an
interactive device to ensure that it is not fully buffered but that device
must fail the write. Under Unix you could maybe do this using a pseudo-tty.

Lawrence





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preprocessor directives in codebehind file Theon Greyjoy ASP .Net 1 10-06-2006 08:17 PM
Preprocessor directives must appear as the first non-whitespace character on a line IndyChris ASP .Net 1 08-09-2006 01:21 AM
Preprocessor directives must appear as the first non-whitespace character on a line erin.sebastian@cowaninsurancegroup.com ASP .Net 0 02-14-2006 03:11 PM
how to make elegant use of preprocessor directives seemanta dutta C Programming 13 12-01-2003 02:39 PM
Help needed to count lines between preprocessor directives Nalla C Programming 25 10-08-2003 04:52 PM



Advertisments