6 Replies - 775 Views - Last Post: 25 October 2010 - 01:59 PM Rate Topic: -----

#1 asgot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-October 10

Compilers giving different output

Posted 25 October 2010 - 01:45 PM

Hello everyone. In an exam for my C programming class, there was a question where you had to trace the program and write the output. I got the question marked wrong, but when I came home to test it, my compiler gave the answer I had written. I went to my professor and we concluded that our compilers were giving different answers. So I've tested on a few different compilers, and it seems the newer compiler versions give my answer, and older ones give his.

#include <stdio.h>
int f(int *a, int c, int B)/>;
int main (void) {
    int a=6, b=4, c=7;
    a = f(&b, f(&c,a,B)/>,c);
    printf("a= %d b= %d c= %d\n",a,b,c);
    return 0;
    }

int f(int *a, int c, int B)/> {
    *a = b - 1;
    c = c*2 - (*a);
    b = b + 1;
    printf("a= %d b= %d c= %d\n",*a,b,c);
    return c - *a + 1;
}




On Visual C++ 2010 Express, mingw32-gcc.exe version 4.4.1 (shipped with Code::Blocks), and gcc 4.4.5 on Ubuntu, I get this output:
a= 3, b= 5, c= 9
a= 6, b= 8, c= 8
a= 3, b= 6, c= 3


On mingw32-gcc.exe version 3.4.2 (shipped w/ Dev-C++) and my professor's computer using GCC (I don't know what version, but I think his OS was some form of BSD) we get this output:
a= 3, b= 5, c= 9
a= 2, b= 4, c= 12
a= 11, b= 2, c= 3


The difference seems to lie in line 5 in the program, where there is a function call within a function call. It seems newer compilers use the original value of c in the third parameter to the outer function call, while old compilers use the updated value after the inner function is called, where c is updated to 3, because it is pass by reference.

So my question is: does anyone know what the C standard says about this, and has the C standard changed something that would account for the difference between older and newer compilers?

This post has been edited by asgot: 25 October 2010 - 01:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Compilers giving different output

#2 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 218
  • View blog
  • Posts: 591
  • Joined: 04-August 09

Re: Compilers giving different output

Posted 25 October 2010 - 01:51 PM

Your code shouldn't even compile. You have undeclared variable in main and f.

where is B declared in main?? (line 5)
where is bdeclared in f (lines 11,13 and 14)

This post has been edited by simeesta: 25 October 2010 - 01:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Compilers giving different output

Posted 25 October 2010 - 01:53 PM

Quote

does anyone know what the C standard says about this
Implementation defined.

EDIT: @simeesta: forum bug. It's lowercase b, being changed to uppercase for whatever reason.

This post has been edited by Oler1s: 25 October 2010 - 01:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 asgot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-October 10

Re: Compilers giving different output

Posted 25 October 2010 - 01:53 PM

View Postsimeesta, on 25 October 2010 - 12:51 PM, said:

Your code shouldn't even compile. You have undeclared variable in main and f.

where is B declared in main??

Sorry, for some reason it got changed to a capital B when I pasted the code. Weird.
Was This Post Helpful? 0
  • +
  • -

#5 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 218
  • View blog
  • Posts: 591
  • Joined: 04-August 09

Re: Compilers giving different output

Posted 25 October 2010 - 01:54 PM

sorry. I forgot it sometimes does that.
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1628
  • View blog
  • Posts: 3,089
  • Joined: 30-May 10

Re: Compilers giving different output

Posted 25 October 2010 - 01:57 PM

It's just another variation of these examples.
http://www.dreaminco...ost__p__1042539

> does anyone know what the C standard says about this
Yes, the C standard says that such behaviour is undefined.
Which roughly translates as "sorry bub, you're on your own from now on, expect the unexpected".

The code is broken.

Read this -> http://c-faq.com/expr/index.html
Was This Post Helpful? 3
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: Compilers giving different output

Posted 25 October 2010 - 01:59 PM

View Postsimeesta, on 25 October 2010 - 02:51 PM, said:

Your code shouldn't even compile. You have undeclared variable in main and f.

where is B declared in main?? (line 5)
where is bdeclared in f (lines 11,13 and 14)


That's the board software messing with b ) and converting it to an emoticon.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1