views:

307

answers:

11

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.

A: 

If you know and are comfortable with C++, I can't think of a reason not to use it for this purpose.

Fred Larson
+8  A: 

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++.

John Gietzen
You definitely can't use C++ compiler to build C programs (long long, VLAs, conversion from `void *`, ...). Most C++ compilers, however, support C as well.
avakar
C++ and C are not really "compiled languages." Truthfully, not language is "compiled" or "interpreted." A language is just a specification, it can be compiled or interpreted.
tster
@avakar, good point, but I indeed said compiler. @tster, good point, duly noted. If either of you can think of a good way to improve my answer, please edit it.
John Gietzen
@tster: True, but do we really want to always say "C is a language which is essentially always compiled"? I think we can all handle the simplification.
Jefromi
@Jefromi, +1 I agree. But sometimes I just think people need to keep in mind that a language is a spec and not the compiler/interpreter they happen to use.
tster
+1 avakar. "You can do everything available in C with C++" <- it's a mystery to me how you think that. The C++ Standard contains a list of not few incompatibilities to C. In fact you are very likely to hit such incompatibilities when trying to do the same in C++ you do in C
Johannes Schaub - litb
@litb Almost all of the incompatibilities are semantic differences. The fact still stands that the **functionality still exists** in C++. I never said that *every* '.c' file will compile with a C++ compiler.
John Gietzen
A: 

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.

tster
There's not even a real reason to avoid an interpreted language. Stick a shebang at the top of the file and you're golden.
Steve S
+1  A: 

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.

Doug T.
+6  A: 

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.

Michael Foukarakis
+1 No need to feel the peer pressure when it comes to programming languages. If you're comfortable using it, then use it; if you're after something more, try something new.
Nathan Kleyn
A: 

If you feel confortable with C++... use it. You can use Boost Program Options library for managing input parameters.

Cătălin Pitiș
Yes, the first thing the beginning systems programmer wants to do is become bogged down with building and using boost for utilities that should have as few dependencies as possible.
Eric
why downvotes ensued? your suggestion is good, I thought about using Boost libraries
chester89
I'm not +1 or -1 here, but I would agree that linking in Boost is a bit dubious for someone who doesn't know C from C++.
John Gietzen
The OP clearly *does* know C from C++, and prefers C++. If you're using C++, using Boost to eliminate tedium is just plain good sense.
Avdi
@Eric: Is it mentioned anywhere that he is a beginning C/C++ programmer? Or is it the default assumption that every person asking a question tagged with "homework" is dumb
Cătălin Pitiș
Ya he should use boost.program_options to parse command line arguments. And boost.shared_ptr for his pointers, and boost.flyweight for storing each character of the output, and boost.streams for his own formatted cout...man...really..?
Aurélien Vallée
Boost Program Options is kind of cool. But you can just as well use C library functions like getopt or popt.
Zan Lynx
@Catalin: Note that I said beginning systems programmer and not beginning programmer. I'm more concerned about your C/C++ level when suggesting boost for implementing shell utilities than I am about the the OP's.
Eric
A: 

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.

mouviciel
what I meant is I don`t know it well enoigh to use even 10% of all its advantages. while learning C++ I did pointers operations, arrays, some low-level stuff. but I don`t think linux API is friendly, so I`d like to use C++ to wrap using OOP in the most convenient way possible
chester89
For the person whose only tool is a hammer every problem looks like a nail - or more specifically http://stackoverflow.com/questions/234075/what-is-your-best-programmer-joke/234449#234449
Chen Levy
A: 

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.

pmg
The original UNIX V6 sources (eg. http://minnie.tuhs.org/UnixTree/V6/usr/source/s1/) are probably worth a look for this assignment - although they do use an archaic dialect of C (eg the `=+` operator is now spelled `+=`).
caf
V6 is not going to take him very far. His assignment is not rewriting UNIX v6. These sources still don't use include files which he most certainly should be using. v7 is more similar to what he should be writing. http://minnie.tuhs.org/UnixTree/V7/usr/src/cmd/ But in any case, parent solution is probably a better place to start(v7 is not posix-compatible). GPL-violating homework - that would be fun. In actuality, writing base unix utilities is very easy and the man pages for the utilities and posix functions are more than enough. http://www.openbsd.org/cgi-bin/man.cgi
jbcreix
Ah! Thanks for the links ... old C syntax is much better than GNU indent style!
pmg
+1  A: 

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.

Chen Levy
A: 

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.

jmucchiello
+1  A: 

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.

Mr. Shiny and New