Handmade Hero»Forums»Code
Ameen Sayegh
51 posts
How To use C in Web programming
In Day 142 pre stream people asked questions about web programming and Casey mentioned that he prefer to use C rather that PHP and he did use it on some projects of his.

I have no idea how that works and I would like to know more about that.

Did he meant creating a server and handling sockets and HTTP manually?
This is only way I know about and I can think of, I'm not sure if that what he meant.

If someone can share more information about this topic, how to use C for web programming, it would be appreciated.
Ivan Ivanov
7 posts
How To use C in Web programming
You basically can use whatever language you want as a CGI script, C included. https://www.cs.tut.fi/~jkorpela/forms/cgic.html#ex

But let me assume that the reason why Casey prefers C is because he is just so comfortable with writing in C, whereas he really doesn't want any language (especially one that sucks like PHP) stand in his way. This does not mean that day-to-day web programming _should_ be done in C, and there are very objective reasons for that.
Ian Hern
8 posts
How To use C in Web programming
Edited by Ian Hern on
I remember him talking about how he generates all his layout using floating div's for everything, ignoring the traditional layout techniques. If you go to http://mollyrocket.com/casey/ and view source you can see what the output of his program looks like. Not sure if that actually answers your question at all.
popcorn
70 posts
How To use C in Web programming
That CGI method looks very interesting. I think I should maybe do a project on it later.
I think people should start using C for web development because web sites are so horrible as well as these high level programming languages that let people copy and paste code or use other people's library without any knowledge of whats happening. I don't want to get into how bad this high level languages but I can say that PHP beats most of the web languages out there and is used for top web sites on the interwebs.
Bryan Taylor
55 posts
How To use C in Web programming
C0D3
That CGI method looks very interesting. I think I should maybe do a project on it later.
I think people should start using C for web development because web sites are so horrible as well as these high level programming languages that let people copy and paste code or use other people's library without any knowledge of whats happening. I don't want to get into how bad this high level languages but I can say that PHP beats most of the web languages out there and is used for top web sites on the interwebs.


Let me preface with: I like C. I think we should write more things in C.

I do *not* think we should use C for web apps. C is, in general, a poor match for web programming. Most web programming is string manipulation. Parse the request string, build a database query, insert the results in a template page. Strings everywhere. C's stdlib string handling is a disaster. Subtle bugs abound, where "well, it doesn't crash" isn't enough to verify a piece of code even *works*, much less is not a massive security hole.

More to the point, the web app's performance is rarely relevant to the end user. For something like Facebook or Twitter, the app simply makes a database query. Performance bottlenecks are the database (which *is* written in C, in pretty much all cases) and network latency. Unless you're handling millions of queries a second, you don't really care how slow your app itself is.

The real reasons the modern web "feels" slow are a) Javascript, which runs directly on the client machine, and b) the "best practice" of serving files from a dozen different CDNs, which means a single page load causes a cascade of dozens of requests, each with its own overhead and latency. (Because the first request has to finish for the browser to even *know* it needs to do the other requests, this is a huge problem.)
Ian Hern
8 posts
How To use C in Web programming
I don't think the fact that the stdlib string is bad is an excuse at all, especially since this is handmade hero, write your own string library. I think writing your server in C is probably a fun project, but the big issue is that if you want your site to be dynamic you are going to have to use some sort of php or javascript.
popcorn
70 posts
How To use C in Web programming
Edited by popcorn on
btaylor2401
C0D3
That CGI method looks very interesting. I think I should maybe do a project on it later.
I think people should start using C for web development because web sites are so horrible as well as these high level programming languages that let people copy and paste code or use other people's library without any knowledge of whats happening. I don't want to get into how bad this high level languages but I can say that PHP beats most of the web languages out there and is used for top web sites on the interwebs.


Let me preface with: I like C. I think we should write more things in C.

I do *not* think we should use C for web apps. C is, in general, a poor match for web programming. Most web programming is string manipulation. Parse the request string, build a database query, insert the results in a template page. Strings everywhere. C's stdlib string handling is a disaster. Subtle bugs abound, where "well, it doesn't crash" isn't enough to verify a piece of code even *works*, much less is not a massive security hole.

More to the point, the web app's performance is rarely relevant to the end user. For something like Facebook or Twitter, the app simply makes a database query. Performance bottlenecks are the database (which *is* written in C, in pretty much all cases) and network latency. Unless you're handling millions of queries a second, you don't really care how slow your app itself is.

The real reasons the modern web "feels" slow are a) Javascript, which runs directly on the client machine, and b) the "best practice" of serving files from a dozen different CDNs, which means a single page load causes a cascade of dozens of requests, each with its own overhead and latency. (Because the first request has to finish for the browser to even *know* it needs to do the other requests, this is a huge problem.)


If string lib is a problem, then you can write your own. I wrote one for reuse and it might have some security problems but it's not like PHP or Python or Ruby or Java (lols) has security problems also.
Also, I wasn't really talking about speed as the problem but as a problem. I'm mostly talking about functionality and I can say not all of it, is a problem of Javascript but problems with the backend programming, being poorly programmed or poorly designed sites that just doesn't make sense. This usually stems from people using these high level languages and either create bad code, copy and pasting or using somebody's bad library/gems/whatever. It's just really that horrible because most thing end up not functioning right or functions at all and has nothing to do with speed or security.
I guess it's believed that people only used C for optimization which is not true for at least me. C gives you control and flexibility that most high language don't. It's not about speed, it just comes with it.