What To Do To Learn C & C++

About programming and getting involved with Linux Mint development
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
JoeFootball
Level 13
Level 13
Posts: 4673
Joined: Tue Nov 24, 2009 1:52 pm
Location: /home/usa/mn/minneapolis/joe

Re: What To Do To Learn C & C++

Post by JoeFootball »

billmartinn wrote: I want to learn C & C++ but I do not know where to start.
C Tutorial for Beginners
https://www.youtube.com/watch?v=KJgsSFOSQv0

C++ Tutorial for Beginners
https://www.youtube.com/watch?v=vLnPwxZdW4Y

Edit: Adding Python, if interested ...

Python Tutorial for Beginners
https://www.youtube.com/watch?v=rfscVS0vtbw
Last edited by JoeFootball on Wed Sep 15, 2021 7:37 am, edited 1 time in total.
User avatar
Moem
Level 22
Level 22
Posts: 16228
Joined: Tue Nov 17, 2015 9:14 am
Location: The Netherlands
Contact:

Re: What To Do To Learn C & C++

Post by Moem »

billmartinn wrote: Tue Sep 14, 2021 9:42 am I have just joined this forum to get an answer of my question.
Then you may not have made the best choice there. Most of us here are not programmers. Aren't there any forums for programmers and people who want to become one?
Image

If your issue is solved, kindly indicate that by editing the first post in the topic, and adding [SOLVED] to the title. Thanks!
deepakdeshp
Level 20
Level 20
Posts: 12334
Joined: Sun Aug 09, 2015 10:00 am

Re: What To Do To Learn C & C++

Post by deepakdeshp »

You can try and post on stackoverflow. This forum is Linux Mint help.
If I have helped you solve a problem, please add [SOLVED] to your first post title, it helps other users looking for help.
Regards,
Deepak

Mint 21.1 Cinnamon 64 bit with AMD A6 / 8GB
Mint 21.1 Cinnamon AMD Ryzen3500U/8gb
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: What To Do To Learn C & C++

Post by rene »

Step 1 would generally be to not conflate C and C++. If you learn C++ then learning basic C is to some degree automatic but conversely this is very much not the case: C++ is a very different language than C; when used as intended, object oriented, C++ inherits basic syntax from C but that's also just about the only thing it still does: C++ uses a significantly different programming paradigm and is a much larger language so as to support and make full use of such.

That is, what I'm saying is that you should explicitly know that you want to learn C++ if starting to learn C++; not just do so by wanting to know "C & C++" as a matter of you believing the two to be close anyway; C++ is not close to C in any real, functional sense.

Wanting to learn C can make sense even as a first language: in in fact much the same way as it gives a boost when later learning C++ even if as to those mentioned basics only/mostly, it provides for a similar boost for in fact many languages; is one of the lower level languages still in common use and is as such methodologically often more or less a template for other languages -- and certainly on UNIX/Linux where C can be termed "the native programming language".

That however also says that you very much need to DIY in C; that if you do it right, e.g. manual, boring error handling tends to be more than half your code and that even something basic like string parsing tends to for a large part be roll-your-own. For an actual, real first language this is not generally good: it disallows focusing on programming constructs as such; forces to concentrate on what in a language such as Python is or can be insignificant details, and tends to moreover say that you'd not these days use it just to "get something done". There's few for whom C was actually a first language, even if only if an earlier one was only e.g. childhood BASIC. If you're the type and frankly the age, sure, but as a first language for someone past 30... maybe consider e.g. Python.

If you insist; C aficionados will/would to this day point you to K&R;

https://www.amazon.com/Programming-Lang ... 131103628/

You will want to ignore that advise, but I do suggest you go the old-fashioned book route. There's just little more effective for learning than the linearity which a book naturally implies. I have no personal favourites but if I quickly search this seems to not be considered bad:

https://www.amazon.com/Programming-Abso ... 0789751984

But, then --- be very certain then that you in fact want to. I would generally and as a first language much more advise e.g.

https://www.amazon.com/Learning-Python- ... 1449355730

with or without https://www.amazon.com/Python-Pocket-Re ... 1449357016
User avatar
SMG
Level 25
Level 25
Posts: 31760
Joined: Sun Jul 26, 2020 6:15 pm
Location: USA

Re: What To Do To Learn C & C++

Post by SMG »

billmartinn wrote: Tue Sep 14, 2021 9:42 amI want to learn C & C++ but I do not know where to start.
I suggest reading this thread Wanting to learn 'C' but gcc not installing as there is a discussion of why learning python might be a better choice than learning C.
Image
A woman typing on a laptop with LM20.3 Cinnamon.
ivar
Level 5
Level 5
Posts: 617
Joined: Sun Mar 21, 2021 10:30 pm
Location: far north

Re: What To Do To Learn C & C++

Post by ivar »

billmartinn wrote: Tue Sep 14, 2021 9:42 am I have never written any code in my enter life.
I wrote some stuff in Basic 40 years ago but I'd rate my programming skills to be very poor. I'm starting to study Python, as suggested elsewhere in this thread , as I suspect the learning curve to be quite a bit more gentle. I'd think with some basic grasps of programming under the belt, it will be much easier to learn other programming languages
xanaki
Level 1
Level 1
Posts: 15
Joined: Fri Sep 17, 2021 8:40 am
Location: Finland
Contact:

Re: What To Do To Learn C++

Post by xanaki »

This is a bit longish so hope you have time to read :-)

When I started studying programming initially, I mean programming generally not just C/C++, I found a perfect way that fits for me and my little male tunnel brains. That was 36 years ago, I was 12 years old.

I started copying the games I was playing on my current computer at that time. Okay, the programming language was initially Basic. But.

My point is that the best way to learn new programming language for me is to pick up a project and just start implementing it. You will bump into big obstackles but you will solve them as you go forward with your project. Whenever you face some issue you do not know to do, study some, find information and you will learn.

Do you prefer to watch videos or learn by reading? I prefer the reading method. But that comes from the fact that when I was learning programming, the books were the only source of information, there was no Internet :). However, I have *never* read a programming book from cover to cover before starting to do my task. I read the basics and start implementing as soon as I am confident enough to be able to do something.

When I want to learn a new programming language, I will always pick up a project I want to do. Then start tackling it and the challenges, step by step.

I can give you one good practice I've been using myself in the past many times in different environments. I used this exact task 3 months ago when I started (FINALLY!) learning Python.

Task: Implement a console program which can show a binary file in ASCII hexadecimal representation.
The program shall take an argument telling how many bytes per row are displayed and another argument for how many lines are displayed out. The program always reads from the beginning of the file, for this excersise.
To start the application you use following syntax:
hextype.exe -i <filename> [-l <linecount>] [-b <bytes_per_row>]
Where
-i defines the input file, binary or text, whatever
-l defines how many lines are displayed. Optional. if omitted displays the whole file
-b defines how many bytes per row are displayed. Optional, if omitted displays 16 bytes per row.
Desired output format:

OFFSET (hex, 32-bit) : Bytes in ASCII (hex) : ASCII characters for the bytes, unprintable as '.'

e.g. for command "hextype.exe -i myfile.bin -l 3":

Code: Select all

00000000 : 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f : ................
00000010 : 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f :  !"#$%&'()*+,-./
00000020 : 30 31 32 41 42 43 61 62 63 64 65 66 67 68 69 6a : 012ABCabcdefghij
For reference, ASCII table can be found here., There are many alternatives.

Challenges to conquer:
  • How to get program arguments
  • How to open (and close) a file from filesystem
  • How to read the file in binary format
  • How to process the read file data
  • How to convert the bytes to ASCII representation
  • How to output the ASCII bytes to console output
  • How to format the output as required by the task
  • ...
For the binary -> ASCII conversion here's an "advanced" example I made few weeks ago. It uses templates and streams for conversion between binary<->ASCII bytes, words and longs.

This task makes you to dive into many fundamental concepts in any programming language. For example:
  • How to get program arguments
  • Control and program structures
  • Arrays and/or containers
  • Input/output, file and console
  • Error handling
  • ...
Always do incrementally, do one task and test it. Then expand. No need to tackle all the challenges at once :-) You would be overwhelmed very easily.

Here are some pointers you could start learning C++ from:

Written
Here's a written C++ tutorial which goes step by step:
C++ Tutorial @ w3schools.

Another nice tutorial from basics to advanced:
C++ Language @ cplusplus.com

Another good tutorial here:
Learn C++, simply easy learning @ tutorialspoint.com

Here's a collaborative collection of tutorials and information on C and C++:
Coding Den Wiki

FAQ in question/answer format:
Written C++ FAQ for newbies and veterans

Once you get to actually doing something you will eventually bump into this site for C++ reference up to C++20 standards:
C++ Reference

That one is quite technical, friendlier version is here @ cplusplus.com

Videos
A friend of mine has many good videos on C++ programming here from basics to advanced concepts:
One Lone Coder (javidx9)

If you type a YouTube search for example "C++ tutorial for beginners" you will find tens if not hundreds of channels which teach you C++, step by steam.

Finally, if you are interested My GitHub has some basics and some advanced stuff. And if anything else does not help use Google. Just type a question and you'll find many solutions, for example "C++ output text to console" will surely give you answers :P

Good luck! It's fun!
Feel free to ask any questions if you so want, I'll try to answer. <3
Last edited by xanaki on Sat Sep 18, 2021 12:48 pm, edited 1 time in total.
Bring me a shrubbery, Shrubbery of Mint. Ni!
From Finland with love and liquor since 1972.
jmorris84
Level 3
Level 3
Posts: 185
Joined: Mon Mar 19, 2012 6:40 pm

Re: What To Do To Learn C & C++

Post by jmorris84 »

John's tutorials at caveofprogramming.com are very helpful for learning C++.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: What To Do To Learn C & C++

Post by rene »

xanaki wrote: Sat Sep 18, 2021 5:35 am Task: Implement a console program which can show a binary file in ASCII hexadecimal representation.
So as to emphasize my "get something done" remark; below is a short and quite to the point Python implementation of that specification. That print() statement requires a lot more code in e.g. C and error handling as to e.g. open/read can moreover simply be left to Python.

N.B. Dislike that you kept the "C/C++" language in the reply; C++ really is a very different language than C is.

Code: Select all

#!/usr/bin/env python3

import argparse
import string

parser = argparse.ArgumentParser()
parser.add_argument('-i', required=True, metavar='<filename>')
parser.add_argument('-l', type=int, default=float('inf'), metavar='<linecount>')
parser.add_argument('-b', type=int, default=16, metavar='<bytes_per_row>')
args = parser.parse_args()

p = [ ord(c) for c in string.digits + string.ascii_letters + string.punctuation + ' ' ]

f = open(args.i, 'rb')

o = 0
while args.l > 0:
    d = f.read(args.b)
    n = len(d)
    if n == 0:
        break

    print(('{:08x} : ' + n * '{:02x} '
                       + (args.b - n) * '   '
                       + ': '
                       + n * '{:c}').format(o, *d, *[c if c in p else ord('.') for c in d]))

    if n < args.b:
        break
    o += args.b
    args.l -= 1

f.close()
xanaki
Level 1
Level 1
Posts: 15
Joined: Fri Sep 17, 2021 8:40 am
Location: Finland
Contact:

Re: What To Do To Learn C & C++

Post by xanaki »

Sorry for the C :) I know the difference. I just don't edit the topics when I reply to messages.

I don't have my implementation of that Hextype I implemented when I trained Python during summer time. It was done at work as a part of induction so it's their possession now. o.O.

I'll check your implementation later but it at least looks pretty compact. I remember my implementation was longer :)

EDIT: I removed the C part from my message as as you said I only spoke about C++ in my message. <3
Bring me a shrubbery, Shrubbery of Mint. Ni!
From Finland with love and liquor since 1972.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: What To Do To Learn C & C++

Post by rene »

Thanks; I always feel it a fairly important point to make. C++ by name gives the impression of being C and a bit but, yeah, no, it absolutely is not. It's C and a shitload at least, and more realistically and when used as intended, a shitload sprinkled with a bit of C-inspired syntax. People aiming to learn either should I feel explicitly elect for either.
xanaki
Level 1
Level 1
Posts: 15
Joined: Fri Sep 17, 2021 8:40 am
Location: Finland
Contact:

Re: What To Do To Learn C & C++

Post by xanaki »

My original point is :

Be creative

Make a target what you want to do and then do (small) steps towards to your goal. You all know, the goal is less important to the journey. Still the Journey gives you most of the kicks.

Follow my tutorial with ANY programming language and you will have a good understanding how things work under the hood.

My tutorial project is here viewtopic.php?p=2067029#p2067029
Bring me a shrubbery, Shrubbery of Mint. Ni!
From Finland with love and liquor since 1972.
legacypowers
Level 4
Level 4
Posts: 270
Joined: Sat Dec 19, 2020 8:53 am

Re: What To Do To Learn C & C++

Post by legacypowers »

Since lots of good suggestion were already made, i would like to add a simple hint, knowing the language is not the whole thing that you need to develop
Logic and algorithm is a really important factor, looking at a problem, and develop an algorithm to solve the issue is important, as a matter of fact, from experience, those kind of things are teached at college before any programming language, i'm not saying that those colleges are mandatory, or are in fact good, on my experience i did those at college with ease(i code since i was about 14).
like this logic to calculate a factorial of a number for example
Step 1: Start
Step 2: Declare Variable n, fact, i
Step 3: Read number from User
Step 4: Initialize Variable fact=1 and i=1
Step 5: Repeat Until i<=number
5.1 fact=fact*i
5.2 i=i+1
Step 6: Print fact
Step 7: Stop
Seems like a chore and tedious and long to do at first, but with time and experience you do it in your head automatically in a matter of seconds.
Terminal - zsh wrote: ╭─legacy@forums.linuxmint.com
╰─➜ _
ARC1450
Level 1
Level 1
Posts: 3
Joined: Fri Dec 20, 2019 12:35 pm

Re: What To Do To Learn C & C++

Post by ARC1450 »

I started with the Kernighan and Ritchie book around 8 or 10 years old (so 1988 to 1990). Before that I was doing BASIC on a TRS-80. If you like books, pick up a book (I suggest K&R, but it's a bit dated). Hit up YouTube if videos are more your style. Either way, figure out how you're wanting to code. Do you want to use an IDE or just a text editor? IDEs can be clunky, but helpful because they tie everything together. Text editors can be sleek, but very sparse and may require you to run the compiler/linker/debuggers on your own.

Beyond just learning C, you'll also want to understand logic flow for a program as well as how computers work unless you're just programming for fun. You'll also want to figure out what you want to write. Programming is about problem solving, and honestly C isn't the best tool at times. You'll also discover via Google most people have had similar problems and solved them before (so why re-invent the wheel if you don't need or want to do that). I've been messing around with Linux since 2000 and over that time, I've only had a handful of times where a little Google-foo didn't produce a program or script written by someone else. All of those times where I couldn't find something pre-written, C was *not* the tool to do that job (it was usually a combination of BASH and Perl/Python.

As others have said, pick one of C or C++. Don't confuse them for the same language; they're relatives and that's about it.
User avatar
act
Level 1
Level 1
Posts: 26
Joined: Thu Sep 23, 2021 1:19 am

Re: What To Do To Learn C & C++

Post by act »

Well firstly, I'd recommend that you understand that programming is simply applied math and logic. If you don't have a very solid grasp on those two concepts, then maybe programming isn't the best choice of using your spare time.
Now, firstly ignore the Python guys. Python in the end may teach you a little bit about programming altogether, but it falls short in establishing how to deal with the inevitable complexities of a language. Not to mention it's extremely slow, and so as someone who can't afford a beefy rig, it just makes me butthurt. When it comes to learning a, kinda "higher study" type thing like programming, you need to adapt a more Hindu philosophy where you believe that the reward is not the outcome, but the simple fact that you're doing something.
Anyways you should consider reading through "The C Programming Language" by Dennis Ritchie and the other guy that I can't be bothered to find out his name. It'll help you learn the basics of the language and how to use the standard library to a good potential. From there, you can go and do anything you want. As long as you're willing to learn, the world will be your oyster.
And this mostly applies to C. I don't use C++.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: What To Do To Learn C & C++

Post by rene »

act wrote: Sat Jan 22, 2022 3:37 pm Now, firstly ignore the Python guys. Python in the end may teach you a little bit about programming altogether, but it falls short in establishing how to deal with the inevitable complexities of a language. Not to mention it's extremely slow [ ... ]
I'll note that this is very much not the case. Python is a serious, full and complete programming language and any complexity you'd not deal with in Python was, apparently then, not al that inevitable. Python supports multiple programming paradigms -- plain old procedural, object-oriented, (some) functional -- and may be considered a super-set of many other languages in that sense and others, certainly of C. What you "miss out on" is e.g. latter's manual memory management but try and find any modern not garbage-collected language. I.e., "miss out on" something in the end not inevitable at all.

Don't get me wrong, I personally tend to enjoy C more as well but that's just an age/experience/change-resistance thing; for most any ordinary modern purpose it really doesn't matter one whit if you have say C, Python or shell wait 100 microseconds for data coming in from the network, and for most non-ordinary purposes algorithmic complexity rather than programming language decides 99% of runtime; it's not for nothing that Python is these days the by far most widely deployed tool for numerical-mathematics goals. I.e., big number-crunching.

C has its uses (one of which can be writing Python extensions for a few time-sensitive parts of one's Python code...) but they are in these days of multi-gigahertz CPUs and slow "world-wide" data few and far between, and a few dozen times over as a first language: as mentioned somewhere above, mandatory attention to detail in C takes away time/focus from attention to general programming constructs / algorithm and latter is what a person's first language should concentrate on more than e.g. manual pointer juggling.

C has as said its uses and learning it makes sense especially on UNIX/Linux and/or for the historical perspective, but as an actual tool its time is over other than for low-level code and/or very specific parts of code. Much like the time of those of us that once learned it as state of the art is over. Deal with it...
Locked

Return to “Programming & Development”