Live data from Hacker News

Getting started with C

not.cafe

51–60 of 106 posts

Re: Getting started with C

#51

This is a very strange "getting started" guide to C, and I'm not sure who the intended audience could possibly be. The tone at the start really does seem to be aimed at "absolute beginners" ("[...] programs must be written in a very strict way. Miss a comma and the program will stop working"), but then it quickly gets into the weeds without much explanation. I'm trying to imagine an beginner reading this: The first t…

Wrt "the first line loads the stdio.h function library, which provides the printf() function", you're right, "loading a library" is not clear and, what's worse, is actively misleading as actually we're just telling the compiler that there is a function somewhere called printf that will get "loaded" (linked) later on. I remember that this was one of the things that confused me most about C when I learned it.

We're really just telling the compiler to find a file called stdio.h and act as though its contents were included at that part in our program.

Whether the full function is there or linked later is a matter of implementation (some headers have full functions, typically trivial "inline" functions).

I can't remember what the standard says via the stdlib, but in general a library you load later on may not have all/some of those functions, but as long as you didn't use them you'll be fine (just as with a redundant forward declaration you yourself did).

And finally the whole "linking" part of C is mostly just a widespread convention, not something that the standard really dictates. See section 6.2.2 [1] for the brief words on it mainly to do with "static"/"extern". But the whole "compile" -> "link" (to some existing .so/.a) -> "program" is all implementation defined.

1. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

Re: Getting started with C

#52
Christ. What is this nonsense, and how did it get into the front page.

If by any chance you reading this and want to learn C, do obtain and read The C Programming Language 2nd edition instead.

Re: Getting started with C

#53
post #51

Earlier quoted context omitted.

Wrt "the first line loads the stdio.h function library, which provides the printf() function", you're right, "loading a library" is not clear and, what's worse, is actively misleading as actually we're just telling the compiler that there is a function somewhere called printf that will get "loaded" (linked) later on. I remember that this was one of the things that confused me most about C when I learned it.

We're really just telling the compiler to find a file called stdio.h and act as though its contents were included at that part in our program. Whether the full function is there or linked later is a matter of implementation (some headers have full functions, typically trivial "inline" functions). I can't remember what the standard says via the stdlib, but in general a library you load later on may not have all/some o…

Yes, one of the confusing things for C newbies is the difference between "works this way on almost all implementations you are likely to encounter" and "actually part of the language". Both provide great opportunities for either the compiler or stackexchange answerers to make the newbie feel inadequate.

Re: Getting started with C

#54

This is a very strange "getting started" guide to C, and I'm not sure who the intended audience could possibly be. The tone at the start really does seem to be aimed at "absolute beginners" ("[...] programs must be written in a very strict way. Miss a comma and the program will stop working"), but then it quickly gets into the weeds without much explanation. I'm trying to imagine an beginner reading this: The first t…

Maybe they meant an absolute beginner to C and not programming in general.

Re: Getting started with C

#55
Can AI generate a better tutorial?

To test my theory, I asked the OpenAI API to complete the following sentence:

    This tutorial will guide you through writing the "Hello World" program in the C programming language.

And here's the result:

    This tutorial will guide you through writing the "Hello World" program in the C programming language.
    
    Hello World Program Structure

    The "Hello World" program is a very simple program. It consists of two parts: the main function and the printf function. The main function is the first function called when your program starts running. It is responsible for starting all other functions and initializing everything that needs to be initialized before your program starts doing anything useful. The printf function is responsible for printing text to the screen. In this case, it prints "Hello World!" to the screen.
    
    The main Function
    
    The main function must have one argument which is an integer that represents how many arguments were passed into it from the command line (if any). The following code shows a simple main function:

    #include 
    int main(int argc, char *argv[]) {
        printf("Hello World!n");
        return 0;
    }

    This code will print "Hello World!" to the screen when executed.

    Let's go over each line of this code:

    #include 
    This line tells the compiler to include stdio.h in our program so we can use its functions (like printf ).

    int main(int argc, char *argv[])
    This declares a new variable called main . It has two arguments: argc and argv . These are special variables that are used by C programs to pass information into their functions from their command line arguments (if any). We'll talk more about them later on in this tutorial when we cover how to use command line arguments with your programs. For now, just know that they're there so we can use them later on if we need them! ;-)

    printf("Hello World!n");
    This line calls our friend printf . It tells it to print "Hello World!" followed by a newline character (the n at the end).

    return 0;
    This line returns 0 back into our calling environment (in this case, it's our shell). If you don't know what returning values means or why you would want to do it, don't worry about it for now; just know that every C program must return a value back into its calling environment when it finishes running (we'll talk more about this later on in this tutorial).
Pretty good, eh? (I just formatted the text for clarity.)

Re: Getting started with C

#56

The article uses nano throughout. Is nano a popularly used editor? I have not seen anyone around using nano. I see vim, atom, sublime, vs code but never nano. Just curious if nano is popular. If nano is popular what are its pros and cons?

to be honest nano is the best thing in this article, everything else is just disaster, nano is at least light weight and functional (imagine using an IDE for this)

Re: Getting started with C

#57
post #48
post #25

That escalated quickly, poor beginners :').

Exactly! The author assumes the reader doesn't know what a terminal is, and goes from a "hello world" to a cross-platform graphical "hello world" using GTK-3 without explanation. "Trust me, I don't have time to explain!"

You can teach anyone anything in ten minutes if you don't care about understanding or retention!

Re: Getting started with C

#58

This is a very strange "getting started" guide to C, and I'm not sure who the intended audience could possibly be. The tone at the start really does seem to be aimed at "absolute beginners" ("[...] programs must be written in a very strict way. Miss a comma and the program will stop working"), but then it quickly gets into the weeds without much explanation. I'm trying to imagine an beginner reading this: The first t…

Maybe they meant an absolute beginner to C and not programming in general.

I definitely agree that's possible, but then they go over what a terminal is and what the "ls" command means, and how case-sensitivity and using commas in the correct places are important when programming, which strike me as things which would be assumed knowledge if the audience were expected to have any programming experience.

Re: Getting started with C

#59

This is a very strange "getting started" guide to C, and I'm not sure who the intended audience could possibly be. The tone at the start really does seem to be aimed at "absolute beginners" ("[...] programs must be written in a very strict way. Miss a comma and the program will stop working"), but then it quickly gets into the weeds without much explanation. I'm trying to imagine an beginner reading this: The first t…

Reminds me exactly of my attempt at C tutorial:

1. Aim for newbies. Vastly under estimate the amount of explaining that must entail

2. Peter out somewhere between the first and second real code example

Post reply on HN