i am trying to write a program such that if a % 2^j = 0 then it prints 1, else it prints 0 so at the end i get a 16 bit binary code. But i get no error and after i enter an input number (a) terminal crashes. Thank you for your help.
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
uint16_t a, j, b, mod;
printf(" Please insert a number between 0 and 65535 \n ");
scanf("%d", &a);
j = 16;
while (j > 0)
;
{
b = pow(j, 2);
mod = a % b;
if (mod == 0) {
printf("%d", 1);
} else {
printf("%d", 0);
}
j = j - 1;
}
return 0;
}
You have undefined behavior as you use the %d
format for scanf
. This format specifier expects the argument to be a pointer to int
.
Mismatching format specifier and argument type leads to UB.
For uint16_t
use the macro SCNu16
(as documented in eg this reference ):
scanf("%" SCNu16, &a);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.