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

+2

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. https://code.sololearn.com/cg4d04gshHKW/?ref=app

5/22/2020 5:24:48 AM

Onur

5 Answers

New Answer

+2

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

+3

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

+2

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) https://code.sololearn.com/cY72EPxUquwz/?ref=app

+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...

0

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