Thread: Do C++ compilers generate the same code as C compilers with the same code?

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    217

    Do C++ compilers generate the same code as C compilers with the same code?

    I donno where this belongs.

    So do C++ compilers generate the same code as C compilers when it comes to C code?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Two different C compilers probably don't generate the same code from the same C program in the first place. Anyway, you can compile to assembly output and compare for yourself.

    EDIT:
    If you're asking because you're thinking of mixing C and C++ code in the same program, then you should be aware that C++ compilers typically do stuff like name mangling, so if you want C code to be treated as C, you need to use extern "C" appropriately.
    Last edited by laserlight; 02-16-2019 at 06:41 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    217
    I asked because: Would it make sense to write C code (take it to the next level and say production level C code if you want) in a C++ compiler?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There are two schools of thought:
    • Yes, because C++ compilers will do things like stricter type checking.
    • No, "I never saw a project for which C was better than C++ for any reason but the lack of a good C++ compiler." (Stroustrup)

    Okay, related to the latter, but more positive for C, there is another argument: C and C++ are different programming languages.
    Last edited by laserlight; 02-16-2019 at 06:55 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generate python code out of C++
    By merl111 in forum C++ Programming
    Replies: 12
    Last Post: 08-14-2015, 02:59 PM
  2. Generate Assembly code and Binary code also
    By Hannibal2010 in forum C Programming
    Replies: 16
    Last Post: 07-07-2011, 05:43 AM
  3. To generate bar code from a number
    By darkducke in forum C Programming
    Replies: 18
    Last Post: 01-16-2008, 06:33 AM
  4. Do compilers remove unused code?
    By tretton in forum C Programming
    Replies: 6
    Last Post: 12-27-2005, 12:53 AM
  5. Is this possible (code between diff. compilers)?
    By jonnie75 in forum C Programming
    Replies: 1
    Last Post: 11-12-2001, 12:52 PM

Tags for this Thread