setting macro

R

Roman Mashak

Hello, All!

I'm trying to set up macro:

....
int main(int argc, char *argv[], char *envp[])
{
....
if ( argc > 1 ) {
if ( strcmp(argv[1], "-d") == 0 ) {
#define DEBUG
...
}
}
....
#ifdef DEBUG
fprintf(stdout, ...);
#endif
....

}

So, when I put '-d' switch on a command line, DEBUG should be on and
available for usage further up in code. But it fails, where did I make bug?
Thanks.

With best regards, Roman Mashak. E-mail: (e-mail address removed)
 
S

Simon Morgan

...
int main(int argc, char *argv[], char *envp[]) {
...
if ( argc > 1 ) {
if ( strcmp(argv[1], "-d") == 0 ) {
#define DEBUG
...
}
}
...
#ifdef DEBUG
fprintf(stdout, ...);
#endif
...


}
So, when I put '-d' switch on a command line, DEBUG should be on and
available for usage further up in code. But it fails, where did I make
bug? Thanks.

The preprocessor and compiler are two distinct systems so by the time the
compiler gets around to compiling the above code all of the preprocessor
directives will have been stripped out. Unless the #ifdef section is part
of a conditional statement it will always get executed because you've
defined DEBUG earlier in the program.
 
C

Cong Wang

Roman said:
Hello, All!

I'm trying to set up macro:

...
int main(int argc, char *argv[], char *envp[])
{
...
if ( argc > 1 ) {
if ( strcmp(argv[1], "-d") == 0 ) {
#define DEBUG
...
}
}
...
#ifdef DEBUG
fprintf(stdout, ...);
#endif
...

}

So, when I put '-d' switch on a command line, DEBUG should be on and
available for usage further up in code. But it fails, where did I make bug?
Thanks.

With best regards, Roman Mashak. E-mail: (e-mail address removed)

You can not do that.Because macros are proccessed before compiling.In
fact,whether strcmp(argv[1], "-d") == 0 or not,DEBUG all are defined!So
you make a bug!
 
D

David Resnick

Roman said:
Hello, All!

I'm trying to set up macro:

...
int main(int argc, char *argv[], char *envp[])
{
...
if ( argc > 1 ) {
if ( strcmp(argv[1], "-d") == 0 ) {
#define DEBUG
...
}
}
...
#ifdef DEBUG
fprintf(stdout, ...);
#endif
...

}

So, when I put '-d' switch on a command line, DEBUG should be on and
available for usage further up in code. But it fails, where did I make bug?
Thanks.

With best regards, Roman Mashak. E-mail: (e-mail address removed)

#define is a preprocessor command, NOT a runtime one.
The code shown will have DEBUG defined below the point in the
code where you have the #define... It matters not whether you
invoke the program with "-d". You use the preprocessor in this
way to include or omit sections of code, not to make runtime
choices available.

You could do the same with a static, as in

static int debug_on = 0;
(at file scope)

if ( strcmp(argv[1], "-d") == 0 ) {
debug_on = 1;
}

Then use debug_on to determine if you are debugging.

-David


-David
 
S

Suman

Roman said:
Hello, All!

I'm trying to set up macro:

...
int main(int argc, char *argv[], char *envp[])
{
...
if ( argc > 1 ) {
if ( strcmp(argv[1], "-d") == 0 ) {
#define DEBUG
...
}
}
...
#ifdef DEBUG
fprintf(stdout, ...);
#endif
...

}

So, when I put '-d' switch on a command line, DEBUG should be on and
available for usage further up in code. But it fails, where did I make bug?
Thanks.

So, I wrote a small program that might help:
/*-----start-----*/
#include <stdio.h>

int main(int argc, char *argv[])
{
#ifdef DOIT
printf("Macro:\n");
#endif
if ( argc == 2 )
{
if ( argv[1][1] == 'd' )
{
printf("command line option:\n");
}
}
else
{
printf("only other usage: ./test -d\n");
}
return 0;
}

/*-----end-----*/

And compiled-and-ran with gcc 4.0.0 in the following way:

1. The macro is undefined now:
a-power-mac-g5:~ kr$ gcc -std=c99 -Wall -W -pedantic test.c -o test
a-power-mac-g5:~ kr$ ./test
only other usage: ./test -d
a-power-mac-g5:~ kr$ ./test -d
command line option:
a-power-mac-g5:~ kr$

2. The macro is defined now:
a-power-mac-g5:~ kr$ gcc -DDOIT=1 -std=c99 -Wall -W -pedantic test.c -o
test
a-power-mac-g5:~ kr$ ./test
Macro:
only other usage: ./test -d
a-power-mac-g5:~ kr$ ./test -d
Macro:
command line option:
a-power-mac-g5:~ kr$

Of-course, setting macros like that[2] is not guaranteed for all other
compilers.
So RTFM ;)
 
S

Suman

Roman said:
Hello, All!

I'm trying to set up macro:

...
int main(int argc, char *argv[], char *envp[])
{
...
if ( argc > 1 ) {
if ( strcmp(argv[1], "-d") == 0 ) {
#define DEBUG
...
}
}
...
#ifdef DEBUG
fprintf(stdout, ...);
#endif
...

}

So, when I put '-d' switch on a command line, DEBUG should be on and
available for usage further up in code. But it fails, where did I make bug?
Thanks.

You have it all messed up :) Or, is it me?
If you are trying to get a command line argument and use it to change
the flow, use if-else construct or friends.

If you are trying to set a Macro value, using command line,
it has to be during compiler(preprocessor actually) invocation,
not at runtime. Check manuals for your compiler/IDE whatever, on how
to do such things.
 
K

Keith Thompson

Simon Morgan said:
...
int main(int argc, char *argv[], char *envp[]) {
...
if ( argc > 1 ) {
if ( strcmp(argv[1], "-d") == 0 ) {
#define DEBUG
...
}
}
...
#ifdef DEBUG
fprintf(stdout, ...);
#endif
...


}
So, when I put '-d' switch on a command line, DEBUG should be on and
available for usage further up in code. But it fails, where did I make
bug? Thanks.

The preprocessor and compiler are two distinct systems so by the time the
compiler gets around to compiling the above code all of the preprocessor
directives will have been stripped out. Unless the #ifdef section is part
of a conditional statement it will always get executed because you've
defined DEBUG earlier in the program.

That's not quite it. The preprocessor is actually part of the
compiler. It may be implemented as a separate program, but that's an
irrelevant implementation detail.

It's true that preprocessing is an early phase of compilation, and
that preprocessing is completed before the compiler examines
constructs like declarations and statements, but that's not really the
point here.

The point is that preprocessor directives are handled during
compilation, and the "-d" argument is handled when the program is
executed. The running program cannot affect the way preprocessing
directives are handled.

If you want to make decisions during execution based on command-line
arguments, you'll need to use if statements, not #if or #ifdef
directives.

(Also, the additional envp argument, though it's a common extension,
is non-standard. The standard getenv() function provides limited
access to environment variables. If that's not sufficient, many
systems provide things like a putenv() function and/or an external
variable called "environ".)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Command Line Arguments 0
Linux: using "clone3" and "waitid" 0
buffer overflow 20
tolower() and toupper() 8
Communicating between processes 0
Compiling a c program 5
problem with macro 6
qsort man page 23

Members online

Forum statistics

Threads
473,786
Messages
2,569,626
Members
45,328
Latest member
66Teonna9

Latest Threads

Top