Search code examples
cunsigned-integer

Unsigned integer notation in C


I have written some code but I keep getting this error and I am unsure on how to fix this issue.

hw6.c:14:13: warning: data argument not used by format string [-Wformat-extra-args]
        scanf("&u",n);
              ~~~~ ^

This is my code below:

#include <stdio.h>

unsigned int reverse_bits(unsigned int n);

int main(void) {
    unsigned int n;
    printf("Enter an unsigned integer: ");
    scanf("&u",&n);
    printf("%u",reverse_bits(n));
    return 0;
}

unsigned int reverse_bits(unsigned int n) {
    unsigned int reverse = 0;
    while(n>0) {
        reverse <<= 1;
        if((n & 1) == 1) {
            reverse = reverse^1;
        }
    }
    return reverse;
}

Thank you!


Solution

  • Scanf uses % for its format specifiers, so the correct way would be

    scanf("%u",&n);