LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2010, 01:47 AM   #1
naruponk
LQ Newbie
 
Registered: Feb 2009
Posts: 27

Rep: Reputation: 15
Should I start with C or C ++


Hi,

I'm going to start programming in Linux.
Should I start programming with C or C++ ?
Which one is better to start learning ?
And where can I get best practice to learn myself

Please advise
 
Old 11-04-2010, 01:55 AM   #2
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
If you aimed to finally program mainly in C++ then start with it. But there is no reason to learn C++ to write 'hello world' programs.
 
Old 11-04-2010, 03:39 AM   #3
fcdev
Member
 
Registered: Sep 2005
Posts: 47

Rep: Reputation: 15
It all comes down to personal preference, and people's opinions will surely set off a religious war when they say that C++ is better, or C is better. So I'll start off with my opinion, and you can all flame me for it...

I would probably suggest using the G++ compiler (which compiles C++ code) and then start learning C, with the intention of expanding your knowledge to C++.

By using a C++ compiler (eg: G++), you have extra features which make life a little easier, and you can bend some of the rules in C a little which makes it a bit easier to learn things, instead of being beaten over the head by the C compiler whenever you make a minor mistake.

As for the languages, C++ gives you more features, like classes, templates, etc ... however those features come at a cost ... classes require a little extra overhead for the CPU (they run just a tiny bit slower than non-class code), and templates make your code next to unreadable (yes, I know people who have lost their jobs for excessive use of templates). Most of the features you gain in classes can be incorporated into structures, which are available in C, and have a lower overhead.

Now the catch is that structures are easier to work with in C++ than they are in C.

So there you have it, my twisted reasons for writing "not-really C, but not quite C++" code using a C++ compiler.
 
Old 11-04-2010, 04:24 AM   #4
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
I am a bit dogmatic. There are different classes of programing languages: procedural and others.
My point is that C++ defines its own class - nowadays C++ it is not an extension of C. There are
no 'procedural programing' in C++. The C and C++ are two different paradigms of programing. You cannot switch easily from one to the other. You will need to change completely your mind,
your way to think about programing tasks. Of course if you want to program efficiently. C and C++
programmers speak two different languages - that's they cannot understand each other.
 
Old 11-05-2010, 01:38 AM   #5
naruponk
LQ Newbie
 
Registered: Feb 2009
Posts: 27

Original Poster
Rep: Reputation: 15
Thanks guys ^ ^
 
Old 11-05-2010, 08:45 AM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by naruponk View Post
Should I start programming with C or C++ ?
C++ is a better language. The right subset of C++ is also a better beginner language than C.

C++ streams are better for beginner output that printf and much better for beginner input than scanf.

C++ strings are much better for beginner operations on text than C strings. C strings will make much more sense if you learn them after you have basic competence with pointers and arrays (features in common in C and C++ that you need to learn either way). Understanding C strings is important even for a C++ programmer (unlike printf/scanf), but you can get to them a little later in learning C++ than you would in learning C.

Quote:
Originally Posted by fcdev View Post
So I'll start off with my opinion, and you can all flame me for it.
If you insist.

Quote:
classes require a little extra overhead for the CPU (they run just a tiny bit slower than non-class code)
That isn't an opinion. It is a falsehood.

C++ code can include hidden work, extra work that must be done at run time because of some design/code choice made by the programmer, but which wouldn't be obvious to a beginner programmer. The work implied by a chunk of C code tends to be more obvious.

So a beginner might easily write slower code in C++ than that same beginner might write in C. An expert will tend not to write slower code in C than C++ and when performance matters, an expert will know how to get maximum performance from C++ code, often more easily than getting the same performance from C code.

But there is no fundamental overhead to class code vs. non class code. There is no uniform slow down. The only slow down is when you code more work for your program to do than you intended it to do.

Quote:
templates make your code next to unreadable
That one is an opinion (see the difference?). But it is still wrong. Templates, like almost any feature of a powerful language can be abused to make unreadable or otherwise bad code. Templates can also be used to make much more readable and maintainable code in C++ than you can make in any other popular language.

Quote:
(yes, I know people who have lost their jobs for excessive use of templates).
I know a couple who should have lost their jobs for excessive use of templates, plus one more who was even worse in excessive use of templates, but too valuable in other ways to deserve losing his job for that. (One fired, two quit, and I need to maintain the messes they left, but none were fired or quit over templates). I can think of several other programming styles in C or C++ that are equally bad and few programmers who exhibit them. The fact that a few programmers use templates to write bad code doesn't mean templates are bad.
 
Old 11-05-2010, 12:14 PM   #7
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
@johnsfine
I like it. Good intro for any C++ programing reference.
 
Old 11-05-2010, 03:45 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by igadoter View Post
... There are
no 'procedural programing' in C++. ...
Huh ?

C99 is almost a strict subset of C++, and there are some things in C99 which do not exist in C++ (very few).

If you know what you are doing, using a smart preprocessor you can effectively translate a higher than C++ level language into "C".
 
Old 11-05-2010, 04:03 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by igadoter View Post
... nowadays C++ it is not an extension of C. ...
You might be surprised, but still: http://llvm.org/docs/FAQ.html#translatecxx . I.e. C++ can be translated into C automatically.
 
Old 11-06-2010, 01:04 AM   #10
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
Hm, I am not surprised. Any language can be automatically translated into
assembler.
 
Old 11-06-2010, 03:58 AM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by igadoter View Post
Hm, I am not surprised. Any language can be automatically translated into
assembler.
No. There are, for example, interpreted languages which are not translated into "C" or assembly. The point of "interpreted" in this case is that they are languages with late binding. I.e. the runtime engine not only is used for running the programs, but also, when necessary, for compiling and linking during runtime additional pieces of code.

But back to our C/C++ debate. It is very useful for the end user to understand when (compile or run time) and what price is paid for object orientedness. As johnsfine has already pointed this out.

For the OP it might interesting to learn both "C" and C++ at the same time, looking into implementation details.
 
Old 11-06-2010, 06:51 AM   #12
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Quote:
Originally Posted by igadoter View Post
The C and C++ are two different paradigms of programing. You cannot switch easily from one to the other. You will need to change completely your mind, your way to think about programing tasks. Of course if you want to program efficiently. C and C++
programmers speak two different languages - that's they cannot understand each other.
Ah, then I must be breaking the laws of this universe, being able to "easily" switch from one to the other. You can write programs that can be compiled and understood to be C and C++. The way of thinking applies to more than just C/C++; you can program in different ways using plain C as well. In some cases you can choose freely between them, using your own preferences, in some cases it's perhaps easier to choose one over the other. But claiming that C and C++ are from completely different worlds, "unable to understand each other" (whatever you mean by that, exactly, is a bit unclear), that's just not right.

Do learn both of them, at least the basics, and you'll see what they have in common and what they don't. The more languages (of different kind) you learn, the more you know, as goes with everything else. Don't limit yourself to one language, however, because that'll just limit what you can do.
 
Old 11-06-2010, 07:28 AM   #13
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
Quote:
Originally Posted by b0uncer View Post
Do learn both of them, at least the basics
You can learn 'basics' for C but what do you mean by 'basics' for C++? For me these are templates, classes, overloading, iterators, containers, streams, exceptions, class hierarchy, STL - all features which make this language so powerful. On the other, C side we have data types, arrays,pointers, structures, functions, all statements.
Now what is this common point for both of them?

Last edited by igadoter; 11-06-2010 at 07:29 AM.
 
Old 11-06-2010, 08:22 AM   #14
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by igadoter View Post
what do you mean by 'basics' for C++? For me these are templates, classes, overloading, iterators, containers, streams, exceptions, class hierarchy, STL - all features which make this language so powerful.
That might be more than the average C++ programmer ever learns. It is absurd to call that the basics.

Quote:
On the other, C side we have data types, arrays,pointers, structures, functions, all statements.
Now what is this common point for both of them?
That was all part of the common points for both of them (data types, arrays, pointers, structures, functions, the basic statement types). The more significant question is what parts of the basics aren't in common. All the semantics of C and very close too all the syntax is part of C++. So all the basics of C are part of C++, but some of them aren't part of the basics of C++.

As I said before, printf and scanf are part of the basics of C, but in C++ you would use std::cout and std::cin. The common C string functions (strcmp, strcat, etc.) are part of the basics of C, but in C++ you would use std::string.

In both C and C++, the basics should include the major storage classes: Static (including global), Automatic, and Dynamic. But in C you would use malloc/free for dynamic, while in C++ you would use new/delete. Either way, you need to introduce the use of pointers for dynamic storage and elsewhere (hopefully later) deal with pointers to non dynamic storage and pointers into aggregates.

Learning templates is fairly advanced C++. Learning advanced features of templates that would be needed to write replacements for some of the std templates is even more advanced. In other threads people have pointed out that a beginning C++ is immediately using templates because of std::cout. I think that is like saying a beginning driver is using the car's fuel injection system as soon as he presses the gas pedal. It is true, but pointless. A beginning driver doesn't need to know the fuel injection system even exists. He just learns to use the gas pedal. A beginning C++ programmer can learn to use std::cout without learning about templates.

I think object oriented programming is a source of confusion to beginners. Object oriented fanatics and their influence over company programming standards and policies are a major impediment to good software engineering. Object oriented programming should be a tool a good programmer learns and understands and then uses when appropriate to the task. It is not a universal tool. If I were teaching C++, I would get through quite a lot of the basics (including structures) before introducing any true object oriented programming.

Last edited by johnsfine; 11-06-2010 at 08:23 AM.
 
Old 11-06-2010, 08:29 AM   #15
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
Now I understand why some people call themselves C and C++ programmers.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
start script bash when system start,stop,reeboot, how ?? melmar Linux - General 4 12-10-2009 06:58 AM
when i try to start samba pdc's smb service nmbd failed to start . sandeepchau123 Linux - Newbie 2 10-20-2007 01:59 PM
Computer doesn't start; fans start then stop; 1 long, 1 short beep PatrickMay16 General 18 09-26-2007 07:48 PM
disable cold start and warm start snmp traps chkisco Linux - Newbie 0 10-28-2005 02:58 AM
Can I gett KPPP to start when I start a browser on a network machine? duffboygrim Linux - Networking 0 03-25-2004 04:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration