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

output is -100.000 how is it possible??

Salem commented: Use the frikkin' code tags!!!!! -7

Recommended Answers

All 3 Replies

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;
a=2*(s[B]-u*t[/B])/squ(t);

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:

a=2*(s-u*t)/t*t;

* 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:

a=2*(s-u*t)/(t*t);

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.

commented: nice explanation +11
commented: I agree with you :) +10

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

commented: word. +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.