Using scanf() with multiple inputs | SoloLearn: Learn to code for FREE!


Using scanf() with multiple inputs

Hi everyone, I'm trying to program a 12/24 hours converter. However I can't read multiple inputs using scanf. The input should be in one line like 12:25 PM. Can get 12 and PM but can't get 25. Googled about scanf but couldn't find what's the problem is.

5/22/2020 5:24:48 AM


5 Answers

New Answer


diego Code, it's very enlightening :) Thank you.


char ap[3]; // pm + \0 needs 3 chars :)


Look at the memory address where the variables are stored; the ap[2] overwrite m (Because you did't reserve memory for 3 elements... Just 2)


This is why scanf("%s", ap); is unsafe... Even if you declare char ap[3]; If someone enters "amm" variable m gets overwited... If you use scanf("%2s". ap); only 2 chars sre stored... Even if someone inputs more...


diego Code omg you are right... Thanks. But how this affected the second input not the third?