Why variable b always shows 1? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
22nd Jun 2021, 5:24 AM
Lucifer Samael Morningstar
18 Answers
+ 5
Lucifer Samael Morningstar because you assign to b the return value of scanf() function, wich occurs after assigning input to b ^^
22nd Jun 2021, 5:32 AM
visph
visph - avatar
+ 4
Ipang garbage value? are you sure to have provided 2 inputs?
22nd Jun 2021, 8:43 AM
visph
visph - avatar
+ 4
Visph, Looks like something was left behind in the input stream after reading string input <a> using fgets(). As you said, it happens when input for <a> was longer than 4 characters. And that leftover data somehow, was accidentally got assigned into <b>. Input by scanf() works fine if we remove the leftover character as scanf() reads value for <b> in line 8. It appears that this trick only works when input for <a> is 5 characters in length, it won't help when input length for <a> exceeds 5 characters. Line 8: scanf( "%*c%ld", &b ); (Edit) Variable <b> still gets garbage when length of input for <a> exceeds 5 characters, even if we remove the leftover character in the input stream buffer.
22nd Jun 2021, 9:25 AM
Ipang
+ 3
Lucifer Samael Morningstar %*c is used to skip exactly one char... but it only works fine if there remain zero or one char in the input buffer, as the final \n if still in input buffer is automatically skipped ;) Ipang the clean solution is a few verbose: testing if \n is in first input... if not, then get char until \n encountered... before taking second output: int i; for (i=0; i<5 && a[i]!='\n'; ++i); if (i<5 && a[i]!='\n') while (getchar()!='\n');
22nd Jun 2021, 12:56 PM
visph
visph - avatar
+ 3
Ipang problem is if user enter more than 4 (with your actual code) or 5 (with its solution) character, then second input is not the one expected... because not all input line has been consumed when taking second input... input buffer is the memory place where is stored the char typed by user... (or in sololearn context the memory place where is stored the string got before sending the script to server ^^)
22nd Jun 2021, 1:18 PM
visph
visph - avatar
+ 1
Ipang yes, he did ^^
22nd Jun 2021, 8:37 AM
visph
visph - avatar
+ 1
Visph, Yes of course, first input string "abcde" and second input is whole number (randomly chosen). I only tested in Code Playground though, haven't checked elsewhere ...
22nd Jun 2021, 8:45 AM
Ipang
+ 1
Ipang oh, I've only tested with one char as first input... then b show correct value ^^ in fact, as char array of length 5 is used, you get garbage value for b if you provide string of length greater than 4 as first input ;)
22nd Jun 2021, 8:52 AM
visph
visph - avatar
+ 1
Ipang Can you add few i/o examples please.. Variable b takes long intigers.. why you're using format specifier '%*c' isn't this for characters?
22nd Jun 2021, 12:09 PM
Lucifer Samael Morningstar
0
thanks visph
22nd Jun 2021, 5:36 AM
Lucifer Samael Morningstar
0
visph I have one more question
22nd Jun 2021, 5:37 AM
Lucifer Samael Morningstar
22nd Jun 2021, 5:38 AM
Lucifer Samael Morningstar
0
Lucifer, Did you change the code from its original state when you posted this question?
22nd Jun 2021, 8:36 AM
Ipang
0
Visph, Thanks, just asking cause it looks irrelevant with original question. It outputs what seems to be garbage value for variable <b>, not value of 1.
22nd Jun 2021, 8:40 AM
Ipang
0
visph what's input buffer? Though second part of the reply was to Ipang, I want to know what is Ipang's problem?
22nd Jun 2021, 1:12 PM
Lucifer Samael Morningstar
0
visph Oh thanks and what is steam buffer?
22nd Jun 2021, 1:22 PM
Lucifer Samael Morningstar
0
Lucifer Samael Morningstar :D don't know what's "steam" buffer, but "stream" buffer is the memory place where is stored part of a data stream (could be string, as is input buffer) ;)
22nd Jun 2021, 1:26 PM
visph
visph - avatar