Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
This is my first post here. So, firstly, let me say "Hi", I expect that I'll be a pretty regular visitor here in future, I certainly need the help and it's my hope that, some time in the future, I will be in a position to return the favour.
In the mean time though...
I am pretty much a rank newbie to C (I am currently doing a part time beginner's C course and I am in week 4 of 18) so I apologise in advance to all you veteran C coders out there who are no doubt preparing to roll your eyes and say to yourself, "Is this guy is thick or what?". I throw myself on the mercy of the forum and plead newbiness in the first degree.
On to my specific question...
After reading the forum FAQ and searching for posts on this topic, it seems that the common consensus is that using fflush(stdin) is a no-no (although, since my current knowledge base is so narrow, I don't fully understand the reason). However, I think that the fact that it's use is discouraged partially answers my question - but not all of it - so I hope no one minds if I lay out my problem.
If anyone could help me make some sense out of it or even just point me in the direction of some clarifying documentation, I'd be extremely grateful. I don't expect people to hand me answers on a platter and I'm happy to put in the spade work, but I just have no idea where to even start on this one and my lack of background knowledge is a huge hindrance. In short, I am completely stumped.
On to the issue at hand...
Everyone in my class, except me, is using Windows based machines (WinXP or 98) but, since I have a PowerBook G4 (1.5Ghz, 100GB HD, 1GB RAM), I am running MacOSX 10.4.4. Both platforms are using GCC to compile code so, from what little I know (or thought I knew) about compliers, I figured that there should be no difference, code-wise, between the two. However, after today, I am not so sure.
I am also using Xcode v2.2 as an IDE but I also use emacs and GCC in a terminal session for short and/or simple programs, which is most of the code I have written so far.
As I said, I'm 4 weeks into an 18 week C course (ie. plain vanilla C, not C++, Objective C or C#) and today I bumped up against what seemed to me to be a very strange problem. While compiling an in-class example program, I came across the fflush(stdin) function for the first time. The relevant section of the code follows...
...the rest of the code goes on to do calculations and display the output but is not relevant to the problem.
int num1, num2, ans;
printf("Enter number 1 --> ");
printf("Enter number 2 --> ");
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*/
As indicated by the comment line in the code above, the program is supposed to pause and wait for user input after each prompt (ie. at each scanf function). This works fine the first 2 times but, when the third comes along (right after the fflush(stdin) function), it just rolls right through and doesn't wait for user input. Of course, as you will have realised, it doesn't get an entry for that variable so assumes a blank and gives an incorrect output result. I figured that I had just made a stupid error somewhere and set about finding it but 20 minutes of going over the code with a fine toothed comb revealed no errors. This got me good and confused.
However, what really got me confused was that the exact same code that did this on my Mac worked perfectly on a Windows machine.
After several hours of trying various things, scratching my head and saying "What the <insert curse-word of your choice>?!?" a lot (my teacher was not a lot of help. His advice was basically ..."I dunno about Macs. You're on your own."), I stumbled across a man page that seemed to say that fpurge could be substituted for fflush. I didn't really understand it but I did the swap and...BINGO!...working code on the Mac. This was great but I still had no idea why fflush worked on the Windows machine but not the Mac.
I mentioned the fix to my teacher who told me that fflush was the standard proper C function and that fpurge was deprecated and non-standard. Now, please correct me if I am wrong but that seems to be the exact opposite of what the posts I have read here say, so, as you might expect, I am now very confused. Who do I believe?
So, can anyone explain to me what is going on here? Why is it that the same source code can be run through the same complier and come up with 2 different outcomes? I apologize profusely if this is a really dumb question. I am aware that topics very similar to this have already been discussed on this forum (and I did read many of them) but I wasn't able to find anything that specifically related to this problem and, to be honest, the few discussions that did touch on this topic were a fair bit above my head so I didn't really follow them.
I am hoping someone will be able to really spell it out in newbie-tastic terms for me.
Putting this specific problem aside for a moment, can anyone tell me if I am likely to come up against anything like this again? Are there many functions that work for windows but don't work at all (or don't work properly) on a Mac (and vice versa)? Is there some sort of list or database that details the differences between the two?
My concern is that I'm going to encounter things like this regularly and it is going to severely hamper my progress in class which would be a pity because, apart from this little hiccup, I have really been enjoying learning about C and I'd hate to get left behind simply because I happen to use a different OS to the teacher.
RELATED BUT SEPARATE QUESTION - NOT IMPORTANT BUT I AM INTERESTED
Just to further complicate the issue, what about Linux (or UNIX) systems? Would the code for a Linux/UNIX machine be the same as the code for a Mac, the same as the code for a Windows box, or would it be a third, ever-so-slightly-different-to-both-the-others?
As you can probably tell, this issue has pretty much thrown me for a loop and I'm now questioning my whole understanding (such as it is) of the way compilers interact with the OS and hardware and of C in general. I really feel like the rug has been pulled out from under me just when I thought I was getting a handle on how it all worked.
Anyway, anyone who can point me in the right direction will certainly earn my undying gratitude.
Once again, please accept my apologies if I've asked anything really silly and/or unforgivably basic and my thanks for taking the time out to help me, the poor dullard, out with what is bound to be for most of you, I'm sure, such an elementary concept.