This question already has answers here:
sizeof() operator in if-statement
(5 answers)
Closed 1 year ago.
int main()
{
if(sizeof(double) > -1)
printf("M");
else
printf("m");
return 0;
}
I expected the output to be M but it is m. Can anybody please explain me the reason for the output?
That's because sizeof returns a size_t value, that is, a unsigned integer type, so, -1 will overflow.
Related
This question already has answers here:
How "a<b<c" or "a>b>c" are evaluated in C
(2 answers)
Chaining multiple greater than/less than operators
(6 answers)
Compound condition in C: if (0.0 < a < 1.0)
(4 answers)
Closed 1 year ago.
The following code supposed to print true. But it is printing false. Does anyone know why is it so?
int main(void)
{
int a=15,b=10, c=1;
if(a>b>c)
{
printf("true");
} else
{
printf("false");
}
}
In C, a>b>c means (a>b)>c. It does not mean (a>b)&&(b>c).
The value of a>b is either 0 or 1 (false or true, respectively). Since c is 1, neither of those possible values can be greater than c, so the comparison is always false.
This question already has answers here:
C/C++ unsigned integer overflow
(4 answers)
Closed 4 years ago.
I was trying to get into c programming, but in a question a get stuck, please explain this.
int main()
{
char c = 255;
c=c+10;
printf("%d",c);
return 0;
}
the output it gave is
> 9
kindly explain this to me.
The maximum value of a char is 255.
By adding 10 to that number you get 265.
Because that value is not a suitable value for a char it will do 265 % 256 resulting 9
That's why your result is 9
This question already has answers here:
sizeof() operator in if-statement
(5 answers)
What will be the value of strlen(str)- 1 in a 'for' loop condition when str is empty?
(2 answers)
why is -1>strlen(t) true in C? [duplicate]
(3 answers)
Closed 4 years ago.
Why does the shorter string ("paid") get printed by this program?
#include <stdio.h>
int main()
{
char s[] = "paid", t[] = "paviDboss";
if ((strlen(s) - strlen(t)) > 0)
printf("%s\n", s);
else
printf("%s\n", t);
}
Return type of strlen is size_t which is an unsigned type. The result of the subtraction is also size_t and can therefore only be positive.
Just use
if(strlen(s) > strlen(t))
This question already has answers here:
What is the "-->" operator in C++?
(29 answers)
Closed 8 years ago.
I have a question, what mean "-->" in C? For example:
int a, b, c, x;
a=2001;
b=1000;
c=2;
x=a-b*c;
printf("First: %i", x-->0);
It will print "1".
But:
printf("Second: %i", x-->0);
will print "0". Why when I use it second time, it print "0"?
x --> 0 is to be read (x--) > 0.
x-->0 is parsed as (x--) > 0.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
Code in C language.
#include<stdio.h>
#define PRODUCT(x) ( x * x * x)
int main()
{
int i =5,k;
k = PRODUCT( ++i );
printf("i is :%d ",k);
return 0;
}
My Question is why i is : 392? According to me output should be 336. (because 6 * 7 * 8 = 336)
Am I really messed up here??
Preprocessed code will have
( ++i * ++i * ++i)
which have Lack of sequence point between the two execution on same variable resulting Undefined behaviour.