I code from a long time but I was never asked questions like this before.
main()
{
int a=5, b, c;
b=a=15;
c=a<15;
printf("%d %d",a,c);
}
What will be the values of a and c? How do we interpret '<' or '>' ?
The < operator yields 0 or 1 of type int.
It yields 1 if its left operand is less than its right operand, 0 otherwise.
In your case the left operand has the value a or 15; the right operand has the value 15. So the operator yields the value 0.
Then that 0 is assigned to c.
Your statement with extra whitespace, parenthesis, and a comment can be written as
c = (a < 15); // assign 0 or 1 to c
main()
{
int a=5, b, c;
b=a=15; // a = 15
c=a<15; // a is not < 15, a is 15, so c is 0
printf("%d %d",a,c);
}
First of all, the result of this code is undefined, since you do not supply a function prototype for printf
Second of all, using main() is further undefined since a function needs a return type and main() in particular needs to be int return type (handling of other implementations is implementation-defined.
Now assuming you correct your code to:
#include <stdio.h>
int main(void)
{
int a=5, b, c;
b=a=15;
c=a<15;
printf("%d %d",a,c);
return 0;
}
Then the output for c is 0.
This is because the < operator returns int type 0 or 1 based on if the comparison is true.
Related
output in this case is 1
int main() {
int i = 500;
while( (i++) != 0 );
printf("%d\n", i);
return;
}
output in this case is 0
int main() {
int i = 500;
while( (i=i+1) != 0 );
printf("%d\n", i);
return;
}
I'm Not sure why get this different output in every case I mean why 1 in first case and 0 at second case
For starters the both programs have undefined behavior. Instead of declaring the variable i as having the signed integer type int you should declare it as having the unsigned integer type unsigned int to make the programs correct.
This while loop
while( (i++) != 0 );
stops its iterations when the expression i++ is equal to 0. So when the variable i is equal to 0 the loop stops. But due to the postfix increment operator its value is incremented and becomes equal to 1.
According to the C Standard (6.5.2.4 Postfix increment and decrement operators)
2 The result of the postfix ++ operator is the value of the operand.
As a side effect, the value of the operand object is incremented (that
is, the value 1 of the appropriate type is added to it).
This while loop
while( (i=i+1) != 0 );
also stops its iterations when i is equal to 0 after the assignment i = i + 1.
I don't know why the sizeof operator is not evaluated in a for loop condition at run time. I am trying this simple code with different C compilers but it always print nothing. But if I replace sizeof(i) with 4 then it works fine:
for(int i = -2; i <= 4; i++)
#include <stdio.h>
int main()
{
for(int i = -2; i <= sizeof(i); i++)
printf("Hello World");
return 0;
}
The problem is , the result of sizeof() operator is of type size_t, which is an unsigned type.
Next, in the comparison, i <= sizeof(i) as per the usual arithmetic conversion rules, -2, which is a signed value, gets promoted to an unsigned value, producing a huge value, evaluating the condition to false. So the loop condition is not satisfied and the loop body is not executed.
Run your program through a debugger and see the values in each step, it'll be more clear to you once you see the promoted values in the comparison.
sizeof yields a value of unsigned type variety (size_t). The i is converted to that type and the comparison executed as
(size_t)-2 <= 4
something like 4000000000 < 4
you need to typecast sizeof(i) into int. that should solve the problem.
so just replace for(int i = -2; i <= sizeof(i); i++) with for(int i = -2; i <= (int) sizeof(i); i++)
sizeof() returns size_t which is an unsigned type.
You can try this code instead:
#include<stdio.h>
int main()
{
int i=-2,j;
j=(int)sizeof(i);
for(i=-2;i<=j;i++)
printf("HELLO\n");
return 0;
}
You can typecast sizeof() to int.
int main()
{
int a = (1,2,3);
int b = (++a, ++a, ++a);
int c= (b++, b++, b++);
printf("%d %d %d", a,b,c);
}
I am beginner in programming. I am not getting how does this program shows me output of 6 9 8.
The , used in all the three declarations
int a = (1,2,3);
int b = (++a, ++a, ++a);
int c = (b++, b++, b++);
are comma operator. It evaluates the first operand1 and discard it, then evaluates the second operand and return its value. Therefore,
int a = ((1,2), 3); // a is initialized with 3.
int b = ((++a, ++a), ++a); // b is initialized with 4+1+1 = 6.
// a is 6 by the end of the statement
int c = ((b++, b++), b++); // c is initialized with 6+1+1 = 8
// b is 9 by the end of the statement.
1 Order of evaluation is guaranteed from left to right in case of comma operator.
The code is not in any way good and nobody in their right mind would ever write it. You should not spend any time in looking at that kind of code, but I will still give an explanation.
The comma operator , means "do the left one, discard any result, do the right one and return the result. Putting the parts in parentheses doesn't have any effect on the functionality.
Written more clearly the code would be:
int a, b, c;
a = 3; // 1 and 2 have no meaning whatsoever
a++;
a++;
a++;
b = a;
b++;
b++;
c = b;
b++;
The pre- and post-increment operators have a difference in how they act and that causes the difference in values of b and c.
I am beginner in programming. I am not getting how does this program
shows me output of
Just understand comma operators and prefix ,postfix .
according to rules mentioned in links given to you
int a = (1,2,3); // a is initialized with 3 last argument .
int b = (++a, ++a, ++a); // a is incremented three time so becomes 6 and b initilized with 6 .
int c = (b++, b++, b++); // b incremented two times becomes 8 and c initialized with 8.
// b incremented once more time becomes 9
Please explain me why it behaves differently.
int main() {
int p;
p = (printf("stack"),printf("overflow"));
printf("%d",p);
return 0;
}
This gives the output as stackoverflow8. However , if I remove the paranthesis , then :
p = printf("stack"),printf("overflow"); gives the output as stackoverflow5
The Comma Operator
The comma operator has lower precedence than assignment (it has a lower precedence than any operator for that matter), so if you remove the parentheses the assignment takes place first and the result of the second expression is discarded. So...
int a = 10, b = 20;
int x = (a,b); // x == 20
int y = a,b; // y == 10
// equivalent (in terms of assignment) to
//int y = a;
Note that the third line will cause an error as it is interpreted as a re-declaration of b, i.e.:
int y = a;
int b;
I missed this at first, but it makes sense. It is no different than the initial declaration of a and b, and in this case the comma is not an operator, it is a separator.
what is wrong with this code ? can anyone explain ?
#include <stdio.h>
#include <malloc.h>
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};
int main()
{
int num;
int d;
int size = TOTAL_ELEMENTS -2;
printf("%d\n",(TOTAL_ELEMENTS-2));
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);
return 0;
}
when i print it gives 5, but inside for loop what is happening ?
The sizeof operator returns a value of type size_t, which is an unsigned value. In your for loop condition test:
d <= (TOTAL_ELEMENTS-2)
you are comparing a signed value (d) with an unsigned value (TOTAL_ELEMENTS-2). This is usually a warning condition, and you should turn up the warning level on your compiler so you'll properly get a warning message.
The compiler can only generate code for either a signed or an unsigned comparison, and in this case the comparison is unsigned. The integer value in d is converted to an unsigned value, which on a 2's complement architecture ends up being 0xFFFFFFFF or similar. This is not less than your TOTAL_ELEMENTS-2 value, so the comparison is false.
You're starting the loop by setting d = -1, it should be d = 0. So for the first element you're reading random bits of memory.
If you fix that, then you can change your printf to be
printf("%d\n",array[d]);
As you've also marked this as homework, I'd advise also take a look at your loop terminating condition.