Understanding Scanf - dealing with formatted input

I'm trying to figure out exactly how `scanf` works. If I code something like this: scanf("%s %d %f", name, &age, &wage); enter this as standard input: james 20 34000.25 and then print out name, age, and wage respectively, I get exactly what one would expect, the same thing as I put into standard input. However, if I enter something like this: scanf("%s/%d/%f", name, &age, &wage); and I enter this as standard input: james/20/34000.25 I get the string `james/20/34000.25` for the string value and 0 and 0.00000 respectively for the integer and float values. I thought `scanf` would treat the slashes the same as it would treat the spaces in the first version. How would I get it so if a user enters a slash separated value, I can appropriately assign the values to variables?
There's something crucial missing in every answer I ever see about scanf. How do you tell whether or not the %s, %d, %f, %[^/] or `%[^ /] format specifications were successfully matched? What happens if the user simply presses CTRL+Z in Windows, or CTRL+d in Linux to close stdin? scanf returns and your program is happy to use garbage values? What does scanf return? Scanf should do this! Why does it do that, instead? Because the manual said so. Have you read the manual?

以上就是Understanding Scanf - dealing with formatted input的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » CSS3 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准