This is my first post, so if I'm being too vague or giving information that everyone would intuitively assume, please let me know.
I'm very new to writing in C and am just trying to get a better understanding of preprocessing. I'm writing a simple program that can take in arguments either directly from the console using gcc -Wall -std=c99 -DSEED=argument
, where my argument should be a an integer, or if the -D
is not defined the user will input it.
The SEED value is simply used in srand()
. I'm very confused why my code will not compile if I put in an -DSEED=a
as my argument while if I put -DSEED=1
it will compile. I'm getting a "âaâ undeclared (first use in this function)" error and really don't understand the difference between the two. I thought the #define
matched up the variable type with the input, so if I put in an "a" #SEED would be a char and if I put in a "1" #SEED would be an int.
If the SEED is not defined I'm using a #ifndef SEED
command and this works well.
I think I'm supposed to "stringify" the input SEED
and then can check if it is an integer or not. After reading some articles online I'm trying to use:
#ifndef SEED
//code
#else
#define TO_STRING( input ) #input
char c;
c = TO_STRING( SEED )
//Then I was going to use c to figure out if it was an int.
#endif
This is not working and anyone able to point out any misconceptions that you think that I may have would be greatly appreciated.
EDIT - So I did figure out why I was receiving the error message when trying the -DSEED=a
, because it was reading it as a variable.
To stringify a
#define
you need to use a two-step approach:If you only want to use one character simply access it via
*pszSeed
orpszSeed[0]
.