109

I am a C# programmer, and most of my development is for websites along with a few Windows applications. As far as C goes, I haven't used it in a long time, as there was no need to. It came to me as a surprise when one of my friends said that she needs to learn C for testing jobs, while I was helping her learn C#.

I figured that someone would only learn C for testing if there is development being done in C. From my knowledge, all development related to COM and hardware design is also done in C++. Therefore, learning C doesn't make sense if you need to use C++. I also don't believe in historic significance, so why waste time and money in learning C?

Is C still used in any kind of new software development or anything else?

2

12 Answers 12

227

C has the advantage that it is a relatively small language, which makes it easy to implement a C compiler (whereas a C++ compiler is a monster to write), and makes it easier to learn the language. Also see the TIOBE index, according to which C slightly ahead of C++.

In (IMO) decreasing order of justification, C is still used a lot for

  • Embedded stuff
    It's way easier to port a C compiler to a small platform than it is to port a C++ compiler. Also, C advocates claim that C++ "does too much behind their backs". However, IMO that's FUD.

  • Systems programming
    Again, that's usually due to claims that it is easier to "know what the compiler is doing". However, many embedded programs would benefit from, e.g., templates and other C++ key features.

  • Open source software
    That's mostly an attitude problem, though: OSS has always preferred C over C++ (whereas it's the opposite in large parts of the industry). Torvalds' irrational hatred might actually be the most important reason for this on Linux.

36
  • 17
    It's more history than attitude. Many of what you might consider the "core" open source packages were originally developed when C++ wasn't as widely available as it is now and resources were still scarce.
    – Blrfl
    Commented Aug 26, 2011 at 14:07
  • 74
    The TIOBE index is a joke. Search engine hits are meaningless.
    – DeadMG
    Commented Aug 27, 2011 at 11:08
  • 32
    @Sedate: That templates generally cause code bloat is a myth, coming from the times of ancient C++ compilers. Modern compilers will fold identical template instances. OTOH, templates allow template meta-programming, which executes code at compile-time, rather than run-time, resulting in less code being generated. Also, they make for much safer programs (less casting), something that's often very important in the embedded domain. EC++ has been bashed to death by C++ experts over (among other things) the sheer stupidity of throwing out templates.
    – sbi
    Commented Aug 29, 2011 at 6:25
  • 22
    @James: You mean things like efficient abstractions, generic programming and type-safety? Yeah, who'd want that.
    – Xeo
    Commented Nov 4, 2012 at 21:34
  • 13
    @JesperE As it happens, I have changed jobs since I wrote that, and I am now programming for embedded devices. We're using C++, and it's remarkable what the STL and template meta-programming can do for you when you have weak hardware and hard realtime constraints as well as reliability necessities. (We're doing power plants.) Yes, you have to know whether you should use a std::vector or an std::map for a certain piece of code — but you do not have to implement it yourself, but can rely on well-tested, highly performant, and dependable library implementations offering high abstractions.
    – sbi
    Commented Mar 3, 2013 at 21:21
123

C is used a lot in embedded hardware programming where resources are scarce.

Linux kernel is written in C because, according to Linus Torvalds, C++ is a horrible language.

21
  • 15
    I think large part of Windows kernel is also C. And a lot of legacy systems.
    – Coder
    Commented Aug 26, 2011 at 12:38
  • 16
    To be complete, Linus did give a try to C++ in the kernel. That was more an issue than a plus. Anyway, kernel devellopement is a really specific topic, that doesn't means C++ is bad in general.
    – deadalnix
    Commented Aug 26, 2011 at 12:40
  • 79
    According to others, Linus' argumentation is horrible.
    – sbi
    Commented Aug 26, 2011 at 12:41
  • 37
    Linus's arguments may or may not be valid, but Linux kernel is still written in plain C :-) Commented Aug 26, 2011 at 12:51
  • 13
    As a side note: BEOS was written in C++ and superior to other OS of its time failing mainly due to the politics of the day. As an indication that the OS need not be written in C. Commented Aug 26, 2011 at 13:00
99

All of the modern languages I have seen may interact with C:

  • C++
  • Java
  • C#
  • Python
  • Haskell
  • Objective C
  • Rust

The need to interact with C derives from:

  • C having a simple ABI
  • C being around for a long time

It means that since those languages can communicate with C, they can:

  • leverage its libraries
  • communicate with each other through C (for example, Clang is written in C++ but offers Python Bindings hooked on its C interface).

And I would bet that all of them rely on C for their runtimes (unless they went full assembly ? dubious).

C is the Lingua Franca of the programming languages and one of the simplest (ABI-wise) not tied to a specific architecture (like assembly is), it'll take a major shift to get rid of it.

2
  • "C is the Lingua Franca of the programming languages"—in other words, the greatest common denominator.
    – Anakhand
    Commented Aug 6, 2020 at 15:39
  • C language is the first level above machine (assembly language). And it is at a high enough level, and anyone learning to program, can with little effort, read and follow a C program. Commented Aug 12, 2022 at 1:45
46

In my opinion this is a very short sighted question akin to "My friends and I listen to Reggae. Does anybody really still listen to Rap?".

Every language out there has its use. Different languages definitely have their niches. But asking about C! I am sure fewer people use C# than C on a daily basis (from the totally biased view point of working in a shop where nobody uses C#).

Quick google looking on the relative popularity of languages.
I am sure none of this is authoritative but we can use it to see trends:

http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
http://langpop.com/

Even looking at SO ratio of question on tags:
https://stackoverflow.com/tags

  • C#: 209845
  • 16 other tags
  • C: 38790

So C is the 18 most popular topic on SO (and there are a lot of other languages there).

Note: The TIOBE index above has been constantly updated for over a decade (and has some data going back 3 decades) is supposed to measure engineers working in each language (though I have no idea how accurate that is). Of the top 10 languages except Java/Visual Basic it reflects what people in my shop know (though our ratios will be slightly different as we have a much smaller sample size).

8
  • 2
    This answer confuses me...you go on about C# and then show SO question tags but none of that has anything really do to with C being used. Popularity (especially on langpop, where they use search engine queries to determine popularity) doesn't really show the modern day usage of a language, just modern day searches on a language. You must take into account, for searches, that C is used frequently in Universities for the lower level classes so that can increase the number of queries and also SO posts.
    – Jetti
    Commented Aug 26, 2011 at 14:58
  • 3
    @Jetti: Thats why I explicitly say: I am sure none of this is authoritative but we can use it to see trends But I disagree with your second statement; C is no longer the major language taught at institutes of higher learning (if it was then the new batch of graduates would not be as useless). People tend to be learning Java/C# sharp nowadays. Also the Tiobe report is about jobs not queries. Commented Aug 26, 2011 at 16:57
  • Looking back, there seems to be a poor word choice on my part. I didn't mean the low number classes (beginning classes), I meant the systems classes (computer architecture) is where C is used.
    – Jetti
    Commented Aug 26, 2011 at 17:48
  • 4
    SO tag count does not define language popularity in general, it simply shows language popularity between users of SO. Commented Aug 26, 2011 at 19:42
  • 4
    @Ed S. Obviously. But the question is not about popularity. Its about the existence of C as a language. The SO tag count shows us that it is definitely not a dead language. The fact that C is in the top 2 on the other sites does not make it the first/second most used language. But its existence in the top 10 is a significant marker that it is not dead. Of course none of this is proof just strong indicators that C is still being actively used. Commented Aug 26, 2011 at 21:32
23

You may need to use C when you are low on resources and don't need object oriented capabilities.

Many softwares in use today are still written in C, not to mention hardware drivers.

According to Tiobe index, C is still the most used language.


As tcrosley suggested, you may want to take a look at this related question.


You should also check for some related articles on the differences between C and C++, like this wiki or this for example.

10
  • 4
    ahem !! thats a great point. I never gave a thought that "OOP capabilities actually add overhead for the language". Thanks for making this valid point clear. Now, i can understand, where C is ahead of others Commented Aug 26, 2011 at 12:54
  • 7
    @Pankaj C++ generally doesn't necessarily add much run time overhead, a lot of the complexity of the language is the principle of "not paying for what you don't use" - if you don't use exceptions then exceptions don't slow down or add size to your code. The compiler is larger and more complex though Commented Aug 26, 2011 at 15:27
  • 2
    re C in the embedded field, see also this question and answers: programmers.stackexchange.com/questions/84514/…
    – tcrosley
    Commented Aug 26, 2011 at 17:00
  • 7
    You never actually need OOP capabilities, it simply works well in some scenarios. Commented Aug 26, 2011 at 19:41
  • 2
    @Jose Faeti: My boss would agree because my boss is an experienced, rational guy. He doesn't buy into programming religion. Commented Aug 29, 2011 at 16:54
20

It sounds like you are trying to convince yourself that C is useless and therefore can be ignored. Let's break your question down:

"I figured that someone would only learn C for testing only if there is development done in C."

No, there are many reasons to learn C. Even if you didn't know that I would still avoid using blanket statements like that, especially in conjunction with circular logic. Obviously one needs to know the language the code is written in to be able to properly test/fix it but that assumes that the langauge is still used as a given and is true for any language and not just C.

"In my knowledge, all the development related to COM and hardware design are also done in C++."

That is incorrect.

"Therefore, learning C doesn't make sense if you need to use C++. I also don't believe in historic significance, so why waste time and money in learning C?"

This is the most questionable logic of all. First of all, historic significance is something you should believe in, because if you did you'd know that C is a subset of C++ and, because of that, knowing C can help you be a better C++ programmer. Of course, C was also influential to most languages that came after it so the benefits don't stop there. In addition, because C is so important it can not be considered as having only historical significance. It is still widely used and thus cannot be relegated to a secondary position like that. You can argue that it's not a language that every programmer needs to use and have a thorough knowledge of and that would be right but please don't build your argument on saying that you don't believe something without examining its true merits first.

5
  • 7
    C is a subset of C++ , Is that what you meant ?? . C is not a Subset of C++; infact they are pretty different. Yes, C++ is an enhancement of C, or sometimes referred as C with classes and OOP, but to say C is a subset, doesn't justify Commented Aug 31, 2011 at 5:37
  • 9
    C++ is mostly a superset of an old version of C, and C's gone in a somewhat different direction since then. Some aspects of the languages have gone in largely parallel directions, but others have not (and C++ has a lot of other things besides). Commented May 26, 2012 at 14:22
  • I agree in voting for clarification of that fact, not all valid C programs are valid C++ programs, i.e. C++ is not a superset of C. However, it is a superset of how C was at the point of making the decision for it to be a superset, as Donal Fellows mentioned. It simply doesn't make sense to say that it is anymore, when it's no longer true however. Commented May 25, 2017 at 19:18
  • Similarly: Almost nobody actively speaks Latin these days (except in the Vatican) but knowing Latin immensely boosts your understanding of a multitude of other languages.
    – Lagerbaer
    Commented May 27, 2020 at 3:31
  • @Lagerbaer It is used in the scientific community a lot :0 You would be suprised... :) Commented Feb 21, 2021 at 22:24
16

In addition to embedded systems, most newer languages have some way to interface with C. When writing a library that you want to have an easy time using in all of those languages, C is an obvious choice. C++, while it can also interface with some languages (such as Python (CPython only)), C++ cannot interface with a greater number of languages due to some of its features (name mangling especially, but templates don't help the issue). The C ABI is one of the easiest to interface with (I know you can write C++ and use extern "C" for the interface. I don't care).

It also has the benefit that C and C++ are really the best languages for systems programming and the C compile times are much faster. C++ compile times are noticeably the worst of any language I've used.

Now while there are other languages that want to become the popular systems language out there (I know about D in particular), a far majority of software is written in C/C++. Languages like D require someone to create a wrapper around the C library instead of just using it directly (like you would from C++).

2
  • D can call C code directly, just like C++. All you need if the function prototype (again, just like C++). You just write extern(C) in D, whereas in C++ you write extern "C" Commented Aug 27, 2011 at 12:29
  • @Peter Alexander I'm aware of extern(C) in D. That's what I was referring to when I said wrapper file. You cannot directly include the C header (which you can do in C++, assuming the C header uses extern "C" and has the #ifdef __cplusplus blocks, which most do). There are then other incompatibilities among just using extern(C) (particularly how strings are handled. To my knowledge, they do not have a null terminator in D. So you have to specially change the array when passing it to C).
    – jsternberg
    Commented Aug 27, 2011 at 13:12
6

Well I think C is the most powerful language Due to the following reasons!

1) AT first C , It's a systems language (which means it can be used to do low-level programming with minimal or no run-time).

2) Speed of the resulting application. C source code can be optimized much more than higher-level languages because the language set is relatively small and very efficient. It is about as close as you can get to programming in assembly language, without programming in assembly language. and you can even use assembly and C together!

3) C has which is its application in Firmware programming (hardware). That is due to its ability to use/work with assembly and communicate directly with controllers, processors and other devices.

4) C is a building block for many other currently known languages. Look up the history of C and you will find that it has been around for some time (as programming languages go anyway). Take a look at Python for example a fully Object-Oriented High-Level programming language. It is written in C (perhaps C++ too). That tells you if you ever want to know what is going on under the hood in other languages; understanding C and how it works is essential.

An applications language is used for high-level programming, e.g. writing a word processor or game. Examples of applications languages are Java, C#. The reason is because they contain garbage collection, automatic typing, run-time validation, etc. - where the focus is productivity.

A systems language is used for low-level programming. e.g. A micro-controller, a driver, and OS kernel. Examples include assembly, C. They require little or no runtime to run code directly on the hardware, and the focus is for the programmer to have direct control over the hardware.

Overall, it's declining as an applications language, but still holding strong as a systems language.

2

Oh yes, it is used. I work in the field of network packet processing. I have been at two different companies where we process network packets. So, we are operating at the Ethernet or IP level, not at the level above TCP.

Interestingly, in both companies C was chosen over C++. In one of the companies, one of the two products was built on top of Linux kernel, whereas the other product was built in Linux userspace. The kernel product obviously used C as Linux kernel is programmed in C, but they chose to use C for the userspace product as well. Both products were developed starting from about year 2000 (the kernel product a bit before 2000 and the userspace product a bit after 2000).

In the company where I went after that, the product was built on C, not on C++. It is actually a continuation of a project from the mid-1990s, although due to recent performance improvement demands, it was decided that essentially everything will be rewritten. We had an option to select C++ due to this rewrite, but didn't do so.

In the field of network packet processing, performance counts a lot. So, I want to implement my own hash table having higher performance than existing hash tables. I, not the hash table author, am who selects what hash function is to be used. Perhaps I want performance and go for MurMurHash3. Perhaps I want security and go for SipHash. Memory allocators are obviously custom. In fact, all the important data structures we use have been custom-implemented for the highest possible performance.

While there is nothing that would prevent the use of C++, it is usually a bad idea. A single thrown exception per packet will drop the packet processing rate to unacceptable levels! So, we cannot use C++'s exceptions. Way too slow. We are already using kind-of object-oriented C code by implementing data structures as structs and then implementing functions operating on those structs. C++ would allow having virtual functions, but then again virtual function calls would kill performance if used everywhere. So, it's better to be explicit and have a function pointer if virtual function calls are needed.

C++ will do a lot of things behind your back: memory allocation, etc. On the other hand, in C that usually doesn't happen. You can write a function that allocates memory, but usually it is apparent from the interface of the function that allocation is happening.

As example of the kind of micro-optimizations that you can do when programming in C, take a look at the container_of macro in the Linux kernel. Sure, you could use container_of in C++ code, but who does that? I mean, it is entirely acceptable in most C programs, but typical C++ programmers would immediately propose something else, such as a linked list that allocates the link nodes as separate blocks. We don't want that because every allocated memory block is bad for performance.

Perhaps the only thing that would benefit us in C++ is that C++ allows template metaprogramming, meaning you can sometimes avoid virtual function calls while still having a function parameter, and allow the compiler to inline the functions. But template metaprogramming is complicated, and we have managed to fulfill all requirements in C, so the benefit of this feature in C++ is not so critical.

In one of the companies, we actually had a custom compiled language where part of the features were implemented in. Guess which was the target language of the compiler? Assembly? No, we had to support both 32-bit and 64-bit architectures. C++? Surely you jest. Obviously, it was C with GCC's computed goto. So, the custom language was compiled to C (or actually the gcc variant of C that supported computed goto), and the C compiler produced assembly.

2

C Language was born in 1972 it's 48 years old, C++ was born in 1985, so it's 35 years old, they have 13 years difference: It's incredible to realise how much more C has achieved as a language in comparison to C++ but also to a lot of other languages, I don't believe its just due to those 13 years difference.

C is elegant, easy and extremely powerful; the first thing that motivated me to use it was the closeness with assembly language: those 32 keywords could almost be directly translated into machine code, and the freedom in using memory allocation and pointers was in par with those years computing: you could do anything with C

Is C better than C++ then? I have no idea, comparing two programming languages is a critical task, you need to take into account the field, the application and when you compare them: what I believe is that there is still going to be a reason in the future to teach C in a computing course, I don't believe the same for other languages that are more application based: C is sort of the Math of computing IMHO.

1

"Here's a thought ..." (in fact, it's exactly the "thought" that the language's original designers had in mind:

"C" is much better than Assembler!"

"The Linux® Operating System" supports more than 20 different hardware platforms – "and this is why." Because the only difference needs to be in the /arch directory ... and the boot-time "trampoline" code. All of the source code that does not have to be "architecture-specific" ... isn't. Instead, the system assumes the presence of a language-compiler – most often but not necessarily gcc – which supports the target.

"C" is intended to be a language which directly supports "low-level constructs," and whose source-code can be predictably associated with them. So, the Linux® developers could "code very close to the metal," as of course they must, while calling-out to architecture-specific (assembly-language) subroutines only when they must.

Incidentally, this was quite an advanced concept, at the time. All operating systems prior to Unix® were written in assembler. The "C" programming language was originally conceived as a tool for writing Unix.®

Today: "the uber-cool language 'X' ... is written in ..."

0

I still use C on a daily basis and one of the primary reasons is because of interop with other languages and an SDK designed to be used by plugins built by all kinds of compilers in various languages.

I can't write a C++ API that uses classes with constructors and destructors and vtables, function overloading, throws exceptions, etc. that can be used from Lua, C#, Python, C, etc. let alone a C++ plugin written using different compilers and settings from our own.

I can't write a C# SDK that can be called from Python, e.g., or a Python SDK that can be called from C#.

C is the only language here that allows me to create an API that can be called from any of these languages. That said I often use C++ to implement these C interfaces (though I sometimes just implement them in C).

Besides that, I sometimes find C the easiest language to work with for things like low-level data structures and memory allocators. All the extra type safety you gain in C++ doesn't help if you're writing a memory allocator designed to pool aligned bits and bytes. And against C++'s rich type system and exception-handling, it's not easy to roll your own data structures -- just look at how much effort it takes to write a data structure as trivial as std::vector if you want to make it exception-safe and avoid invoking ctors and dtors on elements you didn't insert to the container (I'm speaking as one who has implemented the entire C++ standard library). When just a growable array is very difficult to implement well, then imagine the work required to implement a production-quality BVH.

I prefer C++ over C when I want to use existing data structures or implement higher-level ones by using existing ones, but if I'm going to be implementing a low-level data structure at the core of an engine that has no use for existing data structures, C definitely makes that a lot easier to do with its uber simplistic type system that lets you just memcpy things here and memmove things there, malloc a contiguous block and realloc it there, without worrying about constructors, destructors, and exceptions being thrown.

Not the answer you're looking for? Browse other questions tagged or ask your own question.