Switching from C++ to C: any suggestions?

Started by
21 comments, last by ravyne2001 17 years, 6 months ago
The biggest bugaboos in C that I no longer encounter when using C++ include the following (and may not apply to your particular applications):

(1) strcmp() returns false for true (ie. 0, which is akin to false, means the strings are equal).

(2) [fs]scanf() takes a _pointer_ to the destination variables.

(3) forgetting to make strings big enough to hold the terminating null.

(4) sign extension on implicit char-to-int conversions (eg. char c=0xba; assert(c==0xba); will assert on some platforms).

(5) See 3. Repeated for emphasis.

Those above items continued to bite my behind years after I mastered the intricacies of the language and had decades of professsional C coding behind me.

Stephen M. Webb
Professional Free Software Developer

Advertisement
Quote:Original post by joanusdmentia
[...]* There's no reason you can't do object-oriented programming in plain old C. You can even do inheritence and polymorphism. [Source Removed][...]
Or you could use virtual tables:
typedef struct s_Object Object;typedef struct{   void (*Destroy)(Object*);   bool (*Update)(Object*, int);} Object_VTable;typedef struct s_Object{   Object_VTable *VTable;   double X, Y;} Object;/*...*/typedef struct{   Object SuperClass;   /*...*/} Model;Object_VTable Model_Object_VTable = {Model_Destroy, Model_Update};/*...*/Model M;M.SuperClass.VTable = &Model_Object_VTable;

With VTables, you don't need for each object to store a function pointer for every function, only to store a pointer to the table of functions for that type. The cost is that it takes an extra indirection, which might or might not be significant on the embedded platform (depending on the timing requirements and the complexity of the system)

You might want to look at COM headers to see how MS handles that kind of thing (porting polymorphic C++ interfaces to C)
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
There exists C++ compilers that compile to ANSI C. So if you really want to write in C++, you can.

You may want to examine the specs of the hardware, however.
Some of the biggest announces are going to be string related. Stream operators (<< and >> ) dont exist, and obviously streams themselves no longer exist, so get to know printf/scanf and fopen well.

bool isnt a keyword. This one can lead to some nastiness back porting code.

Obviously new/delete dont exist, so your back to malloc() and free().

Casting is less safe... or more flexable... depending if you are a glass half empty/full kinda guy :) But no dynamic or static_cast in C land.

A really minor one, that annoys the hell out of me... no single line comments

// So this comment
needs to be
/*
This comment
*/

On the topic of comments, nesting of comments is a no no, which is also annoying.

/* So this /* is illegal */ in C */

I *believe* there is no such concept of pubilc or private in C. I dont remember 100%, but I think all structs are basically public. Don't quote me on this one.


And finally, and this one burns me the most if I ever do C code. As you know, all variables need to be pre-declared. That includes in loops, so no more:

for(int i =0; i < 42; i++)

That one I seem to always screw up.
Considering all the other problems you're going to be facing, I recommend you ignore all the people suggesting you program using objects and inheritance and stuff. Unless you've got experience in robotics and embedded development, that's just going to be one more headache you don't need. KISS is your friend.

Really, it isn't so much "how do I switch from C++ to C", but "How doI switch from computer programming to microcontroller programming?" You're going to make extensive use of global variables, for instance, its just unavoidable. You have to keep extremely close watch on how much memory you use...assuming malloc is available, it shouldn't be used willy nilly. Things like interupts are going to force you to watch for concurrency. Macros should be prefered to functions for simple operations. And so forth.

But when it comes right down to it, I suggest you just get to coding. Get your board setup sufficiently that you can figure out how to interact with everything, and just have fun. You'll figure out the important bits as you go.

*edit: BTW, /* this /* is also illegal */ in C++ */

CM
Not having references can be really annoying!
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:(1) strcmp() returns false for true (ie. 0, which is akin to false, means the strings are equal).

im pretty sure stl::string compare does as well
Quote:Original post by Conner McCloud

*edit: BTW, /* this /* is also illegal */ in C++ */

CM


Oops.

Well... I was still right... it is illegal in C! [smile]

I havent used a block comment in years, outside of C. Got foggy on that one.
I wrote some stuff in C recently.

I wrote it in C++ using just C features. Then I switched my compiler to C mode and fixed the syntax errors. It was easy.
Thanks all! Sounds like lots of the common mistakes are string-based, and I doubt I'll be doing much of that.

This topic is closed to new replies.

Advertisement