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!!!!
Related
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?
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
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
My code runs until it reaches this section of code then it stops
int i,y;
short int** audiodata = (short int **)calloc(nsample*2, sizeof(short int*));
for( i=0 ; i<nsample ; i++)
{
for( y=0 ; y<1 ; y++)
{
audiodata[i][y]= (short int) (32700.0* sin(2*pi*freq*(float)i/44100) );
}
}
what errors am i not seeing and i have a printf statement after this section of code that let the user know it finished loading the 2-d array but the printf never get executed.
The problem is that you did not allocate arrays of pointers of the type short int *. So this statement
audiodata[i][y]= (short int) (32700.0* sin(2*pi*freq*(float)i/44100) );
^^^^^^
has undefined bahavior.
What you mean is the following
short int** audiodata = calloc( nsample, sizeof( short int* ) );
for ( i = 0; i < nsample; i++ )
{
audiodata[i] = calloc( 2, sizeof( short int ) );
}
for ( i = 0; i < nsample; i++ )
{
for ( y = 0; y < 2; y++ )
{
audiodata[i][y]= (short int) (32700.0* sin(2*pi*freq*(float)i/44100) );
}
}
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
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I wrote a program which prints all the letters in English but there is a run - time error in the code which I wrote. Where is it? Why did it happened?
Can you please help me and solve it?
void printArray(char* p, int len)
{
for( p ; p < p + len ; p++ )
{
printf("%c", *p);
}
printf("\n");
}
int main()
{
char* abc = "abcdefghijklmnopqrstuvwxyz";
printArray(abc, 26);
return 0;
}
When will p not be less that p + len?
for( p ; p < p + len ; p++ ) // Loop forever
You might want something like:
char* stop = p + len;
for( p ; p < stop ; p++ )
p < p + len is never false --> infinite loop.
Suggest
for(int i = 0; i<len ; i++ )
{
printf("%c", p[i]);
}
Because you are editing p as you check against it "p < p + len" this loop will never end. p will always be less than itself plus a constant.
try it like
char* end = p + len;
for(p ; p < end; p++)
then it should work