#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Calculating Binary


    Hey guys,

    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.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by dwise1_aol
    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.
    Are there any tools I could use to convert it once I write it or no?
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    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.
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,111
    Rep Power
    1803
    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.

IMN logo majestic logo threadwatch logo seochat tools logo