Should Beginners Learn C Before C++?

  • Thread starter Stephanus
  • Start date
  • Tags
    Programming
In summary: I find nowadays programmers don't know much about addressing modes.This is mostly true, but pointers are still important for dealing with memory management and accessing data in memory. I am lucky, I ever studied programming in DOS.I actually did study programming in DOS. However, I don't think many people today study programming in DOS.
  • #1
Stephanus
1,316
104
Dear PF Forum,
I read this link
Amine Quentin said:
Hi Guys
Do I need to learn C before C ++?
That makes me think, "How, straight with C++, do we teach programming language to someone who never knows progamming language?"
 
Technology news on Phys.org
  • #2
By "straight with C++" do you mean teaching someone who knows nothing about programming the OOP aspects of C++ without their knowing any of the sequential programming basics? If that's what you mean, I think it could only be done at a conceptual level. That is, you could give them a fair amount of solid information about the principles of OPP constructs but it would be a terrible approach to have them try to do useful programming from that point without their first going back and learning the basics of sequential programming.
 
  • #3
phinds said:
By "straight with C++" do you mean teaching someone who knows nothing about programming the OOP aspects of C++ without their knowing any of the sequential programming basics? If that's what you mean, I think it could only be done at a conceptual level. That is, you could give them a fair amount of solid information about the principles of OPP constructs but it would be a terrible approach to have them try to do useful programming from that point without their first going back and learning the basics of sequential programming.
I mean like your quote before Phinds.
Teaching the "c" first than later (much later) the "++".
I really love BASICA
No procedures, just line numbers.
No variable definition.
It's the easiest language to teach someone about programming language.
Later when they know how to program, we just skip GOSUB, RETURN, DEFINT, DEFDBL, DEF SEG, PEEK, POKE, and go straight to Pascal.
And introduce them with just:
Code:
program MyFirstProgram;
{ all variables here }
{ all goto Labels here }

begin
{ all the instructions here }
end.
I call this BASICing Pascal.
 
  • #4
But on secound thought, I think it's wrong to introduce GOTO for someone who wants programming language for the first time.
 
  • #5
Stephanus said:
"How, straight with C++, do we teach programming language to someone who never knows progamming language?"

When I taught an introductory programming course using C++, I used the following sequence, more or less:
  1. The basic process of writing, compiling and running programs (i.e. how to use programming tools), using very simple "Hello, world!" type programs that just do text output.
  2. Variables: int and double for numbers, and strings for text data. (Not char arrays and pointers; I used the 'string' data type.).
  3. Programs with both input and output.
  4. If-statements and loops.
  5. How to use functions that have already been written for you, e.g. library functions from the standard library.
  6. How to use member functions of objects that have already been written, e.g. strings and other objects from the standard library: myname.length() gives you the length of myname, etc.
  7. Grouping data together into vectors (I prefer these to plain old arrays) and structs.
  8. How to write your own functions, and design programs effectively using functions to organize tasks and sub-tasks.
  9. How to write your own classes (objects), and their member functions, and design programs to use them to organize your data and the actions that you do with them.
I've probably left something out; it's been ten years since I last taught that course.
 
  • Like
Likes Stephanus
  • #6
jtbell said:
When I taught an introductory programming course using C++, I used the following sequence, more or less:
  1. The basic process of writing, compiling and running programs (i.e. how to use programming tools), using very simple "Hello, world!" type programs that just do text output.
  2. Variables: int and double for numbers, and strings for text data. (Not char arrays and pointers; I used the 'string' data type.).
  3. Programs with both input and output.
  4. If-statements and loops.
  5. How to use functions that have already been written for you, e.g. library functions from the standard library.
  6. How to use member functions of objects that have already been written, e.g. strings and other objects from the standard library: myname.length() gives you the length of myname, etc.
  7. Grouping data together into vectors (I prefer these to plain old arrays) and structs.
  8. How to write your own functions, and design programs effectively using functions to organize tasks and sub-tasks.
  9. How to write your own classes (objects), and their member functions, and design programs to use them to organize your data and the actions that you do with them.
I've probably left something out; it's been ten years since I last taught that course.
Good steps.
But I didn't see pointer?
I find nowadays programmers don't know much about addressing modes.
I am lucky, I ever studied programming in DOS.
At least I can tell that in pascal: A = B + C; that would be
mov AX,
add AX,[C]
mov [A], AX
And all those segment : offset
 
  • #7
Stephanus said:
But I didn't see pointer?

I didn't do pointers until near the end of the course. Using the standard string data type means you don't need char*. Using vectors instead of arrays eliminates another place where pointers come up early in C. When passing stuff to functions I used references instead of pointers. The only thing left where pointers made sense was dynamic memory allocation using 'new' and 'delete'.

In fact, I remember having trouble coming up with a situation involving dynamic memory allocation that I could use to motivate introducing pointers. "OK, here's what I'd like to do, but none of the tools we've learned so far can accomplish it. So here's something new: pointers." I don't remember what I used. (Hey, it was ten years ago!) Probably something involving linked lists.

The guy who taught the upper-level computer science courses told me not to worry about it if I didn't do much with pointers. The students would study them in the computer architecture course anyway. That's where they did stuff like assembly language, addressing modes, etc.
 
  • Like
Likes Stephanus
  • #8
jtbell said:
I didn't do pointers until near the end of the course. Using the standard string data type means you don't need char*. Using vectors instead of arrays eliminates another place where pointers come up early in C. When passing stuff to functions I used references instead of pointers. The only thing left where pointers made sense was dynamic memory allocation using 'new' and 'delete'.

In fact, I remember having trouble coming up with a situation involving dynamic memory allocation that I could use to motivate introducing pointers. "OK, here's what I'd like to do, but none of the tools we've learned so far can accomplish it. So here's something new: pointers." I don't remember what I used. (Hey, it was ten years ago!) Probably something involving linked lists.

The guy who taught the upper-level computer science courses told me not to worry about it if I didn't do much with pointers. The students would study them in the computer architecture course anyway. That's where they did stuff like assembly language, addressing modes, etc.
"Nothing can accomplish it" A very good phrase for pointer introduction. I remember, 20 years ago I used to teach programming language when I was in college for some cash. Just fire the pointer away.
 
  • #9
Stephanus said:
I mean like your quote before Phinds.
Teaching the "c" first than later (much later) the "++".
I really love BASICA
No procedures, just line numbers.
As I recall from many years ago, BASICA (and GWBASIC and several other variants of BASIC) had subroutines (called with GOSUB) and functions.
Stephanus said:
No variable definition.
It's the easiest language to teach someone about programming language.
Later when they know how to program, we just skip GOSUB, RETURN, DEFINT, DEFDBL, DEF SEG, PEEK, POKE, and go straight to Pascal.
And introduce them with just:
Code:
program MyFirstProgram;
{ all variables here }
{ all goto Labels here }

begin
{ all the instructions here }
end.
I call this BASICing Pascal.
 
  • #10
Stephanus said:
Good steps.
But I didn't see pointer?
I find nowadays programmers don't know much about addressing modes.
I am lucky, I ever studied programming in DOS.
At least I can tell that in pascal: A = B + C;
Nope, that isn't Pascal. For assignment, you use := .
Stephanus said:
that would be
mov AX,
add AX,[C]
mov [A], AX
And all those segment : offset
Your first MOV instruction is missing something.
 
  • #11
Mark44 said:
Nope, that isn't Pascal. For assignment, you use := .

Your first MOV instruction is missing something.
Sorry, you have sharp eye, Mark!
I typed to fast I think. I have been using pascal since 1990.
Code:
var
  A,B,C: Word;
begin
  A:=B+C;
  { assembly equivalence
  mov AX,[B]
  add AX,[C]
  mov [A],AX
  }
end.
Should use Word or Integer in Turbo Pascal works fine. In Delphi, should be Word.
A little try (just try that this very time. 'Integer' doesn't work in Delphi in asm mode }
Code:
var
  A: Integer;
  B: Word;

begin
  B:=A; //works
  asm
    mov AX,A // syntax error
  end;
end;
 
  • #12
Stephanus said:
But on secound thought, I think it's wrong to introduce GOTO for someone who wants programming language for the first time.

I couldn't agree more. I started with BASIC on a C64 and produced nothing but spaghetti code just because it was possible. As BASIC was too slow to do something useful I then switched to assembler. That made it even worse. Learning programming this way was a really bad idea.
 
  • Like
Likes Stephanus
  • #13
DrStupid said:
I couldn't agree more. I started with BASIC on a C64 and produced nothing but spaghetti code just because it was possible. As BASIC was too slow to do something useful I then switched to assembler. That made it even worse. Learning programming this way was a really bad idea.
What bad idea? Using GOTO or go straight to assembly after BASIC?
Judging your first programming language is BASIC, I think you learned it at the 70's. If you study programming language in the 50's you'd be lucky just to have the punched card FORTRAN
 
  • #14
Stephanus said:
What bad idea? Using GOTO or go straight to assembly after BASIC?

Starting with programming languages that doesn't force me to structured programming.
 
  • #15
DrStupid said:
Starting with programming languages that doesn't force me to structured programming.
But I find BASIC is very useful to teach someone the first programming language.
After the pupil is experienced enough. Writing a pascal triangle or fibonacci sequence, he/she should immediately be taught Pascal, then after that, is just choices.
 
  • #16
Stephanus said:
But I find BASIC is very useful to teach someone the first programming language.

That's what it is purposed for. And with a BASIC dialect that does not include GOTO it is even useful to teach good programming from the beginning.
 
  • #17
DrStupid said:
That's what it is purposed for. And with a BASIC dialect that does not include GOTO it is even useful to teach good programming from the beginning.
Couldn't agree more. NO GOTO, GOSUB. At least not GOSUB. Switch to Pascal before GOSUB.
 
  • #18
Stephanus said:
Couldn't agree more. NO GOTO, GOSUB. At least not GOSUB. Switch to Pascal before GOSUB.
Why Pascal? I might be wrong, but as far as I know it's pretty much a dead language. After doing a little searching, I find that it still exists, though, as Delphi, which Borland sold off in 2008 to a company named Embarcadero.

I don't know of many schools that teach Pascal/Delphi, and we don't get a lot of questions about it here, so it would seem that not a lot of people are writing code with Pascal/Delphi.
 
  • #19
DrStupid said:
That's what it is purposed for. And with a BASIC dialect that does not include GOTO it is even useful to teach good programming from the beginning.
Although the use of goto often leads to unreadable "spaghetti" code, and was excoriated in a paper by Edsger Dijkstra in 1968, its use is recommended in certain instances, such as for "end-of-function error handlers and for multi-level breaks from loops." -- Per Dennis Ritchie and Brian Kernighan from wiki article in link below.

https://en.wikipedia.org/wiki/Goto
Other programmers, such as Linux Kernel designer and coder Linus Torvalds or software engineer and book author Steve McConnell, also object to Dijkstra's point of view, stating that GOTOs can be a useful language feature, improving program speed, size and code clearness, but only when used in a sensible way by a comparably sensible programmer. According to computer science professor John Regehr, in 2013, there were about 100,000 instances of goto in the Linux kernel code.

Some quotes from Steve McConnell, in "Code Complete", pp. 348-349.
A well-placed goto can eliminate the need for duplicate code.
...
The goto is useful in a routine that allocates resources, performs operations on those resources, and then deallocates the resources.
...
In some cases, the goto can result in faster and smaller code.
 
  • Like
Likes Stephanus
  • #20
Mark44 said:
Why Pascal? I might be wrong, but as far as I know it's pretty much a dead language. After doing a little searching, I find that it still exists, though, as Delphi, which Borland sold off in 2008 to a company named Embarcadero.

Embarcadero still sells it for prices that nobody would pay just for fun. Thus there seem to be some professional users left. But I would also say that it is close to extinction.
 
  • #21
DrStupid said:
Embarcadero still sells it for prices that nobody would pay just for fun. Thus there seem to be some professional users left. But I would also say that it is close to extinction.
Borland made a great marketing decision back when they released TurboPascal in 1983. As I recall, it sold for $49 (?), and they sold a ton of copies, and got a lot of people interested in programming. Back about '83 or so I had a version of UCSD Pascal that ran on Apple II computers. After I started learning C in '85, I was all done with Pascal.
 
  • #23
Mark44 said:
Why Pascal? I might be wrong, but as far as I know it's pretty much a dead language. After doing a little searching, I find that it still exists, though, as Delphi, which Borland sold off in 2008 to a company named Embarcadero.

I don't know of many schools that teach Pascal/Delphi, and we don't get a lot of questions about it here, so it would seem that not a lot of people are writing code with Pascal/Delphi.
Aside from Borland text editor which allow cursor to go up/down vertically :smile:, what I like about Pascal is its nested procedure.
It's very convenient to access local variable globally.
Code:
procedure Level1;
var
  A: Integer;
  procedure Level2;
    procedure Level3;
    begin
     A:=10; { don't have to put A in global scope }
    end;
  begin
  end;
begin
end;
And it's array definition which has a unique characteristic.
Pascal: var A: Integer[7..10];
Basic: dim A(10)
C: int a[10];
Pascal can set the element of the array from 7 not from zero, not a good programming technique tough. Has to leave this concept, because 99% of Delphi objects starts from 0
TStrings.Strings[Index] starts from zero
TList.Items[Index] starts from zero
TMemo.Lines[Index], TDataSet.Fields[Index], and almost many more.

But of course C++ has multiple inheritance which Pascal don't have.
And operator overloading is the most impressive feature in C++.
But nested procedure is the one that I need most in Pascal.
Pascal OOP has a unique characteristic than C++.
In C++ the inherited constructors) is called automatically. Is it good? Bad? I don't know.
But in Pascal we can delay calling inherited constructor.
Code:
constructor Square.InitSquare;
begin
  A:-10;
  B:=20;
  { some codes here }
  InitRectangle; { call the inherited constructor after some lines processed }
end;
But I know a kid, (not mine unfortunately :frown:), 13 years old, start doing Phyton.
 
  • #24
Mark44 said:
Although the use of goto often leads to unreadable "spaghetti" code, and was excoriated in a paper by Edsger Dijkstra in 1968, its use is recommended in certain instances, such as for "end-of-function error handlers and for multi-level breaks from loops." -- Per Dennis Ritchie and Brian Kernighan from wiki article in link below.

https://en.wikipedia.org/wiki/GotoSome quotes from Steve McConnell, in "Code Complete", pp. 348-349.
GOTO is important for an experienced programmer. For a beginner it's a disaster.
It's like watching violence in GodFather. Good movie for adult, bad movie for children.
 
  • #25
Mark44 said:
Borland made a great marketing decision back when they released TurboPascal in 1983. As I recall, it sold for $49 (?), and they sold a ton of copies, and got a lot of people interested in programming. Back about '83 or so I had a version of UCSD Pascal that ran on Apple II computers. After I started learning C in '85, I was all done with Pascal.
Sorry, Mark if I trouble you. It's just out of curiousity, why C is better than Pascal. I still doing C in Metatrader, I like C, but I think Pascal is better than C.
Perhaps if you could give me some enlightment...
 
  • #26
Stephanus said:
Aside from Borland text editor which allow cursor to go up/down vertically :smile:, what I like about Pascal is its nested procedure.
It's very convenient to access local variable globally.
Code:
procedure Level1;
var
  A: Integer;
  procedure Level2;
    procedure Level3;
    begin
     A:=10; { don't have to put A in global scope }
    end;
  begin
  end;
begin
end;
Right, neither C nor C++ allows for functions to be nested inside other functions, although some current versions of C++ such as Visual Studio 2010 and later, allow the programmer to insert lambda expressions into a function or method. Lambda expressions are anonymous functions that are defined inline.

Python allows functions to be defined inside a function, though.
In your example above, you're not defining A globally -- it is local to Level1. Level2 and Level3 have access to variables defined at outer levels, which A is. If you had defined A inside Level3, it would not be visible to Level2 or Level1, so you wouldn't be able to access it "globally".

Stephanus said:
And it's array definition which has a unique characteristic.
Pascal: var A: Integer[7..10];
Basic: dim A(10)
C: int a[10];
Pascal can set the element of the array from 7 not from zero, not a good programming technique tough. Has to leave this concept, because 99% of Delphi objects starts from 0
TStrings.Strings[Index] starts from zero
TList.Items[Index] starts from zero
TMemo.Lines[Index], TDataSet.Fields[Index], and almost many more.
IMO, it's only mildly convenient to be able to index arrays starting at other than zero. Many languages have this capability, among them Basic and Fortran.
Stephanus said:
But of course C++ has multiple inheritance which Pascal don't have.
And operator overloading is the most impressive feature in C++.
But nested procedure is the one that I need most in Pascal.
Pascal OOP has a unique characteristic than C++.
In C++ the inherited constructors) is called automatically. Is it good? Bad? I don't know.
But in Pascal we can delay calling inherited constructor.
Code:
constructor Square.InitSquare;
begin
  A:-10;
  B:=20;
  { some codes here }
  InitRectangle; { call the inherited constructor after some lines processed }
end;
If you write your own constructor in C++, rather than rely on a default constructor, you can have it do what you want.
Stephanus said:
But I know a kid, (not mine unfortunately :frown:), 13 years old, start doing Phyton.
I'm starting to learn Python, myself. I'm finding it to be a very interesting language with lots of unusual capabilities.
 
  • #27
Mark44 said:
In your example above, you're not defining A globally -- it is local to Level1. Level2 and Level3 have access to variables defined at outer levels, which A is. If you had defined A inside Level3, it would not be visible to Level2 or Level1, so you wouldn't be able to access it "globally".
This is what we have to do in C. Move a to global.
Code:
int a; // accessed by level3
void level1()
{
  int a;
  a=50;
  level3();
  cout << a; / will output 50 instead of 20. C can't do that.
}
void level2()
{
}
void level3()
{
  a=20;
}
 
  • #28
Stephanus said:
This is what we have to do in C. Move a to global.
Code:
int a; // accessed by level3
void level1()
{
  int a;
  a=50;
  level3();
  cout << a; // will output 50 instead of 20. C can't do that.
}
void level2()
{
}
void level3()
{
  a=20;
}
You don't have to do this, and it's usually not recommended. It's usually better to pass variables in the parameter list
C:
void level1()
{
  int a;
  a=50;
  level3(a);
  cout << a; // will output 50 instead of 20. C can't do that.
}

void level3(int b)
{
  // level3 has read access to level1's a.
  printf("b is %d", b);   // prints "b is 50"
}
If level3 also needs write access, level3 can be rewritten so that the parameter is a pointer variable.
 
  • #29
Stephanus said:
And operator overloading is the most impressive feature in C++.

This is also supported by recent versions of Delphi.
 
  • #30
DrStupid said:
This is also supported by recent versions of Delphi.
Wow, I'm still using Delphi 3, 1996 version. I have a qr barcode library, can't find for the newer version. So I am stuck with it. I just realized, it's been 19 years. The longest programming language that I code.
 

Related to Should Beginners Learn C Before C++?

What is the best programming language to learn?

The best programming language to learn depends on your goals and interests. Some popular languages include Python, Java, C++, and JavaScript. It's important to research the job market and industries that use each language to determine which one is the best fit for you.

Do I need a computer science degree to learn programming languages?

No, you do not need a computer science degree to learn programming languages. Many successful programmers are self-taught or have learned through online resources and coding bootcamps. However, a degree in computer science can provide a strong foundation and may be required for certain job opportunities.

How long does it take to learn a programming language?

The time it takes to learn a programming language varies depending on the individual's learning style, dedication, and previous experience. Some people may pick up a language quickly while others may take longer. However, with consistent practice and dedication, most people can learn a programming language within a few months.

What are the benefits of learning multiple programming languages?

Learning multiple programming languages can expand your skill set and make you more versatile as a programmer. It can also make it easier to learn new languages in the future. Additionally, some projects may require knowledge of multiple languages, so being proficient in more than one can make you a more valuable asset in the job market.

Are there any resources available to help me learn programming languages?

Yes, there are many resources available to help you learn programming languages. Online courses, coding bootcamps, and tutorials are popular options. Additionally, many programming languages have official documentation and community forums where you can ask questions and get support. It's also helpful to practice regularly and work on projects to apply your knowledge.

Similar threads

  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
2
Replies
69
Views
5K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
2
Replies
49
Views
3K
  • Programming and Computer Science
4
Replies
107
Views
6K
  • Programming and Computer Science
Replies
1
Views
805
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
12
Replies
397
Views
14K
  • Programming and Computer Science
Replies
13
Views
853
Back
Top