why it is giving output as 'bye'? please discuss [duplicate] - c

This question already has answers here:
sizeof() operator in if-statement
(5 answers)
Closed 4 years ago.
Please give the reason for the output...why it's giving 'bye' while the condition if statement is true because the size of int is 2 or 4 byte.
#include<stdio.h>
#include<conio.h>
int main()
{
if(sizeof(int)>-1)
printf("hi");
else
printf("bye");
return 0;
}

See http://en.cppreference.com/w/c/language/sizeof
Both versions return a value of type size_t.
See size_t, http://en.cppreference.com/w/c/types/size_t
which states that it is unsigned.
If therefor the comparison is effectively with a high positive value,
then the logical expression is basically always false.
If you would help the compiler understand what you really want to do,
e.g. by changing to
((int)sizeof(int))>-1
things are different.

It's because the return type of sizeof() is size_t and sizeof() never gives size in negative bytes.
sizeof(int) results is type of unsigned.
And here
if(sizeof(int)>-1)
Comparison is happening between different types i.e signed(-1) and unsigned. So internally compiler will do implicit typecasting i.e signed gets converted into unsigned and -1 equivalent unsigned value is all one's i.e 4294967295.
So now condition looks like
if(4 > 4294967295)
Which is false so it prints bye.
See this for sizeof() return type http://en.cppreference.com/w/c/types/size_t

Related

How sizeof Operators Works inside the IF Statement in C [duplicate]

This question already has answers here:
Why does this if condition fail for comparison of negative and positive integers [duplicate]
(6 answers)
Closed 5 years ago.
int main()
{
if (sizeof(int) > -1 )
printf("True");
else
printf("False");
return 0 ;
}
I expected the program results in "True" but after executing it results in "False". Could anyone explain why this is the case?
sizeof returns size_t, which is unsigned.
Comparison unsigned and signed numbers needs an attention in C, because of such comparison usually gives surprising results for beginner programmers, as we can see here.
Basically, -1 gets converted to a very big unsigned int, so your condition is false.

Why EOF coincides with valid char value? [duplicate]

This question already has answers here:
What is EOF in the C programming language?
(10 answers)
Closed 6 years ago.
As said in comments to the answer to this question: Why gcc does not produce type mismatch warning for int and char?
both -1 and 255 are 0xFF as 8 bit HEX number on any current CPU.
But EOF is equal to -1. This is a contradiction, because the value of EOF must not coincide with any valid 8-bit character. This example demonstrates it:
#include <stdio.h>
int main(void)
{
char c = 255;
if (c == EOF) printf("oops\n");
return 0;
}
On my machine it prints oops.
How this contradiction can be explained?
When you compare an int value to a char value, the char value is promoted to an int value. This promotion is automatic and part of the C language specification (see e.g. this "Usual arithmetic conversions" reference, especially point 4). Sure the compiler could give a warning about it, but why should it if it's a valid language construct?
There's also the problem with the signedness of char which is implementation defined. If char is unsigned, then your condition would be false.
Also if you read just about any reference for functions reading characters from files (for example this one for fgetc and getc) you will see that they return an int and not a char, precisely for the reasons mentioned above.

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.

What doe this C code do with (unsigned) and (long) cast [duplicate]

This question already has answers here:
!! c operator, is a two NOT?
(4 answers)
What is "!!" in C? [duplicate]
(7 answers)
Closed 8 years ago.
doing some exam prep and this is a past question.
Describe what the following pieces of C do and re write in a simple programming style with the same functionality.
(The bad indentation was the intention of the question).
With regards to section A i'm not sure what unsigned cast is doing to a. I have tested it a few times and can't seem to get a result that makes sense.
Similarly In B im not sure what how the while loop is working with the long cast and the !! being another problem
The Code :
//code section A
int f(int a,int b){
return(((unsigned) a)>b);}
//code section B
int h(int *x, int y){
int * z= x-- +y;
w=0;
while( (long) z-- ^(long) x) w += !!(*z);
return w;}
Any help would be appreciated, Thank you.
!! negates a boolean expression twice, essentially converting an expressions value to 0 or 1.
As in C all values other than zero mean true, and zero means false, !! can be used to convert it into 0 or 1, in the case you need to use it later in a function or expression which doesn't accept any value for true, only the number 1.
About the rest: unsigned interprets the internal representation of your int a from your function argument to unsigned int, so for example -1 becomes 4294967295 if your compiler uses two's complement and 4 byte ints.
About the casting to long : I strongly recommend against in similar situations unless you absolutely know what you are doing. In your example, it does some pointer arithmetic, in interpreting your pointer as numeric values, essentially working with the addresses of your variables as if they were just numbers. They have probably chosen long because on their system it had the exact same size as a pointer. This is not guaranteed to be so on all systems.
So, to give a very short answer to your question: The code does undefined behavior with those expressions, except for the !! which just give 0 if the expression was zero, and 1 otherwise.
The operator ! is the logical negation.
!true is false (0), and !false is true (1).
When a value is used as a boolean value, anyhting other than 0 (0 in a large sense) is true; 0 is false.
So, !!(*z) has either the value 0 or 1.
It will be 0 if *z was NULL to begin with
It will be 1 if *z was not NULL.
! is the boolean operator for not.
false is 0, and true is 1 in C.
So when you take any int, which is not 0- and you run ! on it, you'll get 0 (true becomes false), and zero will become one .
So the action of !! is changing each non-zero value to 1 , and leaving each 0 a 0.

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