2

Can I write all the c language in the .cpp file. Because I often use the C language, there are some things that the C language does not implement, and C++ implements it. For example, functions cannot set default parameter values in C language. Can I change all c languages to .cpp files? So it supports some features.

Use .cpp files to write C language. For example, I often use pointers, __mm_flush() and other hardware-related functions. Will it affect it? Why do many of the codes I see use C language instead of the more powerful C++.

Gerrie
  • 736
  • 3
  • 18
  • 2
    Does this answer your question? [How to use C code in C++](https://stackoverflow.com/questions/17448014/how-to-use-c-code-in-c) – IndieGameDev Nov 11 '20 at 07:48
  • C++ is more or less a superset of C, so C code can often be compiled with a C++ compiler. Minor adaptations may be neccesary. – Jabberwocky Nov 11 '20 at 07:50
  • 1
    If you want to program in C++, I suggest you study it and learn it properly instead of using it as a kind of "C with classes". Invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), or even take classes. – Some programmer dude Nov 11 '20 at 07:53
  • One popular C feature unsupported in C++ is [variable length arrays](https://stackoverflow.com/q/39334435/14215102). This comes up rather often. –  Nov 11 '20 at 08:31
  • Another nasty surprise could be not returning from a function that declares a return value. This can be valid C, compiles to gibberish under C++ (crashes, invalid instructions, etc.). If you're compiling with gcc or clang, the option `-Werror=return-type` is a must. –  Nov 11 '20 at 08:35

0 Answers0