learning C with a C++ background

J

jonathanmcdougall

I just got a new job yesterday where most of the programming is done in
C. Although I have several years of experience in C++ and other
object-oriented languages, I have next to none in C.

What I am looking for is a book (or some other resource) on "how to
learn C if you already know C++" or, more exactly, "how to program in a
procedural language when your only experience is with object-oriented
languages".

I find myself wanting to emulate many OO features and I have the
impression this is wrong. I need to know how source files are
organized, I'm lost without namespaces, classes and the standard
library (containers, someone?), how do you cope with all these variable
definitions at the top of a function, do you really create
as-big-as-you-can arrays of chars for strings, etc. etc.

Pretty much everything I was told and teached to avoid in C++ seem to
be needed in C. That's counter-intuitive for me and hard to get over.
Is there a book of wisdom somewhere that could help me understand
idioms, design patterns or oft-used construction?

Thank you,


Jonathan
 
I

Ian Collins

I just got a new job yesterday where most of the programming is done in
C. Although I have several years of experience in C++ and other
object-oriented languages, I have next to none in C.
You poor bugger!
What I am looking for is a book (or some other resource) on "how to
learn C if you already know C++" or, more exactly, "how to program in a
procedural language when your only experience is with object-oriented
languages".
Just read K&R, with an open mind.
I find myself wanting to emulate many OO features and I have the
impression this is wrong.

It's not wrong, it might not be idiomatic C, but you can write good,
clean OO code in C.
I need to know how source files are
organized, I'm lost without namespaces, classes and the standard
library (containers, someone?), how do you cope with all these variable
definitions at the top of a function, do you really create
as-big-as-you-can arrays of chars for strings, etc. etc.
File organisation isn't much different form C++, except your headers
define a functional interface rather than an OO one.

If you can use C99, variable declaration rules are the same as C++. If
you follow good programming practice and use short methods, the
traditional C way isn't a big deal.
Pretty much everything I was told and teached to avoid in C++ seem to
be needed in C. That's counter-intuitive for me and hard to get over.
Is there a book of wisdom somewhere that could help me understand
idioms, design patterns or oft-used construction?
Study a good C code base.
 
J

Jonathan Mcdougall

Ian said:
You poor bugger!

:) Thanks
Just read K&R, with an open mind.

Got that.
It's not wrong, it might not be idiomatic C, but you can write good,
clean OO code in C.

I find the Gtk library to be a bit cumbersome, for example, as if it
wasn't C nor C++. I might be wrong here, but that's the impression I
have.
If you can use C99, variable declaration rules are the same as C++.

I can't. Funny, I spent twenty minutes today trying to understand a
"invalid identifier before 'variable_name'" before remembering that
variables must be declared on top of a function.
Study a good C code base.

I will, but I am not in a good position of knowing whether some C code
is "good" or not.


Jonathan
 
I

Ian Collins

Jonathan said:
I find the Gtk library to be a bit cumbersome, for example, as if it
wasn't C nor C++. I might be wrong here, but that's the impression I
have.
Have a look at XView, it may be rather old, but it's fairly clean OO C
code. I first learned about OO by using it many years ago.
 
A

Andrew Poelstra

I just got a new job yesterday where most of the programming is done in
C. Although I have several years of experience in C++ and other
object-oriented languages, I have next to none in C.
Ooh. That's one of my main reasons for avoiding C++ (other than to make
sure I can still write it if I need to).
What I am looking for is a book (or some other resource) on "how to
learn C if you already know C++" or, more exactly, "how to program in a
procedural language when your only experience is with object-oriented
languages".
This newsgroup is probably the best source I can think of. K&R is
recommended all willy-nilly here, and I'd start with that book. C
Unleashed is another well-written textbook.
I find myself wanting to emulate many OO features and I have the
impression this is wrong. I need to know how source files are
organized, I'm lost without namespaces, classes and the standard
library (containers, someone?)

Using structs and explicit this pointers in functions, you can emulate
classes. Namespaces are fairly easy to get along without. C has a
standard library that works great. Well-written header files will be
easy to understand (I personally find them moreso than C++ headers,
because they lack extra keywords for namespaces and the like).
, how do you cope with all these variable
definitions at the top of a function,

It's much easier. If you need to find where a variable is declared, you
know exactly where to look. Also, you learn to use less variables that
way (hopefully as a side effect of writing more efficient code). For temp
variables you always have block scope.
do you really create
as-big-as-you-can arrays of chars for strings, etc. etc.

No. We do strings much the same as in C++, but our functions aren't part
of a string class. Things tend to be easier to understand that way.

In C99 you have VLA's, but personally those look tricky to maintain to me.
Pretty much everything I was told and teached to avoid in C++ seem to
be needed in C. That's counter-intuitive for me and hard to get over.
Is there a book of wisdom somewhere that could help me understand
idioms, design patterns or oft-used construction?

Conversely, everything that C++ textbooks say not to do make me wonder what
Bjarne was thinking when he made that crazy language. Basically, you'll get
used to it pretty quickly.
 
J

Jonathan Mcdougall

Andrew said:
This newsgroup is probably the best source I can think of. K&R is
recommended all willy-nilly here, and I'd start with that book. C
Unleashed is another well-written textbook.

K&R and C Unleashed, noted. Thanks.
Using structs and explicit this pointers in functions, you can emulate
classes.

But is this what I want to do? Do I want to emulate classes in C?
C has a
standard library that works great.

But I find it lacks important several features. I searched a while for
an "STL" equivalent (containers and algorithms). What do people use
when they need a linked list? Are there commonly used libraries
available, such as boost for C++?
Conversely, everything that C++ textbooks say not to do make me wonder what
Bjarne was thinking when he made that crazy language. Basically, you'll get
used to it pretty quickly.

Just to specify here: I am hoping this thread won't become a religious
war. Let's try to be... uhmm.. neutral :)


Jonathan
 
I

Ian Collins

Jonathan said:
But is this what I want to do? Do I want to emulate classes in C?
Only you can answer that! If that you are happy with that style, use it
(I do).
But I find it lacks important several features. I searched a while for
an "STL" equivalent (containers and algorithms). What do people use
when they need a linked list? Are there commonly used libraries
available, such as boost for C++?
Have a look back in this group's archives for a thread "Boost process
and C", it will give you an idea...
 
C

CBFalconer

Jonathan said:
Ian Collins wrote:
.... snip ...

I will, but I am not in a good position of knowing whether some C
code is "good" or not.

What you find in K&R, or "The Practice of Programming", or "C
Unleashed", or in my releases
<http://cbfalconer.home.att.net/download/> are all good. Some may
quibble at the last.

In particular my hashlib is written with an OO attitude.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
F

Friedrich Dominicus

Jonathan Mcdougall said:
But I find it lacks important several features. I searched a while for
an "STL" equivalent (containers and algorithms). What do people use
when they need a linked list?
I'd go for glib http://developer.gnome.org/doc/API/2.0/glib/index.html
but that's off-topic here I guess.
Are there commonly used libraries
available, such as boost for C++?
I'd argue this depends on the operating system you are using.

Regards
Friedrich
 
W

William Ahern

But is this what I want to do? Do I want to emulate classes in C?

Well, not necessarily for polymorphism. But starting from the premise that
globals are bad, any interdependent set of variables should co-exist
within an allocated structure object (object in the C sense), and
operations on that set should occur via routines which take as an argument
a pointer to that structure. Whether you contract to make member variables
of that structure opaque, or even go further and employ a member table of
function pointers with which to operate on that structure is up to you.
Typically you don't need to go that far.
But I find it lacks important several features. I searched a while for an
"STL" equivalent (containers and algorithms). What do people use when they
need a linked list? Are there commonly used libraries available, such as
boost for C++?

Goto a BSD respository (OpenBSD, FreeBSD, NetBSD, etc) and pull down
sys/queue.h and sys/tree.h. You'd be hard-pressed to find better list or
tree implementations (for most qualities one can come up with). And
they're type-safe to boot, just like STL.

For hash tables CBFalconer's implementation is always available :)

- Bill
 
C

Chris Torek

... Do I want to emulate classes in C?

Sometimes. The cost (in terms of complicating-up the source) can
be high, so one should do it only when the benefit is high too.
But I find [the Standard C Library] lacks important several features.
I searched a while for an "STL" equivalent (containers and algorithms).
What do people use when they need a linked list?

Personally, I find linked lists so trivial that I would rather
open-code most of them -- there is nothing difficult about:

for (p = head; p != NULL; p = p->next)
... operate on p ...

and even ordered list insertion is pretty easy:

for (pp = &head; (p = *pp) != NULL; pp = &p->next)
if (item goes here)
break;
*pp = new;
new->next = p;

-- but if the lists get more complicated, or in projects that
have to be maintained by people who find the above insertion code
puzzling :) , I tend to use the BSD <sys/queue.h> macros.
 
I

Ian Collins

Bill said:
I'd love to see some of the regular's opinions on common code
that is good as well as bad. eg, a short list of well written code
and a (shorter!) list of code that is written poorly. (Although that
might generate a lot more heat.)
I think we had a good example of the latter from GNU last week :)
 
A

Andrew Poelstra

K&R and C Unleashed, noted. Thanks.
That's "The C Programming Language" by Kernighan and Ritchie. But it
looks like you're bright enough to use Google, so I didn't need to say
that. ;-)
But is this what I want to do? Do I want to emulate classes in C?
Oftentimes you will, but emulating classes should be a side effect,
not a goal. You'll find that you can do a lot of stuff without using
any complex data structures. (For example, crypto usually requires a
few primitive arrays and that's it).

When you use structs, you'll almost always have a few functions that
go with it.
But I find it lacks important several features. I searched a while for
an "STL" equivalent (containers and algorithms). What do people use
when they need a linked list? Are there commonly used libraries
available, such as boost for C++?
There was talk of making a clc library, but that disintegrated for various
superficial reasons, if I got the history right.

For linked lists and such, each programmer usually builds these kind of
libraries up over the years, until he can write a two-month killer app in
2 days.

You can get all the useful stuff (linked lists, trees, etc) off of the C
Unleashed CD, if you want. It's rather unfortunate that we haven't stand-
ardized stuff like that.
Just to specify here: I am hoping this thread won't become a religious
war. Let's try to be... uhmm.. neutral :)
I didn't mean 'crazy' in a bad way. Just... different. :)
 
C

Charles Richmond

I just got a new job yesterday where most of the programming is done in
C. Although I have several years of experience in C++ and other
object-oriented languages, I have next to none in C.

What I am looking for is a book (or some other resource) on "how to
learn C if you already know C++" or, more exactly, "how to program in a
procedural language when your only experience is with object-oriented
languages".

I find myself wanting to emulate many OO features and I have the
impression this is wrong. I need to know how source files are
organized, I'm lost without namespaces, classes and the standard
library (containers, someone?), how do you cope with all these variable
definitions at the top of a function, do you really create
as-big-as-you-can arrays of chars for strings, etc. etc.

Pretty much everything I was told and teached to avoid in C++ seem to
be needed in C. That's counter-intuitive for me and hard to get over.
Is there a book of wisdom somewhere that could help me understand
idioms, design patterns or oft-used construction?
Now you know how C programmers feel who have to learn C++...
 
S

santosh

Jonathan said:
But is this what I want to do? Do I want to emulate classes in C?

Not necessarily. Emulating classes is useful only if your application
fits well into the OO paradigm. Otherwise functions manipulating
structures will do most of what you need.
But I find it lacks important several features. I searched a while for
an "STL" equivalent (containers and algorithms). What do people use
when they need a linked list? Are there commonly used libraries
available, such as boost for C++?

There are several popular libraries that provide these useful
algorithms and data structures. GLib is one example. If none are
suitable for your application, (either technically or legally), then
you'll have to implement your own.

If you implement them in a flexible, and portable manner, then you can
re-use them whenever you need it.
 
D

Default User

Jonathan said:
Andrew Poelstra wrote:

But is this what I want to do? Do I want to emulate classes in C?

I'm someone who has worked extensively in both languages. My personal
opinion is that bending C around to look like C++ is NOT the way to go.
Two reasons:

1. C is more suited to procedural programming. It doesn't have a lot of
the features that simplify OO programming.

2. Probably no one else there is doing C coding like that. Likely
you'll get torn apart in code reviews and your supervision will make
you redo it all anyway. If they wanted C++, they USE C++.





Brian
 
J

Jonathan Mcdougall

Andrew said:
Oftentimes you will, but emulating classes should be a side effect,
not a goal.

That's an interesting statement. Actually, I find it too easy to get
sloppy, as if all the restrictions I applied to C++ were suddenly gone
in C. Many of the code I saw use global variables, 100+ line functions,
bad names and terse statements.

However, I do feel I am getting somwhere. The next pay is in two weeks,
so the books will have to wait though :)
There was talk of making a clc library, but that disintegrated for various
superficial reasons, if I got the history right.

It's sad, really. A strong standard library is to me very important in
a language.
For linked lists and such, each programmer usually builds these kind of
libraries up over the years, until he can write a two-month killer app in
2 days.

I can't believe, in all thoses years, that the standard comitee wasn't
able to build a standard library. Are there (this is perhaps off-topic)
historical reasons for that?


Jonathan
 
C

CBFalconer

Jonathan said:
Andrew Poelstra wrote:
.... snip ...

It's sad, really. A strong standard library is to me very
important in a language.


I can't believe, in all thoses years, that the standard comitee
wasn't able to build a standard library. Are there (this is
perhaps off-topic) historical reasons for that?

There is a well-standardized library. See the C-library and C99
links in my sig. below.

--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
<http://clc-wiki.net> (C-info)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top