views:

281

answers:

12

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 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?

+6  A: 

Well ... You might be in for something of a culture shock. These are the 32 standard keywords in C, and that includes the basic types.

C's standard library is pretty functional (more so than people perhaps expect), but very very thin when compared to what higher-level languages give you. There is no hash table in sight, and you are correct to assume that C does not have syntactic or semantic support for objects.

It is possible to write pretty object-oriented code anyway, but you will have to jump through a few hoops, and do much more manually since the language won't help you. See for instance the GTK+ UI toolkit for an example of a well-designed object-oriented C library/API.

unwind
I know how Dorothy felt so far away from Kansas... Thanks for the keyword ref... it's... sparse :)
Assembler
Hash tables?! Try looking for a mere string type to start with! :)
Daniel Earwicker
+1  A: 

Dynamic arrays and garbage collection. It's not built in to C so you'll need to roll your own or use a pre-existing solution.

The standard procedure is that you manage the memory yourself which might sound like something horrible but it really isn't. For example in AS3 and PHP you can create an array and forget it when you're done with it. In C you'll have to make sure to deallocate it yourself or memory will leak and bad stuff can/will happen.

Skurmedel
A: 

I'm pretty sure you want to be looking at C++, not C. C++ is basically object oriented C.

Brent Baisley
Bad C++ is 'basically object oriented C'. Good C++ is nothing like C.
Nick Presta
When I get to c++ I'll be starting with Bad C++ and working up.
Assembler
+2  A: 

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.

Why do you want to do C programming? What are your reasons, what do you hope to achieve? Is it in order to write software for the Nintendo DS?

From a higher level, what basic things/creature comforts are going to go missing?

Given your background, I think you'll personally miss the lack of dynamic typing support, in other words you will have to be very explicit in your C programs, your data must be specified with proper types, so that the compiler knows what type of data you are working with. This also applies to any sort of memory management, i.e. basically anything once you start working with data structures that are non PODs.

For example, where you would do something like this in php:

function multiply(x) {
 return (x*x);
}

You would have to do something like this in C:

int multiply(int x) {
 return (x*x);
}

While these may seem fairly similar, there are big differences, namely typing restrictions: the php version will also work with floating point values, while in C you would have to explicitly provide versions for different types and ranges of values (C types are constrained to certain ranges).

I can't find [for... in] loops, so assume they aren't there

in C, it looks more like the following:

int c; for (c=0;c<=10;c++) { // loop body }

it looks like I'm going to have to declare things religiously

Yes, very much so - much more so, than you'll appreciate

and I assume I have no objects (which I dealt with in PHP a while ago).

correct, no objects - but OOP can still be emulated using other ways, such as function(struct obj)

Depending on your goals and motivation, I think you may find C a pretty frustrating language to start serious programming with, you may want to look into some of the related alternatives like for example Java instead.

none
He specifically stated that he wanted to program for the Nintendo DS. Thus Java and other like languages are completely inappropriate.
Kibbee
No he didn't state anything like that explicitly, you are making an assumption...an safe one, though. I was assuming the same, but still it wasn't clear, that's why I was asking for clarification.
none
Either way, recommending Java as an alternative to learning C, when he specifically stated was wanted to learn C, isn't really all that helpful.
Kibbee
I am not going to continue arguing any further about reading comprehension related issues, still it's a widely known fact that Java is a language that borrows many elements and concepts from C, which is in fact one of the reasons why Java is frequently being used by universities in order to introduce students to programming, preparing the eventual transition to C. In this sense, Java might actually be an excellent pathway towards becoming more familiar with programming in a C-like language.
none
kids! chill out! A good answer, and Java may completely r0x0r5, but yes, for the DS specific libraries I am looking at C seems to be the way to go. Learning Java beforehand may be good, but I am impatient.
Assembler
+1  A: 

You'll particularly miss automatic memory management, and semantically meaningful datatypes such as strings, tables &c. However, learning C well is quite instructive, even though you probably don't want to use it for application-level programming, so I suggest you grab a "K&R" (Kernighan and Ritchie's seminal book) and give it a go -- you'll find plenty of free libraries on the web to use and study as you proceed beyond that, though you'll have to discipline yourself to use proper memory management heuristics... happy learning!

Alex Martelli
A: 

What you'll REALLY miss is the ability to rapidly prototype and test changes. You can't just change a line of code and run. Even using build tools like "make" a recompile can often take several minutes. This is even worse when you consider that it's really easy to make mistakes in C/C++. On large projects I reckon I spend more time compiling than actually coding. As a long-term user of script languages this is my biggest issue with using C.

SpliFF
A: 

A lot of things from OOP is the same or almost the same in PHP and C#.

You don't play with pointers in C# (compared to C++) so I would definitely recommend going with C# if you want to play with C.

What C are you talking about?

C# foreach(string item in itemsCollection) { ... }

PHP foreach($itemsCollection as $key=>$value) { ... }

etc.

I like C# because it is strongly typed and your types are automatically checked while you write a code... The possibility of trying to save integer into string or vice versa is zero compared to php where you can save anything into anything...

RomanT
C and C# are about as similar as Car and Carpet.
Kibbee
Didn't say it is similar ;-)
RomanT
A: 

Moving directly from a higher-level language running on a machine with effectively infinite resources to a DS is going to be a challenge, and not just because of the language.

The Nintendo DS has only 4MB of RAM, a 66MHz ARM-7, no operating system, and the development libraries available (such as libnds) provide only a thin abstraction over the hardware itself.

So, in addition to having to deal with manual memory management, a simpler language with fewer creature comforts, static typing, lack of objects, and the need to run a compile step before you can see any changes, you also have to deal with memory fragmentation, a very slow CPU by modern standards, and needing to interact with the hardware directly in order to do anything useful.

Writing code for the DS, the only other option is C++. You can't use a lot of the advanced features that make C++ worthwhile on such a limited system. You'd be writing C code using a C++ compiler.

That said, it's a lot of fun. You can screw around with the hardware all you like, and there's no need to interface with the operating system, because there isn't one.

BlackAura
A: 

C is the next level above straight assembler and allows you to operate close to the metal. This gives power to do amazing stuff but also to easily shoot yourself in the foot!

One such example is direct memory access and the perils and wonder of pointer arithmetic. Pointers are very powerful, fast, and handy however require careful management. See this SO question for an example.

Also as mentioned by the other answerers you will have to do your own memory management. Again powerful and painful.

I would recommend studying up a good textbook and find some quality example code. The key thing is to learn the patterns that make all this stuff hang together correctly and elegantly (well, as much as possible). A good debugger will also really help and get familiar with the standard C libraries too.

You may notice your applications crashing at the drop of a hat initially but perservere as C is definitely worth at least dabbling in. You will understand some of the amazing abstractions higher level languages provide and what is really going on under the hood.

Alex Angas
+8  A: 

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!

coobird
+1. Very comprehensive.I didn't start with C but with other "friendlier" languages. However when I delved into C I started to understand why alot of stuff were like they were in other languages, like Java. So I think it will be a very educative experience.
Skurmedel
+1  A: 

I was just doing some research online, and it seems there's a viable possibility to use lua for developing on the "nintendo DS", this may in fact be the easiest way for someone familiar with high level languages to get started doing embedded development, without sacrificing too much HLL power and without experiencing the inevitable culture shock when migrating from a HLL to C: microlua, here are the API docs.

So you might want to give it a go, possibly using an emulator for starters.

Keep us posted!

none
woah! lua? I didn't think I had any alternatives. I might have a look at that too! thanks
Assembler
A: 

We need more homebrew developers. I am a GBA/NDS and many other embedded platform developer and hope to see that you continue with this. I would say skip to arm assembler and then back up to C or any other language you like, once you know how the processor works, languages are just syntax.

I assume your prior experience covers the programming mindset, breaking things down into bite sized chunks and then writing code to perform those chunks. Then another module that links those together and so on. Then C is just another language, a very very simple language, no need to dive into the corners of it, drive down the middle. It is a good habit to declare variables, etc, and here you will have to. The compilers will tell you when you have forgotten something. You are not going to need big concepts, big structures, language magic, this is embedded, you are resource limited, write some bytes here, read a register there, extract a bit from the data to see if a button has been pressed, write a register in response to move a sprite, etc.

I think you will find the NDS much harder than C at first, there are two processors and some infrastructure to get the simplest of working binaries. Granted there are many many examples out there as well. I generally (and still do) recommend starting with the GBA then graduate to the NDS. bite size chunks.

dwelch
for real? you think I should go down to assembler? woah. I knew this was going to be challenging...
Assembler
Not even BASIC is as easy as assembler. Just think you dont have to declare variables, you just use registers at will.
dwelch