-1

I am trying to achieve that C interprets my string as macro.

Hey, let's suppose there is a defined macro as,

#define ABC 900

If i define;

char* s[] = "ABC" ; 

then,

printf("%d",s) ; 

Is there any way the compiler understands that "ABC" as macro ABC and passes 900 integer value to printf ?

#include<stdio.h>
#define abc 15

int main(void) {

    char a[] = "abc" ;

    printf("%d",a); 


    return 0;
}

When i try the above code, instead of my desired output 15 , i get 6487568 which i guess the integer equivalent of that string.

Edit : those were random values , or address of strings. ( as stated below by others )

muyustan
  • 1,555
  • 1
  • 11
  • 23
  • Not as you describe it. Describe more abstractly, please. What are you trying to achieve? Have a look here to understand why I am asking: https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Yunnosch Jun 26 '19 at 05:39
  • @Yunnosch what i exactly trying yo achieve is, I am working on STM32 MCU, in its library there are 4 LED definitions which have set equal to hexadecimal numbers(RAM adress). f.e, #define LD3_Pin 0x000F #define LD4_Pin 0x0010 etc; I wanted to iterate between those LED's by passing their macros stored in a string array, char * [10] = {"LD3_Pin" , "LD4_Pin" , ... } ; But i realized that those strings are not identified as macros, instead they pass random integers. I solved my problem with another method, but I got interested whether this kind of implementation possible or not. – muyustan Jun 26 '19 at 05:46
  • Please [edit] to add helpful information to your question. – Yunnosch Jun 26 '19 at 05:48
  • Why not just iterate over the values? `unsigned int a[] = { LD3_Pin, LD4_Pin, ... }`? – melpomene Jun 26 '19 at 05:48
  • For what purpose do you want to have names of macros in your string array? You can just store the values instead. – Gerhardh Jun 26 '19 at 05:49
  • @melpomene , Gerhardh I didn't think like that, thanks for advice. As i said above i solved problem with passing values itself. I just wondered whether this ( my question ) was possible or not. Thanks. – muyustan Jun 26 '19 at 05:52
  • you can use [macro stringification](https://stackoverflow.com/questions/2653214/stringification-of-a-macro-value) – Agnius Vasiliauskas Jun 26 '19 at 06:35
  • An important detail to remember is that, in c, macros are little more than text substitution that occurs in the preprocessor – user3629249 Jun 27 '19 at 13:35

2 Answers2

2

No, what you're trying to do is double impossible. You can't access variables by name at runtime (string -> variable) because the compiled machine code knows nothing about the names in your C code, and you can't access macros from the compiler because the compiler knows nothing about macros (they're expanded by the preprocessor before the compiler even sees the code).

In other words, compilation / execution happens in multiple stages:

  1. C source code is preprocessed (which gets rid of directives like #include or #define and expands macros).
  2. The preprocessed token stream is passed to the compiler, which converts it to machine code (a runnable program).
  3. Finally the program runs.

Simplified example:

// original C code
#define FOO 42
...
int x = y + FOO;

After preprocessing:

...
int x = y + 42;

After compilation:

movl %ecx, %eax
addl $42, %eax

There is no trace of FOO in step 2, and the final code knows nothing about x or y.

Variable values such as strings only exist at runtime, in step 3. You can't get back to step 1 from there. If you wanted to access information about macros at runtime, you'd have to keep it explicitly in some sort of data structure, but none of this is automatic.

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • Thanks for explicit example, by the way what is that "foo" thing, is that have a meaning or just a convention, i see it among these kind of examples a lot. p.s : non-native speaker, i hope foo is not something everybody knows except me – muyustan Jun 26 '19 at 06:07
  • 1
    @muyustan https://en.wikipedia.org/wiki/Foobar, http://www.catb.org/jargon/html/F/foo.html. – melpomene Jun 26 '19 at 06:08
  • @melpomene saying more precise proprocessor does know anything about the C and only textually replaces the tokens, and C is compiling this source with everything already replaced – 0___________ Jun 26 '19 at 07:16
  • @melpomene saying more precise proprocessor does know anything about the C and only textually replaces the tokens, and C is compiling this source with everything already replaced – 0___________ Jun 26 '19 at 07:16
0

Macros are simple copy paste and they are pretty limited. A macro will not expand if it's quoted or commented.

One solution would be:

#define ABC "900"
char s[] = ABC; 

But no, macros cannot be used for what you're trying to do.

When i try the above code, instead of my desired output 15 , i get 6487568 which i guess the integer equivalent of that string.

It's undefined behavior. Most likely it's the address of the string. If you compile with -Wall you will get a warning for this.

klutt
  • 30,332
  • 17
  • 55
  • 95