# Thread: Beginner Question -- From Absolute Beginner's Guide to C

1. ## Beginner Question -- From Absolute Beginner's Guide to C

I have begun using Absolute Beginner's Guide to C to learn the C programming language, and I am trying to work through examples in the book. However, I don't seem to be able to get the below copied program (page 31) to run. I realize that the scanf function doesn't play nice with getchar, but it's what I am being told to use at this point. I am hoping someone can give me some pointers here. Tried running after successful compilation in Dev C++ and the command line. Result for avg I am getting is a very large random number.

Code:
```* Written by David Ghelerter, finished on December 25, 2013*/
/* Filename: avg.exe*/
/* Computes an average*/

#include <stdio.h>
main()
{
float gr1, gr2, gr3; /* Variables to hold grades */
float avg; /* Variable holds averages*/
/* Asks for a students grades */
printf("What grade did the first student get ?\n");
scanf(" %.1f", &gr1);
fflush(stdin);
printf(" What grade did the second student get ?\n");
scanf(" %.1f", &gr2);
fflush(stdin);

printf("What grade did the thrird student get ?\n");
scanf("%.1f", &gr3);
fflush(stdin);

avg = (gr1 + gr2 + gr3)/3.0;
printf("\nThe student average is %.2f", avg);
getchar();
return 0;

}```

2. To start, try changing your calls to scanf() from:

Code:
`scanf("%.1f")`
to

Code:
`scanf("%f")`
While scanf has provision for field width (i.e., you can limit the amount of characters taken in), it does not do precision (in other words, it is not analogous to printf() where you could specify how many digits after the decimal point are displayed). Your best bet might be to consult your local documentation for the scanf() function.

Google Search: man scanf

3. Excellent! Thanks for your insight

4. It would also be useful to check the return value from scanf() - it tells you how many values it succeeding in reading.

Also, if your book is guiding you to use fflush(stdin), burn it. fflush() has undefined behaviour for any input stream. It does not necessarily, contrary to what poor books or library documentation often say, discard pending input.

5. @ the OP: grumpy is right about the book you are using. Unfortunately it seems that there are multiple poor introductory C books published for every good one. One of the challenges of writing a tutorial type book is to quickly get the reader writing programs that work, in order to keep things interesting. After all, who wants to spend time writing and compiling programs that don't really do anything?

Yet one has to walk before they can run. The quickest (easiest?) way to get some input in C would be either the gets() function or (more often seen) the scanf() function. Yet these pose their own problems, and what often accompanies these functions in example code are hacky solutions to make them "work." As grumpy noted, fflush(stdin) is not a great way of doing things.

What is the best course of action then? As grumpy hinted, get a better book if possible. I would recommend The C Programming Language: Second Edition. It's a bit old now - it covers C89 standard C - yet there is still much there that is helpful. If you were to get that book, I would recommend reading Steve Summit's Notes to Accompany The C Programming Language, by Kernighan and Ritchie (``K&R'') along with it. Steve Summit is the maintainer of the comp.lang.c C-FAQ.

And speaking of FAQs, you should familiarize yourself with these three (in order):

Being familiar with these will help you to recognize bad code practice. For example, all three answer questions about using fflush(stdin):

comp.lang.c FAQ:
• 12.26a How can I flush pending input so that a user's typeahead isn't read at the next prompt? Will fflush(stdin) work?
• 12.26b If fflush won't work, what can I use to flush input?

cprogramming.com FAQ:

CPWiki FAQ:

There is a lot of reading there - perhaps it is overwhelming - but don't be discouraged. Remember you have to walk before you can run, but don't stop at walking.

6. Kermit and Grumpy:
Thanks very much for your insightful comments and help getting started. I'll familiarize myself with the materials you suggested.

Popular pages Recent additions