Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What types of identifier do #define pragmas create?

Status
Not open for further replies.

greadey

Technical User
Oct 25, 2001
231
GB
When you use a #define pragma, what is the type of identifier created, for example;

#define BAUD 9600 // is BAUD an int or a char[]
#define PARITY 8N1 // Same question.

Thanks for your help,

greadey.
 
Just the macro calls (BAUD or PARITY) are replaced by the defined constants (9600 or 8N1) in the program by the preprocessor.

After this preprocessor substitution the compiler will do its work. So 9600, and 8N1 will be treated based on the code.

--Maniraja
 
Hi,

The Bottom line is #defines are a simple TEXT SUBSTITUTION mechanism and what their types are is dependent on where the text is substituted.

Most likely

#define BAUD 9600

is will be used integer in the code

#define PASSWD "passwd"

will be used in place of a char[] in the code.

#define PI 3.14

will probably be used as a float. however if it all depends on where you use it.

printf("The Baud is %d\n",BAUD );

will become

printf("The Baud is %d\n",9600 );

and the compiler would allow this. It will be an integer however

printf("The Baud is " #BAUD "\n");

will be translated as a string

printf("The Baud is " "9600" "\n");

now if you had

#define PARITY 8N1

and tried

printf("Parity is %s\n", PARITY);

this would translate to

printf("Pariry is %s\n", 8N1);

and the compile would flag this as an error because 'N' is not a number. however

printf("Parity is %s\n", #PARITY);

this would translate to

printf("Pariry is %s\n", "8N1");

and that is perfectly fine.




----

 
luverly jubbly,

thanks peeps

greadey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top