0
#include<stdio.h>

int main()
{
    unsigned char a,b;
   
    printf("Enter first number : ");
    scanf("%d",&a);
    printf("Enter 2nd number : ");

    scanf("%d",&b);
    printf("a&b = %d\n", a&b);

    printf("a|b = %d\n", a|b); 

    printf("a^b = %d\n", a^b);
    
    printf("~a = %d\n",a = ~a);
    printf("b<<1 = %d\n", b<<1); 

    printf("b>>1 = %d\n", b>>1); 

    return 0;
}

i am taking input from user but i am getting wrong output how i modify i***

error


5
  • %d is not the correct specifier for the variable you are passing to scanf, using the wrong specifier in scanf leads to undefined behavior. Commented Oct 5, 2022 at 15:13
  • There is nothing in your code that can produce the output you claim it does. Commented Oct 5, 2022 at 15:18
  • You also forgot to mention what the correct output is. Commented Oct 5, 2022 at 15:19
  • This printf("~a = %d\n", a = ~a); is also strange, why not printf("~a = %d\n", ~a);? Commented Oct 5, 2022 at 15:28
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Oct 5, 2022 at 18:14

1 Answer 1

0

These calls of scanf

scanf("%d",&a);

and

scanf("%d",&b);

use an incorrect format specification.

The variables a and b are declared as having the type unsigned char

unsigned char a,b;

If you want to enter integers in these variables you need to write

scanf("%hhu",&a);

and

scanf("%hhu",&b);

So your program will look like

#include <stdio.h>

int main( void )
{
    unsigned char a, b;

    printf( "Enter first number : " );
    scanf( "%hhu", &a );

    printf( "Enter 2nd number : " );
    scanf( "%hhu", &b );

    printf( "a&b = %d\n", a & b );

    printf( "a|b = %d\n", a | b );

    printf( "a^b = %d\n", a ^ b );

    printf( "~a = %d\n", a = ~a );

    printf( "b<<1 = %d\n", b << 1 );

    printf( "b>>1 = %d\n", b >> 1 );
}

The program output might look like

Enter first number : 10
Enter 2nd number : 15
a&b = 10
a|b = 15
a^b = 5
~a = 245
b<<1 = 30
b>>1 = 7
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.