What's the best way to migrate to C from higher level languages?
I am a web coder: I am currently enjoying AS3 and doing PHP. I own a Nintendo DS and want to give C a go.
From a higher level, what basic things / creature comforts will disappear? I cannot find the [for ... in] loops, so I assume they are not there. It looks like I'll have to declare things religiously, and I assume I don't have objects (which I ran into in PHP a while ago).
Hash tables? Funny data types?
a source to share
To summarize, you will basically get:
- Typed variables
- Functions
- Pointers
- Standard Libraries
Then you do the rest - it might be overly simplistic, but this is a rough idea of โโwhat to do with it.
It can be a tricky one to start with, and there can be a learning curve to overcome. Here are a few speed bumps you may encounter:
Line? Which line?
One big thing to get used to is strings. There is no such thing as string
in C. A string is a " null
-termized character array" (sometimes called C strings ), which basically means an array of type char
, with the final element being \0
( char
value 0
).
In memory, an array of char
length 4 containing Hi!
would look like:
char[0] == 'H'
char[1] == 'i'
char[2] == '!'
char[3] == '\0'
Also, strings do not know their own length (there are no objects like "objects" in C), so you would need to use a standard library call strlen
, which more or less is for
, that goes through the string until it reaches a character \0
. (This means that this is an O (N) operation - the length of the string is longer than it takes to find the length, as opposed to the O (1) operation of the larger implementation string
in modern languages.)
Garbage collection
There is no such thing as a garbage collector in C. Actually, you need to allocate and free 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);
Not being able to clean up after allocating memory can lead to things called memory leaks , which I'm sure you've heard before.
Pointers
And as always, when we talk about C, we cannot forget about pointers . The whole concept of variable references and dereferencing pointers can be a serious concept headache, but once you get the hang of it, it really isn't that bad.
Except when you expect it to work one way, but you find that you don't understand pointers well enough, and in fact it does something else, as they say, there did it.
Oh, and pointers are likely to be one of the first times you actually see a program crash so badly that the operating system screams at you. A segmentation fault is not something that the computer likes a lot .
Types
All variables in C will have types. C is a statically typed language , which means that the types of variables will be checked at compile time. This may take a while in the beginning, but can also be seen as a good thing as it can reduce runtime errors like type errors when you try to assign a line number.
However, it is possible to execute typecasts , so it is possible to use a type int
(which is an integer) for a double
(a float value). However, it is not possible to try to pass int
directly into a type string 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 call a function itoa
to get the char*
view int
, then use strcat
to concatenate the two strings.
Conclusion
These things say that learning C coming from a higher language can be very eye-opening and probably tricky to start with, but once you get the hang of it, it can be very enjoyable to work with.
I would recommend starting experimenting with the C compiler and having a good book or link.
I think a lot of people will recommend the K&R book , which is a really great book.
At the beginning, I didn't think to recommend K&R as the first C book would be a good idea because it might be a little tricky side, but on the other hand I think this is a very comprehensive and well-written book that might be good for switch to C if you already have programming experience.
Good luck!
a source to share
Well ... Maybe you might have some kind of culture shock. These are 32 standard C keywords and include the major types.
The C standard library is pretty functional (more than what people might expect), but very thin compared to what higher-level languages โโgive you. There is no hash table in the table, and you're right to assume that C has no syntactic or semantic object support.
You can write pretty object-oriented code anyway, but you'll have to jump through a few hoops and do a lot more by hand as the language won't help you. See, for example, the GTK + toolkit for an example of a well thought out object oriented C / API library.
a source to share
I am a web coder: I am currently enjoying AS3 and doing 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 for what you hope to achieve? Is it to write software for the Nintendo DS?
From a higher level, what basic things / creature comforts will disappear?
Given your background, I think you personally miss the lack of support for dynamic text input, in other words, you will need to be very explicit in your C programs, your data must be specified with the appropriate types so that the compiler knows what data you are working with. This also applies to any kind of memory management, i.e. basically anything when you start working with data structures that are not PODs.
For example, where do you do something like this in php:
function multiply(x) {
return (x*x);
}
You would need to do something like this in C:
int multiply(int x) {
return (x*x);
}
While they may seem quite similar, there are big differences, namely the restrictions on the input: the php version will also work with floating point values, and in C you will have to explicitly provide versions for different types and ranges of values โโ(C types are limited to certain ranges).
I can't find the [for ... in] loops, so let's assume they are not there
in C, it looks more like this:
int c; for (c = 0; c <= 10; C ++) {// loop body}
looks like i have to declare things religiously
Yes, very much so - the more that you will appreciate
and I am assuming I have no objects (which I had in PHP before).
correct, no objects - but OOP can still be emulated with other ways like function (struct obj)
Depending on your goals and motivation, I think you might find C a rather frustrating language to get started with serious programming, you might want to look into some of the related alternatives, like Java.
a source to share
Dynamic arrays and garbage collection. It is not built into C, so you will need to collapse or use an existing solution.
The standard procedure is that you manage the memory yourself, which may sound like a terrible thing, 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 that it frees it up on its own, or a memory leak occurs and something bad happens.
a source to share
You will especially miss out on automatic memory management and semantically significant data types like strings, tables, and c. That said, learning C is well instructive, although you probably don't want to use it for application-level programming, so I suggest you take "K&R" (Kernighan and Ritchie's original book) and make it clear - you will find many free libraries on the web to be used and explored as you go beyond this, although you will have to discipline yourself to use the correct memory management heuristic ... happy learning!
a source to share
I was just doing some research on the internet and it seems like there is a viable opportunity to use lua for development on the "nintendo DS", this might be the easiest way for those familiar with high level languages โโto start making embedded without sacrificing too much power HLL and without experiencing the inevitable culture shock of moving from HLL to C: microlua , here are the API docs .
As such, you might want to give it away, perhaps using a beginner emulator.
Keep us updated!
a source to share
What you'll REALLY be is the ability to quickly prototype and test changes. You can't just change the line of code and run. Even using build tools such as "make" recompilation can take several minutes. It's even worse when you consider that it is very easy to make mistakes in C / C ++. On large projects, I find myself spending more compilation time than I actually do. As a long-term user of script languages โโthis is my biggest problem using C.
a source to share
Moving directly from a higher level language running on a machine with efficient infinite resources for DS will be a challenge, not just language.
The Nintendo DS only has 4MB of RAM, 66MHz ARM-7, no operating system, and the development libraries available (like libnds) only provide a subtle abstraction over the hardware itself.
So, apart from having to deal with manual memory management, a simpler language with fewer creature comforts, static typing, no objects, and having to do a compile step before you can see any changes, you also have to deal with memory fragmentation. is a very slow processor by modern standards and needs to interface directly with the hardware to do anything useful.
Writing code for DS, the only other option is C ++. You can't use the many extra features that make C ++ stand out on such a limited system. You write C code using a C ++ compiler.
However, it is a lot of fun. You can screw whatever you like and there is no need to interact with the operating system because there are none.
a source to share
C is the next level above direct assembler and allows working close to metal. This makes it possible to do amazing things, but it's also easy to shoot in the foot!
One such example is direct memory access, as well as the concerns of pointer arithmetic. Pointers are powerful, fast and easy to use, but they require careful management. See this SO question for an example.
Also, as mentioned by other autoresponders, you will need to manage memory yourself. Powerful and painful again.
I would recommend looking into a good tutorial and looking for quality example code. The key is to learn the patterns that make it all depend correctly and elegantly (well, as much as possible). A good debugger will also really help and get familiar with the C standard libraries.
You may notice that your applications crashed when the hat was dropped initially, but perservere as C is definitely worth at least tackling. You will understand some of the amazing abstractions that higher-level languages โโprovide and what really happens under the hood.
a source to share
We need more novice developers. I am GBA / NDS and many other embedded platform developers and I hope you continue with that. I would say skip the assembler and then go back to C or whatever language you like, once you know how the processor works, languages โโare just syntax.
My guess is that your previous experience covers the thinking of programming, breaking things down into bite-sized chunks and then writing code to execute those chunks. Then another module that ties them together and so on. Then C is another language, a very simple language, no need to dive into its corners, go down the middle. It's a good habit to declare variables, etc., and this is where you have to. Compilers will tell you when you forgot something. You don't need big concepts, big structures, language magic, it's built in, you are limited by resources, write a few bytes here, read a register there, extract a bit from the data to see if a button was pressed, write a register in response to a sprite move, etc. .d.
I think NDS is much more complex than C, first there are two processors and some kind of infrastructure to get the simplest of executable binaries. Of course, there are many examples. I generally (and still) recommend starting with GBA and then ending with NDS. bite sized pieces.
a source to share
Many things from OOP are the same or nearly the same in PHP and C #.
You are not playing with pointers in C # (versus C ++), so I would definitely recommend switching from C # if you want to play with C.
What are you talking about?
FROM#
foreach(string item in itemsCollection)
{
...
}
PHP
foreach($itemsCollection as $key=>$value)
{
...
}
and etc.
I like C # because it is strongly typed and your types are automatically checked when you write your code ... The ability to try to store an integer to a string or vice versa is zero compared to PHP, where you can store something to whatever .. ...
a source to share