I was just wondering if anyone can give me some info on converting C++ to C. I don't really care about finding a program to do it for me, I would just like to know what is different between the two. My program is 'fairly' simple. uses file IO, prints, reads and all the other usual stuff.
Does anyone know a decent website that looks at these differences and hows what the ifferences are?

What I have worked out.
No headers except stdio.h
cout = printf
fstream = FILE
new = malloc

Other changes?

Thanks

Well, since you want to do it yourself, the old cfront C++ precompilers would do that for you just fine (up to the point you need to convert template classes and functions), and I believe that the current GNU C++ compiler will do that with the -E option. However, the differences between C and C++ from both the syntactic as well as the semantic perspectives are VERY different languages. At the most simple, you can consider C++ to be C with structures that have embedded functions, and replace the functions with external functions that pass a pointer to the structure as the first argument (the "this" pointer). In fact, the cfront precompilers do just that. However, getting to modern C++ with templates and such, that is a VERY different kettle of fish!

So, I have to ask why you want to do this? I don't suppose it is an assignment for a computer languages class?

In any case, there is NOTHING simple about this. I am considered to be an expert in C and C++ - I've been programming in C since 1982 and C++ since 1991, and have used both languages to design and develop complex software that runs many major enterprise systems, and I would not bother to translate C++ to C these days. Maybe 20+ years ago before templates and such - and consider that I have also designed and implemented non-trivial computer languages (compilers and interpretors) for specialized purposes (including the US Navy).

Thanks for the advice.
I am currently going through changing what I can. I just have some questions about some errors I am getting.

Can C pass by reference?
Is there something about structs C doesn't like because everywhere my sruct is used it gives an error.
Does C have 'for' loops?

and lastly

I created a new node:
node = new animalTree(data);

with a constructor that initializes data and the left right nodes.

how do I do that in C

node = (animalTree(data)*) malloc(sizeof(animalTree)); ???'

THanks for the help

Can C pass by reference?

Yes, with a slightly different syntax.

Is there something about structs C doesn't like because everywhere my sruct is used it gives an error.

Generally, no. Since I can't see your errors from here, it's hard to tell what the problem is.

Does C have 'for' loops?

You're kidding, right?

I created a new node:
node = new animalTree(data);

with a constructor that initializes data and the left right nodes.

how do I do that in C

node = (animalTree(data)*) malloc(sizeof(animalTree)); ???'

For the most part. Since you did't post any real code, it's hard to tell. There are no 'constructors', you define structures and they are 'constructed' when you assign a variable name to them.

Ok thanks for the help. I didn't nother posting code or errors because it wouldn't really help much.

Hey Walt, by saying C can pass a reference using a different syntax ,do you mean Pointers?
just wanna make sure I didn't miss anything while studying ;)
func(&val)
and prototype : void func(T *val)

Edited 4 Years Ago by Despairy: more info

Can C pass by reference?

No, but you can fake it with pointers.

Is there something about structs C doesn't like because everywhere my sruct is used it gives an error.

C requires that structure names be qualified with the struct keyword. So while you can do this with C++:

struct foo { int x; };

foo my_var;

In C it would need to be this:

struct foo { int x; };

struct foo my_var;

C++ implicitly wraps the equivalent of a typedef around the structure so that you don't need the struct keyword. Conveniently enough, you can do the same thing explicitly in C to the same effect. This code would work the same way in both C and C++:

typedef struct foo { int x; } foo;

foo my_var;

Does C have 'for' loops?

Yes.

I created a new node:
node = new animalTree(data);

with a constructor that initializes data and the left right nodes.

how do I do that in C

As far as allocating memory, you'd use malloc:

node = malloc(sizeof *node);

C doesn't have constructors, so you're left with writing an initialization function, an allocation and intialization function, or ad hoc initialization after you allocate memory. I'd prefer the second, because it's a bit closer to how a constructor would work:

animalTree *make_node(void *data)
{
    animalTree *nn = malloc(sizeof *node);

    if (nn) {
        nn->data = data;
        nn->left = 0;
        nn->right = 0;
    }

    return nn;
}

...

node = make_node(some_data);

Hey Walt, by saying C can pass a reference using a different syntax ,do you mean Pointers?

That's correct. Pointers can be used to simulate pass-by-reference by passing the address of an object and accessing it through indirection.

This article has been dead for over six months. Start a new discussion instead.