Sign in to follow this  
AsOne

C++ to C

Recommended Posts

Hello everyone, I made a lounge post a day or two ago about programming in C when you started with C++. Although some told me to stay away, while others told me to go for it. I decided I would learn plain C, for anything, just to say I can program in C/C++ not just C++. I should also state that I know using const, classes, templates, etc; Are more 'correct', and using C for a project that could be accomplished just the same in C++ would be a waste really. I merely want to take a break from C++ and learn C to broaden my knowledge. I've been looking though a bunch of tutorials on C, and trying to lay-out in my brain the things I can do in C++ but can't do in C, so I thought I'd start a little list and everyone maybe could add to it to help me out, this may also be pretty helpful to others in a similar situation, maybe coming from C to C++ as well. I'll start this off ... - When declaring a variable of struct and enum you must use the keyword struct and enum to declare the variables (unless a typedef is used). - #define or enum must be used for constants. - No classes of course (but I know OO design can still be implemented). - Cannot 'pass by reference' as in C++ (must use pointers and & operator when passing). - Cannot use new and delete must use malloc and free, you must manually cast void* to correct type when allocating. (Hope the statements above are correct :S) EDIT: Of course the differences in I/O libraries as well. I've been reading up on this also clicky! (Thanks dcosborn for the link) Since I have this list above, I want to use this post to compile a list of syntax differences that are directly visible when writing the code. Anything that the compiler does ... I'm less concerned with, and can find that information in the link above. Thanks for any help / insight.

Share this post


Link to post
Share on other sites
No templates, no namespaces, no overloading, no references, no inheritence, no member functions, no exceptions.
Note that C does have const (and volatile).

I guess it depends on the area, but here in Detroit much of the work is embedded for automotive applications and there's embedded medical applications close by in Ann Arbor as well. Almost all use C exclusively (typically with MISRA C requirements, which stipulate no dynamic memory allocation is allowed among other requirements).

Share this post


Link to post
Share on other sites
Another one is that you have to declare your variables at the top of the function.

Also, not to de-rail your thread, but have you thought about learning another modern language instead of getting into C? If you already know C++, I think that adding C# skills to your toolbox would be more useful in than straight C skills. Just something to think about...

-John

Share this post


Link to post
Share on other sites
You should have a look at the 'C++ tutorial for C users' (http://www.4p8.com/eric.brasseur/cppcen.html), it's a list of (I think) all the differences between the two languages and they are explained very clearly there.. but to be honest, I don't understand why you want to learn C when you already know C++. C++ has many advantages and is definatly not slower when you don't use it's 'extra' features like classes.

Bas

Share this post


Link to post
Share on other sites
C++ is a super-set of C (it meens that C++ has everything C does). Don't waste your time. There are tons of things you must master to become a pro. If you finished your first C++ steps, proceed to advance C++ topics. This reminds me my C# book. Half of the book has been wasted for comparison between C#, C++, Java.... I DONT CARE, I JUST WANT TO LEARN C# !!!

Share this post


Link to post
Share on other sites
Quote:
Original post by basananas
I don't understand why you want to learn C when you already know C++.


Well, there's a C compiler for every platform in Christendom. The same cannot be said for C++.

Share this post


Link to post
Share on other sites
Quote:
Original post by cpp_boy
C++ is a super-set of C (it meens that C++ has everything C does).


This is not entirely true. From "The C++ Programming Language, Third Edition and Special Edition":
"With minor exceptions, C++ is a superset of C (meaning C89, defined by ISO/IEC 9899:1990). Most differences stem from C++’s greater emphasis on type checking. Well-written C programs tend to be C++ programs as well."

Two examples:
- "C allows transfer of control to a labeled-statement to bypass an initialization; C++ does not."
- "In C, an array can be initialized by an initializer that has more elements than the array requires."

Share this post


Link to post
Share on other sites
there's also a lot of c99-stuff that hasn't been taken into c++ (yet?).

learning c isn't just about learning what c++ is and what c is not, but it's about learning a different, less object oriented code style. i did the c++ -> c -> c++ path myself, and i learned a lot from it. it's a good experience.

edit: another difference (iirc) is that in c, int is implicit. this means that you can just write something like "i;" if you want an integer called "i". this isn't allowed in c++.

Share this post


Link to post
Share on other sites
Quote:
Original post by crocomire
- "C allows transfer of control to a labeled-statement to bypass an initialization; C++ does not."
- "In C, an array can be initialized by an initializer that has more elements than the array requires."


WHO CARES...

Share this post


Link to post
Share on other sites
Quote:
Original post by furby100
Quote:
Original post by basananas
I don't understand why you want to learn C when you already know C++.


Well, there's a C compiler for every platform in Christendom. The same cannot be said for C++.


What do you mean by 'christendom'?

Share this post


Link to post
Share on other sites
Correct me if I'm wrong, but one difference is that you have to declare all variables at the beginning of a function.


void DoSomething ( unsigned int numberOfTimesToBreakDance )
{

int funk = 0;

DoSomethingElse();

//This is valid C++, but in C this line would have to come before
//DoSomethingElse
char randomVarNameThatIsntFooOrBarBecauseIReallyHateThat = 30;

}



Also, in C if your function takes no arguments, you have to declare it like this, otherwise it will be a function that takes an arbitrary number of arguments


//note the void parameter
void IHaveAnIrrationalHatredTowardsCallingThingsFooOrBar ( void );



I was forced to use C when programming for the Net Yaroze, and I absolutely hate it. The first programming language I learned was BASIC, but I'm one of those people that finds object oriented programming to be a lot more intuitive than procedural. Once I learned OO, there was no going back. Aside from that, I like all the nice features of C++, such as templates.

Share this post


Link to post
Share on other sites
Quote:
Original post by AsOne
- Cannot use new and delete must use malloc and free, you must manually cast void* to correct type when allocating.


No, you don't need to cast the void*, C will cast void* to any pointer type automatically (at least, any type with the same number of *s IIRC, so char* is okay but char** needs a cast). I'm sure somebody will say that its better code to always cast but I don't think it makes the code more readable and I've never created a bug that way.

On the subject of local variables they can only be declared at the beginning of a block, not just at the top of a function, e.g. (I may be getting my terminology mixed up):

void func (int param)
{
if (param)
{
int x = 1;
printf("%i\n", x);
}
}

I do a fair bit of C programming and the only thing I'd really like in the language is simple single inheritance with automatic type casting to get round the annoying macro hacks I always have to put in my linked list code.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
actually..there is little difference in c and c++

I program in strait c but all my tutorials i could find were in c++
most of the time i could cut n paste with no problems.

as far as "classes"
you can always go

typedef struct{
int a;
float b;
char *c} CLASS;

after all..the c++ spec was built on the c spec.

c can do anything imaginable..there is no limitations, and you can use the code on more than 1 platform, depending on the libraries you used.
Most libraries are written for c and also work with c++
going from c++ to c is not even complicated

i like being original and thats what c lets me do.

Share this post


Link to post
Share on other sites
Quote:
Original post by cpp_boy
Quote:
Original post by crocomire
- "C allows transfer of control to a labeled-statement to bypass an initialization; C++ does not."
- "In C, an array can be initialized by an initializer that has more elements than the array requires."


WHO CARES...


Perhaps people who think your statement ("it meens that C++ has everything C does") is correct.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
i like being original and thats what c lets me do.


You arn't implying C++ won't let you be original too, are you?

(For what it's worth, I've had to work with platforms with no working C++ compiler. Of course they're so far fetched and out of date that half the time it'd be advisable to upgrade to a platform which HAS a C++ compiler...)

Knowing C is mainly useful for mantaining legacy programs. I wouldn't recommend learning how to use it unless you know (or highly suspect that) you soon will be doing this - similarly for SPARC assembly. It's a niche language, not generalized. No point in learning it instead of a more general language unless you're planning on working with that niche anytime soon.

Quote:
Original post by cpp_boy
Quote:
Original post by crocomire
- "C allows transfer of control to a labeled-statement to bypass an initialization; C++ does not."
- "In C, an array can be initialized by an initializer that has more elements than the array requires."


WHO CARES...


WHO CARES ABOUT WHO CARES...

Whiner.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
http://www.q-software-solutions.de/products/lcc-win32/index.shtml

thats the link of where i got my c compiler
there is a pdf tutorial and a chm that covers most if not all of the c language

the compiler is free too btw

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
this is kinda off topic..but im stumpling in the dark with wavein

any help please?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
im stuck with this..whats broken..this is my first attempt with wave api

if(!waveInOpen(&inHandle,WAVE_MAPPER,&waveFormat,0,0,CALLBACK_NULL)){
memset(&waveinhdr,0,sizeof(waveinhdr));
waveinhdr.lpData = c;
waveinhdr.dwBufferLength = 4;
waveinhdr.dwBytesRecorded = 1;
waveinhdr.dwFlags = WHDR_BEGINLOOP | WHDR_ENDLOOP;
waveinhdr.dwLoops = 1;
waveInPrepareHeader(inHandle,&waveinhdr,sizeof(waveinhdr));
waveInAddBuffer(inHandle,&waveinhdr,sizeof(waveinhdr));

PlayTimer=0;
while(MainData.Mode){
Sleep(1);
PlayTimer++;
if((waveinhdr.dwFlags & WHDR_DONE)==WHDR_DONE){
waveinhdr.dwFlags &= ~WHDR_DONE;
PlayTimer--;
TextOutA(GetDC(MainData.Hwnd),0,0,d,4);
waveInReset(inHandle);
waveInAddBuffer(inHandle,&waveinhdr,sizeof(waveinhdr));

if(PlayTimer>30){
PlayTimer=1;}
Sleep(PlayTimer);}}
waveInStop(inHandle);
waveInReset(inHandle);
waveInClose(inHandle);
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i feel so stupid..notice how the buffer is the char * c (supposed to be char *d)
my bad

Share this post


Link to post
Share on other sites
Every language out there can talk to C, and the operating system is written in C. This makes C fundamental.

C++ on the other hand is just some language.

Share this post


Link to post
Share on other sites
Quote:
Perhaps people who think your statement ("it meens that C++ has everything C does") is correct.


Most of the C++ books starts with the statement: "C++ is superSet of C". Its not my imagination.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this