December 23rd, 2013, 12:28 AM
I am trying to write a computer program in C that reads two binary numbers and outputs their sum, difference, and product in binary. Now I am familiar with C++ but not at all with C. I have not a clue what it should look like nor how really to compile it in Visual Studio 2012 (soon to be 2013). Thank you for any help you can give.
December 23rd, 2013, 01:00 AM
For the most part, C++ is a superset of C. So write it in C++ and then convert it. Just don't use iostreams nor the basic string class, but rather use C I/O functions (see stdio.h) and C-style strings (see strings.h).
In order to get VS to use the C compiler, it needs to have the .c file extension.
December 23rd, 2013, 01:06 AM
Are there any tools I could use to convert it once I write it or no?
Originally Posted by dwise1_aol
December 23rd, 2013, 01:11 AM
Yeah, your Mk1 language converter: you.
Just write it in C++. Don't use object-oriented programming. Use C I/O instead of iostreams. Use C-style strings instead of the basic string class. If you do that, then it should already be in C.
It's not like they're entirely different languages. There is no need for any translation application to convert from C++ to C.
December 23rd, 2013, 05:47 AM
C supports the procedural programming paradigm (as does C++), but not OOP.
The fundamental programming structure in C is the function, if you don't use classes - just functions you are most of the way there. Also avoid the STL, iostream and string libraries and you should end up with something that is close to C compilable. A few other features such as function overloading and reference types are also not part of C.
In the end though, there are few reasons to to use C over C++, especially on a desktop application. Why don't you just do it in C++? Or is the truth of this is that while you are familiar with C++, you could't solve the problem in C++ either?
The simplest method is to convert the input "1's and 0's" string to an integer, perform the operations, then convert the resulting integers to "1's and 0's" strings to output - that is simple because each digit represents exactly on bit on an integer. However whether that is acceptable depends on whether the print of the exercise is to demonstrate conversion to/from binary, or the mechanics of binary arithmetic.