-5

A problem occurred to me. Somebody might show me how to remove the @.

I am writing C for a UC and I am lazy; so I want to solve easy problems with macros, e.g. switching on an LED.

I managed to do something like that:

#include <stdio.h>

#define BIT_STD_SET(PORT, BITNUM) ((PORT) |= (1<<(BITNUM)))
#define BIT_STD_CLE(PORT, BITNUM) ((PORT) &= ~(1<<(BITNUM)))
#define BIT_STD_TOG(PORT, BITNUM) ((PORT) ^= (1<<(BITNUM)))

#define LEDPORT_0 C
#define LEDPAD_0 3 /*Blau*/

#define LEDPORT_1 D
#define LEDPAD_1 4 /*GelbWeis*/

#define PO(n) LEDPORT_##n
#define POR(n) PORT@PO(n)

#define PA(n) LEDPAD_##n
#define PAD(n) PA(n)

#define LEDAN(n) BIT_STD_SET(POR(n),PAD(n))


#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)

int main()
{
  printf("%s\n",h(LEDAN(0)));
  printf("%s\n",h(LEDAN(1)));
  printf("\n");
  printf("%s\n",h(LEDAN(1)));
  printf("\n");
  printf("%s\n",h(POR(0)));
  printf("%s\n",h(POR(1)));
  printf("%s\n",h(f(0,1)));
  printf("%s\n",g(f(0,1)));
  return 0;
}
p@d:~/$ gcc ./mak.c

And got:

p@d:~/$ ./a.out 

Answer:

((PORT@C) |= (1<<(3)))
((PORT@D) |= (1<<(4)))

((PORT@D) |= (1<<(4)))

PORT@C
PORT@D
01
f(0,1)

The @ should be removed. Unfortunately, I do not know how. I have read some manuals, but I do not know how to express myself.

Biffen
  • 6,249
  • 6
  • 28
  • 36
Hert
  • 11
  • 2
  • 3
  • 2
    The right tool to solve such problems is to use your debugger, but not to ask at Stack Overflow before you did so. Tell us all your observations you made when inspecting your code stepping through line by line in 1st place. Also you might want to read **[How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** At least leave us with a [MCVE] that reproduces your problem. (This is a personal stock comment provided by πάντα ῥεῖ™) – πάντα ῥεῖ Aug 21 '16 at 10:25
  • 2
    read the docs: mark+ctrlK does the job for you no need to wear out your space bar. and please clarify. From where do you need to remove the `@`? – Jean-François Fabre Aug 21 '16 at 10:25
  • 1
    for clarity for the reader of your code (including your self some months from now) write out what you want the code to do, Don't try to hide the actual code behind a bunch of macros – user3629249 Aug 21 '16 at 10:42

2 Answers2

0

If you do not want to print the @, replace this:

#define POR(n) PORT@PO(n)

with this:

#define POR(n) PORT PO(n)

Hope I helped.

Shay Gold
  • 403
  • 4
  • 14
0

You can concatenate tokens with this:

#define CATx(a,b)  a##b
#define CAT(a,b)   CATx(a,b)

Then you can use it like #define POR(n) CAT(PORT,PO(n)).