Deutsch English Français Italiano |
<v8llu7$3hdij$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.nobody.at!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Bart <bc@freeuk.com> Newsgroups: comp.lang.c Subject: Re: is STC a good supplementary library for C? Date: Sat, 3 Aug 2024 17:29:59 +0100 Organization: A noiseless patient Spider Lines: 81 Message-ID: <v8llu7$3hdij$1@dont-email.me> References: <j4KdnZzYDexmlDP7nZ2dnZfqn_idnZ2d@brightview.co.uk> <containers-20240803130427@ram.dialup.fu-berlin.de> <python-20240803162221@ram.dialup.fu-berlin.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 03 Aug 2024 18:29:59 +0200 (CEST) Injection-Info: dont-email.me; posting-host="d986d97f32d940f33aebcc88e980ad64"; logging-data="3716691"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18lJNFHSC1FgGc0iJ76AVTN" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:cSmgEOvtr0wlV2A1f+yD0mH46NM= In-Reply-To: <python-20240803162221@ram.dialup.fu-berlin.de> Content-Language: en-GB Bytes: 3750 On 03/08/2024 16:25, Stefan Ram wrote: > ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted: >> Embracing these traits leads to more maintainable, efficient, and >> understandable code. Instead of trying to force C to act like C++, >> it's generally better to leverage C's strengths and use techniques >> that are well-suited to the language's design and philosophy. > > If you absolutely need a library, you (Mark) should totally check > out the Python library! It's been around forever, super reliable, > and constantly getting better. Here's a quick example program: > > #include <Python.h> > > int main() { > // Initialize the Python interpreter > Py_Initialize(); > > // Create a Python list > PyObject* pyList = PyList_New(0); > if (!pyList) { > fprintf(stderr, "Failed to create Python list\n"); > Py_Finalize(); > return 1; > } > > // Add some integers to the list > for (int i = 0; i < 5; i++) { > PyObject* pyInt = PyLong_FromLong(i); > if (!pyInt) { > fprintf(stderr, "Failed to create Python integer\n"); > Py_DECREF(pyList); > Py_Finalize(); > return 1; > } > > // PyList_Append increments the reference count of pyInt > if (PyList_Append(pyList, pyInt) < 0) { > fprintf(stderr, "Failed to append to list\n"); > Py_DECREF(pyInt); > Py_DECREF(pyList); > Py_Finalize(); > return 1; > } > > // Decrease reference count of pyInt, as it's now stored in the list > Py_DECREF(pyInt); > } > > // Print the list size > printf("List size: %zd\n", PyList_Size(pyList)); > > // Read and print the integers from the list > for (int i = 0; i < PyList_Size(pyList); i++) { > PyObject* item = PyList_GetItem(pyList, i); // Borrowed reference > if (PyLong_Check(item)) { > long value = PyLong_AsLong(item); > printf("List item %d: %ld\n", i, value); > } else { > fprintf(stderr, "List item %d is not an integer\n", i); > } > } > > // Clean up > Py_DECREF(pyList); > Py_Finalize(); > > return 0; > } > > , output: > > List size: 5 > List item 0: 0 > List item 1: 1 > List item 2: 2 > List item 3: 3 > List item 4: 4 > > . This looks like it wouldn't run much faster than in Python. So why not just use Python? It would be a lot simpler!