This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/UCD3138: function calls

Part Number: UCD3138


Tool/software: TI C/C++ Compiler

Dear expert,

Currently I'm using UCD3138 device, CCS7, compiler is ARM v5.2.4.

Please see below codes, in file A, clarify the func_test with on argument with return value, but in file B, the func_test definition without argument. I found that this can be build, link correctly, and run file and the result is correct.  Can you please help me know the reason why it won't affect the result? Is there any risk if write code in this way? Thanks...

File A:

extern long func_test(long i);

long result = 0;

void main(void)

{

    ... ...

    result = func_test(500);

    ... ...

}

file B:

long func_test(void)

{

long a = 100;

long b = 200;

return (a+b);

}

  • You are not observing common programming practice.  The prototype for func_test ...

    Jack Tan80 said:
    extern long func_test(long i);

    should not appear in any C file.  Instead, it is should appear in a header file, and that header file should be included by all the source files which define or call func_test.  When you do that, then the compiler can catch the mismatch on the arguments, and issue a diagnostic similar to ...

    "file.c", line 3: error: declaration is incompatible with "long func_test(long)" (declared at line 1 of "header.h")

    Thanks and regards,

    -George

  • Hi George,

    When I clarify the function in c source file, it could build and link fine. Please see below picture. 

    I know that it's not a good way to program like this, but it existed in our codes, and have used already. So what I'd like to know if there are any risk of this, then I will decide if it's necessary to change the code.

  • Can you please help me know why clarify function should not in c source file? Is there any C standards demonstrate this?

  • You will probably find this article on function prototypes (not from TI) helpful.  It is part of an online course for learning the C programming language.

    With regard to your situation, the key point is:  If a function prototype is not visible, then it is impossible for the compiler to check whether the call to that function is correct.

    Thanks and regards,

    -George

  • Yes, you should change it. Lying to the compiler is like lying to your spouse. It always comes back to bite you in the butt.