#define macro problem!!

Vishal Sharma

Vishal Sharma

@vishal-pysGmK Oct 23, 2024
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<conio.h>
#include<stdio.h>
int main()
{
char a;
printf("Enter a character= ");
scanf("%c",a);
if(a==hexa)
  printf("hexadecimal");
return 0;   
}

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • [Prototype]

    [Prototype]

    @prototype-G9Gn5k Jul 16, 2012

    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<stdio.h>
     
    #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

    @kenjackson-mBf7HF Jul 16, 2012

    Were you specifically trying to avoid isxdigit()?
    #include <ctype.h>
    ...
        printf("Is %shexadecimal\n", isxdigit(a) ? "" : "NOT ");
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Jul 17, 2012

    KenJackson
    Were you specifically trying to avoid isxdigit()?
    #include <ctype.h>
    ...
        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

    @kenjackson-mBf7HF Jul 17, 2012

    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]

    @prototype-G9Gn5k Jul 17, 2012

    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

    @vishal-pysGmK Jul 18, 2012

    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]]