By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

learning C with a C++ background

P: n/a
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

Jun 14 '06 #1
Share this Question
Share on Google+
21 Replies


P: n/a
jo***************@gmail.com wrote:
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.

--
Ian Collins.
Jun 14 '06 #2

P: n/a
Ian Collins wrote:
jo***************@gmail.com wrote:
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!


:) Thanks
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.


Got that.
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 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.
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.


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

Jun 14 '06 #3

P: n/a
Jonathan Mcdougall wrote:
Ian Collins wrote:

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.

Have a look at some of the newer code in OpenSolaris, something like ZFS:

http://cvs.opensolaris.org/source/xr...libzfs/common/

--
Ian Collins.
Jun 14 '06 #4

P: n/a
Jonathan Mcdougall wrote:
Ian Collins wrote:
jo***************@gmail.com wrote:
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 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.

--
Ian Collins.
Jun 14 '06 #5

P: n/a
On 2006-06-14, jo***************@gmail.com <jo***************@gmail.com> wrote:
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.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 14 '06 #6

P: n/a
Andrew Poelstra wrote:
On 2006-06-14, jo***************@gmail.com <jo***************@gmail.com> wrote:
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.


K&R and C Unleashed, noted. Thanks.
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.


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

Jun 14 '06 #7

P: n/a
Jonathan Mcdougall wrote:

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?

Only you can answer that! If that you are happy with that style, use it
(I do).
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++?

Have a look back in this group's archives for a thread "Boost process
and C", it will give you an idea...

--
Ian Collins.
Jun 14 '06 #8

P: n/a
Jonathan Mcdougall wrote:
Ian Collins wrote:

.... snip ...

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.


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:
news:news.announce.newusers
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
Jun 14 '06 #9

P: n/a
"Jonathan Mcdougall" <jo***************@gmail.com> writes:
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?

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

--
Please remove just-for-news- to reply via e-mail.
Jun 14 '06 #10

P: n/a
On Tue, 13 Jun 2006 19:41:15 -0700, Jonathan Mcdougall wrote:
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?


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.
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++?


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

Jun 14 '06 #11

P: n/a

Ian Collins wrote:
Jonathan Mcdougall wrote:
Ian Collins wrote:

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.

Have a look at some of the newer code in OpenSolaris, something like ZFS:

http://cvs.opensolaris.org/source/xr...libzfs/common/

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.)

Jun 14 '06 #12

P: n/a
Bill Pursell wrote:

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 :)

--
Ian Collins.
Jun 14 '06 #13

P: n/a
In article <11**********************@y43g2000cwc.googlegroups .com>
Jonathan Mcdougall <jo***************@gmail.com> wrote:
... 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.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jun 14 '06 #14

P: n/a
On 2006-06-14, Jonathan Mcdougall <jo***************@gmail.com> wrote:
Andrew Poelstra wrote:
On 2006-06-14, jo***************@gmail.com <jo***************@gmail.com> wrote:

> 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.


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. ;-)
> 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.


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.
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++?

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.
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 :)

I didn't mean 'crazy' in a bad way. Just... different. :)

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 14 '06 #15

P: n/a
jo***************@gmail.com wrote:

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++...

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Jun 14 '06 #16

P: n/a

Jonathan Mcdougall wrote:
Andrew Poelstra wrote:
On 2006-06-14, jo***************@gmail.com <jo***************@gmail.com> wrote:

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".

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?


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.
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++?


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.

Jun 14 '06 #17

P: n/a
Jonathan Mcdougall wrote:
Andrew Poelstra wrote:

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?


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
Jun 14 '06 #18

P: n/a
Andrew Poelstra wrote:
> 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.
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.


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

Jun 15 '06 #19

P: n/a
Jonathan Mcdougall wrote:
Andrew Poelstra wrote:
.... snip ...
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?


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)
Jun 16 '06 #20

P: n/a
"Jonathan Mcdougall" <jo***************@gmail.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
Andrew Poelstra wrote:
On 2006-06-14, jo***************@gmail.com <jo***************@gmail.com>
wrote:
> 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.


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


Not in general, no. You pointed out Gtk as seeming cumbersome, and the
reason for that is that Gtk is basically C++ translated (mentally) to C.

As the saying goes, "You can write FORTRAN in any language." That doesn't
mean doing so is a good idea. If OOP is what you need, you already know
C++. If it's not what you need, don't import it and force C into a mold it
doesn't fit -- do things the C way instead.
C has a standard library that works great.


But I find it lacks important several features.


Remember that C90 was basically an attempt to document what already existed
at the time; very, very few new features were added to the language. C
evolved, whereas C++ had an "intelligent designer" (only US residents are
likely to catch that reference).
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 common implementations of many of the things you're looking for,
but common practice is to write your own as an integral part of the code.
Need a vector<>? Use an array. Need a list<>? Add a next (and possibly
prev) pointer to your structs. Need an iterator? Use a pointer of the same
type. etc. "Generic" implementations add a lot of overhead; if we wanted
to pay that cost, we'd go use C++ instead of C.

The biggest thing you need to learn is how different memory management is,
i.e. malloc() and free() instead of new and delete. "Pass by reference" in
C lingo means what you think of as "pass by pointer"; there's no equivalent
to C++'s references. And, of course, strings are totally different since
you do the memory management yourself instead of letting some class handle
it for you.

The only reason I can see someone wanting to move from C++ to C is to get
closer to the metal and get rid of all the OOP overhead. If you don't look
at it in that light, a lot of C won't make sense to you. Each language has
its strengths; trying to solve problems that C++ is good at in C will not
make sense, so try to figure out what problems C++ is bad at and examine why
C solves them more efficiently. Then you can choose which to use for any
given problem.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin
--
Posted via a free Usenet account from http://www.teranews.com

Jun 19 '06 #21

P: n/a
Stephen Sprunk wrote:

The only reason I can see someone wanting to move from C++ to C is to
get closer to the metal and get rid of all the OOP overhead


I don't think either of these are reasons, C++ gets you just a close to
the metal and you don't have any OOP overhead if you don't use OOP.

The most common reason is lack of a C++ compiler for the target platform.

--
Ian Collins.
Jun 19 '06 #22

This discussion thread is closed

Replies have been disabled for this discussion.