I'm new to C/C++ and I have just downloaded the CodeLite IDE (any other suggestions for free ones would be welcome). I intend to write a C program, but it only gives me an option to create a C++ workspace. Can I write my C in this workspace all the same?
-
In 99% cases you won't see the difference, at least at the begining of your journey. – woockashek Nov 05 '16 at 23:33
-
Top of Google for `CodeLite`: _"CodeLite is a free, open-source, cross-platform IDE for the C, C++, PHP, and JavaScript (Node.js) programming languages."_ You're missing an option. It may have been present at install-time. – Lightness Races in Orbit Nov 07 '16 at 17:46
3 Answers
It depends on your IDE, your compiler (which is invoked by IDEs to compile code, and usually [not always] not a part of the IDE), and the type of code you write.
Practically, quite a few C++ compilers have a "C mode" - for example, command line switches or other settings that may be configurable through your IDE - to compile as C. You'll need to read your compiler and IDE documentation. Bear in mind that some compilers, even in C mode, support C++ features that are not valid in C (and that the reverse is also true).
It also depends on what sort of C code you intend to write, and how well you know the features of C that are not part of C++, and vice versa.
But, yes, as a general rule you can write a C program in a C++ workspace. Just be aware of concerns like the above when doing so.
Also, don't be surprised when you proudly show some of your lovingly crafted code to a seasoned C or C++ developer, only to be informed you are writing a hybrid of C and C++. Unfortunately, writing a hybrid of C and C++ is considered poor form by both C and C++ developers - mostly because the code is likely to work with some compilers and fail with others. Too many compiler vendors intermix C and C++ in their documentation, and too much learning material (introductory texts, etc) does the same. If writing C code in a (suitably configured) C++ environment, there is a fair chance your compiler won't complain about your code doing that. This isn't a problem inherent to writing C in a C++ environment - too many vendors of C compilers support C++ features as an extension, and vice versa - but it is a phenomenon you are more likely to encounter if you write your C code in a C++ environment, unless you go out of your way to learn the differences between standard C and standard C++ independently of your compiler and compiler documentation.
I've lost track of the number of beginners who end up writing code that is some hybrid of C and C++ (e.g. using C++-specific features while believing they are writing C, or vice versa). Only to have their code break when they build it with a different compiler (or, worse, an update of their compiler).
If you actually intend to write code that includes a mix of C and C++ (by which I mean pure C functions interfacing with pure C++ functions, not a hybrid like I mentioned above), you will need to allow orchestration of the build process - particularly linking - to be controlled by C++ settings, even if compilation settings for particular files mean they are compiled as C or C++.

- 35,646
- 4
- 32
- 74
Yes its possible. You must use your C++ compiler when compiling main() (e.g., for static initialization) Your C++ compiler should direct the linking process (e.g., so it can get its special libraries) Your C and C++ compilers probably need to come from the same vendor and have compatible versions (e.g., so they have the same calling conventions

- 9
- 6
Non-trivial C programs will not compile as C++ code without modification — C is not a subset of C++. See Where is C not a subset of C++?
Set compiler options to treat your code as C.
For free compiler with IDE on linux check this one: Code::Blocks and for windows: Microsoft Visual Studio Community
You can find extensive list of free C and C++ compilers here.