What is the outcome for the arithemic in C? [duplicate] - c

This question already has answers here:
How does the bitwise complement operator (~ tilde) work?
(18 answers)
C Unsigned int providing a negative value?
(3 answers)
Closed 2 years ago.
There is an interview question in C as below.
int main()
{
unsigned int a = 9;
a = ~a;
printf("%d\n", a);
}
I though it was supposed to be 6 but it is -10.
~a is assinged back to an unsigned integer then printed out.
It should not be a negative value.
Isn't it? Hoe come?

Related

Stuck with the prediction of output to the below snippet. Can someone help me out as to why the output is 40 [duplicate]

This question already has answers here:
Who defines C operator precedence and associativity?
(5 answers)
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Why is a = (a+b) - (b=a) a bad choice for swapping two integers?
(10 answers)
Closed 11 months ago.
#include<stdio.h>
int main()
{
int y=0;
int z;
z=(--y)+(y=20);
printf("%d",z);
return 0;
}
Why is the precedence considered from right to left in spite of parentheses?

sizeof() in C conditional operator [duplicate]

This question already has answers here:
How to compare signed and unsigned (and avoiding issues)
(1 answer)
Why is this happening with the sizeof operator when comparing with a negative number? [duplicate]
(2 answers)
Closed 11 months ago.
#include<stdio.h>
int main(){
int i;
// printf("%d",sizeof(i)) ;
printf("%d",(sizeof(i) > (-1))) ;
return 0;}
why does the code print 0 when sizeof(i) gives 4 in 64 bit OS?
why does (sizeof(i) > (-1))) gives false(0) ?
Use a better compiler and enable warnings. Under any sane compiler you should have gotten a warning about comparing an unsigned and a signed value.
This should be closer to what you want:
printf("%d", (int)sizeof(i) > -1);
Or at least this:
printf("%d", sizeof(i) >= 0);
However your code is a no-op anyway, because it's impossible to have a negative size of a type.

why does this program output 37? [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 1 year ago.
#include <stdio.h>
int main()
{
float c = 5.0;
printf ("Temperature in Fahrenheit is %.2f", (9/5)*c + 32);
return 0;
}
Change your statement to (9.0/5.0)*c + 32 as 9 and 5 are integers, their division returns integer that is 1. So write them in float variable format.

Float prints Int in C [duplicate]

This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
What is the behavior of integer division?
(6 answers)
Closed 2 years ago.
I am beginner at programming and I am just writing a very simple program to return the solution of 23/5. My code is below:
float res = 23/5;
printf("%.3f", res);
The expected answer is 4.6, however, the code outputs: 4.000.
You are setting its value to an int
float res = 23.0f/5.0f;
printf("%.3f", res);

how - 5%3 is equal to - 2? [duplicate]

This question already has answers here:
Modulo operation with negative numbers
(12 answers)
Closed 7 years ago.
I'm learning C basics right now. I have a question which is confusing me little bit.
My question is how the below program's output is - 2 ?
#include<stdio.h>
int main()
{
printf("%d", -5%3);
return 0 ;
}
The % operator is gives you the remainder left after of integer division.
Then -5/3 = -1 with -2 as remainder of division as 3*(-1)=-3 and -5-(-3)=-5+3=-2.

Resources