-2

I am new to C and I use to code in python, and I usually print too much variables and text to test my code and printf statement is irritating sometimes. I want to write a function in c which works exactly like print function in python.

I am facing two problem while writing the function.

  1. I want to take n number of arguments as input to the function.
  2. I want to take any data type as input to the function, for Eg: print(12); print("hello"); print(123.12) should not raise error.

What have I done so Far

  1. I found solution for taking n number of arguments using <stdarg.h>, but I have to specify the data type of the first argument which is not what I want.

  2. "generic selection" Macros can be used to call different function on different data type of argument passed but not sure how it can be done, here is the link which I used for reference.

wetler
  • 374
  • 2
  • 11
  • 1
    What's your question? – ForceBru Dec 25 '20 at 18:01
  • 3
    Python and C are *very* different programming languages. You should read [*Modern C*](https://modernc.gforge.inria.fr/), then the documentation of your C compiler and debugger. If you use [GCC](http://gcc.gnu.org/) be sure to compile with all warnings and debug info : `gcc -Wall -Wextra -g`. Take inspiration from the source code of existing open source C software, like [GNU bash](https://www.gnu.org/software/bash/) or [GNU make](https://www.gnu.org/software/make/) – Basile Starynkevitch Dec 25 '20 at 18:02
  • 3
    In python, the runtime has information about the type of the objects passed to the function. In C, that is not the case. IOW, you cannot easily do what you are trying. – William Pursell Dec 25 '20 at 18:04
  • 2
    You _could_ have done something like this with `_Generic` and recursive macros, but C doesn't have recursive macros... You can [hack around that](https://stackoverflow.com/questions/12447557/can-we-have-recursive-macros), but it looks pretty complicated – ForceBru Dec 25 '20 at 18:13
  • 1
    I don't understand why you don't just use `printf`? – mkrieger1 Dec 25 '20 at 18:24
  • I am learning C and it's best to learn using real life problems. I am not building any program. @mkrieger1 – wetler Dec 25 '20 at 18:39
  • 2
    If you want to code in C, then code in C. If you want to use those features you want, then use Python instead. – klutt Dec 25 '20 at 18:51
  • 1
    What you want to do might be fun, but it's not how you code C. And this is essentially completely impossible. It cannot be done. At least not in a nice way. So continue if you find it as a fun exercise, but if it is just to avoid format strings in `printf` then rethink. – klutt Dec 25 '20 at 19:00
  • @klutt yeah it's more like learning experience to me, it raises my interest if I do stuff like this while learning. – wetler Dec 25 '20 at 19:08
  • @wetler I'd suggest learning the basics first. What you want to do (which is impossible, but might work with some workarounds) requires good knowledge about the language. – klutt Dec 25 '20 at 19:09
  • But the best you can do is probably stuff like `int print_int(int x) { return printf("%d", x); }` but it's not really worth the effort – klutt Dec 25 '20 at 19:14
  • Start learning c++ instead – 0___________ Dec 25 '20 at 19:32

1 Answers1

5

I want to write a function in c which works exactly like print function in python.

You simply cannot do that (because of type erasure : at runtime, type information is lost in C). Read Modern C then see this C reference.

In practice, you'll better write one function per datatype in C to print it. So it would be void print_int(int); to print an integer, void print_double(double); to print a double, etc. Once you have a collection of such functions you might use _Generic inside a macro (but that is rarely useful; it can handle a finite set of types).

Study for inspiration the source code of existing open source C software on github or elsewhere. Look for inspiration inside the source code of GNU bash, sqlite, GTK or GNU bison (and perhaps inside the source code of Python; half of the Python interpreter is coded in C)

Read also the documentation of your C compiler, e.g. GCC. So compile with all warnings and debug info: gcc -Wall -Wextra -g then use the GDB debugger.

Perhaps you want to implement some tagged union abstract data type in C. Then take inspiration from GNU guile or the runtime of Ocaml and dive inside their source code.

Once you are more familiar with C, read some C draft standard, e.g. n2176

Consider using Frama-C or the Clang static analyzer.

Consider also sometimes generating some C code, like SWIG does.

Budget weeks of work.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547