0

Didn't understand when should I use *

Hey! Can someone please explain to me why would I ever use *? In the example code they don't assign a variable to the %*f so I added it myself (both to the scanf and printf) but its still doesn't print it at the end instead my output is a bunch of numbers My input is: 12 3 4.5 abcde https://code.sololearn.com/cyfKLMJoBPOO/?ref=app

22nd Aug 2019, 4:17 PM
Ema Il
3 Answers
+ 2
Quoted from http://www.cplusplus.com/reference/cstdio/scanf/ "An optional starting asterisk indicates that the data is to be read from the stream, but ignored (i.e. it is not stored in the location pointed by an argument." You used %*f specifier which means the 3rd input (float number) will be read from the stream, but its value will not be stored into variable <z>. I got totally different outputs during tests, seems to me that the first 4 characters (size of float) of "elephant" that was supposedly for <text> was rather read and stored as float number into <z>. 12 34 5.7 elephant ↓ ↓ ↓ ↓ <x> <y> <skipped> <z> Hth, cmiiw
22nd Aug 2019, 5:27 PM
Ipang
+ 1
Thanks! So why would I use it? If I can't use the value then what good is it
22nd Aug 2019, 6:18 PM
Ema Il
+ 1
In this snippet we are reading coordinates in "x,y" format, the x and y are integer, but we want to ignore the comma in between those x & y values, so we read it, but we don't use it, because we only want the x & y. #include <stdio.h> int main() { char input[23]; int x, y; puts("Enter coordinates:\nEnter 0, 0 to quit.\n"); while (1) { scanf ("%d%*c%d", &x, &y); if (!x && !y) break; printf ("Coordinate X:%d Y:%d\n\n", x, y); } return 0; }
22nd Aug 2019, 7:43 PM
Ipang