-2

Maybe a stupid question, but I don't understood why my program in C works with integers, but not with float.

#include <stdio.h>

main()
{
  float a, b;
  a = 4.5;
  b = 9.6;

  printf("Result of %.2f + %.2f = %.2f", a, b, added_up(a, b));
}

/****************************************/

added_up(a,b)
{
  float c;
  printf("Value a: %.2f \n", a);
  printf("Value b: %.2f \n", b);
    c = a + b;
  return (c);
}

My output:

Value a: 0.00
Value b: 0.00
Result of 4.50 + 9.60 = 0.00

If I define the float to int the program works. What's the failure? Should I declare the function as float, but if I try I get another failure.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
Hermann12
  • 1,709
  • 2
  • 5
  • 14
  • 3
    You never declared the return type for `added_up`. That's a legacy construct which defaults to `int`, but would ideally give an error. Similarly, you forgot to add a forward declaration for `added_up`. Again, it defaults to a return type of `int` with unknown argument types. Add complete prototypes, and forwards, for all your functions. Then try it again. – Tom Karzes Oct 29 '22 at 12:31
  • 3
    Whatever resource you're using to learn C, it's severely outdated. In C since a few decenniums you *must* declare function with a return type, and *must* use actual types for variable declarations, and *must* declare functions before you call them. – Some programmer dude Oct 29 '22 at 12:46
  • gcc compiler gives only 3 warnings, but no error message. – Hermann12 Oct 29 '22 at 13:00
  • The warnings no doubt indicate your problem(s). The parameters to added_up() are int, not float. Turn up your compiler warning levels (max when you're learning the language) and pay attention to them. – Paul Lynch Oct 29 '22 at 13:08
  • @Hermann12 warnings should most of the time be considered as error. Especially those containing the words _implicit_ and _defaults_. – Jabberwocky Oct 29 '22 at 13:20
  • 1
    Add the options `-Wall -Wextra -Wpedantic` and you'll get more. Also add `-Werror` to turn all warnings into errors, which is a good habit, and actually *fix* the warnings. The compilers are very good at finding problems in your code. – Some programmer dude Oct 29 '22 at 13:35

1 Answers1

2
  1. You must declare a function before using it

  2. You have to define what type the function returns and you must define type of arguments you are passing to the function


#include <stdio.h>
float added_up(float a,float b); // 1)
int main()
{
  float a, b;
  a = 4.5;
  b = 9.6;

  printf("Result of %.2f + %.2f = %.2f", a, b, added_up(a, b));
  return 0;
}

/****************************************/

float added_up(float a,float b)// 2)
{
  float c=a+b;
  printf("Value a: %.2f \n", a);
  printf("Value b: %.2f \n", b);
  return c;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Damian
  • 56
  • 3
  • With the comments from @Tom above I figured it out, too. For the main I used now 'void main(void) without 'rint main() and 'return 0''. I think a lot of flavors are available in C. – Hermann12 Oct 29 '22 at 13:15
  • 2
    @Hermann12 `int main(void)` is the correct way. – Jabberwocky Oct 29 '22 at 13:17
  • @Hermann12 `main` returns an `int`, which is the exit status. It will implicitly return a value of `0` (as a special case), but it should still be declared to return an `int` (as opposed to `void`). – Tom Karzes Oct 29 '22 at 13:23
  • @Jabberwocky thank you for editing answer. I am newbie user in Stack and i didn't know how to format answer :) – Damian Oct 29 '22 at 13:26