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 .......???
10 Respuestas
+ 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
+ 4
im wondering if the problem might be the scanf("%d,%d",&a,&b); part becuase the rest looks fine?
+ 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
+ 2
Read this thread also for undestand better like scanf work https://www.sololearn.com/Discuss/1550114/
+ 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
+ 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
+ 1
KrOW thanks man ...i got it
i appreciate the effort you put in to make me understand
0
KrOW please explain why my code allows me to enter two values but gives a incorrect comparison?
0
Jishnu I try (if i can) to answer in detailed way when someone WANT understand really how work something 😉. Any, you are welcome 👍👍👍
0
@Jishnu when is the time when i need to insert comma in the scanf format.
why should i insert comma