Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform calculation in preprocessor with stringification in C [duplicate]

Possible Duplicate:
How to stringify an expression in C

I hope I can find some solution of my issue here.

I want to add the hex numbers defined in ADD1 and need to put the same in *str in string form.

This is only a example, to prove its possibility first. I am actually doing the same in a header file.

#include <stdio.h>


#define XMK_STR(x)      #x
#define MK_STR(x)       XMK_STR(x)
#define ADD1 0x6000+0x10

main()
{
        char *str = "START " MK_STR(ADD1) "\n";
        printf(str);
}
like image 512
linuxexplore Avatar asked Oct 07 '22 09:10

linuxexplore


1 Answers

No. You can't perform calculations using preprocessor. It's essentially a text-replacer.

You can use sprintf() and print the added result into str for that you can use ADD1.

like image 60
P.P Avatar answered Oct 10 '22 02:10

P.P