I need to implement a few commands of Linux shell for my homework - 5 or 6 of them, including ls. Do not know much about which parameters to implement for each of commands...
I planned to use C++, but when I asked my colleague for advice what language to choose - plain C or C++, he said that interpreter was not a program in traditional meaning, it`s a functional tool, and it absolutely must be implemented in C.
My arguments on C++ is great code reuse, better separation of concerns, and in fact I do not know C very well - actually, I learned C++ and enjoyed it.
So, what is your point on this? Thanks in advance.
It is an individual assignment - I mean for every person in my group, so no collaboration supposed.
I have experience of low level programming, pointers arithmethic, void*, etc.
views:
307answers:
11If you know and are comfortable with C++, I can't think of a reason not to use it for this purpose.
First: Use what you know.
There is no reason to enter uncharted waters if you can get there with a familiar route.
C++ is a very viable option in your circumstance, anyways. So, you aren't making a mistake to just use it.
Second: Your friend is wrong. (I would use harsher words, but I'll be nice.)
C++ and C are both compiled languages. A C++ program absolutely is a program in the traditional sense. Both C and C++ are statically typed as well.
PS: You can still use a C++ compiler to build C programs. You can do everything available in C with C++.
You friend has no idea what they are talking about. Both C and C++ are normally compiled (interpreters exist, but normal usage is to compile it). In the end, a C compiled binary and a C++ compiled binary will behave the same way. Use whatever you want to use, it's a matter of preference.
It really doesn't matter. What are you most comfortable using? You can get reasonably well written code in either language.
- C if you are comfortable with the procedural/modular paradigms of programming
- C++ if you want to use object oriented and/or generic programming tools
If you're just interested in getting something done, I'd probably use python or another scripting language for this task.
You know C++, you like it, so use C++.
Or, you want the challenge, you want to learn C, that will help homework feel like something useful, so use C.
PS. "must be implemented in C" is complete crap. Don't fall into that trap.
If you feel confortable with C++... use it. You can use Boost Program Options library for managing input parameters.
About your first argument: Great code reuse and better separation of concerns is achieved also in C.
About argument of your colleague: I don't know what a program in traditional meaning is. There is no technical or legal issue which prevents from using C++ for an interpreter.
About your second argument: If you don't know C, don't program in C (unless your objective is to learn it, which is great). For the person whose only tool is a hammer every problem looks like a nail. This may be a joke, but I think this is a good advice as well.
Use whatever you're more confortable with.
But, with C you can cheat :)
Don't cheat! The sources for the various utilities are not meant for general use.
This is not directly answer your question, but perhaps it will answer your situation. I remember myself getting a similar homework way back when...
The original goal, I believe, was to make the student be familiar with section 3 of the man pages "C library routines for C programs", and to a lesser extent section 2 "Unix and C system calls". I remember asking the instructor if I could write my homework in C++, and got NO for an answer - C was a requirement.
Anyhow, my point is that most of the time I wasted on parsing the command line arguments. The utilities themselves were trivial. And in hindsight it was a shame that nobody pointed man 3 getopt
to me. I hope you will get to be smarter then I did.
Actually, you might use both. It depends heavily on what commands you are writing.
C++ has one drawback, executable size. If you are really implementing shell commands, you want the executable to be smallish and C is more likely to produce a smaller executable. Of course, that isn't the only constraint. You want the code to be easy to work with too. Thus a mixed approach might provide benefits. For a command like ls where there is little input processing, you might do all your output using the C stdio.h library so that you don't have to include the much larger C++ iostream library.
OTOH, this is a homework assignment so I doubt you'll get points off for a large executable size so in this specific instance you probably should stick with what you know best.
Since this is for a homework assignment, I'd check with the instructor or person grading your work. A good instructor should be able to make sense of a C++ program as well as a C program if it's not written in a crazy manner (but how can students know what is crazy?)
If your instructor doesn't care what language you use, use the language you are most familiar with if you want to get the work done faster. If you want a challenge, use the language you are less familiar with. School is a good time to make mistakes, but at the end of the term your grades are important.
Finally, don't get too caught up trying to wrap the native API or re-invent it if the assignment's purpose is to learn the API. If you can come up with an elegant wrapper that demonstrates you understand the API, fine, but otherwise your instructor might be annoyed that your program works against the API instead of with it.