Categories


Archives


Recent Posts


Categories


Just Enough C for PHP

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

The funny thing about the C programming language is, if you look hard enough, you can see how it maps out the next 40 years of professional programming.

The syntax is the most obvious thing — functions, variables, nested braces, conditionals, loops, indexed lists (i.e. arrays), indexed dictionaries (structs), variable references (pointers), functions-as-variables (function pointers). These are things we all take for granted now, but in 1978 there was no clear consensus on what sort of base constructs a programming language needed.

Even in things C hadn’t quite figured out yet, you can see a clear path mapped to the future. Take includes, for example. We may have moved away from header files, but we’re still importing or including code modules at the top of our files. We’ve just gotten a little better about naming collisions (unless we’re still writing a certain sort of PHP or javascript program. cough).

There’s also C’s boogey man — macros. A seemingly simple system for making changes to a source file before its compiled — macros are probably the most confusing thing for someone whose first programming language is C. On the flip side, macros have also allowed smart systems programmers to create things like Objective C, so they’re not all bad. While modern javascript developers like to compare their pre-processing work to compilers, its often closer in spirit and practice to C’s macro system.

More than any particular thing though — C’s biggest contribution to the next 40 years of programming is it showed the power of a practical, but not perfect, high level abstracted programming language. Depending on your age, it may surprise you to hear C called a high level language, but at the time of its invention that’s exactly what it was.

Prior to C, most programs were written in assembly language. When you’re writing a program in assembly language, you’re targeting a single computer chip’s architecture. That means if you want your program to run on a computer using Intel’s x86 architecture and a second computer running on Motorola’s Power PC architecture, you need to write two different programs. C’s killer feature was how it let a programmer write a single program, and then through a mysterious (to some) black box process called compilation, turn that program into something that could run on any chip architecture.

Regardless of how high level we get with our languages, this pattern of different systems needing slightly different code so lets manage that with a separate layer repeats itself over and over again. Javascript is one of the world’s most popular programming languages, but in 2017 most people writing javascript are writing code to someone else’s abstraction of javascript.

All of this is preamble to the idea that while C is going to be a foreign land to many programmers working today, at the end of the day its all just code. If you’re learning how to program then the skills you have now are sufficient to understand C programs.

You may not need to understand C in 2017, but doing so can give you super powers compared to other programmers. Many modern “high level” programming languages are implemented in C or C++. Understanding the source code for the language you’re working in means you’re not longer left collecting tidbits of magic knowledge like

always use strpos to compare strings. its faster just. trust us

Instead, you can jump directly to the implementation and understand for yourself why some seemingly innocuous bit of code is grinding your entire system to a halt.

In this new series, Just Enough C for PHP, we’ll take a quick tour of the C programming language, and then show you how you can use this knowledge to create and understand native C PHP extensions. While not recommended for folks new to programming, if you can assign a value to a variable or call a function/method in another language, you’ll be plenty prepared for what we have to show you.

Next Steps

In our next article, we’ll create the inevitable Hello World program in C. We’ll also teach you how to compile and run this program, and how running a program in C is a slightly different affair from other platforms you may be used to. If you’re the sort who likes to study ahead, consider this small hello world program written in C

#include<stdio.h>

int main()
{
    printf("Hello World\n");
    return 0;
}

Can you get this program, in C, to run and compile on a computer? If you can’t, don’t worry, that’s exactly what we’ll be covering next time.

Series NavigationJust Enough C for PHP: Running C Programs >>

Copyright © Alana Storm 1975 – 2023 All Rights Reserved

Originally Posted: 2nd September 2017

email hidden; JavaScript is required