-5
float x = 4.5;
main()
{
    float y,float f(); 
    x*=2.0;
    y=f(x);
    printf("\n%f%f",x,y);
}

float f (float a)

{
    a+=1.3;
    x-=4.5;
    return(a+x);
}

This program is from the book: Let us C, by Yashwant kanetkar. And the output it says is 4.500000 5.800000. I get error.

sunp
  • 21
  • 6

5 Answers5

1
//global variable x, can be accessed from anywhere
float x = 4.5;

//inform the compiler, that a function that is defined later, but it exists in the code, and to use it before it's defined, we need to know how it looks like:
float f(float a);

//I need to specify what my main function returns, typically "int"
int main()
{
    //local variable for saving the result in:
    float y;

    //multiply x, with 2, and save the result in x:
    x*=2.0;

    //run the function:
    y=f(x);

    //show the result:
    printf("\n%f%f",x,y);

    //return a value to let the OS know how the program ended (0 is success)
    return 0;
}

//define the function
float f (float a)
{ 
    //whatever a is provided, add 1.3 to it before doing anything else
    a+=1.3;
    //subtract 4.5 from the global variable, before doing anything else:
    x-=4.5;
    //return the sum of a and x, as the result of this function.
    return(a+x);
}

the math here is:

x = 4.5
x = (x * 2) = 9
a = (x = 9) = 9
a = (a + 1.3) = 10.3
x = (x - 4.5) = 4.5
y = a + x = (4.5 + 10.3) = 14.8
Henrik
  • 2,180
  • 16
  • 29
0

Point 1:

Remove float f(); from main(). Declare float f(); outside main(). Thus, it will serve the purpose of forward declaration.

Point 2.

Remove the , and add a ; after float y.

Point 3.

main() should be int main(void).

Note: Consider adding an explicit return statement at the end of main(). Though is is not mandatory, but is considered a good practice.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

float y,float f();

Use semicolon after float y

float y;

And float f(); //(wrong prototype). you need to pass argument to this function.

No need to declare function inside main. you can remove float f(); from your main() function.

main not returning any value

Use

int main()
{
   //your code
    return 0;
}
Himanshu
  • 4,327
  • 16
  • 31
  • 39
0

When you declare f():

float y,float f();

you aren't using a prototype. So when the call to f() is made:

y=f(x);

the float argument x is being promoted by the compiler to a double type.

To solve the problem, declare the function using a full prototype:

float f(float a);
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
0

There is no need to declare functions like variables in your main. 2. Also, main() should be int main(). 3. Define/ declare your function before using it.

 #include <stdio.h>
float x = 4.5;

float f (float a)

{
a+=1.3;
x-=4.5;
return(a+x);
}
int main(void) {
// your code goes here
float y;

x*=2.0;
y=f(x);
printf("\n%f%f",x,y);
return 0;
}

Ideone link: http://ideone.com/W7V1Qr

Ayushi Jha
  • 4,003
  • 3
  • 26
  • 43
  • But if I declare main() first and then the other function, why does it show error: ag.c:13:7: error: conflicting types for ‘f’ float f (float a) ^ ag.c:9:7: note: previous implicit declaration of ‘f’ was here y=f(x); – sunp Apr 20 '15 at 07:43
  • That is because whenever you refer to a function, the compiler should already know about that function. – Ayushi Jha Apr 20 '15 at 07:57
  • Yes, Fundamentals! just forgot. Got it now though. Thanks. – sunp Apr 20 '15 at 08:03