-1

I'm new to programming. So, details are appreciated.

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

    int main()
    {
        float x, f(x);
        printf("Enter x=");
        scanf("%f", &x);
        f(x)= 3*pow(x, 5)- 5*sqrt(x)-6*sin(x); /*in this line compiler shows error*/
        printf("f(x)= %f", f(x));
        return 0;
    }
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • http://code.geeksforgeeks.org/5x2pXV – msc May 19 '17 at 05:47
  • @rsp Comments which answer the question should be answers. Answers which are link-only should be edited to provide explanation and not require the reader to visit externally. – Yunnosch May 19 '17 at 05:52
  • If you're new to programming and want to learn C, a [**good book**](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) would serve you above all, in particular as a reference for syntax, language specifics, and library offferings. I suspect you're trying [to do something like this](http://ideone.com/mYEsD6), but you should spend the dues to learn more about the language and understand how it works. – WhozCraig May 19 '17 at 06:02
  • @WhozCraig I like your helpful reference to books, not so much your link-only-answer-in-comment however. – Yunnosch May 19 '17 at 06:28

3 Answers3

0

In your code, f(x) is a valid identifier, but not a variable. It's a very poorly written (and now invalid, as per the latest C standard) function prototype. You cannot assign to it, it's not a modifiable lvalue.

That is why in case of

  f(x)= 3*pow(x, 5)- 5*sqrt(x)-6*sin(x);

compiler screams.


On why your code did not throw an error for the invalid format, it's the legacy support in compiler. In your case

  float x, f(x);

is treated the same as

  float x, float f ( int x ) ;  //missing data type is treated as 'int', 
                                // DON'T rely on this "feature"
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

The following could work.

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

int main()
{
    float x, f;

    printf("Enter x=");
    scanf("%f", &x);
    f = 3 * pow(x, 5) - 5 * sqrt(x) - 6 * sin(x);
    printf("f(x)= %f", f);

    return 0;
}
Yunbin Liu
  • 1,484
  • 2
  • 11
  • 20
0

Excuse me for assuming that you are a C beginner looking for the way to write a function in C. There are many C tutorials out there, I recommend finding one for further learning.
Here is an example of using an actual C function for what you are doing.

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

/* first define the function */
float f(float x) /* function head */
{ /* start of function body */
    return  3*pow(x, 5) - 5*sqrt(x)-6*sin(x);
} /* end of function body and definition */

int main(void)
{
    float x; /* definition of function has already happened, so no f(x) here */
    printf("Enter x=");
    scanf("%f", &x);

    printf("f(x)= %f", f(x) /* call the function */);

    /* Note that some coding styles do not like calling a function 
       from within a more complex statement. Using a variable to
       take the result of the function is preferred.  
       I chose this way to stay more similar to your own code.
     */
    return 0;
}
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Another answer, by @YunbinLiu shows a clean way without a function, using a variable instead. Your own way is probably somewhere in between. – Yunnosch May 19 '17 at 06:15
  • An answer by @SouravGHosh explains why the declaration of `float f(x);` does not already give errors, useful additional info. – Yunnosch May 19 '17 at 06:21