user755: I would like to know if there is a way to call functions that are contained within C files using an Arduino sketch? 
My C file declares and defines a function. To save putting the messy function definition into my Arduino sketch, I'd like to call the function straight from the sketch. 
Is there a standard way to do this using Arduino and C?
Here is the sketch: 
#include "crc16.h";

void setup(){

}

void loop(){

  CalculateCRC16("<09M", 4);

}

and this is the trimmed down C file:
#include <stdio.h>
#include <stdint.h>

uint16_t crctable[256] =
{
    0x0000, 0x1189,.....



uint16_t // Returns Calculated CRC value
CalculateCRC16( // Call example CalculateCRC16("<09M", 4);
    const void *c_ptr, // Pointer to byte array to perform CRC on
    size_t len)        // Number of bytes to CRC
{

    uint16_t crc = 0xFFFF // Seed for CRC calculation
    const uint8_t *c = c_ptr;

    while (len--)
        crc = (crc << 8) ^ crctable[((crc >> 8) ^ *c++)];

    return crc;
}


user84: Yes, just copy its declaration line in your sketch:
extern "C" {
    void myfunction(int arg);
}


user42: Your CRC function can easily be converted to C++ so that it can go into a *.cpp file. All you need to do is use an explicit cast when you initialise your c pointer. Here's the 'proper' C++ way to do it:
const uint8_t *c = static_cast<const uint8_t*>(c_ptr);

However, an old C-style cast would also work:
const uint8_t *c = (const uint8_t*)c_ptr;

The problem is basically that C can be a little more permissive about letting you convert pointers implicitly between types. To do it in C++, you need to tell the compiler explicitly that the conversion is intentional.

user666: You can extern "C" #include like the following:
extern "C"{
#include "crc16.h"
};

void setup(){
}

void loop(){
  CalculateCRC16("<09M", 4);
}

And the crc16.h file could be (some minor fixes, the #pragma once, a cast):
#pragma once

#include <stdio.h>
#include <stdint.h>

uint16_t crctable[2] ={ 0x0000, 0x1189};

uint16_t CalculateCRC16( // Call example CalculateCRC16("<09M", 4);
    const void *c_ptr, // Pointer to byte array to perform CRC on
    size_t len)        // Number of bytes to CRC
{
    uint16_t crc = 0xFFFF; // Seed for CRC calculation
    const uint8_t *c = (const uint8_t *)c_ptr;

    while (len--)
        crc = (crc << 8) ^ crctable[((crc >> 8) ^ *c++)];

    return crc;
}


755: <p>I would like to know if there is a way to call functions that are contained within C files using an Arduino sketch? </p>

<p>My C file declares and defines a function. To save putting the messy function definition into my Arduino sketch, I'd like to call the function straight from the sketch. </p>

<p>Is there a standard way to do this using Arduino and C?
Here is the sketch: </p>

<pre><code>#include "crc16.h";

void setup(){

}

void loop(){

  CalculateCRC16("&lt;09M", 4);

}
</code></pre>

<p>and this is the trimmed down C file:</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdint.h&gt;

uint16_t crctable[256] =
{
    0x0000, 0x1189,.....



uint16_t // Returns Calculated CRC value
CalculateCRC16( // Call example CalculateCRC16("&lt;09M", 4);
    const void *c_ptr, // Pointer to byte array to perform CRC on
    size_t len)        // Number of bytes to CRC
{

    uint16_t crc = 0xFFFF // Seed for CRC calculation
    const uint8_t *c = c_ptr;

    while (len--)
        crc = (crc &lt;&lt; 8) ^ crctable[((crc &gt;&gt; 8) ^ *c++)];

    return crc;
}
</code></pre>
 84: <p>Yes, just copy its declaration line in your sketch:</p>

<pre><code>extern "C" {
    void myfunction(int arg);
}
</code></pre>
 42: <p>Your CRC function can easily be converted to C++ so that it can go into a *.cpp file. All you need to do is use an explicit cast when you initialise your <code>c</code> pointer. Here's the 'proper' C++ way to do it:</p>

<pre><code>const uint8_t *c = static_cast&lt;const uint8_t*&gt;(c_ptr);
</code></pre>

<p>However, an old C-style cast would also work:</p>

<pre><code>const uint8_t *c = (const uint8_t*)c_ptr;
</code></pre>

<p>The problem is basically that C can be a little more permissive about letting you convert pointers implicitly between types. To do it in C++, you need to tell the compiler explicitly that the conversion is intentional.</p>
 666: <p>You can extern "C" #include like the following:</p>

<pre><code>extern "C"{
#include "crc16.h"
};

void setup(){
}

void loop(){
  CalculateCRC16("&lt;09M", 4);
}
</code></pre>

<p>And the crc16.h file could be (some minor fixes, the #pragma once, a cast):</p>

<pre><code>#pragma once

#include &lt;stdio.h&gt;
#include &lt;stdint.h&gt;

uint16_t crctable[2] ={ 0x0000, 0x1189};

uint16_t CalculateCRC16( // Call example CalculateCRC16("&lt;09M", 4);
    const void *c_ptr, // Pointer to byte array to perform CRC on
    size_t len)        // Number of bytes to CRC
{
    uint16_t crc = 0xFFFF; // Seed for CRC calculation
    const uint8_t *c = (const uint8_t *)c_ptr;

    while (len--)
        crc = (crc &lt;&lt; 8) ^ crctable[((crc &gt;&gt; 8) ^ *c++)];

    return crc;
}
</code></pre>
