Sign in to follow this  
johnmarinelli

How can I gain a deeper understanding of C/C++?

Recommended Posts

snowmanZOMG    1205

This is probably a little extreme, but in addition to just programming a lot and learning from other programmers, you really should try to learn about issues in programming language design and compilers, and should at least build a compiler.  It provides huge insight into why programming languages are the way they are, and that's a huge advantage.

Share this post


Link to post
Share on other sites
mychii    898

Do a lot of programming, and make it bigger and bigger too see how repetitive your code could become that you finally learn how to make stuff generic (generic programming). If you finally end up wanting to read about patterns and architecture books, that's when you realize you're getting deep. It won't be about C++ anymore, it's about the concepts, and only comes up when you face real problems every day based on your requirements.

 

Learning from other programmers make it quick as everyone always have opinion on the way they code, as coding doesn't always about right or wrong when it comes to patterns or architecture, it's what's fit on your current problem.

Edited by mychii

Share this post


Link to post
Share on other sites
Pufixas    1167

Some of the advices might be true here. But if you really want to understand how programming and computers really work together, you should learn Assembly language, at first you might be scared of it, because a lot people think it's the holy grail of programming languages, and they think that only best of the best can learn it.

 

While it might have some some truth to it, but even knowing a little bit of Assembly will help you A LOT! I was learning assembly for a month or so (I just really wanted to try it out), and I already understand better how linker works, how everything is compiled, how exactly stack works, where passed arguments go, memory management, and all that stuff. I can even write my own simple x86 kernel!

 

So if you want to get started, download or buy a book called "Assembly Language Step by Step". It is meant for total newbies who want to learn Assembly as a first language, and he has some good points on why one should learn Assembly as first language, and not Python or Java as other people tell you.

I promise, you will gain a better understanding of programming langues and deep C. 

Afterwards you might even want to disassemble your C code and read it in Assembly to  better understand how things work inside.

 

Also, I checked out those slides, I saw few parts where it talked about registers, data segment, and other stuff I wouldn't probably have known before reading book on assembly.

 

So go learn some Assembly and good luck with your learning! smile.png

Edited by Edvinas Kilbauskas

Share this post


Link to post
Share on other sites
Khatharr    8812

Some of the advices might be true here. But if you really want to understand how programming and computers really work together, you should learn Assembly language, at first you might be scared of it, because a lot people think it's the holy grail of programming languages, and they think that only best of the best can learn it.

 

Ah, yep. Saw the title, read the post, scrolled down and someone beat me to it.

 

If you want a deep understanding of what's going on under the hood, then take the hood off. Learn assembly and start using the disassembly view in your debugger to see what your compiler does in order to implement things. It's fun, it's hands-on, and it will change the way you look at the code you write.

Share this post


Link to post
Share on other sites
Nicholas Kong    1535

A deeper understanding requires a lot of questioning.

 

Use the 5 W's (What, Who, Where, When , Why) and 1 H (How?)

 

A big part of programming is problem-solving. Find the cause and diagnose the problem. Figure how to solve it.

Share this post


Link to post
Share on other sites
alvaro    21246

I didn't mod you down, so I can only guess what part of your post bothered others. What do you think "syntactical" means? The syntax of C is very easy to learn, and C++'s syntax is not all that hard to learn either. The tricky parts of the language are primarily about things like what is guaranteed behavior by the standard, how do compilers actually implement it, and how have these things evolved.

 

Using libraries written by others is a great way to learn how to write clean interfaces, which is a very good thing indeed, but it has little to do with this thread.

Share this post


Link to post
Share on other sites
alvaro    21246

If you like the type of things in the video, what happens when you compile this C program?

#include <stdio.h>

int main(void) {
  char a[2] = "42";
  printf("%c%c\n", a[0], a[1]);
}

 

What does this C++ program do?

#include <iostream>

int main() {
  volatile char const *s = "Hello,world!";
  std::cout << s << '\n';
}

Share this post


Link to post
Share on other sites
Nicholas Kong    1535

The best way to learn new syntactical concepts is to use libraries written by others.

syntax is good to know but in terms of deeper understanding it realy means understanding the underlying language of C++. What is really going on is the question.

Edited by warnexus

Share this post


Link to post
Share on other sites
King Mir    2490

What does this C++ program do?
 

#include <iostream>

int main() {
  volatile char const *s = "Hello,world!";
  std::cout << s << '\n';
}

Might be better if fewer people knew this one. Volatile variables are rarely useful, except in specific fields, and that's a bad example of their use. It's also something that can easily be learned when it is encountered as opposed to being part of a rounded C/C++ knowledge base. If it weren't for its contagious semantics, volatile would be better off in a rarely used c header, like va_args and friends.

Share this post


Link to post
Share on other sites
alvaro    21246

What does this C++ program do? 

#include <iostream>

int main() {
  volatile char const *s = "Hello,world!";
  std::cout << s << '\n';
}
Might be better if fewer people knew this one. Volatile variables are rarely useful, except in specific fields, and that's a bad example of their use. It's also something that can easily be learned when it is encountered as opposed to being part of a rounded C/C++ knowledge base. If it weren't for its contagious semantics, volatile would be better off in a rarely used c header, like va_args and friends.

I agree. I like it as an example of dark corner of the language. g++ happily prints a 1 and doesn't even warn you about the conversion to bool that nobody expects.

The other snippet is a subtle difference between C and C++ that few are aware of.

They are both just curiosities.

Share this post


Link to post
Share on other sites
TheComet    3896

 

 

What does this C++ program do? 

#include <iostream>

int main() {
  volatile char const *s = "Hello,world!";
  std::cout << s << '\n';
}
Might be better if fewer people knew this one. Volatile variables are rarely useful, except in specific fields, and that's a bad example of their use. It's also something that can easily be learned when it is encountered as opposed to being part of a rounded C/C++ knowledge base. If it weren't for its contagious semantics, volatile would be better off in a rarely used c header, like va_args and friends.

I agree. I like it as an example of dark corner of the language. g++ happily prints a 1 and doesn't even warn you about the conversion to bool that nobody expects.

The other snippet is a subtle difference between C and C++ that few are aware of.

They are both just curiosities.

 

Would someone mind explaining to me why it's converted to a bool?

Share this post


Link to post
Share on other sites

Google fu (volatile bool conversion)

 

http://stackoverflow.com/questions/2501737/why-does-stdcout-convert-volatile-pointers-to-bool

 

There is no volatile overload for char* however all pointers can be implicitly converted to bool (and there is no conversion from volatile pointers to void* either, for type safety reasons), in a nutshell.

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites
SeraphLance    2603

I'm actually more curious about the other one, as my google fu seems weak today.

 

At first (after trying to compile) I thought that C just ignored everything after the array size, but then I made the literal longer and it broke.  Then I thought "oh, it probably allocates an extra byte for the null char", but printing sizeof(a) yields 2.  So... uh...  yeah, what the heck causes that?

Edited by SeraphLance

Share this post


Link to post
Share on other sites
TheComet    3896

Perhaps I'm missing the point, but I took that program and it seems to do what I expected it to do.

#include <stdio.h>

int main(void)
{
    char a[2] = "42";
    printf("%c%c\n", a[0], a[1] );
}
$ cc -W -Wall -Wextra -o test main.c
main.c: In function 'main':
main.c:7:1: warning: control reaches end of non-void function [-Wreturn-type]
$ ./test
42
$

I note that the array should have 3 elements to hold the null-terminator, and the main function is missing a "return 0;". Is there anything else I'm missing?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this