This is a program used for swapping the nibbles of a byte, which is perfect for a byte but I faced a problem.
Code:
#include<stdio.h>
void main()
{
unsigned char a = 0;
scanf("%d", &a);
a = ((a << 4) | (a >> 4));
printf("the value of a is %d\n\r", a);
}
You can see in the scanf
statement that I've received it as %d
instead of receiving it as %c
which is for a char
. The above code works perfectly. But if I replace %d
with %c
, I am getting a different undesired answer
Why?