5 ms·
Moreover, don’t use C at all if you have the choice. A std::string in C++ makes mincemeat of the boilerplate that C requires for the same code. In C++, you can
by tediousdemise 6y ago
Moreover, don’t use C at all if you have the choice. A std::string in C++ makes mincemeat of the boilerplate that C requires for the same code.
In C++, you can make a very simple REPL that is superior to the one in the OP in almost every way.
#include <iostream>
#include <string>
int main() {
std::string input;
while (true) {
std::cout << "> ";
std::getline(std::cin, input);
if(input == "exit") {
return EXIT_SUCCESS;
} else {
std::cout << "Unrecognized command: " << input << '\n';
}
}
}
Code quoted from “Setting Up A Better REPL” (https://mikeloomisgg.github.io/2019-04-10-set-up-repl/ https://mikeloomisgg.github.io/2019-04-10-set-up-repl/). It offers good insight into why C is bad in light of the alternatives like Rust and C++.
- david2ndaccount 6y agoNothing stops you from coding a semantically equivalent solution in C, just with structs and function calls that abstract over the details of io and calling realloc correctly.
- windowojji 6y agoDoes C have destructors?
- jonathankoren 6y agoOnly if you make it and call it. C has free, which deallocates a block of memory pointed to by a pointer. If it's a struct, the struct is deallocated. If the struct contains pointers, that memory is still there. If you've lose all the pointers to that chunk of memory, you now have a memory leak. It is not uncommon to make your own allocators and deallocaters for complex structs.
- jhgb 6y ago> In C++, you can make a very simple REPL that is superior to the one in the OP in almost every way. And in Scheme, you can make a very simple REPL that is vastly superior to the one of yours in every way. But I suspect that neither fulfills the goal of learning how things are done in C, which seemed to be the goal of the author of the article.
- enriquto 6y ago> A std::string in C++ makes mincemeat Using bit-shift operators for input/output is a heinous crime against human dignity, basic decency and social order. Every use of it should be punished with 10 lashings, and the promoters of this atrocity should be tarred and feathered.
- deleted 6y ago[deleted]
- rtpg 6y agoalternatively, one could consider that bit-shifting isn't nearly as common of a thing as I/O stream redirection. I'm agnostic on C++'s std stream stuff but I am pretty anti-"yes bitwise logical operators are so important that they get priority over all these symobls".
- leetcrew 6y agoamong other things it's just not a very friendly syntax for the common case of formatting strings and getting input. printf/scanf are much less verbose (if a bit less safe).
- rtpg 6y agoThat's a good point, actually. Beyond the "default formatting" case, it seems pretty hard to imagine a good syntax that would not be very awkward.
- jcelerier 6y agobut it removes three classes of errors entirely when compared to printf / scanf: - wrong arguments types (mostly solved by libraries such as fmt tho): printf("%d", 0.5); - too much / not enough arguments passed: printf("%d", 0, 1); - passing unsanitized strings as template argument: printf(some_string_i_got_from_the_network_lol, arg_1, arg_2); the << method makes these three errors impossible. Also << does not allow you to have something like printf's %n, which writes to one of its arguments. fmt is able to give compile-time validation of string literals, but at a hefty compile-time cost. In my opinion the best solution would be string interpolation, but we're definitely not there yet...
- gens 6y agoI would never use C++ if i had a choice. And i do have a choice.