4 ms·
Look at C++ as a scripting language and you possibly will not dislike it as much.
by habibur 5y ago
Look at C++ as a scripting language and you possibly will not dislike it as much.
- rahen 5y agoCERN actually does that with CLING for interactive HEP data science. https://root.cern/cling/ https://root.cern/cling/
- sigjuice 5y agoThanks! $ brew install cling $ cling ****************** CLING ****************** * Type C++ code and press enter to run it * * Type .q to exit * ******************************************* [cling]$ #include <stdio.h> [cling]$ #include <sys/utsname.h> [cling]$ struct utsname u; [cling]$ uname(&u); [cling]$ printf("%s %s %s\n", u.sysname, u.release, u.machine); Darwin 20.5.0 arm64
- rahen 5y agoYou can even use it interactively like ipython and get rid of the printf, as it automatically evaluates expressions. [cling]$ int i=21 (int) 21 [cling]$ i*2 (int) 42
- sigjuice 5y agoThanks! Is there a way to see strings nicely without printf? [cling]$ u.sysname (char [256]) "Darwin\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
- clappski 5y agoI imagine if you printed an actual string it would look as you expect, issue is that you're printing a char array and not a C++ std::string/_view
- sigjuice 5y agoSure, but I’d expect the interpreter to have some smarts to do the right thing within reason.
- clappski 5y agoBut a char[] could be binary data for all the tool knows, do you want it to do a buffer overflow and end up bricking your computer? std::string etc. exist exactly because char[] is unsafe for holding strings unless you carry around a size.
- deleted 5y ago[deleted]
- sigjuice 5y agoThere is a size in the example I posted above. [cling]$ u.sysname (char [256])
- rahen 5y agoIndeed, this is the expected behavior with C arrays, regardless their type. The solutions are either to use a C++ std::string as you mentioned, or a typedef struct with a length property.
- sigjuice 5y agostruct utsname is what it is. For me the appeal for something like cling would be doing quick experiments without any boilerplate.
- ExtremisAndy 5y agoAgreed! I use C++ for small scripts/tools and love it. Granted, I first spent some time writing a library of convenience functions like starts_with(), ends_with(), contains(), get_file_contents(), read_csv_file(), etc. that so many languages have built into their standard libraries, but to be honest, even that part was a lot of fun!