Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
perc=(sum/total)*100;
i've been trying to put this in the code in cprogramming,but output for this part is showing 0,,,Why is this happening and what else should I do in these types of scenerios?
Since you didnt post source and this question will be closed soon, the cause is from integer division most likely. Try this instead.
float perc = ((float)sum / (float)total) * 100.0;
Heres a post on integer division if you are interested in learning why this behavior is doing what it did:
What is the behavior of integer division?
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I am learning C right now and I understand that I can't add an integer with a decimal like so:
#include <stdio.h>
int main() {
printf("%d",15+9.0);
return 0;
However when running this I expected some sort of an error. Instead, I got a weird output:
-1866308488
Can someone help me understand why it gave me this output?
As #PaulMcKenzie said, C expected an int based on the %d format specifier. You gave it a double instead. C often gives unexpected behavior instead of throwing an error like Java or C#. What happens to a double variable when %d is used in a printf? says that the resulting behavior is undefined and OS-specific.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
can I use post-increment in a function return in C like this?
int meta_solve() {
//some codes
return metaData[head++]; //head is global variable
}
I asking this question because it showing the different results on windows and mac. thanks for your attention. have a great day!
Yes, that will work.
The return will not happen until the expression metaData[head++] is fully evaluated so the (global) variable head is incremented before the function returns.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have experienced a strange problem when using assert in my program.
The program does not terminate even when I add a line of codeassert(false).
But the assert works when I write several lines of sample code. Anybody know why it happened?
If you have:
#define NDEBUG
this turns all assert's into nop's.
If you have differing behaviour, depending on the amount of code, then I guess you don't have NDEBUG defined and I would guess the compiler is simply compiling out the redundant code.
More details about environment are required, however, you give a definitive answer.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
This code written in c objective with Visual Studio
Today a friend of mine sent this code. In university they tried to make a grade calculation program. The problem is when you write -0 to input and press enter it gives result as the last if statement regardless the if statement is.
Same conclusion appears when you write +0 etc.
Why this is happening any ideas? Thanks in advance.
The concept of negative zero doesn't exist in the C language (and all other languages I'm aware of).
So if you enter 0, the expression grade == -0 will be true and therefore your program will display Zero can not be negative.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
The Question goes this way :
Add two numbers and if the addition of two numbers gives the result in single digit print YES and if the result of the two numbers is in double digit print NO
As an idea. It is enough to check whether the result of the addition is less than 10 and greater than -10 if the numbers are signed.:)