Re: another printf question!!
From: Jonathan Burd (jburd_at_nospam.com)
Date: 02/03/05
- Next message: Alex Fraser: "Re: Memory management and allocation"
- Previous message: Michael Mair: "Re: Pointer-to-pointer-to-pointer question"
- In reply to: drM: "another printf question!!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 03 Feb 2005 13:15:38 +0530
drM wrote:
> I have looked at the faq and queried the archives, but cannot seem to
> be able to get this to work. It's the usual factorial recursive
> function, but that is not the problem. It hangs after the user enters a
> number. However, as I indicate, if one adds something else after the
> number, the function proceeds and finishes successfully.
>
> I would appreciate some helpful hints.
> thanks in advance.
>
>
>
> #include <stdio.h>
> #include <math.h>
Don't need math.h.
>
> int factorial( int);
> main() {
``implicit int"? Please make the return type explicit.
Choose one of these formats for your main function definition:
int main (void) ...
or
int main (int argc, char *argv[]) ...
or
any format type-compatible with the above.
>
> int i, k;
>
> printf ( "Please enter a number "); // <<<<< seems to hang here if
It's best to use fflush(stdout) after a printf call, the format string
of which does not end with a '\n'. This way you can be sure that
your prompt will definitely be displayed.
> user enters number only, but
> // works if user enters number eg 8 plus something else eg the letter
> "m".
> scanf (" %d ", &i);
Here's the problem. This should be scanf("%d", &i);
Whitespace characters inside strings *do* count.
A _better_ approach is to read input from stdin into a string buffer
using fgets(), and then use sscanf() to read the integer from
the string buffer into your variable, although you can use scanf
if you know how to use it properly.
[Note: Remember that fgets() will also include an
additional '\n' just before the EOS (end of the string == '\0').]
>
> k = factorial (i);
> printf ( " the factorial of %d is %d\n ", i, k);
>
> return (0);
Although, it is not incorrect, do not do this because return is not a
function.
>
> }
>
>
> int factorial ( int j){
> if (j == 1)
>
> return 1;
>
> return j * factorial(j-1);
>
> }
>
>
>
Including a whitespace character at the end of a scanf()
format string is a common mistake. For example, see what this does:
/* err_scanf_space_end.c */
#include <stdio.h>
int
main (void)
{
int foo;
int ret;
printf ("Enter an integer: ");
fflush (stdout);
if ((ret = scanf ("%d ", &foo)) == 1)
printf ("foo: %d\n", foo);
/* ... */
return 0;
}
Output:
=======
enter an integer: 123
4
foo: 123
=======
Don't include whitespace characters at the end of a scanf format string.
Whitespace characters inside strings *do* count.
[Please don't use Google to post. It is buggy and reading
improperly formatted code hurts the eyes. Many people will
simply ignore posts with improperly formatted code. I suggest you
get a decent NG client and subscribe to a NG server.]
Regards,
Jonathan.
-- Email: "jonathan [period] burd [commercial-at] gmail [period] com" sans-WSP "We must do something. This is something. Therefore, we must do this." - Keith Thompson
- Next message: Alex Fraser: "Re: Memory management and allocation"
- Previous message: Michael Mair: "Re: Pointer-to-pointer-to-pointer question"
- In reply to: drM: "another printf question!!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|