Is unary minus equivalent to binop minus? [duplicate] - c

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.

Related

sqrtf function without including math.h library [duplicate]

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.

Why did my float get truncated? [duplicate]

This question already has answers here:
C integer division and floor
(4 answers)
Closed 7 years ago.
#include <stdio.h>
int main(void)
{
float c =8/5;
printf("The Result: %f", c);
return 0;
}
The answer is 1.000000. Why isn't it 1.600000?
C is interpreting your 8/5 input as integers. With integers, C truncates it down to 1.
Change your code to 8.0/5.0. That way it knows you're working with real numbers, and it will store the result you're looking for.
The expression
8/5
is an all int expression. So, it evaluates to (int )1
The automatic conversion to float happens in the assignment.
If you convert to float before the divide, you will get the answer you seek:
(float )8/5
or just
8.0/5
When you don't specify what data types you use (for example, in your code you use the integer constants 8 and 5) C uses the smallest reasonable type. In your case, it assigned 8 and 5 the integer type, and because both operands to the division expression were integers, C produced an integer result. Integers don't have decimal points or fractional parts, so C truncates the result. This throws away the remainder of the division operation leaving you with 1 instead of 1.6.
Notice this happens even though you store the result in a float. This is because the expression is evaluated using integer types, then the result is stored as is.
There are at least two ways to fix this:
Cast the part of the expression to a double type or other type that can store fractional parts:
Here 8 is cast to the double type, so C will perform float division with the operands. Note that if you cast (8 / 5), integer division will be performed before the cast.
foo = (double) 8 / 5
Use a double as one of the operands:
foo = 8.0/5

sizeof operator giving output false while using in this way [duplicate]

This question already has answers here:
sizeof() operator in if-statement
(5 answers)
Why is this happening with the sizeof operator when comparing with a negative number? [duplicate]
(2 answers)
Closed 8 years ago.
I am not able to understand why this piece of code is giving output False:
if (sizeof(int) > -1)
printf("True");
else
printf("False");
As I tried to print what sizeof(int) is returning is 4.
The result of the sizeof operator has type size_t. Your -1 is a signed int. When the two are compared, the latter is converted to size_t, which results in a rather large unsigned value.
By standard sizeof returns an unsigned integer type size_t. Although the exact type is implementation defined it is certain to be unsigned. When you try to compare it to the signed integer -1, -1 gets converted to max value of this type(try writing (unsigned)-1 and examine the value) and thus the comparison is false.

Is the assignment expression after "sizeof" never executed? [duplicate]

This question already has answers here:
Why does sizeof(x++) not increment x?
(10 answers)
Closed 9 years ago.
e.g.
int a = 3;
int b = sizeof(++a);
int c = a;
Is c equal to 3 or 4 as the result?
Does the result depend on the specific compiler?
The specification states that the increment operator does NOT take affect when used within a sizeof operator.
This also makes sense from an abstract viewpoint. Specifically, the sizeof operator returns the number of bytes used by an object. While incrementing an integer or even a pointer does NOT change the size of that integer, the ++ operator would mis-lead a new programmer into thinking the size does change.
If interested look-up the topic "side-effects" for more discussion about this subject.

Why is "int sum=ch1+ch2+ch2" not giving overflow when right-side operands are character variables & result >255? [duplicate]

This question already has answers here:
Addition of two chars produces int
(3 answers)
Closed 8 years ago.
In this program I am attempting to assign the result of the addition of character variables to an integer variable.I have made sure that the size of the addition is greater than 255.So I expect an expression overflow on the right and even though the result is 362,due to overflow I expect 106 to be assigned after the result is cast to int,not 362.But strangely 362 is being assigned.
The result is the same irrespective of whether the characters are signed or unsigned.Why is there no overflow and 362 being assigned?Since there is no integer on the right side during addition and all operands are characters,I don't expect them to be promoted to int.
#include<stdio.h>
int main(void)
{
unsigned char ch1='z',ch2='x'; //Same result for signed too
int sum=ch1+ch2+ch2;
printf("%d",sum);
}
all calculation starts minimum at integer precision so your statement will work like following
int sum=(int)ch1+(int)ch2+(int)ch2;

Resources