hello.c

来自「python改写的<<C语言百例>>! 很经典的程序开发」· C语言 代码 · 共 29 行

C
29
字号
#include <Python.h>
#include <string.h>

/**//* module functions */
static PyObject *                                 /**//* returns object */
message(PyObject *self, PyObject *args)           /**//* self unused in modules */
...{                                                 /**//* args from Python call */
    char *fromPython, result[64];
    if (! PyArg_Parse(args, "(s)", &fromPython))  /**//* convert Python -> C */
        return NULL;                              /**//* null=raise exception */
    else ...{
        strcpy(result, "Hello, ");                /**//* build up C string */
        strcat(result, fromPython);               /**//* add passed Python string */
        return Py_BuildValue("s", result);        /**//* convert C -> Python */
    }
}

/**//* registration table  */
static struct PyMethodDef hello_methods[] = ...{
    ...{"message", message, 1},       /**//* method name, C func ptr, always-tuple */
    ...{NULL, NULL}                   /**//* end of table marker */
};

/**//* module initializer */
void inithello( )                       /**//* called on first import */
...{                                      /**//* name matters if loaded dynamically */
    (void) Py_InitModule("hello", hello_methods);   /**//* mod name, table ptr */
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?