sizeof() in C conditional operator [duplicate] - c

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.

Related

Why the code in this simple C while loop is never executed? [duplicate]

This question already has answers here:
sizeof() operator in if-statement
(5 answers)
Closed 2 years ago.
I didn't touch C for a while and now I am surprised that this while-loop is more mystic than I ever realized.
I tried while ((byte_sent < sizeof(int))) and while (byte_sent < sizeof(int)), the same result I got. The code inside the loop is never reached.
Tested with Gcc, Clion build chain, online compiler.
#include <stdio.h>
int main()
{
int byte_sent = -1;
while ((byte_sent < sizeof(int))) {
printf("We are in\n");
break; // Just want to know if the inner loop gets executed
}
return 0;
}
Try compiling with -Weverything(clang):
c.c:6:23: warning: comparison of integers of different signs: 'int' and 'unsigned long' [-Wsign-compare]
while ((byte_sent < sizeof(int))) {
~~~~~~~~~ ^ ~~~~~~~~~~~
1 warning generated.
sizeof returns an size_t (Here a typedef for unsigned long), byte_sent is an int.
The integer is casted to size_t. So byte_sent is now 4294967295, as size_t(unsignd long) has no negative numbers.
You need to cast sizeof(int) to an int: (int)sizeof(int), or it will automatically cast byet_sent to a size_t which is an unsigned long.
Edit: Look at JCWasmx86's answer for more detailed explanation!

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

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?

Pointer arithmetic output issues [duplicate]

This question already has answers here:
C/C++: Pointer Arithmetic
(7 answers)
Closed 4 years ago.
Here are we use typecasting from pointer to integer, but output of arithmetic operation are different from expected answer, Why?
Source Code :
int main(){
int *p,*q;
p = 1000;
q = 2000;
printf("%d",q-p);
return 0;
}
Output: 250
The same reason due to which p++ will give you 1004. Since sizeof(int) on your machine is 4, hence each operation is shown wrt sizeof(int), even the difference i.e. 1000/4.

Why this code is printing "False", though size of int is greater than -1? [duplicate]

This question already has answers here:
Why is this happening with the sizeof operator when comparing with a negative number? [duplicate]
(2 answers)
sizeof() operator in if-statement
(5 answers)
Closed 5 years ago.
According to the code below, size of int is not greater than -1. Why is it so? Why "False" was printed instead of "True"?
#include <stdio.h>
#include <stdlib.h>
int main() {
if(sizeof(int) > -1) {
printf("True\n");
}
else {
printf("False\n");
}
// Here size of int is equals to 4
printf("Size of int: %ld\n", sizeof(int));
return 0;
}
Well sizeof returns size_t which is unsigned and when it is compared with int the int is promoted to unsigned and the bit representation all 1's now considered as unsigned, which is bigger than -1 and also that of sizeof int. That's why this result.
Correct format specifier for size_t is %zu.

Overflow not beeing recognized, p is not printing warning [duplicate]

This question already has answers here:
How do I detect unsigned integer overflow?
(31 answers)
Closed 6 years ago.
I'm having a problem with my little c program.
I'm trying to print out a warning if an overflow occurs, I'm using the limits.h library to recognise an overflow with INT_MAX.
I guess the problem is in my if loop, but I can't really find the problem...
#include<stdio.h>
#include<limits.h>
int main()
{
int x = 1627964;
int y = 9;
for(int i=1; i<y; ++i){
x*= i; // x= x * i
printf("%d * %d \n",x , i+1);
if(x >= INT_MAX){
printf("An Overflow has occured!\n");
return 0;
break;
}
}
}
I think the overflow occurs after multiplying with 7:
1627964 * 2
3255928 * 3
9767784 * 4
39071136 * 5
195355680 * 6
1172134080 * 7
-384996032 * 8
1214999040 * 9
I can't really tell why the warning is not printed out...
Some help would be really appreciated, thanks :)
Only unsigned int has defined overflow behavior, signed int does not, so compiler could throw away half of your code if it detect it. Anyway in your code int never will be bigger than INT_MAX because on overflow it should be negative value.
If you try int max / i < x for the overflow ?
x == INT_MAX only tests if x is exactly equal to INT_MAX. Not less, not more, exactly equal.
What you should be testing for is signed integer overflow, which happens when you multiply by 1627964 by 8: if (x==INT_MAX || x < 0) - the sign bit gets incremented which makes your value negative.

Resources