Live data from Hacker News

Dennis Ritchie's first C compiler on Github

github.com

61–70 of 88 posts

Re: Dennis Ritchie's first C compiler on Github

#61
post #28

Earlier quoted context omitted.

Never using a space before the paren is more consistent. I don't know why people put a space there...

I don't want my function calls to be consistent with control structures.

I don't recall ever mistaking function calls with control structures.

Re: Dennis Ritchie's first C compiler on Github

#62
post #57

I don't understand this main(argc, argv) int argv[]; { Is that still valid today?

Function declaration for main. The type declarations for the function parameters default to int, but can be specified outside of the parens before the curly brace.

The int argv[]; is where the declaration for argv is happening and is being declared as an pointer for ints.

You can also see elsewhere in the code where they are passing pointer addresses (as int params) into functions and then using the address to build pointers referencing that data.

Re: Dennis Ritchie's first C compiler on Github

#63
post #57

I don't understand this main(argc, argv) int argv[]; { Is that still valid today?

It's a holdover from Fortran (and probably from before that). Whereas modern day we would define functions as

    int foo(int i, int j) {...}
in Fortran you would do (! is comment)

    function foo(i, j)
        integer :: foo !return type
        integer :: i
        integer :: j
        !body goes here
Early C stuck to that style, so you would just put the names of the variables in the declaration, and then before the body give them types. The reason only argv is mentioned in that example is that C assumes a variable is an int if not declared otherwise, so there's no reason to put "int argc" like "int argv[]".

Re: Dennis Ritchie's first C compiler on Github

#64
post #57

I don't understand this main(argc, argv) int argv[]; { Is that still valid today?

Yes it is. Originally that's how you specified parameters: just the name in the parentheses, then (optionally) the types in a format similar to variable declarations before the function body. If you didn't specify a type it would default to int, so argc above would be an int.

All modern C compilers still accept this style for backwards compatibility. I'm not sure about C++ compilers.

Re: Dennis Ritchie's first C compiler on Github

#65
post #48

He already used the right brace style (hanging braces, cuddled else) and the right indentation (tabs, not spaces).

tabs are an infinite source of pain and inconsistencies... Everyone must support the space character, it cannot be banned. But a simple commit hook to ban tabs can make indentation and alignment not get messed up over time with many collaborators with default-configured editors (that mess up and use tabs for alignment).

The inventor of the tab character is on my list of people to assassinate when they're young if I ever get access to a time machine, the other people on the list being Hitler and Charles Douglass.

The tab character is a nice idea, but they do not seem to have worked out at all. I'd much rather have syntax-aware indenting in the editor, now that available compiler technology and CPU power make it practical.

Re: Dennis Ritchie's first C compiler on Github

#66
The declaration of printf is both scary and pretty cool.

What happens when you have more then 9 substitutions specified in the string? :D

edit: decided the code was a bit long to have pasted into my post. Can find it at the bottom of http://cm.bell-labs.com/cm/cs/who/dmr/last1120c/c03.c

Re: Dennis Ritchie's first C compiler on Github

#67

I'm ashamed for having to google his name but for others like me here's a glimpse: "Dennis MacAlistair Ritchie (born September 9, 1941; found dead October 12, 2011) was an American computer scientist who "helped shape the digital era." He created the C programming language and, with long-time colleague Ken Thompson, the Unix operating system" http://en.wikipedia.org/wiki/Dennis_Ritchie

You should have seen HN when he died. It was that hell week of a bunch of early computer pioneers all passed away...

Re: Dennis Ritchie's first C compiler on Github

#68
post #65
post #48

Earlier quoted context omitted.

tabs are an infinite source of pain and inconsistencies... Everyone must support the space character, it cannot be banned. But a simple commit hook to ban tabs can make indentation and alignment not get messed up over time with many collaborators with default-configured editors (that mess up and use tabs for alignment).

The inventor of the tab character is on my list of people to assassinate when they're young if I ever get access to a time machine, the other people on the list being Hitler and Charles Douglass. The tab character is a nice idea, but they do not seem to have worked out at all. I'd much rather have syntax-aware indenting in the editor, now that available compiler technology and CPU power make it practical.

I am just thinking, why not solve this at the scm level, e.g. make a git plugin that will check out the sources indentation independent and only show changes to contents of the file not the indentation.

You could even make a plugin that works without collaboration by others: check out the file in your preferred style and transform the changes back into the original style.

Re: Dennis Ritchie's first C compiler on Github

#70
post #57

I don't understand this main(argc, argv) int argv[]; { Is that still valid today?

It's called a K&R style function definition, which was the way to do it back in the day. Basically, you define your parameter names first, then you define the parameter types immediately after the function but before the opening curly brace. It's definitely not recommended today and can result in undefined behavior if your compiler doesn't recognize it. If you're working with legacy code, though, I'm pretty sure you can set some C compilers to allow for it.

To explain further:

    main(argc, argv)
    int argv[]; {
is equivalent to:

    int main(int argc, int argv[]) {
The old style definition works because C had a default type of int, so the type specifications for the function main and the parameter argc could be omitted.

As for int argv[]? What that actually represents is an array of memory addresses that hold the command line arguments given. Obviously this becomes a problem if you're on a 64-bit system, where int and (void * ) are two different sizes. However, I checked this out on my 64-bit machine and it works just fine:

    int main(int argc, unsigned long long argv[]) {
      char *firstarg = (void *)(argv[1]);
      printf("%s", firstarg);
    }
which, given "./a.out pickles" will print "pickles" (argv[0] gives the memory address of the cstring "./a.out"). I'm guessing that, in the case of a compiler, the memory addresses of arguments are more relevant to have than the arguments themselves.
Post reply on HN