What happens when i use , (comma) here between %ds in => scanf("%d,%d",&a,&b); | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

What happens when i use , (comma) here between %ds in => scanf("%d,%d",&a,&b);

int a,b; scanf("%d,%d",&a,&b); c=(a>b)?a:b; printf("%d",c); input:67 68 output:67 it should have been 68 .......???

21st Oct 2018, 6:46 AM
Jishnu
Jishnu - avatar
10 Respostas
+ 1
If in format string is present a not-whitespace char (space, newline, tab etc) that its not format specifier (like %d, %%, %c etc) then scanf read one char and compare with the char in format string and: - if they match, then scanf continue with reading - if they dont match, then scanf will end WITHOUT reading next tokens In your case, then, you have to insert a comma JUST after the first int. All following inputs are valids: 67, 68 67, 68 67, 68
21st Oct 2018, 9:32 AM
KrOW
KrOW - avatar
+ 4
im wondering if the problem might be the scanf("%d,%d",&a,&b); part becuase the rest looks fine?
21st Oct 2018, 7:08 AM
D_Stark
D_Stark - avatar
+ 3
A few test runs on Playground showed that a comma is necessary immediately after the first input, before the second input can be accepted e.g. 67,68. The numbers are not captured well if they are separated by other character but a comma, I have also tested with 67, 68 and 67 ,68 (notice the different position of space), the resulting output differs between runs. So I guess with that specifier we need to enter the input separated by comma, but without spaces. Hth, cmiiw
21st Oct 2018, 8:56 AM
Ipang
+ 2
Read this thread also for undestand better like scanf work https://www.sololearn.com/Discuss/1550114/
21st Oct 2018, 11:23 AM
KrOW
KrOW - avatar
+ 1
if there is an unknown character between, it'll only take the first input, or whatever before the unknown character so in this case you're comparing 67 and uninitialzed variable which have unknown value
21st Oct 2018, 7:00 AM
Taste
Taste - avatar
+ 1
First, you have to think that the input is ONE string (given from standard input usually keyboard). Its scanf work parse this string and assign values to your vars and it parse this input string following string format that you pass like first parameter. Said this, lets go to see what happen with your code at scanf call: 1) Just before scanf start to parse it, input is "67 68" 2) scanf get the first token from format string (%d) then it TRY to read an int. It read "67" from input, convert in int and put it in a var. 3) Now input is " 68". scanf parse next directive for reading, and encounter a comma. Because what i said in previous comment, it TRY to read next char from input (that i remember you now is " 68") and compare with comma char. Because readed char is a space, and it dont match with comma, scanf ends own processing and return to you the number of value correctly readed (1 because it have readed only the first int). 4) At this point (after scanf execution), because scanf not processed all input, input is "68" (then you can use another scanf call and read it but this is logically BAD) 5) After scanf, you compare two value BUT one (the "a" var) is inited (by scanf) and the other ("b" var ) not inited (because scanf havent inited it) then you comparison depends from compiler because some compilers init automatically uninitialized var to 0 (at bytes level) and other not (in this case your uninited var will contain so called garbage value that you can think like a random value). Probably, your compiler init to 0, then comparing 67 with 0 for greatness, c will be always 67 on your compiler
21st Oct 2018, 11:12 AM
KrOW
KrOW - avatar
+ 1
KrOW thanks man ...i got it i appreciate the effort you put in to make me understand
21st Oct 2018, 12:17 PM
Jishnu
Jishnu - avatar
0
KrOW please explain why my code allows me to enter two values but gives a incorrect comparison?
21st Oct 2018, 10:07 AM
Jishnu
Jishnu - avatar
0
Jishnu I try (if i can) to answer in detailed way when someone WANT understand really how work something šŸ˜‰. Any, you are welcome šŸ‘šŸ‘šŸ‘
21st Oct 2018, 12:39 PM
KrOW
KrOW - avatar
0
@Jishnu when is the time when i need to insert comma in the scanf format. why should i insert comma
12th Mar 2020, 12:20 PM
Saumyojit Das
Saumyojit Das - avatar