Hi,

I'm trying to get two small programs that use macro to work but neither of them is doing so

the first macro selects the least significant bit from an unsigned char

#include <stdio.h>
#define LBIT(X) (((X)&1)?1:0)
int main()
{
  unsigned char a;
  printf("Enter a character:\n");
  scanf("&c",a);
  printf("Least signficant bit is %d\n",LBIT(a));
  return 0;
}

I'm trying to say if x and 1 is 1 then the bit is equal to 1 otherwise it is 0, but the code is returning 1 for any character i try

the second macro selects the nth least significant bit from an unsigned char

#include <stdio.h>
#include <stdlib.h>
#define NBIT(X,N) (((X&(1<<N))?1:0)
int main()
{
  unsigned char a;
  unsigned int number;

    printf("Enter an integer and a bit number : ");
    scanf("%uc %d",&a,&number);

   printf("%uc has bit %ud in position %ud\n",a,NBIT(a,number),number);

    return 0;
}

this code is not even compiling

Can anyone please help me =)?
Thanks

The first macro issue is a double problem within the scanf statement. The line should be replaced with:

scanf("%c",&a);

For your second macro, the code is not compiling because of an extra parenthesis in the macro itself. It should look like:

#define NBIT(X,N) ((X&(1<<N))?1:0)

On an other hand, the scanf and printf statement should be:

scanf("%c %d",&a,&number);

   printf("%x has bit %d in position %d\n",a, NBIT(a,number), number);

With those minor changes, your code works perfect !!

this code is not even compiling

Can anyone please help me =)?
Thanks

You have a bracket mismatch in this line: #define NBIT(X,N) (((X&(1<<N))?1:0) there is 1 opening bracket to many. so changing it to : #define NBIT(X,N) ((X&(1<<N))?1:0) should solve the problem.

then remove all the unsigned crap. There is no need to make everything unsigned, as long as you verify the input given by the user.

And last: it would be a good idea to avoid scanf


Niek

[edit] too slow , LouPascalou beat me to it ;) [/edit]

Thank you =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.