fflush(stdin) before gets() in c [duplicate] - c

This question already has answers here:
Using fflush(stdin)
(7 answers)
Closed 8 years ago.
Okay , i was solving a problem in code chef (very easy).
It briefly states that :
-A question as a string will be given , and another string has to be produced which does not have any letter used in question string. Uppercase and lowercase are assumed to be same.
-If all alphabets have been used just print ~ sign.
My whole logic is correct except i caught error in my input and this was caused by using fflush(stdin) before gets(). Please explain why was this an error ? Ignore the rest of the code.
Link to problem :http://www.codechef.com/problems/NOLOGIC/
Link to wrong solution :http://www.codechef.com/viewsolution/3881817
Link to corrected solution :http://www.codechef.com/viewsolution/3881827
Pls Note :In corrected code i have only made change in not using fflush(stdin) and using getchar()

Being that the behavior of fflush(stdin) is implementation specific; and being that the implementation (for this question) is unknown, the behavior will also be unknown.
You may safely expect the behavior of fflush(stdin) to be unpredictable.

Related

Do I'm missing something in code? The results are not right [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 1 year ago.
I'm new to programming, trying to learn C.
The program below is showing no signs of error still the output is not correct. I tried it without using '&' sign as "ms" and "sc" are characters but still that doesn't work. What am I doing wrong?
the problem is that you missed enclosing quotation marks in the scanf call
replace the following
line 5: scanf("%c",&ms)
line 7: scanf("%c",&sc)

Discrepancy betwenn scanf and get_int [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 2 years ago.
discrepency
Hi everyone,
Currently working on some Harvard's CS50 problems, I've noticed that substituting get_int by scanf makes my program behave erratically.
The problem at hand here consists in creating a plurality election mockup. And the simple fact that I replace get_int by scanf in order to register the number of votes during the election ends up signalling the first vote as 'invalid' (a function later in the code is charged of checking vote validity).
I've checked multiple times, used the debugger, but to no avail.
Any hints as to why that happens would be deeply appreciated.
scanf("%i", &vote_count) does not consume anything after the number. What comes after the number is most likely a newline character, which is what will be seen by get_string. That probably causes the first name to be an empty string.
You should see all that if you use a debugger.

Difference between scanf and scanf_s in C [duplicate]

This question already has answers here:
Difference between scanf and scanf_s
(3 answers)
Closed 8 years ago.
Although my program gives the required output. There are many warnings showing scanf() shouldn't be used and try using scanf_s() instead.
Possible cause of this warning??
This is happening to all the programs which are using the scanf function. Even simple addition of numbers.
With scanf, with some format arguments it is possible to crash your program if you use unlimited size inputs. scanf_s requires to provide size of output buffers, thus limiting possibility of buffer overflows (provided you specify output buffer sizes correctly).

C Programming Increment and Decrement Operator Problem [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
$void main()
{
int a=10,c;
c= ++a + ++a;
printf("%d",c);
}
this program Actualy Print Value Of c=24 but By Calculation we can say it should be
c=23 ,how it possible?
Your program has a bug -- you modify the same variable twice without an intervening sequence point. Fix the bug and the mystery will go away.
A very deep understanding of not just how the language works but how compilers work is required to understand why buggy code happens to do what it happens to do. I would just suggest not writing buggy code and, when you find a bug, simply fix it instead of trying to understand precisely why and how it broke.
My advice to you is to stop. You learned the right lesson -- code that triggers undefined behavior is unpredictable and frequently doesn't do what you might expect it to do. That's all you need to know about UB until you're an expert at using the language correctly.
'++' > '+'
Here post increment operation is done before.Since you gave it two times if does post increment two times so the value of 'a' becomes 12 and adds it up (12+12).So the final value is 24.

What is the correct output of this statement? [duplicate]

This question already has answers here:
Turbo C++: Why does printf print expected values, when no variables are passed to it?
(5 answers)
Closed 9 years ago.
int a=9,b=6,c=3;
printf("%d%d%d");
I executed this in code blocks 10.05. I got some garbage values. But in a website the output was given as 3 6 9. What is the correct one?
You will get garbage values, because you're not providing any arguments to the printf() call.
The correct code would be
printf("%d%d%d",c,b,a);
(to get the numbers in the order quoted)
The correct one is neither of the two you described. Since no values were passed to printf, only the formatters, whatever was on the stack at that moment (which is undefined) is passed.
What is the correct output of this statement?
This code invokes undefined behaviour and so there is no correct output. The output is undefined.
The code invokes undefined behaviour because the format string you passed to printf requires you to pass more parameters (3) than you supplied (0).

Resources