#define macro problem!!

How to define multiple characters using macros??
I want to declare all the hexadecimal digits under a name "hexa". Does anyone know it how to do that??

The below code is wrong! Any suggestions to make it correct??
#define hexa 'A','B','C','D','E','F','0','1','2','3','4','5','6','7','8','9'
#include
#include
int main()
{
char a;
printf("Enter a character= ");
scanf("%c",a);
if(a==hexa)
  printf("hexadecimal");
return 0;   
}

Replies

  • [Prototype]
    [Prototype]
    During compilation, all the macro occurrences in the code are BLINDLY replaced by their definition. So after compilation, your code will look like

    if(a=='A','B','C','D','E','F','0','1','2','3','4','5','6','7','8','9')
    
    which is certainly incorrect as you can see.

    One solution is like shown below

    #include
     
    #define HEXA (int)ARR[t]
     
    int main()
    {
        char a, t;
        char ARR[16]={'A','B','C','D','E','F','0','1','2','3','4','5','6','7','8','9'};
     
        printf("Enter: ");
        scanf("%c", &a);
     
        t = a>=65?(a-65):(a-48)+6;
     
      if((int)a == HEXA)
        puts("\nYes, its Hex");
      else
        puts("\nNah, not a hex");
     
        return 0;
    }
     
    
    Here, I've just made an array containing all the Hex values & created a macro HEXA with definition ARR[t].

    Now,

    t = a>=65?(a-65):(a-48)+6;
    
    Since our array's have indexes starting from 0 - 15, having A in 0th position, B in 1st position and so on, we need to compute proper reference for these location based on the user input.
    What I just did is, converted the character to their equivalent ASCII. 65 is the ASCII of character 'A'.

    Hence using the above logic, if the user enters A as input, the condition a>=65 will be true, and a-65 = 0, so t=0 and hence ARR[t] = ARR[0] = 'A' and our if condition will satisfy printing the confirmation.

    This is not the best way to do it, but since I've noticed that you're trying to perform the things with macro, I've taken the example using macro.

    If you've any confusion, feel free to ask or PM me.

    Regards.
  • KenJackson
    KenJackson
    Were you specifically trying to avoid isxdigit()?
    #include 
    ...
        printf("Is %shexadecimal\n", isxdigit(a) ? "" : "NOT ");
  • Vishal Sharma
    Vishal Sharma
    KenJackson
    Were you specifically trying to avoid isxdigit()?
    #include 
    ...
        printf("Is %shexadecimal\n", isxdigit(a) ? "" : "NOT ");
    yeah!!
    Actually i didn't wanted to use an array for that. So tried to declare as a macro, but as #-Link-Snipped-#
    said that its incorrect.
    do you have any other way to do that??
  • KenJackson
    KenJackson
    The isxdigit() function or macro already does what you are trying to do. For example:
    isxdigit('a') is true
    isxdigit('X') is false

    I asked if you were trying to avoid using it. If you're working on an assignment to implement it, then obviously you can't use it. Otherwise, it's a lot simpler to just use what's already implemented, tested and readily available.
  • [Prototype]
    [Prototype]
    Vishal0203
    yeah!!
    Actually i didn't wanted to use an array for that. So tried to declare as a macro, but as #-Link-Snipped-#
    said that its incorrect.
    do you have any other way to do that??
    Well as I said, the one I've proposed is not the best one. @#-Link-Snipped-# suggested the easier way to do it. If your motive is just to verify hex, then this is the way you should do it, otherwise if you've to do it without library (for sake of assignment) then you can adopt my method.

    May I ask, what's the exact reason you're trying to avoid use of array?

    Besides, the syntax of scanf in your original code is incorrect. You always pass the address of the variable & not the variable itself.#-Link-Snipped-#
  • Vishal Sharma
    Vishal Sharma
    NAaaah!! I'm not asking all this to do my assignment or project given in college. I do my assignments on my own 😉
    I asked this, just to know all the possible ways!
    Well, I got a lott of info from you guys! 😀 Thanks for that! #-Link-Snipped-#
    @[Prototype]]

You are reading an archived discussion.

Related Posts

Hi guys, can anyone give any information on organizations that conduct workshops in colleges on any topic of interest for EE and ECE. Thnx..
Yahoo! had announced Marissa Mayer (a long term Google associate) as new CEO of Yahoo! This was a shell shocking decision to appoint her as CEO of the company when...
Microsoft announced the new release of the world's most popular software: Microsoft office 2013, may be named as THE NEW MICROSOFT OFFICE (may be stolen from NEW IPAD) https://www.microsoft.com/office/preview/en With...
One of the World's most popular online video chat company Skype had identified the issue and working on the fix that's real tough. Skype expressed apology to all the users...
package rotatinganimation; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import javax.swing.*; import java.awt.Graphics2D; import java.awt.geom.*; public class Demo { Thread t1; int degree=8; public Demo() { JFrame frame=new JFrame("Tara Animation"); frame.setSize(500,500);...