101

I was having a look at a few different web servers this morning when I came across G-WAN. As I understand, its a web server written in C and you have to make use of it by writing your websites/webapps in C. One clear benefit is speed as the G-WAN site suggests.

However, on the forums, the creator of G-WAN asked why not use C for web based apps and I can not think of a single reason apart from it being difficult (for me anyway, I am newbie when it comes to C). There must be more reasons why we all use PHP, Python, Ruby etc apart from it being easy to develop in these languages. I don't see that as a good reason.

So I put it to you: Why do you not use C for your web apps?

Abs
  • 51,038
  • 92
  • 260
  • 394
  • 3
    But C *is* used for creating CGI scripts. A single web application may contain components written in many different languages. – PauliL Jun 20 '10 at 12:32
  • 34
    Why do we use stoves and don't cook our meals directly with fire? Why do we use cars although walking or using the bike is much more healthier? Why... I could go on... – Felix Kling Jun 20 '10 at 13:44
  • 16
    @Felix - as I said, name other reasons apart from it being difficult. Which implies I am aware that other languages exist to abstract difficulty. – Abs Jun 20 '10 at 13:59
  • 2
    facebook has a php to c++ code transformer called [Hip Hop](http://developers.facebook.com/blog/post/358), which can improve your performance when your CPU becomes your bottleneck. – Lucas Jun 20 '10 at 14:18
  • 15
    Never really considered C diffiult. – 3Dave Jun 20 '10 at 16:30
  • performance difference is reducing quite fast due to lots of built in functionality ( which already in compile form ) and JIT for almost all dynamic languages. – iamgopal Jun 26 '10 at 02:26
  • 10
    @David Lively If you failed to put the 'c' in difficult on purpose that's really quite funny. Well done :) – punkrockbuddyholly Oct 31 '10 at 23:12
  • 11
    @MrMisterMan only took someone four months to spot that one. =) – 3Dave Nov 01 '10 at 15:18
  • @Lucas NB: This question is about C, not C++. Take care [not](https://blog.directededge.com/2009/05/21/c-and-c-are-not-the-same-language/) [to](http://harmful.cat-v.org/software/c++/linus) [confuse](https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B#Constructs_valid_in_C_but_not_in_C.2B.2B) [the](http://stackoverflow.com/questions/1201593/where-is-c-not-a-subset-of-c) [two](http://www.lb-stuff.com/cc). – Braden Best May 17 '17 at 19:10
  • have you guys tried Kore Framework. its a Web Framework for C Language. – Eddwin Paz May 23 '18 at 12:59
  • I raise your G-WAN to socket programming! There is where real programmers set apart from the rest. It's doable but you have to be not only good in C but as a programmer in general. – m4l490n Jan 23 '20 at 18:52

25 Answers25

79

It takes a great deal of care to get a C program correct and secure. That care means that you need to have really good people writing your programs. That means you pay more.

Also, C doesn't have the benefit of drawing from an enormous single standard library of functionality as .NET (and the other major web-centric platforms) has. So you may have to either buy components, or perform interop, or roll your own functionality which comes "for free" with a more, shall we say "web-centric" language like PHP or C# or Ruby or whatever. That means you pay more.

Add all of that to the fact that single-threaded computational speed just isn't that important on the web. If you need more scalability, most organizations can economically just throw more cores at the problem and be fine. This is not true for everyone, of course. I'd imagine that the core of Google's engine is written in C or a similar language not only for speed, but to save real money in power costs.

Dave Markle
  • 88,065
  • 20
  • 140
  • 165
  • 48
    Wow, an argument for .NET against C because of *libraries*? Sure, the stdlib is smaller, but we have decades of libraries (many open-source) in C. I'm having trouble thinking of anything in the .NET stdlib that there isn't a mature and free C library for. – Ken Jun 20 '10 at 16:01
  • 3
    I don't think he was arguing for .NET specifically, I think he just meant there are languages out there that have lots of cohesive libraries. I am sure C has loads but to be honest I haven't come across any sort of repo which gathers them all in a single place or packages them. – Abs Jun 20 '10 at 16:11
  • 16
    @Ken String manipulation is a really common web-app task. C libraries exist for that, but they're not nearly as numerous or usable as libraries in [pick a high-level language]. – Andres Jaan Tack Jun 20 '10 at 16:12
  • 23
    @Ken: There's a universe of difference when you have a single, well supported set of functionality vs a plethora of small libraries which differ widely in feature set, licensing, and support. – Dave Markle Jun 20 '10 at 17:48
  • I just read a book "Hardware Hacker". One of his theses was as Moore's law was in full force, it wasn't economic to optimize code or hardware but rather to develop features and ship 'em out. As Moore's Law slows down, it may be worth going back to hardware optimization, and by extension perhaps to using faster lower level languages like C. Just an idea. – gcr Dec 21 '20 at 02:58
49

Hum...

It seems that I am a bit late in this discussion - but I just discovered it now. And I am grateful to all of you for so much input.

I am G-WAN's author, which makes it clear that I have seriously worked on the matter: G-WAN is both faster than all other Web Servers (no processing) and all other Web Application Servers (any processing you can imagine).

Yes, ANSI C also made it possible to process more static content - with less powerful CPUs (ANSI C is not only about making dynamic contents fly).

By the way, G-WAN uses C scripts (no C compiler and linker needed) so the compiling/linking cycle/delay does not exist.

In the process of comparing G-WAN to .NET Java and PHP, I wrote similar applications in all 4 languages: http://gwan.ch/source/

And, to my dismay, the modern scripting languages were not easier to use.

One part of the job which is especially frustrating is to desperately search for the 'magic' API call that will do what you want to do.

Think about how to do 'pretty thousands' in:

C#

String.Format("{0:n}"...

Java

new DecimalFormat("0.00"); ...

PHP

number_format($amount, 2); ...

ANSI C

sprintf("%'.2f", amount);

The "..." mean that some pre-configuration, or post processing, is necessary. ANSI C is clearly easier to use and to remember.

When PHP has more than 5900 API calls (C# and Java not far away), finding the right API call is a challenge on its own. The time wasted to find this (and then to find how badly the native API call is implemented), the time to learn by hart it for the next time you need it, all this time is depriving you from the time necessary to resolve your application problems.

I have read (above) that PHP is more concise than ANSI C? Why then use "//:: this is a comment ::" rather than "// this is a comment"? Why have a so stupidly complex 'pretty thousands' syntax?

The other usual argument is that Java and the like provide dedicated calls for Web applications.

I was not able to find anything to escape HTML in Java so I wrote my version of it:

  // all litteral strings provided by a client must be escaped this way
  // if you inject them into an HTML page
  public static String escape_html(String Name) {
      int len = Name.length();
      StringBuffer sb = new StringBuffer(len);
      boolean lastWasBlankChar = false;
      int c;

      for(int i=0; i<len; i++) {
          c = Name.charAt(i);
          if(c == ' ')  sb.append("&#32;");  else
          if(c == '"')  sb.append("&quot;"); else
          if(c == '&')  sb.append("&amp;");  else
          if(c == '<')  sb.append("&lt;");   else
          if(c == '>')  sb.append("&gt;");   else
          if(c == '\n') sb.append("&lt;br/&gt;"); else {
             c = c&0xffff; // unicode
             if(c < 32 || c > 127) {
                sb.append("&#");
                sb.append(new Integer(c).toString());
                sb.append(';');
             } else
                sb.append(c);
          }
      }
      return sb.toString();
      //szName = sb.toString();
  }

Do you really believe that the same code in ANSI C would be more complex? No, it would be both immensely simpler and faster.

Java (derived from C) is requiring programmers to link multi-line strings with a '+'
C# (derived from C) is requiring programmers to link multi-line strings with a '+'
PHP (derived from C) is requiring programmers to link multi-line strings with a '.'

ANSI C does not have this now completely stupid (obsolete) requirement.

So, were is the so obvious progress claimed by the modern languages? I am still looking for it.

Sincerely,

Pierre.

Gil
  • 3,199
  • 1
  • 13
  • 24
Pierre
  • 1
  • 1
  • 2
  • 10
    I don't quite understand your comment about extra processing of pretty thousands; for C# you left off `, amount)`, PHP is okay as-is, and your ANSI C example needs two more arguments (buffer and buffer length). With the notable exception of Java, your example seems to prove the opposite point. Additionally, I've never seen that `//:: comment ::` syntax before; PHP certainly doesn't require it. – icktoofay Apr 27 '12 at 04:54
  • 1
    To be honest with you, all of the _other_ options look much nicer than ANSI C, which, "is clearly easier to use and to remember" [sic]. – Jarrod Mosen Sep 04 '12 at 02:20
  • How does G-WAN compare with NGINX if both have been written in C? – m4l490n Jan 23 '20 at 18:58
47

The same reason we don't use C for most programming. The benefits (which are mostly performance) don't outweigh the costs (development time, lack of automatic memory management, lack of automatic protection from buffer overflows, having a compile stage between the edit and test stages, etc).

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
  • 10
    Was typing the same thing and you beat me to it. I just want to add that protecting websites from malicious behavior is enough of a challenge as is, we don't need to add potential attack vectors from a misuse of memory management, pointers, etc. – Rob Allen Jun 20 '10 at 12:12
  • @Jordan: I get the feeling you haven't done any web programming in C. What you're saying doesn't fit the model of how web programming is done. –  Jun 20 '10 at 23:20
  • 2
    Sure it does, assuming that your website has any place for user interaction, whether by URL or form input. Once that data is handed to the server, it's up to the programmer to make sure to correctly allocate memory. G-WAN has some abstraction around query parameters but that's not going to completely save you. I'm not saying that correctly done, C web programming can't be secure and fast, but it is more susceptible to harsher errors. – Jordan Jun 21 '10 at 00:56
  • 3
    **sigh** @Kinopiko: If you don't know what a buffer overflow is, you shouldn't be coding in C. **'nuff said.** For more info: https://www.securecoding.cert.org/confluence/display/seccode/CERT+C+Secure+Coding+Standard – L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳ Jul 22 '10 at 05:26
29

Most network applications, especially web servers, are far more "I/O bound" - ie they are capable of pumping out data far faster than the network can accept it. Therefore something that is highly CPU efficient is not a huge win, whereas something that is scalable and maintainable is. So there is no reason to accept the drawbacks of C and lose the advantages of a managed environment like Java, .NET, Python, Perl or other languages.

Paul Tomblin
  • 167,274
  • 56
  • 305
  • 392
  • 14
    If I can fill the network pipe with Java or Perl (and I can), the fact that C is faster is irrelevant. – Paul Tomblin Jun 20 '10 at 13:31
  • 1
    @Kinopiko, are you saying you have a bigger pipe, more page hits and more data to ship than eBay (Java), Stack Overflow (C#/.NET), Google or any of a million highly used web sites written in higher level languages? – Paul Tomblin Jun 20 '10 at 14:45
  • 1
    @Paul Tomblin: The purpose of a web application is not to fill the network pipe but to do some processing. If you only need to serve some text and images, you should use static html pages, which give superior performance. In most cases, the static pages are served from cache, so your server needs do nothing. OTOH, when processing is needed, it may well be the bottleneck (although more commonly the disk is the bottleneck). – PauliL Jun 20 '10 at 15:09
  • 4
    You need to remember that web applications are only a fraction of a "server-stack" that handles user requests, and is not a part that is performance-critical. There are other parts - OS kernel (usually written in C), filesystem (C), web server (usually C), language interpreter (C), database (usually C or C++). Web application usually only passes data from one part to another, applying some basic operation on it, and it's performance is not crucial at all. – el.pescado Jun 20 '10 at 16:40
  • @PauliL - The examples I gave are not "static pages served from cache", they are highly dynamic sites where every page view involves retrieving things from databases, performing calculations on them, and putting all that information together and firing it down the pipe. And all of those sites use high level languages, not C. So show me a site that is CPU bound and needs to be written in C? – Paul Tomblin Jun 20 '10 at 17:49
  • @Kinopiko, don't change the subject. Of course you want your servers to be fast and efficient, but that wasn't the question, the question was "why not use C for web based apps", and I believe I have explained sufficiently why there is no need to do so. – Paul Tomblin Jun 21 '10 at 01:59
  • You don't think Google writes web apps? Google Mail, Google Calendar, Google Documents? They're written in higher level languages. And they handle billions of page hits per day. – Paul Tomblin Jun 21 '10 at 10:33
  • Anyone can fill a pipe with a slow app, a faster app will get it clear sooner and is therefore better. Get pages finished sooner to the client and then you free up a connection. Faster is obviously better :) Still, it'd take ages to write stuff well. – John Hunt Oct 03 '12 at 11:03
15

C isn't a convenient language for manipulating strings.

Compare C#:

string foo = "foo";
string bar = "bar";
string foobar = foo + bar;

Corresponding C:

const char* foo = "foo";
const char* bar = "bar";
char* foobar = (char*)malloc(strlen(foo)+strlen(bar)+1);
strcpy(foobar, foo);
strcat(foobar, foo);
//todo: worry about when/where the foobar memory
//which was allocated by malloc will be freed
ChrisW
  • 51,820
  • 11
  • 101
  • 201
  • 4
    PHP does not handle Unicode properly either, but is very popular Web-oriented language. – el.pescado Jun 20 '10 at 14:11
  • 12
    So we should use C++, as it gets about the same performance as C, but compiles your C# example like a charm? – tstenner Jun 20 '10 at 16:09
  • 2
    As of string manipulation - web applkication usually only output strings, so C's `printf` should do the job. – el.pescado Jun 20 '10 at 16:41
  • 5
    C handles multibyte and unicode just fine via runtime library functions. Using the safe functions like strncpy and so on makes it pretty safe too. – justinhj Jun 20 '10 at 16:53
  • @tstenner I'm not saying you shouldn't; arguments against C++ might be that there are run-time-libraries or frameworks for web applications, which are intended for languages other than C++: for example, the functionality you get from using ASP.NET is more intended for (and easier to use from) C# than C++. – ChrisW Jun 20 '10 at 17:08
  • @ChrisW fastcgi, Wt, CppCMS. They are not as exhaustive as ASP.NET, but they do exist. – tstenner Jun 20 '10 at 18:13
  • @tstenner Do you think you get better performance from C++ than C#? I would have guessed, "You can: if you restrict yourself to a subset of C++ that involves mostly preallocated memory instead of using the heap" (which isn't the case when you use std::string etc., i.e. it's more true for embedded programming that for pplicatin programming). – ChrisW Jun 20 '10 at 18:58
  • @ChrisW C++ has always been fast enough for me, the one major advantage I see over C# is, that I can rent a cheap linux box and that's it, I don't need Windows hosting. – tstenner Jun 21 '10 at 14:37
  • 2
    Or you could use asprintf(&foobar, "%s%s", foo, bar); – Patrick Lorio May 30 '14 at 14:37
11

If difficulty and complexity were not an issue at all (ha!), then I wouldn't stop at C. I'd write x86 assembly. It's been years since I've used any web server that wasn't x86, and it's looking less and less likely every day.

To use C (instead of assembly, or something higher-level) is to suggest that C is the sweet spot of programmer efficiency and computer efficiency.

For the programs I write, this is not the case: C is a poor match to the kinds of programs I want to write, and the advantages of it over a decent macro assembler just aren't that significant. The program I'm writing right now is not hard in my HLL of choice, but the complexity of it in either assembly or C would be so high that it would never get finished. I acknowledge that a sufficiently smart programmer with enough time could make it run faster in assembly or C, but I am not that programmer.

Ken
  • 2,684
  • 18
  • 11
7
  • it's insecure
  • it's hard to read
  • it's hard to maintain, development time is slower on the order of a magnitude
  • most of your web stuff is probably I/O bound, so the would be speedup doesn't even matter, especially when you use a fast language like Java or C#
  • 2
    what does "fast language like Java or C#" mean? – RobS Jun 20 '10 at 13:34
  • @RobS: Java and C# implementations are faster than they typical dynamic languages used for web programming, and as fast or faster than C/C++ depending on what you're doing. @Kinopiko: you're being naive. – L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳ Jun 20 '10 at 13:43
  • 3
    @Kinopiko: what weaknesses do dynamic languages have? When a noob programmer explicitly says he wants to execute arbitrary code via `eval` and php's `include`? Please, in C/C++ you don't state that you want to execute arbitrary code, it just does. `eval` isn't a bug, it's the programmer's intent. In C/C++, any bug can lead to remote code execution, in dynamic languages, this isn't the case unless some fool is messing with `eval`. – L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳ Jun 20 '10 at 13:45
  • @Kinpiko: The link I provided proves (even gives real examples of vulnerable applications) that you have to follow a bunch of unintuitive rules (even I didn't find them all intuitive, after programming assembly for years before starting C) in order to have safe C code. This is not the case for any modern safe language. – L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳ Jun 20 '10 at 13:48
  • 1
    "and as fast or faster than C/C++ depending on what you're doing" maybe, but you are still dealing with a managed memory environment with both Java and C# which can become quite a drawback in high traffic scenarios. – RobS Jun 20 '10 at 14:29
  • 5
    "and C certainly doesn't have as many security weaknesses as any dynamic language does" _That_, my friend, is complete nonsense. Why does that comment have 3 upvotes? – L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳ Jun 20 '10 at 16:29
  • I'm not sure I can explain in the length of a comment box. Please ask that as a separate question. –  Jul 16 '10 at 07:37
  • 2
    @Kinopiko: Well actually, you can't explain it anywhere. There is no explanation for it. C has a **much** bigger problem than any dynamic language. The worse thing that can happen in a dynamic language is "attribute not found, whoop dee doo". – L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳ Jul 22 '10 at 05:22
  • You can argue that C#/JAVA are hard to read. I am a .NET developer for 5+ years and I also code in C/JAVA for my personal projects. I realize that outsiders can read my C code clearer than C#/JAVA. This is due to the fact to all of the syntactic sugar in C#/JAVA. For example C# extension methods and implicit user-defined type conversions. If it's not well documented you have to dig through code or use extensions like Resharper to understand what code is being executed and why. Nothing against managed code. It looks great, but what code can be executed isn't always obvious. – pqsk Feb 13 '15 at 21:19
7

I know this question has already been answered to death, but there are 2 things not mentioned so far that are extraordinarily important to success in any programming paradigm, and especially in web development where you get a lot of people that aren't necessarily programmers, working with the code.

  1. Involved, useful community, aka People That Have Solved My Problem Already. It's pretty easy for even the noobiest of noobs to Google why they're getting "headers already sent" errors in PHP, whereas that information might not be available for a framework or language that is new to the scene, or otherwise doesn't have critical mass.
  2. Frameworks, so that most programmers can get to solving business problems and not hacking code together.

If I had a critical app that required extreme performance, I would use C, but it would take so much longer to write, that I would never get to market. Until there is either #1 or #2 it's not feasible for me to use it.

Jordan
  • 29,324
  • 6
  • 51
  • 63
6

C is quite low level languages for many purposes: no-OOP, lots of manual resource management.

There is limited use of C for web, for example Klone. It is mostly used for low resource embedded application cases.

However there are C++ web frameworks like CppCMS that are used for high performance web applications developments.

C++ allows you to enjoy high abstraction and fine grained access to what you are doing exactly giving much better option for deploying and developing large applications.

But you use them in case when performance and resource usage is much more critical then time-to-market and development costs as generally web development is faster using good web frameworks for languages like Java, Python or PHP. Also generally there less competent programmers for C++ then Java/P* languages for same salary.

So it is question of priorities, also there less tools for C++ Web development then for PHP/Python/Perl or Java.

Artyom
  • 30,091
  • 20
  • 121
  • 208
  • 2
    Interesting, I didn't know about CppCMS a low level web framework written in C++. Might be the best of both worlds, sitting in between C and the Web Languages. – Abs Jun 20 '10 at 14:01
  • It is now low level framework, it is MVC web framework that is very FAST. – Artyom Jun 20 '10 at 14:36
  • Sorry, I just refer to anything that isn't a web language as low level. I understand though its a MVC web framework. – Abs Jun 20 '10 at 14:48
  • There is no such thing as "Web Language", probably only PHP can be called web one. All other languages including Java, Python, Perl, Ruby and C# are general purpose languages that are used for Web. – Artyom Jun 20 '10 at 14:55
  • 1
    Well if we want to be specific like that there are no such thing as general purpose programming languages. There are only Procedural, Structured, Object-Oriented, Functional...etc... – Abs Jun 20 '10 at 15:49
6

There must be more reasons why we all use PHP, Python, Ruby etc apart from it being easy to develop in these languages

This is the entire reason and the only one needed. It has many benefits, chief of which is time to market. If you can get your Web app online in a month using PHP instead of two months using C, you might just win. If you can add a new feature in a week using Ruby on Rails instead of two weeks using C, again you win. If you can fix a bug in a day using Python instead of two days using C, you win once more. If you can add a feature because you are using language X that your competitors cannot add at all because they are using language Y and it's too hard in that language given their resource constraints, then you definitely win.

And by "win" I really mean you don't lose. Your competitors are using higher-level languages and frameworks to develop their sites, so if you use C, you are not competing with other people who use C, you are losing against other people who do not use C. Just to compete, you have to use tools with similar levels of abstraction.

If performance becomes an issue, you can rewrite the slow parts of your site in better-performing languages. Or you can just throw more hardware at it. Really, a performance problem is what we call a "nice problem to have" -- it means you have already succeeded. But spending more time developing the basic functionality of your site is rarely an option. Writing it in C just so it will run faster is premature optimization, which, as Knuth tells us, is the root of all evil.

All this implies that if you can use a language with a higher level of abstraction than Python or Ruby, you could win against people using Python or Ruby. Paul Graham's story of how he and his team used LISP as a "secret weapon" in developing Web sites may be instructive. http://www.paulgraham.com/avg.html

Of course, if you are developing a site for your own amusement, do it in whatever language you want. And if your site is CPU-bound (hardly any are; they are usually I/O bound) then use the fastest-performing language you can. If you are trying to innovate, though, use a high-level language with the best abstractions you can find.

kindall
  • 158,047
  • 31
  • 244
  • 289
  • You're forgetting that the high volume order processing bit of via web was written in C. See the notes at the bottom of http://www.paulgraham.com/avg.html. – Giles Roberts Feb 09 '12 at 14:58
5

@Joeri Sebrechts

F.U.D. in action:

PHP, Python and so on are easy to scale up by throwing hardware at the problem.

Well, actually no. They don't scale at all vertically and scale very poorly horizontally. See: http://gwan.ch/en_scalability.html where it is explained how much trouble is ahead of bad-performers.

Suppose it costs 1 person 1 year of effort to develop an app in PHP, and it costs them 3 years to do it in C (as C requires more effort to do the same thing).

Wrong again. If PHP libraries have been written in C then they are directly usable from C -instantly providing the 'unique productivity' you are claiming PHP has.

That means the reduced hardware need of the C code has to represent 2 years worth of wages for C to become attractive. In practice that (almost) never happens.

Pure F.U.D. (see the answer above).

Facebook's scale is so large that hardware is a big enough cost to care. That's why they developed HipHop, which cross-compiles PHP to C++. It brings the best of both worlds: the simplicity of programming in PHP, and the raw performance of C++. Facebook is still developed in PHP, but when you use it, it's all native code.

HipHop is much faster than PHP, no doubts on that. But if you compare HipHop with a plain-C implementation you have two layers of overhead:

  • the PHP to C++ interfaces (which use the bloated C++ libraries);
  • the C++ bloat itself (which makes C++ 2-10 times slower than plain C).

Plus, HipHop has been written in the clueless inefficient Academic mode (detached from any real-world reality). sure, it can impress PHP coders but show this code to an embedded programmer and he will feel sorry for Facebook.

"A language that doesn't have everything is actually easier to program in than some that do" --Dennis M. Ritchie

Unlike (most of the) programming language END-USERS, Dennis knew a couple of things about the matter, it seems.

Pierre
  • 66
  • 1
  • 1
  • 3
    (1) Don't take stuff so personally, I wasn't out to diss your product because I didn't even know your product. (2) You make an interesting proof of concept with G-wan, but I hope you'll excuse me if I take a wait and see approach until I see real-world web apps using it, and not just a few synthetic benchmarks. (3) You've mischaracterized facebook's complaint about horizontal scaling, because their issue is with memcache and database multi-core performance, the bottleneck of real-world web apps, which is something G-wan doesn't help solve. (4) HipHop welcomes your code contributions I'm sure. – Joeri Sebrechts Jul 18 '10 at 17:51
  • 1
    Speaking to the productivity issue. I've written web apps in C before, many moons ago, and at the time it required way more lines to do the same thing than PHP did, while having a much greater risk of crash / leak sorts of bugs. (I'm just "average" at those sorts of bugs, which is to say that I write them now and then.) I base my judgement on that experience until I see real apps that demonstrate otherwise (e.g. a wordpress port to C that doesn't require a lot more lines of code). – Joeri Sebrechts Jul 18 '10 at 18:06
  • How does something "not scale vertically"? Do you mean it doesn't benefit from faster single core performance and faster memory performance? – rakslice Jul 07 '15 at 22:18
4

You think being easy is not a good reason. I think it's a good reason. If you need ultimate performance then C is ok, but other languages abstract away the hard stuff to improve productivity, maintainability and reduce defects.

Craig
  • 34,658
  • 33
  • 108
  • 191
  • 6
    To get ultimate performance you need not only C, but also ultimate C programmer. And those guys typically want ways more money than Java/P* programmers. – el.pescado Jun 20 '10 at 14:09
4

Consider that I'm not a web developer but will ask these questions anyways and offer a point or two.

What web site is only written in one language? Seriously this thread seems to assume one hammer fits all nails.

When was the last time anybody seriosly stated that C was complex? I mean really guys you can't get much more low level. I'm not talking C++ here as the two are often referenced collectively.

C has security concerns, that can't be denied but are they any less than what is seen in the kludges called PHP & Perl? In either case a secure web site is a function of programmer discipline.

In any event off to the comments. The difficulty of using any given language is very dependant on the problem at hand C & especially C++ can lead to fast solutions to a problem in experienced hands.

Industrial uses for web servers, that is embedded servers/sites simply don't have the language choices a normal web server might have. So you end up using a variant of C or possibly something like BASIC. Your goal is to offer up the functionality the device requires and not to worry about languages. On a mainstream web server the way to do that is with high level languages most of the time. Walk away from big iron and your programming freedom goes out the door.

Without the right Libraries it would be foolish in most cases to do a ground up web project in C. The lack of good standardized libraries is a big negative here.

3

Here is some more web-related code written in C that is worth a look when building your own C library for the web:

  • cgic: an ANSI C library for CGI Programming
  • cgit: a web frontend for git repositories
  • wbox: HTTP testing tool
  • wget html-parse.c
  • curl cookie.c
  • Discount, David Parsons' C implementation of John Gruber’s Markdown text to html language
  • Protothreads (esp. for embedded systems), http://www.sics.se/~adam/software.html
  • protothread, Google code project by LarryRuane
  • uriparser sourceforge project
  • http-parser, http request/response parser for c by Ryan Dahl on github
  • nginx
  • ...
ceeit
  • 1
  • 1
2

I would use C for a web app if:

  1. I have a little budget for the hosting server (small VPS) and my time is not expensive.
  2. I am working for Facebook, Twitter or someone in need to reduce the amount of servers used (with thousands to millions of users).
  3. I want to learn C and I need to find a real world app where I can use it.
  4. I know C much much better than other scripting languages.
  5. I am an eco guy and I want to reduce the carbon footprint of my app.

G-WAN runs code as scripts, yes C scripts like Play! Framework does for Java. G-WAN is very fast and has an easy API. G-WAN makes possible to use C/C++ for web apps when other servers has failed to do that.

One thing is clear: you need a good programmer to write a web site in C, any crappy developer can create a site with spaghetti PHP :-) There are leak detectors and even garbage collectors for C.

rtacconi
  • 12,528
  • 19
  • 64
  • 83
2

Well, given the fact that Web development is a matter of having useful libraries (the kind used by PHP) then I do not see how C would not be useful.

After all, the procedural logic is the same: do while, for, if then else, etc. whether this is C, PHP, .Net or Perl.

And a C loop or test is not more difficult to write because it is written in C.

Most PHP libraries are made in C so the missing-C-libraries-for-the-Web argument does not look that much convincing.

My guess is that C was not advertised as a Web programming language by the promoters of Java (SUN) and .Net (MICROSOFT) because they had their own proprietary (heavily patented) intellectual asset to push for adoption.

As a free (unpatented) standard, C does not offer any 'lock-in' grip on developers... hence, maybe the heavy hand of lobbying in schools and universities to make sure that tax-payer money will fund the adoption of an inferior technology backed by private interests.

If C is good enough for IBM and MICROSOFT (they don't develop their products in PHP or .Net) but not good enough for end-users, then end-users might wonder why they are invited to suffer this double-standard.

Vince
  • 1
  • 1
  • 1
    Uhhhh Microsoft.com and sharepoint are both in ASP.NET. Much of Visual Studio 2010 is in .NET(WPF). This statement is patently false. Use the highest level language that meets your design requirements. For the web, that means C#/Python/etc, and components in C/C++/etc where performance becomes an issue -though that almost never happens. – 3Dave Nov 24 '10 at 22:28
2

All the languages you mentioned are actually written in C/++. The only difference is the advanced classes and libraries that were created from C that make up what is now these other interpreter languages. It is also why they are referred to as scripting languages.

Think of it like baking a cake (PHP/JS):

  • 2 Eggs, 1 cup Milk, 2 sticks of Butter, 4 cups flour, 1 cup Sugar, Baking Soda

But imagine having to make all the element that those things are composed of. (C/++)

  • 17 mg of sodium bicarbonate, 15 tbs of protein, 12 tbs of Vitelline Membrane, Amino Acid, Sulfur, ... Neutron Particles

C is the foundation of many modern languages. It is great and almost the most powerful languages under assembly. You can do it you just need to invest in building all the code that these other languages already did. Like building a cake from the periodic table.

Learn it you can literally make it do anything!

GetBackerZ
  • 408
  • 4
  • 12
1

String handling in C can be made easier using:

Data Types (part of libslack)

Libslack provides a generic growable pointer array data type called List, a generic growable hash table data type called Map and a decent String data type that comes with heaps of functions (many lifted from Perl). There are also abstract singly and doubly linked list data types with optional, "growable" freelists.

or:

Managed String Library (for C)

http://www.cert.org/secure-coding/managedstring.html

ceeit
  • 1
  • 1
1

Another point might be the platform dependency. C needs to be compiled into native code. This code doesn't run on all platforms.

Interpreted languages run wherever an interpreter exists. Many providers for example provide PHP-Interpreters installed on their servers but with a Windows OS. If you now are developing on a Linux-machine. You've got a problem.

Of course this problem could be solved but you see the advantage of developing in PHP in this particular case.

Hope this helps, regards domachine

domachine
  • 1,009
  • 1
  • 11
  • 20
1

"domachine" wrote:

platform dependency: C needs to be compiled into native code. This code doesn't run on all platforms. Interpreted languages (like PHP) run wherever an interpreter exists. Of course this problem could be solved but you see the advantage of developing in PHP in this particular case.

Did you ever wonder in which language the portable PHP interpreter is written?

In ANSI C.

So, before you dismiss the portability of ANSI C again, ask yourself in which language your favorite programming language has been written... (tip: almost all were written in C/C++).

ANSI C compilers are available on all the platforms I had to work on -and the same is not true for PHP and its gigantic runtime.

So much for the portability argument.

CClue
  • 1
  • 1
  • 2
    Well, PHP is NOT my favourite language. I didn't code anything in PHP, yet. I'm a C and C++ programmer. I've never dismissed the portability of C! The only thing I mentioned, was the easiness to apply PHP-Scripts to an existing WebSpace. Show me the case where you can get a low-budget WebSpace where you have a compiler. Maybe I'm wrong but I think you're one of these fanatic Programmers who adore one language but do not see the advantages of other, maybe more appropriate languages. I love C and C++ but I'm able to think outside of the box. – domachine Jul 17 '10 at 09:16
0

Similar to G-WAN, but for Cocoa / Objective-C is Bombax, a web applications framework.

http://www.bombaxtic.com

Speaking of Objective-C I can't resist pointing out MacRuby, which has the potential to revolutionize the way we will make web apps one day.

gregg
  • 1
0

PHP, Python and so on are easy to scale up by throwing hardware at the problem.

Suppose it costs 1 person 1 year of effort to develop an app in PHP, and it costs them 3 years to do it in C (as C requires more effort to do the same thing). That means the reduced hardware need of the C code has to represent 2 years worth of wages for C to become attractive. In practice that (almost) never happens.

Like with every rule, there is an exception. Facebook's scale is so large that hardware is a big enough cost to care. That's why they developed HipHop, which cross-compiles PHP to C++. It brings the best of both worlds: the simplicity of programming in PHP, and the raw performance of C++. Facebook is still developed in PHP, but when you use it, it's all native code.

Joeri Sebrechts
  • 10,770
  • 2
  • 33
  • 49
0

You have to love what you're doing to achieve results. Languages like java and php were created with lot of effort to make people's lives easier. Php especially has benefited many self learned web programmers today. You can see the kind of support it has in the world of web development.

Java iam sure was written to give you help in anything that can be possible in today's world. The enormous book is a clear indication, and it's a beast if you are looming for web development as well. Same goes for Python. These are so.e fantastic languages and platforms. No wonder they are hugely popular.

I am a C devotee and partly it's due to certain limitations which precluded me from giving a look at other languages like php. I am writing in C daily and every day I am proud to e and learning a new thing also. This makes me feel very good and I have also started learning about how C was the default choice when writing applications for websites through Cgi. This is abstracted in other platforms and when you develop websites that have to do with databases and web services, you need to know what's happening behind the scenes.

However if you know all that and still want to use a scripting language, there must be a valid reason, and we don't need anyone to advise against that.

Vijay Kumar Kanta
  • 911
  • 1
  • 13
  • 22
0

In the end you can use absolutely any language to develop sites, including assembler (via CGI etc.). If you meant why don't we use a compiled language, well, we already got .NET, Java and others.

Andrei Rînea
  • 18,961
  • 16
  • 112
  • 162
0

10+ years have gone by since the OP asked this, and Google search trends (within the genre of web programming) still show this as an active debate. I want to give a 2021 answer to this one, and since I've been programming professionally since 1979, and have used every aforementioned language exhaustively, I feel quite qualified to answer it.

  • More internet devices use C than any other language. They just aren't usually the public facing devices per se (Cisco, F5 networks, etc -- their cores are all compiled in C). But these are embedded platforms with a purposeful function that require almost RTOS-like stability. C will continue to be the dominant language here - it's so old that instability issues aren't a factor.

  • Big Tech uses C all the time ; you aren't going to find a cron job in their stack that does does a log flush or backup being written in bash or in PHP ; if it's complex it's likely that it's a gcc-linked binary that's doing the heavy lifting. Nothing is as reliable as C since it's been around forever and we're not finding new bugs in gcc.

  • The tech companies during the 1990s really built up the core of the technologies we use today, and C wasn't sexy [at all] to use. Many apps were developed with Java Servlets and applets which is quickly conducive to an MVC model and it's cross-platform. The latter is very important - as I'll explain.

  • Microsoft didn't quite embrace C or C++.. their Visual C was a terrible IDE compared to Borland's. But Visual Basic was a hit for them, though, which is why VB became ASP classic - which many websites still use today. It was the first language IIS officially supported (VB or Basic). Classic ASP is considered an old language with no support from MSFT anymore, but its usage was widespread in early web apps who wanted to use Windows NT (or Server) for their development. Bill Gates really wrote the best Basic interpreter back in the day, and the simplicity of Basic allowed for a very quick learning cycle to become a developer. However, Borland's IDE was amazing, but the company blew up (along with Novell and other greats from the 90s).

  • Nobody in their right mind in the 1990s were developing web apps quite yet for Linux, really.. other Unix derivatives, yes - but commercial versions of them. Many people deployed completely different OS's altogether (think of AIX or DEC VMS here). The words "free" and "great software" wasn't often part of the vernacular. IT managers, who became the de-facto original webmasters, didn't want their paycheck to ride on something free (Linux) because in part the distributions were fragmented (perhaps excluding RedHat's or Mandrake's).

  • IT Managers weren't programmers - so scripting languages became more popular for web development. Websites written completely out of bash were not unheard of.

  • Java (which is based on C) became perhaps the most popular notion of the future of the web, partially because Sun had great hardware and was renowned for their Solaris OS. Most C programmers who were interested in the web moved onto Java, it was pretty similar and offered concurrency (C wasn't designed as a concurrent language) and mostly stable garbage collection and memory clean up techniques.

  • VB devolved at MS and, seeking a compiled language for IIS, moved it up to what's now the .NET framework which is simply VB or C# wrapped in complexity. But IIS's weaknesses were overcome by the sheer number of Windows NT/2000 deployments.

  • The bubble burst and Oracle's Java, Solaris and DBs weren't a favorite amongst IT managers (though it was a favorite with executives because Oracle's sales team were top-notch). For one, Oracle's products were extremely expensive and they weren't (and really still aren't) known as a cutting-edge technology company.. they are the IBM of late - deploying what works and isolating more of the business problem than the technical one.

  • Post-Y2K, Linux out deploys just about everything on the web because the distributions started really getting to a commercial-level. This whole idea of open source was working. Windows was suffering from virus issues repeatedly and Apple was written off as a dead entity. Commercial platforms were becoming more under fire as CFO's were tightening budgets.

  • Linux started with a legacy PHP interpreter from the get go and the language has really grown from there. Most who deployed Apache would use this language simply because it was stable. Again, at this point we still have IT managers running these efforts.

  • Mid-2000s when Marketing functions started taking over the web presence of their respective companies, the needs quickly changed. Marketing folks love testing (think multivariate A/B campaigns) and so scripting languages really started sneaking up as the best way to accomplish quick changes that could be pushed to the web without having to hardcode HTML.

  • We forget, the ECMAScript (Javascript) really took off, Flash started to die, and so this whole idea of how to control the DOM came about when faster and faster browsers were able to leverage the UI.

  • Of all the modern languages out there, the frameworks really have usually been responsible for propelling the success of newer languages. Bootstrap was quickly absorbed when mobile came about, and integration with a legacy PHP app was easier than it would have been with a hardcoded C app. We started seeing mostly scripting take over compiled languages as the choice-du-jour, though .NET has it's place for certain things yet, though that's diminished in the last 2-3 years.

In the end, C is still the most widely used language "behind" the internet. Routers, load balancers, intrusion detection systems.. it'll all be in C (or C++ but more likely, C). These appliances are now moving to being software deployed but the hardware appliances are still used by the larger datacenters. For example, AWS has a wrapper around a grouping of Cisco switches to change the routes based on ToD (time of day) predictions. Amazon, Google, Facebook all realized that a "follow the sun" model of load balancing was a good starting point and really could only trust that to low level hardware devices to correctly do that. Scripting languages and even compiled ones are surfacing that are - for all intended purposes - really based on C anyway. But with the advent of k8s and Docker it makes more sense to rip a node.js framework and start working on a small unit of code that could be pushed independently of what the other pieces of code was doing. Outsourcing was used alot in the earlier days and this is about the only solid way of programming a system without conflict in a larger development environment.

We are still at the beginning of the internet.. in other words, in another 10 years there will be yet another cycle, and it'll likely be away from typing. Drag and drop DAG nodes will be used to create web apps in a similar way that a VFX artist uses Nuke or Maya to build a motion graphics clip. Twilio does this and even training an ML model is already beng done this way using Peltarion.

We're not yet to the highest level languages possible, as our abstractions in Ruby or PHP are still C-like, and throwing dollars at STEM education won't bring in legions of kids to study computer science. However, the barrier to entry will continually be lowered, but the need for a diverse understanding of languages will still be required for the complex applications or specific applications that require "something" like directly addressing a CUDA core (for example).

We are simply seeing a diversity in languages, as predicted by Ritchie himself, come about. Frameworks have matured so it makes no sense to drop a new developer on a C+CGI project when bringing up an Angular scaffold takes 5 minutes.

This was all expected, actually, by most of us from the 1980s. As programmers become diverse, so do the ways to attack the problem in front of them. As open source becomes evermore possible (and incredible!) there's simply no financial advantage to starting something from scratch. Headless CMS systems in fact are already being deployed so that the backend of the CMS doesn't require the dev team to write a line of code - they just worry about the user experience... and even then, the tools are getting more mature.

My point is this: C is definitely used on the web, and embedded systems use it all the time. MVC applications have progressed to where a framework can build a simple app for you within an hour, without compilation, and it happens that those frameworks are more extensible on the newer languages. It's an apples to oranges comparison for me - I still use C to do data normalization (for example), but if I'm being paid to write a web app, I can begin a Vue.js app while drinking a beer, so why work harder?