-1
    int calc(int x, int y, int z){
        return x+y+z;
    }

    #include "calc.c"
    #include <stdio.h>

    int main()
    {
        int x = 1;
        int y = 2;
        int z = 3;
        int result;

        result = calc(int x, int y, int z);
        printf("x=%d, y=%d, z=%d, result=%d", x,y,z,result);
     } 

I have two .c files, calc.c and calctest.c which includes a main function and calls calc.c.

I have two errors on line 11 of the main function with result. First: expected expression before int. Second: too few expressions to function calc.

byxor
  • 5,930
  • 4
  • 27
  • 44
  • 3
    `result = calc(int x, int y, int z);` --> `result = calc(x, y, z);` – BLUEPIXY Oct 31 '17 at 15:38
  • 1
    `calc(int x, int y, int z);` This is not how you call a function. – Aditi Rawat Oct 31 '17 at 15:38
  • 1
    and you really should **never** include an *implementation* file (`*.c`). Put only declarations of your functions in a header file (`*.h`), include THAT one. –  Oct 31 '17 at 15:39
  • Try searching first, e.g. [How to invoke function from external .c file in C?](https://stackoverflow.com/questions/21260735/how-to-invoke-function-from-external-c-file-in-c) – underscore_d Oct 31 '17 at 15:48
  • Possible duplicate of [Creating your own header file in C](https://stackoverflow.com/questions/7109964/creating-your-own-header-file-in-c) – Andrejs Cainikovs Oct 31 '17 at 15:53

1 Answers1

2

You should create the file calc.h containing the code

int calc(int x, int y, int z);

calc.c will contain the code:

int calc(int x, int y, int z)
{
    return x+y+z;
}

and main.c will contain the code:

#include <stdio.h>
#include "calc.h"

int main()
{
    int x = 1;
    int y = 2;
    int z = 3;
    int result;

    result = calc(x, y, z);
    printf("x=%d, y=%d, z=%d, result=%d", x,y,z,result);

    return 0;
 } 

Note the call to calc() only uses the variable names, not their types.

Luke Smith
  • 893
  • 4
  • 8