A weird syntax in C/C++ [duplicate] - c

This question already has answers here:
What does the integer suffix J mean?
(2 answers)
What does c++ constant value 1j mean? [duplicate]
(1 answer)
Why can't we do three-way comparison in C++? [duplicate]
(6 answers)
Comparing a variable to a range of values
(7 answers)
Closed 4 months ago.
Translation:
"After running the code below, what is the value of k?'
This question was from my friend's homework(we were senior highs). He didn't know how to deal with this, so he asked me and then I tried to test this on my computer(Debian 10, gcc-10.2.1). The result is (A) , which means the value of k is 6. I reflected on the if statement but still had no idea. It utterly made no sense - especially the syntax(who will even write code in that way?) . That's why I would like to ask this question. Perhaps that's a bug I haven't met before.
The C code:
#include <stdio.h>
int main() {
int i, j, k = 0;
for ( i = 0 ; i <= 2 ; ++i ) {
for ( j = 0 ; j <= 2 ; ++j ) {
if ( i != 1i != 2 && j != 1 ) {
k = k + j;
}
}
}
printf("%d\n", k);
return 0;
}
Why can the following if statement work?
if ( i != 1i != 2 && j != 1 )
And what's the value of 1i?

Related

loops and arrays blocks undefines behaviour [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 1 year ago.
#include <stdio.h>
int main()
{
int arr[3],i=0;
while(i<3)
{
arr[i]= ++i;
}
for(int i=0;i<3;i++)
{
printf("%d\n",arr[i]);
}
return 0;
}
why is this outputting garbage value,1,2 ? i expected it to be 1,2,3. Please help
TIA
The expression:
arr[i]= ++i;
is undefined behaviour because i appears on both sides of the assignment operator (=), but the behaviour in this instance is empirically:
i++ ;
arr[i] ;
i.e. i is incremented before arr is indexed.
You need:
arr[i] = i + 1 ;
i++;
for the "expected" output:
1
2
3

Compile time error as 'lvalue required' but not sure why [duplicate]

This question already has answers here:
Error: lvalue required in this simple C code? (Ternary with assignment?)
(4 answers)
lvalue required as left operand of assignment in conditional operator [duplicate]
(3 answers)
"error: lvalue required as left operand of assignment" in conditional operator
(1 answer)
C ternary expression-statement not working [duplicate]
(2 answers)
Getting error "lvalue required as left operand of assignment"
(6 answers)
Closed 3 years ago.
This code throws an lvalue required compile time error.
#include <stdio.h>
void main()
{
int k = 8;
int m = 7;
k < m ? k++ : m = k;
printf("%d", k);
}
Ternary operator has higher precedence than assignment, that's why your code is equal to (k < m ? k++ : m) = k;. Your compiler says that the value in brackets is not assignable.
What you want to do is:
#include <stdio.h>
void main()
{
int k = 8;
int m = 7;
k < m ? k++ : (m = k);
printf("%d", k);
}
The problem is here:
k < m ? k++ : m = k;
with the construct you want to assign a value, but you don't. I guess you want something like this:
k = (k < m) ? k+1 : m;
Now you will assign k a value depending on the condition k < m
if (k < m) -> k = k+1
otherwise -> k = m

Can anyone tell me why this program runs? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
New to c programming. Here is a question from an assignment. Can anyone tell me why this code still outputs two columns 5 and 2 even though i is less than j.
#include <stdio.h>
int main(void) {
int i = 0, j = 5;
for (i > j; i + j == 5; j < 2) {
printf("Two columns\n");
i = 5;
j = 2;
}
printf(" %d %d\n", i, j);
return 0;
}
Here are the steps executed in order:
int i = 0, j = 5; local variables i and j are defined and initialized to 0 and 5 respectively
for (i > j; i + j == 5; j < 2) {:
first executes the initial expression i > j which evaluates to false (0) and has no side effect, the result is ignored, hence probably completely omitted by the compiler.
second executes the test expression i + j == 5 which evaluates to true (1) so the body of the for loop executes.
printf("Two columns\n"); outputs Two columns and a newline.
i = 5; sets i to 5.
j = 2; sets j to 2.
} the increment expression is then evaluated: j < 2, which evaluates to false but has no side effect, the result is ignored.
the loop then evaluates the test expression again: i + j == 5 which now evaluates to false (0) since 5 + 2 is different from 5.
the loop exits.
printf(" %d %d\n", i, j); outputs the numbers 5 and 2 and a newline as you observe.
return 0; main returns the value 0 which is a successful exit status.
This code is very silly and purposely misleading as it has test expressions in all 3 parts of the for statement header. Only the middle one is the test expression, the first and last expressions are only used for side effects, such as initializing and incrementing a loop counter.
for (i > j; i + j == 5; j < 2)
In for loop initialization part i > j no make sense
Then i + j == 5 condition become true and executed for loop body where assigned new values to i and j respectively 5 and 2.
Then control goes to for loop increment part where j < 2 become false. this is also no make sense.
Then again control goes to condition part where i+j == 5 becomes false because i is a 5 and and j is a 2. So, 7 == 5 becomes false.
So, output of your code is 5 and 2.

statement with no effect for-loop C [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
I got this for-loop:
for(int k=4;k<0;k--){
if(k == 0){
test[k] = 5;
break;
}
else{
test[k] = test[k-1];
}
}
it should shift the elements of the array to the right, but nothing happens.
To my knowledge it should work just fine, but the compiler says, that the for-loop statement has no effect: statement with no effect [-Wunused-value]
can anyone help me solve this problem?
The loop initializes k to a positive value (k=4) and then loops while k is negative (k<0).
Since k is never negative, the loop has no effect.
Did you mean to write k >= 0?
The condition expression of the loop is wrong.
for(int k=4;k<0;k--){
You initialized k with 4 and then are checking whether it is less than 0. As 4 is obviously greater than 0 then the loop will iterate never.
I think you mean the following
for(int k = 4; k >= 0; k-- ) {
But in any case the code looks badly. For example it is not clear what the magic number 5 means and there is no need to use the break stztement.
You could write a function. Here is an example of the corresponding program
#include <stdio.h>
void shift_right( int a[], size_t n )
{
if ( n > 1 )
{
size_t i = n - 1;
int last = a[i];
for ( ; i != 0 ; --i ) a[i] = a[i - 1];
a[i] = last;
}
}
int main(void)
{
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
const size_t N = sizeof( a ) / sizeof( *a );
size_t i;
for ( i = 0; i < N; i++ ) printf( "%d ", a[i] );
puts( "" );
shift_right( a, N );
for ( i = 0; i < N; i++ ) printf( "%d ", a[i] );
puts( "" );
return 0;
}
The output is
0 1 2 3 4 5 6 7 8 9
9 0 1 2 3 4 5 6 7 8
In your loop you use condition k<0 -> 4<0 -> false.
so your condition fails in first time itself.so your loop has not executed.so change your conditional statement to k>0 instead of k<0.
use the printf statement to find the loop is executes or not.
for(int k=4;k>=0;k--){
if(k == 0){
test[k] = 5;
break;
}
else{
test[k] = test[k-1];
}
}
The above code works as you expected.

Operator associavity problem with pre and post increment :( [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
#include< stdio.h >
int main()
{
int i = 1;
int x = ++i * ++i * ++i;
printf("%d\n", x);
printf("%d\n\n",i);
return 0;
}
Im getting output of 1!! and 4 in gcc. I use ubuntu linux
The behaviour of your code is undefined since i is modified more than once between sequence points:
int x = ++i * ++i * ++i;
See the FAQ (I urge you to read the entire section 3).
Undefined Behaviour this is:
int x = ++i * ++i * ++i;
Don't do it!!!!

Resources