0

I'm writing an application in C for my university course. In a section of my application I increase an integer iteratively. Using a printf statement I can tell that int1 is increase to 20 as it should be but then the run fails. I would like to know why this is the case?

int main() {
    int i,int1=0, int2=0;
    for (i = 0; i<10; i++) {
        int1 = (int2 + 2);
        int2 = int1;
    }
}

The program has to by inline with ANSI C which I believe states that using:

int1 = (int2 + 2);

is undefined behaviour as the compiler cannot guarantee which of the 'same' variables is processed first. (Please do correct me if I am wrong however!) That is why I have taken the longer way around, but the application fails just the same using either way.

What is the reason this fails?

3
  • "The run fails" what does that mean? Commented Dec 12, 2012 at 15:35
  • In my compiler I am told that the build is successful, when the application then runs it says RUN FAILED (exit value 3, total time: 292ms). If I include a printf statement after the loop it'll inform me that the value is 20 and then say the same thing. Commented Dec 12, 2012 at 15:38
  • try adding return 0; at the end. You also do not have the standard args values for your main. Commented Dec 12, 2012 at 15:39

3 Answers 3

5

Your program doesn't fail, you simply forgot to return 0; at the end of your program, to indicate that your program ran correctly.

As for the "undefined behavior": No, that is perfectly valid C code.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, you're right. The problem in my application must be elsewhere then. I apologise for wasting your time.
@DanMc That's no problem, StackOverflow is meant to be a platform where you can ask questions, your not wasting anybody's time. How do you mean the problem must be elsewhere then? If there's something else wrong with your program, you're welcome to ask another question.
3

Your code looks (and runs) fine... but in regards to your "fail" comment:

In my compiler I am told that the build is successful, when the application then runs it says RUN FAILED (exit value 3, total time: 292ms

Well your program is supposed to be returning a value:

int main()

Says "I'm a function called main, I take no parameters and I return an int", however your program doesn't return anything. Try adding a return statement:

int main() {
    int i,int1=0, int2=0;
    for (i = 0; i<10; i++) {
        int1 = (int2 + 2);
        int2 = int1;
    }
    return 0; // Note: a '0' return is normally success
}

1 Comment

I was trying to quickly take out the piece of code from my application, simplify it and put it in a main function that would run it. In doing that I forgot the return statement, sorry :)!
1

you have

int1 = (int2 + 2);

not

 int1 = (int1 + 2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.