sqrtf function without including math.h library [duplicate] - c

This question already has answers here:
lvalue required as left operand of assignment
(2 answers)
Closed 3 years ago.
I am trying to find length of hypotenuse of a triangle when given base and hight.
i followed recommended method of adding math.h library and get square root by using sqrtf. but i am trying my own logic to get result
#include<stdio.h>
int main(void)
{
int a, b, c;
printf("enter a,b");
scanf("%d%d",&a , &b);
c*c = a*a+b*b;
printf("%d",c);
return 0;
}
i am expecing to value of c but i am getting compilation error.

In C, an assignment expression does not assert that the left operand has the same value as the right operand and leave the computer or software to figure out how to make that true. An assignment expression instructs the computer to store the value of the right operand in the object that is the left operand. So the left operand must designate an object.
To calculate the square root of a value without using library functions, you must write your own code to do the calculations.

Related

Why is the output of the following program showing compile time error? [duplicate]

This question already has answers here:
Why does increment operation ++a++ not work, at least in C?
(3 answers)
Closed 6 months ago.
Why is the output of the following program showing compile time error?
Please explain "lvalue required"
#include<stdio.h>
int main()
{
int a=5;
printf("%d", ++a++);
return 0;
}
A language-lawyer proof explanation is a bit lengthy, here's an attempt at a simplified explanation:
The "l" in "lvalue" comes from "left" as in a value that can appear on the left hand side of an assignment operation, which includes many named variables:
int a;
a = 42; // a is an lvalue and can be assigned to
Now due to operator precedence the expression ++a++ is parsed as ++(a++). And since a++ modifies a "later" but, as expression, evaluates to the current value of a it "returns" a copy of this current value of a, a temporary value.
This temporary value is unnamed (it is not a) and it's not an lvalue, wherefore it can't be assigned to.
You can't write a++ = 42 because you'd be assigning to the temporary value, rather than a variable, and for the same reason you can't write ++a++.
Again, you'll have to dive much deeper, and also give yourself some time to develop an intuitive feeling for what an lvalue is, then this will become much clearer.

warning: right-hand operand of comma expression has no effect [-Wunused-value] in C [duplicate]

This question already has answers here:
warning: right-hand operand of comma has no effect gcc 4.4.7
(3 answers)
warning: left-hand operand of comma expression has no effect
(4 answers)
Two variables in a 'for' loop in C
(3 answers)
Closed 5 years ago.
gcc is throwing warning :
log:-
warning: right-hand operand of comma expression has no effect [-Wunused-value]
for(i=4, data; i
FYI,data is of int type which we are passing to the function which has this forloop
I tried locally to with sample c code which is running fine. Not sure what i need to change in my orig code.
#include <stdio.h>
int main (){
int i;
int data;
for (i=4, data; i<10; ++i){
printf("i valuse is %d", i);
}
I Know "," will evaluate the expression from left to right and it will check last evaluated expression and proceed accordingly.
o/p:-
i valuse is 4
i valuse is 5
i valuse is 6
i valuse is 7
i valuse is 8
i valuse is 9
right-hand operand of comma expression has no effect
The warning is quite clear. The occurrence of the comma operator is in
for (i=10, data; i<10; ++i)
^^^^^^
The variable data is unused, even if you remove from the "expression" part for the for loop. The best way is to completely get rid of that.
It's inclusion is, in fact, potentially very harmful indeed.
data is an expression with value data and because it's the last expression in an outer expression exploiting the expression separator operator, that outer expression has the value data. Strictly speaking its presence means that the behaviour of your program is undefined since it's not initialised and conceptually at least, the compiler is free to generate code that reads the value.
Since it has no useful effect, so your helpful compiler is telling you that, and you should remove it.

What does comma operator in C do when we return an integer with two values? [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 7 years ago.
I was actually returning a float value when I typed , instead of . but it did not give me any error. Then I tried running the below code.
#include<stdio.h>
#include<conio.h>
int getValue();
int main()
{
int a = getValue();
printf("%d", a);
return 0;
}
int getValue()
{
return 2, 3;
}
Now the output is 3, that is it returned the second value. This happened two years ago and was searching for the proper answer since then.
Studying answers to this question I came to know that it returns the second value after evaluating, but what does it do with the first one?
I studied the logic of the stack(how the values are pushed and pop internally) but I don't think this have anything to do with it.
Does it processes the two values or do something else?
Quoting C11 standard, chapter ยง6.5.17, Comma operator
The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.
Now, to answer your question
what does it do with the first one?
we can say, the first operand (left hand operand) is evaluated and the result is discarded.
NOTE: I mentioned the result, not the effect.
Just to clarify, in your case, you won't notice the effect, but you can notice it's effect if both the left and right hand expressions are related to the same variable. For example, let's discuss a simple exmple
return p=3, p+2;
We can break down the return statement like
asign a value 3 to the variable p [left hand side operator of ,]
execute p+2, ehich generates a value of 5. [right hand side operator of ,]
return the value 5 as the value of second argument [following the clause : "the result (of , operator) has its (evaluation of right-operand) type and value."]
See a live demo.
comma operator will evaluate from left to right and return the right most expression or value. This is equivalent to:
int getValue()
{
(void)2;
return 3;
}

Is unary minus equivalent to binop minus? [duplicate]

This question already has answers here:
C: unary minus operator behavior with unsigned operands
(4 answers)
Closed 7 years ago.
My C compiler gave a warning when using unary minus on an unsigned value, so I fixed the warning by doing a subtraction from 0 instead.
Now I wonder if the current code is equivalent to the original one:
uint32_t a, b; // assume b is initialized and non-zero
a = -b % b; // old code
a = (0-b) % b; // current code
My question is: for the same values of b will both lines of code yield the same result for a?
Usually, yes, unless on your platform uint32_t would be a narrow type. Then it would first be promoted to int and the negation would be made in that type.

Confused with pre and post increment operator [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
hello I am learning basics of C programming language, recently i have studied about post and pre increment/decrement operators and also about lvalue and rvalue, the following program shows an error, lvalue required, according to me it should give a value of 6, Can anyone please explain why?
int main(){
int x = 8, y;
y = --x--;
printf("y=%d",y);
return 0;
}
Please explain, why is it so?
Well, let's see what is happening in --x--.
At first, post-decrement executes: --(x--).
(x--) = 7.
After that result of this operation is placed to the original structure:
--7 - doesn't make sense - thus you get lvalue required error
The statement y = --x--; will give you the following error on compilation in C. lvalue required. This is because the post decrement operator -- will return an rvalue after operating on the variable x. So there are no lvalue to perform the pre decrement operator -- afterwards.
But this is one point where C and C++ differs. In C the following statement will also give you the same error lvalue required.
y = (--x)--;
But in C++ the statement y = (--x)--; will compile fine and the value of y is 7. Because unlike C, C++ returns an lvalue after performing the pre decrement operator on variable x.
L Value is left operand of assignment operator which should refer to memory location.As explained by #Pavel your Lvalue is becoming a value not object so you are getting error.
--x means x = x-1 but in your case it is becoming --7 which will be equivalent to 7 =7-1 which is definitely not valid expression.
Other than this more than one operation on same variable without any sequence point in between, results undefined behaviour.
C order of operations specifies that postfix operators have precedence over the prefix operators. The -- postfix operator returns the current value (an rvalue) of the operand and then decrements the operand. Then the prefix decrement operator would be applied...but the decrement/increment operators need lvalue operands since they by definition modify their operands. So, as the compiler says, an lvalue is required.
You should not use it at a time because you will not understand the behavior of compiler. So, you need to guide your code so that they will forced to do what you like.
Now come to your point. If you want to decrease the value by one you can use a-- or --a. They will do the same. If a = 5 and you use b=a-- you will get b = 5 and a = 4 where if you use b=--a you will get b = 4 and a = 4 in --a the value is assigned immediately and in a-- value will be assigned after statement is complete. Hope you are clear.
L value required error shown when it doesn't find any suitable variable where it can be assigned.

Resources