This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
I'm attempting to do a "find the bug" assignment, and this small bit of C code (not runnable as is) is supposed to have a bug in it. Unforunately I can't find one. I put it into runnable code and it seems to run as expected...is this a trick question or am I missing something? Any help is greatly appreciated.
Code snippit:
void example(int a[10]){
int i = 0;
do {
a[i] = i++;
}while (i < 10);
}
My runnable code:
#include <stdio.h>
#include <string.h>
example(int a[10]){
int i = 0;
do {
a[i] = i++;
printf("%i", a[i-1]); //decremented by 1 to offset i
}while (i < 10);
}
int main(void)
{
int arr[10];
example(arr);
return 0;
}
Output:
0123456789
This is wrong and invokes undefined behavior:
a[i] = i++;
There is no sequence point specified for the assignment, increment or index operators, you don't know when the effect of the increment on i occurs.
Take a look to Question 3.1 of C-FAQ
Related
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 2 years ago.
stumbled upon such a puzzle:
What will be shown on the screen?
#include <stdio.h>
void main()
{
int x = 10;
printf("x = %d, y = %d", x--, x++);
}
Curiously enough, but shown at the screen this: x = 11, y = 10;
But how??
Argument Evaluation Order is undefined in both C and C++. It's important to avoid any code that passes expressions dependent on each other that has to be evaluated before the function is called. It's a strict no.
int f1() { printf("F1") ; return 1;}
int f2() { printf("F2" ) ; return 1;}
printf("%d%d", f1(), f2()) ;
You can check out by adding several functions that contain a print statement and pass it to a function to observe this in action. You don't know what's coming, the C standard doesn't specify it, it depends on what compiler you use and how it optimizes your code.
This question already has answers here:
Explain the order of evaluation in printf [duplicate]
(5 answers)
Closed 6 years ago.
How to explain the output of the below code:
include <stdio.h>
int main(void) {
int k;
printf("%d %d\n",k=1,k=3);
return 0;
}
Ideone Link
My thinking was that 1 will be assigned to k variable and then 1 would be printed. Similarly 3 will be assigned to k and output will be 3.
Expected Output
1 3
Actual Output
1 1
I am extrapolating from
int a;
if (a = 3) {
...
}
is equal to
if (3) {
...
}
Please let me know where am I going wrong?
The problem is, the order of evaluation of function arguments are not defined, and there's no sequence point between the evaluation or arguments. So, this statement
printf("%d %d\n",k=1,k=3)
invokes undefined behavior, as you're trying to modify the same variable more than once without a sequence point in between.
Once a program invoking UB is run and (if) there's an output, it cannot be justified anyway, the output can be anything.
I expect the reason you're seeing 1 1 is because the two assignment statements are happening control is passed to printf.
printf("%d %d\n",k=1,k=3);
So in response to the down-votes, yes, this this is undefined behavior, and therefore you shouldn't count on this behavior continuing. However, in terms of identifying why the output was 1 1 and not 1 3, we could infer that the assignment of 3 may have been stomped on by a subsequent assignment of 1.
When printf is invoked, the call stack contains two entries containing the final value of k, which is 1.
You can test this out by replacing those with a function that prints something when it executes.
Sample Code:
#include <stdio.h>
int test(int n) {
printf("Test(%d)\n", n);
return n;
}
int main(void) {
int k;
printf("%d %d\n",k=test(1), k=test(3));
return 0;
}
Output:
Test(3)
Test(1)
1 1
This question already has answers here:
Array index out of bound behavior
(10 answers)
Closed 8 years ago.
I am curious about below code in C
int main(){
int arr[10];
*(arr+120) = 5;
int *px = arr;
int i = 0;
for(i = 0; *px != 5; px++){
i++;
}
printf("%d", i);
}
This code produced output of 120.
Our array is said to hold 10 items. How can I assign some value for index 120, run loop and get my value if there potentially should be some kind of error. Probably I am not getting some C language specifics. In java I would get OutOfBounds exception....
Please, help to clarify it. Thank you!
C does not check array bounds, and the code accessing out-of-array elements is undefined behaviour, which means that anything may happen - including the result you got.
This question already has answers here:
Array index out of bound behavior
(10 answers)
Closed 8 years ago.
I was trying to test what output I would get when I tried to print the array that the index goes out of bound.
The code:
#include <stdio.h>
void main()
{
int arr[] = { 3, 4, 5 };
for (int i = 0; i < 5; i++)
{
if (arr[i] == 0)
{
printf("Breaking out of the loop.");
break;
}
else
printf("%i\n", arr[i]);
}
getchar();
}
When I run this code, the output is:
3
4
5
-858993460
3997200
I expected it to print out "Breaking out of the loop" and break out of the loop and terminate.
I truly have no idea how it even printed those numbers.
Any idea what those numbers mean?
P.S. I am sorry if this is a stupid question, I am quite new to C.
Memory out of bounds of an array, or dynamically allocated memory, doesn't belong to you, and its content is indeterminate. Accessing arrays or memory out of bounds leads to undefined behavior. Just don't do it.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
There is a code following below and i ma facing a very serious problem in understanding the logic for the code.
#include <stdio.h>
#include <stdlib.h>
int main(void )
{
int i = 1 ;
printf("\n%d %d %d %d\n",++i,i++,i++,++i) ;
return 0 ;
}
I am using gcc compiler under the linux distro named Mandriva. In the above mentioned i have used pre and post increment with a variable in the printf statement.
The output that i am supposed to get is 2 2 3 5, but i am getting a different output.
Please help me in this code.
I am feeling much difficult in this code.
It's undefined behavior. There's no sequence points between the increments of i.
Any result is a correct result (including your hard drive being formatted).