how to compare a macro with a constant?

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

is there any way of comparing (using relational operators with) a macro with a constant value?

 

i have defined a macro in my program, the macro is a system variable whose values could be changed later on as per our requirements (like the F_CPU), and now need to check for its values for further logic designing. i am trying to use the same procedure of using relational operators as is done with any variable. but that doesn't seems to work and the compiler is showing errors.

 

how can one achieve such an objective?

 

thank you for the replies.

 

This topic has a solution.

Long live my MASTER.

Last Edited: Tue. Aug 4, 2015 - 05:12 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your question is too vague - you haven't even said what language you're using!

 

In 'C', a macro is just a compile-time text substitution (actually, just before the actual compilation - hence "pre-processor").

 

Check your compiler manual to find out how to see the preprocessor output; for GCC, it's -save-temps

 

When you look at the preprocessor output, you see the text that the actual compilation process sees - you will see how your macro expands and it should then be obvious where you're going wrong ...

 

http://www.8052.com/forum/read/2...

 

https://www.avrfreaks.net/comment...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Mon. Aug 3, 2015 - 09:33 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I was just looking at an example (AVR-LibC's <avr/fuse.h>) this morning for another thread so will cite that here as it shows a good use of conditional operators in macros checking against numeric constants:

#if FUSE_MEMORY_SIZE > 3

typedef struct
{
    unsigned char byte[FUSE_MEMORY_SIZE];
} __fuse_t;


#elif FUSE_MEMORY_SIZE == 3

typedef struct
{
    unsigned char low;
    unsigned char high;
    unsigned char extended;
} __fuse_t;

#elif FUSE_MEMORY_SIZE == 2

typedef struct
{
    unsigned char low;
    unsigned char high;

} __fuse_t;
#endif

Each AVR device header the contains something like:

#define FUSE_MEMORY_SIZE 6
#define FUSE_MEMORY_SIZE 3
#define FUSE_MEMORY_SIZE 2

or whatever.

Last Edited: Mon. Aug 3, 2015 - 09:50 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ok

so we need to use #if, #elif, #else and #endif directives.

there i was going wrong.

 thank you very much

 

 

 and....i am using c.

Long live my MASTER.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

abbas14 wrote:
so we need to use #if, #elif, #else and #endif directives.

 

Not necessarily.

 

Only if you want to control Conditional Compilation.

 

As I said before, macros are just text replacements - so the macros can be used anywhere that their is expansion is valid

 

eg,

 

if( F_CPU == 0 )
{
    printf( "That's not right!!" );
} 

 

or, as clawson showed,

 

typedef struct
{
    unsigned char byte[FUSE_MEMORY_SIZE];
} __fuse_t;

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Aug 4, 2015 - 06:54 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Btw after Andy's last comment be very careful to understand the difference between:

#if F_CPU == 8000000
 delay_ms(50);
#else
 // too slow, no delay
#endif

and

if (F_CPU == 8000000) {
    delay_ms(50);
}

After the C preprocessor has run in each case what the C compiler actually gets to see in each case (for F_CPU = 8000000) is:


 delay_ms(50);


or

if (8000000 == 8000000) {
    delay_ms(50);
}

That is the F_CPU and #if/#else sections are just text substitutions.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note that, when the compiler sees:

 

if (8000000 == 8000000) {
    delay_ms(50);
}

it will almost certainly realise that the 'if' condition never changes and can never be false - so it will actually implement it as just 

    delay_ms(50);

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ok, got ti.

thanks a lot.

Long live my MASTER.