You are here:

C/Functions differing on different platforms (Mac & PC)

Advertisement


Question
Hi,

I am currently learning C programming at a local college (I'm in
week 4 of an 18-week part-time course) and have come across a
problem regarding differing functions on Windows and Mac that
I am having trouble with. I would be grateful if you can help.

The situation regarding environment is as follows...

The course we are doing is straight ANSI C (not C++, objective C
or C# or anything fancy). Most of my class are using P4's
running Windows XP or 98 and are using Dev-C as an IDE.

I am using a Mac (PowerBook G4) running MacOSX v10.4 and
xCode v2.2 as an IDE.

Both types of machine are using the GCC compiler so, as I
understand it, the code should be interpreted in exactly the
same way on both machines (Can you confirm that this is
correct?).

As you may or may not know, MacOSX is built on top of a UNIX
kernel and so, in addition to the normal GUI, it also has a direct
UNIX command line interface (CLI). Instead of using Xcode, I
often create code using emacs using the CLI and then compile it
using the standard GCC compiler (also run directly from the
command line). I have used both methods on the example below
in order to ensure that the problem that I am having does not lie
with the GUI.

So, in short, the situation is that I am using MacOSX while the
rest of the class is using Windows but we are all using the same
GCC complier which, as I understand it, is the crucial factor
when it comes to the interpretation if the source code so, any
code that works under Windows should also work under Mac. At
least that is the theory...

This is a section of the program that I am having problems
with...
-----
#include <stdio.h>

int main(void)
{
 int num1, num2, ans;
 char arithOp;

 ans=0;

 printf("Enter number 1 --> ");
 scanf("%d", &num1);

 printf("Enter number 2 --> ");
 scanf("%d", &num2);

 printf("Enter an operator (+, - or *) --> ");

/*The program is supposed to pause for user input here but
doesn't unless fflush is replaced by fpurge on the following
line*/

fflush(stdin);
scanf("%c", &arithOp);

...the rest of the code goes on to do calculations and display the
output but is not relevant to the problem)
-----

The problem comes with the fflush() function (2nd last line - see
comment in code). For some reason, when run on my Mac, the
program does not pause for user input when it hits the scanf
just below the fflush statement (though it does pause for the
other scanf functions further up). Instead of pausing, it just rolls
through without a value entered and, obviously, gives an
incorrect output result.

If I compile and run the same code on a windows machine, it
works perfectly.

I checked the man page for the fflush() function on the Mac's
command line interface (ie. opened a terminal window and typed
"man fflush") and found that, although it has been deprecated, it
is possible to use the fpurge() function instead. Once I replaced
the line fflush(stdin); with fpurge(stdin); it worked perfectly on
the Mac. Of course, this is great, but I have no idea why fpurge
works and fflush doesn't nor do I understand why it works on a
PC but not on the Mac. As I said earlier, I am pretty much a C
beginner so I am not very familiar with the ins and outs yet and
my teacher has not been much help (his exact words were,
"Dunno. You're on your own."). Gee, thanks a lot, pal.

As I have already mentioned, both the Mac and the windows
machines I tried this on were using GCC as the complier and I
was under the impression that, if the same compiler was being
used on both machines, that the same code should work the
same way on both platforms. These results seem to state
otherwise.

Are you able to tell me why this is happening? Or even just point
me in the right direction? I'm afraid that, being pretty new to C
that I just don't have enough background to be able to work this
out myself. I just don't understand why, if both types of
computer are using GCC, the code works on the PC but not on
the Mac (unless I change fflush to fpurge of course).

This raises the disturbing (for me) question of, if there are
differences, what other differences might there be that will
sneak up and bite me on the butt in the coming weeks just like
this did? What would be great is if there was a list somewhere
that listed the differences between GCC compiled code on
Windows and Mac and what you need to do to work around
those differences. Do you know if there is anything like this
available? If so, where?

Sorry, I hope this all makes sense to you, I'm a little confused by
all this and it shows in the way that I am explaining it. To
summarize, what I really need to know is...

1) Why does the same code, compiled using the same complier
(GCC) work on a PC but not a Mac?

2) What other differences might exist between C on a PC and C
on a Mac and is there a list or database of some kind that tells
me the differences so I can avoid or find alternatives and/or
workarounds?

3) If there is no specific list or database of these differing
functions, how can I avoid getting caught by them in the future?
Are the differences confined to only a certain group(s) or type of
functions so that, once I am aware of them, I can avoid them or
use an alternative whenever they crop up? This particular
problem took me over 4 hours to work out today. If it is going to
happen regularly, I am going to fall behind in my class very
quickly.

4) If/when these differences pop up, how can I work around
them so that the code will work on my Mac?

Thanks for taking the time to read all of this. It ended up being longer
than I had initially hoped but I wanted to be as specific as possible so
that you knew exactly what my problem was. Like I said, I hope
it make sense and I REALLY hope that you are able to help me.

Thanks in advance,
Mark

PS: A couple of years ago (during a less busy time in my life), I was
signed up as a Mac IT expert on this site so I know that, even though it
is quite rewarding to be able to help people, it can be time consuming
and sometimes a strain. I have experienced it from both sides of the
fence, so I know what it is like and I really appreciate that you are
willing to donate your valuable time to help myself and people like me.
Regardless of whether or not you are able to solve my problem, I'd like
to thank you sincerely for your time and effort. Thanks very much!

Answer
> Both types of machine are using the GCC compiler so, as I
> understand it, the code should be interpreted in exactly the
> same way on both machines (Can you confirm that this is
> correct?).
GCC is not an interpreter. It is a compiler and creates an object code.
And it will be linked to the libraries by the linker and an executable will be created.
So, the C code may be same. But, the libraries can be different.

> 1) Why does the same code, compiled using the same complier
> (GCC) work on a PC but not a Mac?
The reason is, there is a difference between the platform.
And hence the drivers will be different.
this could be the reason for the differences.

> 2) What other differences might exist between C on a PC and C
> on a Mac and is there a list or database of some kind that tells
> me the differences so I can avoid or find alternatives and/or
> workarounds?
I don't think there is any checklist for this.
Your experience must be able to help you here.

> 3) If there is no specific list or database of these differing
> functions, how can I avoid getting caught by them in the future?
> Are the differences confined to only a certain group(s) or type of
> functions so that, once I am aware of them, I can avoid them or
> use an alternative whenever they crop up? This particular
> problem took me over 4 hours to work out today. If it is going to
> happen regularly, I am going to fall behind in my class very
> quickly.
You will have to learn from your mistakes:-)

> 4) If/when these differences pop up, how can I work around
> them so that the code will work on my Mac?
There is no set of rules to achieve this.
As I said before, it is your experience which has to help.
If you are a beginer, then you need to go on working, make mistakes, try to correct them and learn from these.

So, in your case, the problem is the way fflush() is written. fflush() will be doing some work related to your memory buffer and memory is hardware related.
So, there must be difference between the driver written on MAC and Windows.
And all your problem is because of the scanf() funciton, which is buffered.
If you use a more raw function like read(), I think your problem can get solved more easily.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.