+ 1

Anyone please tell me the behaviour of this code in C

Especially about scanf() and how it works in this code.... And please help me to understand this second code, which wasn't created by me..... It was created by LastSecond959 https://code.sololearn.com/cD4LRd7155uD/?ref=app https://code.sololearn.com/cqI6avcD27f9/?ref=app

1st Dec 2020, 2:55 PM
Yogeshwaran P
Yogeshwaran P - avatar
13 ответов
+ 5
In line scanf("%dss%d",&a,&b); You are saying to system that first read an integer then 2 's' then another integer. But store only first and second Integer.
1st Dec 2020, 3:11 PM
Arsenic
Arsenic - avatar
+ 5
Yogeshwaran just convert translate it to english. if (h==12 && (time=='A' || time=='a')) h=0; If "h" is equal to 12 AND "time" is either 'A' or 'a' then convert "h" to 0
1st Dec 2020, 3:21 PM
Arsenic
Arsenic - avatar
+ 4
Thank you Davide ,Zatch bell and Arsenic for clearing my doubts...😊
1st Dec 2020, 4:02 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 3
It's how scanf work: http://www.cplusplus.com/reference/cstdio/scanf/ Non-whitespace character, except format specifier (%): Any character that is not either a whitespace character (blank, newline or tab) or part of a format specifier (which begin with a % character) causes the function to read the next character from the stream, compare it to this non-whitespace character and if it matches, it is discarded and the function continues with the next character of format. If the character does not match, the function fails, returning and leaving subsequent characters of the stream unread.
1st Dec 2020, 3:12 PM
Davide
Davide - avatar
+ 3
Thank you Davide and Arsenic 😃👍 Can you please help to understand the second code in my question.... Especially about the IF statement condition process in that code.
1st Dec 2020, 3:18 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 3
Yeah as Arsenic said👆 The parenthesis are there because && have an higher precedence than ||, so without them it would be like if ((h==12 && time=='A') || time=='a') Which means if h is 12 and time is A, or if time is a Anyway in the challenge they give AM and PM uppercase, there's no need for the lowercase condition
1st Dec 2020, 3:27 PM
Davide
Davide - avatar
+ 3
Davide and Arsenic thank you very much....😃 Can you please explain about printf() in that second code 😅 printf("%02d:%02d\n", h, m); what is use of "02" here?
1st Dec 2020, 3:32 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 3
The condition was : if (h==12 && (time=='A' || time=='a')) h=0; else if (h!=12 && (time=='P' || time=='p')) h+=12; so if you give input 1:30 AM both of the condition become false .So it just print the input 01:30 .(here printf("%02d") means it will give atleast 2digit output).
1st Dec 2020, 3:36 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 3
Welcome 🤗
1st Dec 2020, 4:04 PM
Davide
Davide - avatar
+ 2
Arsenic what happens if I input 1:30 AM as a input...[here h != 12 && time == 'A'] Can you please explain related to this input?
1st Dec 2020, 3:26 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 2
😃
1st Dec 2020, 4:06 PM
Yogeshwaran P
Yogeshwaran P - avatar
0
Basically scanf takes value from the user.in the second program as you can see its written in a single scanf it's used to take many inputs without typing scanf too many of times
3rd Dec 2020, 5:32 AM
✨SARANG✨
✨SARANG✨ - avatar