Re: Newbie C Questions
Re: Newbie C Questions
- Subject: Re: Newbie C Questions
- From: Christopher B Hamlin <email@hidden>
- Date: Tue, 29 Jan 2002 23:10:11 -0500
On Tuesday, January 29, 2002, at 10:05 PM, Tom McKenna wrote:
Originally I planned on teaching myself Cocoa from the "Learning Cocoa"
book. Since my programming experience is limited to Commodore Basic, I
found the book to be somewhat over my head. (OK, way over my head...)
I then decided to learn C, since Objective-C is ANSI C plus all of the
object-oriented material. Since I was able to get a beginner's book on
C, it has been much easier to learn.
Here are my questions (finally):
* When I am using Project Builder and I want to create a simple C
application, what kind of project should I be opening? (I have been
using the 'Standard Tool' option in PB...)
Sounds right to me.
* Once I finish this book on C, I would like to learn Objective-C. Is
there a beginner's book that doesn't work under the assumption that you
know C++ or Java? If not, what should I do next?
Go into PB. Select Help -> Cocoa Help. Look under "Getting Started".
* One of my tutorial apps compiles fine in PB, but it won't run in PB.
It does run perfectly in the Terminal. Am I doing something wrong?
Here's the code:
#include <stdio.h>
/* Filename: Avg.C */
/* Computes the average of three class grades */
main ()
{
float gr1, gr2, gr3;
float avg;
// Asks for each student's grade
printf("What grade did the first student get? ");
scanf(" %f", &gr1);
printf("What grade did the second student get? ");
scanf(" %f", &gr2);
printf("What grade did the third student get? ");
scanf(" %f", &gr3);
// Calculates the average of the three student's grades
avg = (gr1 + gr2 + gr3) / 3.0;
printf("\nThe student average is %.2f", avg);
return 0;
}
If you click in the output window (above the source), then type a
number
and hit return three times you will see that it is running. The problem
is
that stdout (standard output, where printf goes) is buffered. Maybe
there is
some easy setting in PB, but I changed your output to this:
// Asks for each student's grade
printf("What grade did the first student get? "); fflush
(stdout);
scanf(" %f", &gr1);
fprintf(stderr, "What grade did the second student get? ");
scanf(" %f", &gr2);
printf("What grade did the third student get?\n");
scanf(" %f", &gr3);
And got this:
What grade did the first student get? 2
What grade did the second student get? 3
4
What grade did the third student get?
The student average is 3.00
tom has exited with status 0.
You can see that the first two solutions did work.
The first one flushes all the output in stdout.
The second one prints to stderr which isn't buffered.
I thought that stdout would be buffered until a newline.
I even tried \r and \r\n (and \n\r) and this didn't work. That
surprised me.
Anyway, maybe this workaround can help until someone
really knowledgeable comments.
Regards,
Chris Hamlin