0

I have the following code:

#include <iostream>

using namespace std;

Sum (int a, int b)
{
    int x = a - b;
    //cout << x << " \n";
    return x;
}

int main()
{
    int s1 = Sum(3, 6);
    cout << s1;

    return 0;
}

System info: Win 7 Sp1 x64 Ultimate/Professional or Win 8.1 x64 Code Blocks 16.01 MinGW Debugger name and version: GNU gdb (GDB) 7.6.1 compiler: GNU GCC Compiler

This code compiles with no problems, but this IS the problem, there should be errors.

1) Function Sum, has no return value, on http://cpp.sh/ it doesn't let me compile because of this.

2) Variable's s1 value is -3 whether I write "return x" or not.

It somehow passes the value of x everytime BUT if I uncomment the cout statement above the "return x" everything starts to work out as expected, what the hell :) --> s1 will have a random value when no return statement is in place (because it was not initialized prior to being used for the function call) and -3 when the return is there.

I've tried this on 3 separate computers and they all exhibit the same behaviour. So I don't think the machine is the problem. I also tried using a different compiler but I don't know if I configured them correctly and they don't have a debugger right ? I tried Borland c++ and Digital Mars. Borland has a new version 10.1 instead of the 5.5 that codeblocks supports and I couldn't make the new one work. I don't even know if this is a compiler or program issue ?

I'm trying to learn C++ and this is very annoying. Our teacher is using the same software in class but on Linux and it works perfectly.

Off topic: Is there a way to insert code with line numbers here ? First post here so i'm still new at this :).

Thank you !

12
  • 1
    Use stricter compiler options. Commented Dec 17, 2016 at 12:46
  • Which version of GCC are you using? Because like you said that should not compile with any version of any C++ compiler. Commented Dec 17, 2016 at 12:51
  • What is THE question? Why an incorrect code behaves undefinedly? Commented Dec 17, 2016 at 12:51
  • ^^ What these guys said. Case and point: ideone.com/b7dFUe Commented Dec 17, 2016 at 12:52
  • 1
    To fill things in a bit: C, back in the olden days, had "implicit int": function declarations with no return type indicated that the function returned int. That was removed for C99, but many compilers continue to support it so the old code doesn't break. Commented Dec 17, 2016 at 13:15

2 Answers 2

1

Go to "Project" -> "Build Options" -> "Compiler Settings" tab -> "Compiler Flags"

And disable -fpermissive

-fpermissive Downgrade some diagnostics about nonconformant code from errors to warnings. Thus, using -fpermissive will allow some nonconforming code to compile.

Or disable it using pragma on the top of your code:

#pragma GCC diagnostic ignored "-fpermissive"

Also you could try to add the flag "-pedantic" in "Compiler Flags" tab

BTW:

If you try online:

#pragma GCC diagnostic error "-fpermissive"

using namespace std;

Sum (int a, int b)
{
    int x = a - b;
    //cout << x << " \n";
    return x;
}

int main()
{
    int s1 = Sum(3, 6);
    cout << s1;

    return 0;
}

You got exactly same behavior you described!

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

3 Comments

I don't think -fpermissive is enabled on my machine. I can't find it in the list of options. postimg.org/image/qavbl3a6n here is a screenshot
Maybe in "Other Compiler options"?
[EDITED] added #pragma
0

As Rama said, you might have enabled -fpermissive in your codeblock. Go to "Project" -> "Build Options" -> "Compiler Settings" tab -> "Other options" and delete -fpermissive.

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.