Hi

I have the following code which is giving an error while trying to use scanf with it. Please help resolve

uint16_t x[3]={1,2,3};
uint16_t y;
printf(" %"PRIu16",y); (works)

printf("Enter a value for y: ");
scanf("%"PRIu16, &y);

warning: format '%u' expects argument of type 'unsigned int *', but argument 2 has type 'uint16_t *' [-Wformat]

try no "&" in the scanf. just a guess.

commented: Don't make suggestions if don't know what you're talking about. -1

Whereas printf will sometimes handle upgrading variables to the appropriate type, scanf is not so nice. y is a 16bit value, but you are scanning for a 32bit value - it doesn't fit so you will lose bits. In printf, the 16bit variable can be promoted to a 32bit value without losing anything.

scanf("%"PRIu16, &y);

The PRI* macros are for printf. Use the SCN* macros for scanf, they're not interchangeable:

scanf("%"SCNu16, &y);
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.