Search code examples
clanguage-features

What is the best way to transition to C from higher level languages?


I'm a web coder: I currently enjoy AS3 and deal with PHP. I own a Nintendo DS and want to give C a go.

From a higher level, what basic things/creature comforts are going to go missing? I can't find [for... in] loops, so I assume they aren't there. It looks like I'm going to have to declare things religiously, and I assume I have no objects (which I dealt with in PHP a while ago).

Hash tables? Funny data types?


Solution

  • To sum it up, you'll basically get:

    • Typed variables
    • Functions
    • Pointers
    • Standard libraries

    Then, you make the rest -- that may be a little too simplified, but that's a rough idea of what to face.

    It can be daunting to begin with and there may be a learning curve to overcome. Here's a few speed bumps you may encounter:

    String? What string?

    One big thing to get used to would be strings. There is no such thing as a string in C. A string is a "null-terminated character array" (sometimes called C strings), which basically means an array of type char with the final element being a \0 (char value 0).

    In memory, a char array of length 4 containing Hi! would appear as:

    char[0] == 'H'
    char[1] == 'i'
    char[2] == '!'
    char[3] == '\0'
    

    Also, strings don't know their own length (no such things as "objects" that come for free in C), so the use of standard library call strlen would be required, which more or less is a for loop that goes through the string until it hits a \0 character. (This means it's an O(N) operation -- longer the string, longer it takes to find the length, unlike O(1) operation of most string implementation in modern languages.)

    Garbage collection?

    No such thing is as a garbage collector in C. In fact, you need to allocate and deallocate memory yourself:

    /* Allocate enough memory for array of 10 int values. */
    int* array_of_ints = malloc(sizeof(int) * 10);
    
    /* Done with the array? Don't forget to free the memory! */
    free(array_of_ints);
    

    Failing to clean up after allocation of memory can lead to things called memory leaks which I'm sure you've heard of before.

    Pointers!

    And as always, when we talk about C, we can't forget about pointers. The whole concept of references to variables and dereferencing pointers can be a serious headache-inducing concept, but once you get a hang of it, it's actually not too bad.

    Except for the times when you expect it to work one way, but you find out that you didn't quite understand pointers well enough and it actually does something else -- as they say, been there, done that.

    Oh, and pointers are probably going to be one of the first times you'll actually see a program crash bad enough that the operating system will yell at you. A segmentation fault is not something the computer likes a lot.

    Types

    All variables in C will have types. C is a statically-typed language, meaning that variable types will be checked at compile time. This might take some getting used to at the beginning, but can also be seen as a good thing, as it can reduce runtime errors such as type errors where you try to assign a number to a string.

    However, it is possible to perform typecasts, so it is possible to cast a int type (which are integer values) to a double type (a floating type value). However, it is not possible to try to cast an int directly to a string like char*.

    So, for example, in some languages the following is allowed:

    // Example of a very weakly-typed pseudolanguage with implicit typecasts:
    number n = 42
    string s = "answer: "
    string result = s + n  // Result: "answer: 42"
    

    In C, one would have to call an itoa function to get a char* representation of an int, then use strcat to concatenate two strings.

    Conclusion

    Those things said, learning C coming from a higher language can be very eye-opening and probably challenging to begin with, but once you get a hang of it, it can be pretty fun to work with.

    I'd recommend starting to experiment with a C compiler, and have a good book or reference.

    I think many people will recommend the K&R book, which is indeed an excellent book.

    At first, I didn't think recommending K&R as the first C book would be a good idea because it may be a little bit on the difficult side, but on second thought, I think it is a very comprehensive and well-written book that can be good for getting into C if you already have some programming experience.

    Good luck!