I just started learning C and trying to run the following code (given in the book I am using) in Code::Blocks 10.05 compiler for windows.

"error: too many arguments to function add_two_numbers ()"

#include <stdio.h>

int main()
{
  int var1, var2;
  int add_two_numbers ();

  var1 = 1;
  var2 = 53;

  add_two_numbers (var1, var2);
  add_two_numbers (1, 2);

  exit(0);
}
add_two_numbers (int a, int b)               /* Add a and b */
{
  int c;

  c = a + b;
  printf ("%d\n", c);
}

Please advise.

Please advise.

You're compiling as C++. Compile as C instead and the error will go away, but the code is still slightly off in terms of modern correctness. This is more correct:

#include <stdio.h>

/* Full prototype (file scope is conventional) */
int add_two_numbers(int a, int b);

/* Full prototype & definition */
int main(void)
{
    /* Better to initialize rather than assign */
    int var1 = 1;
    int var2 = 53;

    add_two_numbers(var1, var2);
    add_two_numbers(1, 2);

    /*
        Return is better than exit() (especially
        since you didn't include <stdlib.h>
    */
    return 0;
}

/* No implicit int because it was removed in C99 */
int add_two_numbers(int a, int b)
{
    int c = a + b;
    
    printf("%d\n", c);
}

Great! Thanks a lot for prompt response.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.