简体   繁体   中英

Original C hello, world program from Kernighan and Ritchie not compiling

I'm trying to learn C using the Kernighan and Ritchie book (The C Bible). Upon trying to compile the first problem using tcc and MinGW (using Windows). It gave me an error message. The most detailed one came from Min GW: helloworld.c:3:8: error: expected ')' before '(' token

Here is my program:

main()

(

    printf("hello, world\n");

)

As far as I can tell it follows the book to the dot. Is this now out of date? I've looked for this and still can't find what I've done wrong. Please help.

Thanks in advance!

( blahblah; ) is not the same as { blahblah; } { blahblah; } , and your book might be a bit outdated (though the code should be fine to learn from, even if some requires changes). Current standard C would be

#include <stdio.h>
int main(void)
{
    printf("hello, world\n");
    return 0;
}

main(void) could also be main(int argc, char *argv[]) if you want to read command line arguments.

You are mistakenly using ( and ) instead of { and } . And presumably the code in the book has a #include statement in order to declare printf .

You used parentheses instead of brackets. It should be

#include <stdio.h>

main() {
    printf("hello, world\n");
}

The symbols that begin and end a function's body are curly braces, not parentheses. It should look like:

main()
{
    printf("hello, world\n");
}

That said, this is indeed woefully out of date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM