-1

I'm sitting right in front of my computer but I don't have a clue where my mistake is. So here is my code in C:

#include <stdio.h>
#include <math.h>

double f (double x);

int main ()
{
    double x;

    printf("Please type in a decimal number: ");
    scanf("&lf", &x);

    printf("%", f(x));

    return 0;
}

double f (double x)
{
    return (fabs(x) * sin(x) * 10e-2);
}

I read the code multiple times but I'm either too stupid or just not able to find the mistake. Please help me. I would be very glad if you'd help me.

Hopefully, you guys have an idea or a tip for me (even that would be helpful).

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Xandi200
  • 23
  • 3
  • 5
    Check your compiler, you may need to enable more warnings. `printf("%f", ..` – Jongware Nov 03 '18 at 15:08
  • 2
    Your format strings are both wrong; Take another look into your C book how they work. – tkausl Nov 03 '18 at 15:08
  • Possible duplicate of [Correct format specifier for double in printf](https://stackoverflow.com/questions/4264127/correct-format-specifier-for-double-in-printf) – Jongware Nov 03 '18 at 15:10
  • 2
    `I'm sitting right in front of my computer but I don't have a clue where my mistake is` Don't simply sit try to look at what compiler is telling. – kiran Biradar Nov 03 '18 at 15:10
  • 2
    The bit about _"sitting right in front of your computer"_ is entirely redundant - all bugs are written by people sat in front of a computer, yet you sound surprised that sitting there does not resolve the problem. – Clifford Nov 03 '18 at 15:21
  • Read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and read *carefully* the documentation of every function you use, notably of [scanf](http://en.cppreference.com/w/c/io/fscanf), [printf](https://en.cppreference.com/w/c/io/fprintf), [sin](https://en.cppreference.com/w/c/numeric/math/sin), [fabs](https://en.cppreference.com/w/c/numeric/math/fabs). Compile with all warnings and debug info, so `gcc -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/) – Basile Starynkevitch Nov 03 '18 at 15:21

1 Answers1

2

Change this:

scanf("&lf", &x);

to this:

scanf("%lf", &x);

and this:

printf("%", f(x));

to this:

printf("%lf", f(x));

Instead of seating in front of the computer, ask for the compiler's help, by compiling with -Wall flag to enable a significant amount of warnings. Then, your posted code should have given you:

prog.c: In function 'main':
prog.c:11:11: warning: too many arguments for format [-Wformat-extra-args]
   11 |     scanf("&lf", &x);
      |           ^~~~~
prog.c:13:13: warning: spurious trailing '%' in format [-Wformat=]
   13 |     printf("%", f(x));
      |             ^
prog.c:13:12: warning: too many arguments for format [-Wformat-extra-args]
   13 |     printf("%", f(x));
      |            ^~~

which would have helped you pinpoint the problem, and start searching the internet, until for example, Reading in double values with scanf in c, would have come around!


PS: I assumed you use GCC compiler. If not, make sure to enable the compilation warnings of your compiler.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    Thank youuuuuuuuuuuuuuuuuuuuuuuuuu. I didn't see that I wrote &lf instead of %lf and % instead of %lf. :D – Xandi200 Nov 03 '18 at 15:14
  • Good advice on compiler warnings, but it is not a given that the unspecified compiler in question is gcc (or other for which `-Wall` is a valid option). – Clifford Nov 03 '18 at 15:17
  • I agree @Clifford, updated! But I think, it's enough for a not so experienced person, for now, at least! – gsamaras Nov 03 '18 at 15:20
  • @gsamaras Possibly, but equally it would probably confuse a novice using Visual Studio who is not even aware that there is a command line to set options for. In fact until VS2015, VS did not even support format specifier checking. – Clifford Nov 03 '18 at 15:25
  • The gcc option `-Wall` does not enable "all warnings". It is a good idea to also use `-Wextra`. See http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html – Dipstick Nov 03 '18 at 15:35
  • 1
    @Dipstick correct, updated. But again, for a start `-Wall` is very good. – gsamaras Nov 03 '18 at 15:48