Live data from Hacker News

Where to begin when learning C? Start by making lots of errors

morganwilde.svbtle.com

41–50 of 90 posts

Re: Where to begin when learning C? Start by making lots of errors

#41
I think I have some ingrained fear of making errors, from the days where a simple error might cause your program to chew through your whole hard drive. Actually I don't know if that was ever the case, but that's how I felt.

Anyway, the hardest step for me in learning a language is when I take some running code and make one change to see what happens. Once I get in the swing of it, it gets much easier, but that first step is still hard to do.

Re: Where to begin when learning C? Start by making lots of errors

#42

By the way, I would love any feedback on this approach and where I should take this further.

As for going further staying with the bare-bones approach, I suppose you'd have to start looking at assembly output and how that fits with what the c-code does. I don't know anything about OSX x86_64 calling conventions etc -- but at least under Linux (and afaik windows) 64bits is a lot more friendly and fun than the mess that was 32bit (and 16bit) x86.

There are a couple of great (free) resources on 32bit x86 assembly I'm aware of:

http://www.drpaulcarter.com/pcasm/ http://www.plantation-productions.com/Webster/HighLevelAsm/i...

There's apparently some plans on upgrading HLA to x86_64 -- I don't know of any good tutorials or guides on working on 64bit assembly specifically I'm afraid.

Just adding "-S" and looking at the source can be helpful of course, although I much prefer nasm/intel syntax, for clang/gcc that should require:

   clang -S -mllvm --x86-asm-syntax=intel main.c
   gcc -S -masm=intel main.c
Note that gas syntax is the "default" in the gnu-world, so it might be easier to just go with that if you're just starting out.

It looks like clang might be generating less "noise" for tiny trivial programs, here's a side by-side-diff (in intel syntax) of int main{} vs int main { return 0;} (slightly reformatted):

    diff --side-by-side main.s main.no-ret 
      .file   "main.c"                          .file   "main.c"
      .text                                     .text
      .globl  main                              .globl  main
      .align  16, 0x90                          .align  16, 0x90
      .type   main,@function                    .type   main,@function
    main:                       # @main       main:          # @main
      .cfi_startproc                            .cfi_startproc
    # BB#0:                     # %entry      # BB#0:        # %entry
      mov     EAX, 0                             mov     EAX, 0
      mov     DWORD PTR [RSP - 4], 0         
It can be fun to do this with stuff like hello world (and contrast puts("Hello, world!"); with printf("Hello world!\n);).

Re: Where to begin when learning C? Start by making lots of errors

#44
post #22
post #16

This situation is why there is "hello world"; that program seems trivial but actually has important implications; for instance, that your environment works, that you know how to feed programs to the compiler, that you can actually run the output. I never took "hello world" seriously until I started doing embedded systems work; it's now an extraordinarily important tool for me (I work an anomalously weird number of di…

I once had a particularly slow week at work, and decided to write an article about how "Hello World" works in C. How it really works -- including compiling, linking, system calls, operating system involvement, hardware I/O, communication between ICs, etc. The path from the block of text to photons emitting from the screen, with fairly detailed explanations at every level. I ended up with a 50 page LaTeX document that…

I have been looking for a guide like that. I would also appreciate a release of the unfinished version.

Re: Where to begin when learning C? Start by making lots of errors

#46
post #39

Earlier quoted context omitted.

Are you trying to dispute the fact that `main()` is THE entry point for any C program? I'd be interested in hearing a more comprehensive explanation.

To some extent, _start is ( https://blogs.oracle.com/ksplice/entry/hello_from_a_libc_fre... ), so you can make a working program that has no main(). And in general entry points are a quirky land heavily dependent on OS and compilation stack.

A C program starts at the function "main", what you're discussing here is beyond the scope of the C language. C without the libc or any other form of runtime is not standard C.

I'm not trying to nitpick but I'm worried newcomers to the language might be misled by your comments, the things you're talking about are not a concern for most coders unless they have to do things like low level embedded code, bootloaders and things like that. And then your entrypoint won't be "start" anyway, it'll be the reset vector or some lower stage jumping to a specific address for instance.

Re: Where to begin when learning C? Start by making lots of errors

#47
post #33
post #7

I learned C by reading K and R and doing the book's exercises . . . on paper. At the time I didn't have access to a C compiler, so I wrote them all out in a notebook. A month later I got a job at a shop that was running Unix, and got the chance to type my programs in and try them. I had a lot of things wrong. It took me a while to understand the difference between control-D and EOF, for instance (how embarrassing). B…

People don't like K&R? I can understand it not being suitable for children and the very beginner, but it's one of a very few programming books that is useful, succinct, pragmatic, doesn't get bogged down in APIs, and is written clearly. (The companion book "Programming in the UNIX environment" is the get-you-started guide from the very beginning, although it assumes 1970s terminal defaults)

K&R is the best second C programming book.

Re: Where to begin when learning C? Start by making lots of errors

#48
post #22
post #16

This situation is why there is "hello world"; that program seems trivial but actually has important implications; for instance, that your environment works, that you know how to feed programs to the compiler, that you can actually run the output. I never took "hello world" seriously until I started doing embedded systems work; it's now an extraordinarily important tool for me (I work an anomalously weird number of di…

I once had a particularly slow week at work, and decided to write an article about how "Hello World" works in C. How it really works -- including compiling, linking, system calls, operating system involvement, hardware I/O, communication between ICs, etc. The path from the block of text to photons emitting from the screen, with fairly detailed explanations at every level. I ended up with a 50 page LaTeX document that…

This sounds like an outstanding Kickstarter project.

Re: Where to begin when learning C? Start by making lots of errors

#49
post #7

I learned C by reading K and R and doing the book's exercises . . . on paper. At the time I didn't have access to a C compiler, so I wrote them all out in a notebook. A month later I got a job at a shop that was running Unix, and got the chance to type my programs in and try them. I had a lot of things wrong. It took me a while to understand the difference between control-D and EOF, for instance (how embarrassing). B…

That's great. I learned C by telling people I had C experience then when I got a job offer I was forced to learn it pretty quickly over a weekend powered by coffee and yorkie bars and a pirate copy of Microsoft C. I had plenty of experience with assembly at the time.

fortunately I wandered into a company that had even less of a clue than me. It's nice to be an expert on day one with only two days' of expertise :)

I learned a hell of a lot in a year. Great year.

Re: Where to begin when learning C? Start by making lots of errors

#50
post #46
post #39

Earlier quoted context omitted.

To some extent, _start is ( https://blogs.oracle.com/ksplice/entry/hello_from_a_libc_fre... ), so you can make a working program that has no main(). And in general entry points are a quirky land heavily dependent on OS and compilation stack.

A C program starts at the function "main", what you're discussing here is beyond the scope of the C language. C without the libc or any other form of runtime is not standard C. I'm not trying to nitpick but I'm worried newcomers to the language might be misled by your comments, the things you're talking about are not a concern for most coders unless they have to do things like low level embedded code, bootloaders and…

Sure, I'm only trying to criticize the idea of learning language from error messages triggered by invalid (due to standards) input, as they directly take you to implementation details and other dirty stuff.
Post reply on HN