C Decision Control Explain [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Why output is z=0 in the program written below ?
why isn't it giving any error?
Please Explain
#include<stdio.h>
int main()
{
int x=3,y,z;
y=x=10;
z=x<10;
printf("x=%d, y=%d, z=%d",x,y,z);
return 0;
}
Output :
x=10, y=10, z=0

According to N1570 draft (c11):
6.5.9 Equality operators
The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence. Each of the operators yields 1 if the specified relation is true and 0 if it is false. The result has type int.
So in your case, condition is false, because 10 is not less that 10, so 0 is stored to the z.

Related

This code should print i=1 but it is printing i=0 Why is it so? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
#include<studio.h>
int main()
{
int a=5,i;
i!=a>10;
printf("i=%d",i);
return 0;
}
This code should print i=1 but it is printing i=0 Why is it so?
That's because you don't do anything to i.
Your "i!=a>10" evaluates to false, but the result is not stored into a variable.
As it is mentioned in the comments, you need something like this:
int a = 5;
int i = !(a > 10);
The != is mostly used in if-clauses, like
if (a != 0) {...}
I hope this helps. ;)
It's not printing because:
the variable i is not initialized
the second statement should be changed and be a variable int i = !(a > 10)
Returning a value and printing it are different things, but that's not the point of the question.

Why this code is not working (if-statement) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Why "if" is not accepting the condition (both "x" and "n" are constants after all).
#include <stdio.h>
int main(){
int x,n;
scanf("%d %d",&x,&n)
if(x==(4n-3))
printf("%d",x);
return 0;
}
Compile and you get this:
error: expected ';' before 'if'
4 | scanf("%d %d",&x,&n)
| ^
| ;
The little ^ points out the exact location of the bug and tells you to write a semi colon there. Go to that location that the compiler points out and do as it says.
Then:
error: invalid suffix "n" on integer constant
5 | if(x==(4n-3))
This means that 4n is nonsense, it is not valid C. You need to write 4*n-3.
You aren't writing a math statement here
It should be if(x==(4*n-3))

Is side-effect operator allowed in C expression with more than a variable? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to use side effect operator in my expression which does not have just a variable. My program was compiled successfully but I got a runtime error "Segmentation fault"
Here is my code:
int main()
{
int x = 1;
printf(1 + (x++));
return 0;
}
C requires you to format the string, this way it knows what it should print. What you have in your example is nothing but memory addresses, which makes the C compiler confused.
int main()
{
int x = 1;
printf("%d\n", (1 + (x++)));
return 0;
}

why a[6] showing 344 value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
#include<stdio.h>
int main()
{
int a[5]={1,2,3,4,5};
int i;
for(i=0;i<5;i++)
{
printf("%d",a[6]);
}
return 0;
}
Que : Why a[6] is showing 344 value, why not zero. Where this value came from ?
C does not check if you are going out of bound for an array. If you do so, it is undefined behavior, then you will get garbage value, in worst case, you can get a seg fault also.
Because a[6] is somewhere in memory which you don't know what holds.
In your case its 344.
Your compiler won't tell you that you are out of bound.

A simple mathematical equation does not work in C programming [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The following equation gives error message but it should not.
#include<stdio.h>
int main()
{
int a;
a=12 + 10/2 + 3*2 – 5;
printf("a=%d",a);
return 0;
}
What is wrong?
You wrote a dash – instead of a hyphen -. Spot the difference:
... 3*2 – 5;
... 3*2 - 5;
It seems that you have copied pasted the code from elsewhere. In expression
a=12 + 10/2 + 3*2 – 5;
^ is not minus sign
Change it to -
In your expression that's not a minus sign, judging by eye it's an en-dash, which is not accepted by the compiler. Don't write code in Word or "normal" word processors, use plain text editors.

Resources