473,387 Members | 1,687 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,387 software developers and data experts.

Preprocessor Directives

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?

Nov 15 '05 #1
5 3043
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
es*****@acm-dot-org.invalid
Nov 15 '05 #2

"Water Cooler v2" <wt*****@yahoo.com> 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.
Nov 15 '05 #3
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
Nov 15 '05 #4
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
Nov 15 '05 #5
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

Nov 15 '05 #6

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,...
21
by: Bogdan | last post by:
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() {
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...
9
by: Bob | last post by:
Hi, Is it possible to change the references in a project by using preprocessor directives? Thanks, Bob
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.