-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd.c
More file actions
36 lines (31 loc) · 752 Bytes
/
add.c
File metadata and controls
36 lines (31 loc) · 752 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// add.c
#include <Python.h>
// Function in C
int add_numbers(int a, int b) {
return a + b;
}
// Python interface
static PyObject* add_numbers_py(PyObject* self, PyObject* args) {
int a, b;
if (!PyArg_ParseTuple(args, "ii", &a, &b)) {
return NULL;
}
return PyLong_FromLong(add_numbers(a, b));
}
// Method table
static PyMethodDef addnums_methods[] = {
{"add_numbers", add_numbers_py, METH_VARARGS, "Add two numbers."},
{NULL, NULL, 0, NULL}
};
// Module definition
static struct PyModuleDef addnums = {
PyModuleDef_HEAD_INIT,
"addnums", //This is module name
NULL,
-1,
addnums_methods
};
// Module initialization
PyMODINIT_FUNC PyInit_addnums(void) {
return PyModule_Create(&addnums);
}