# include <stdio.h>
# include <conio.h>
#define squ(x) x*x
main( )
float s=10,u=30,t=2,a;
printf(“result :%f”,a);

output is -100.000 how is it possible??

Votes + Comments
Use the frikkin' code tags!!!!!
7 Years
Discussion Span
Last Post by MosaicFuneral

28 Posts, and no code tags??

Information about code-tag is posted all over the website :
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) any place CODE tags were used
6) Even on the background of the box you actually typed your message in.

BTW, Where in your code are you making use of the conio library? If there's no need to use it, then don't use it, it's even not portable as well.

>output is -100.000 how is it possible??

float s=10,u=30,t=2,a;

In your code: u multiplied by t is greater than s, so if you subtract u*t from s then it's logical that the result is a negative number right?
(this is strictly according to the rules of algebra)


It helps to know what you expected. I'll guess that you wanted -25.0. Here's why you get -100.0. a=2*(s-u*t)/squ(t); uses the squ macro, and squ replaces itself with the macro text x*x with x replaced by the argument t. Your statement looks like this after the replacement:


* and / have the same precedence and are evaluated left to right. If you wanted -25.0 as a result, x*x has to be surrounded by parentheses:


That's why it's a good habit to surround your macro text expressions with parentheses. And for the same reason, the macro parameter:

#define squ(x) ((x)*(x))

You should be very careful with macros like this because if you pass an expression with side effects as the argument, the side effect will be evaluated twice:

int b = 1;

printf("%d\n", squ(b++)); /* squ(b++) replaced with ((b++)*(b++)) */
printf(“result :%f”,a);

If you don't print a '\n' character somewhere, the output might look weird.

Votes + Comments
I agree with you :)
nice explanation

conio.h isn't used, and should never be.
Macros used like that are dangerous.
It's: int main() { return(0); }

Votes + Comments
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.