News for hackers. Stuff that matters.
Re:Why does this C program compile? by jbuzbee on Wednesday December 19, 2012 @01:12PM (#4943846)
Guess I grew up with old-school C. Looking at the question with example code made me think: "Am I missing something? Looks like pretty ordinary C to me. Why wouldn't it compile?"
Re:Why does this C program compile? by gburt on Wednesday December 19, 2012 @01:15PM (#4943873)
Yeah, I definitely expected code with missing braces/brackets or something. This is pretty clear why it compiles.
Re:Why does this C program compile? by minimax on Wednesday December 19, 2012 @01:25PM (#4943912)
If you were a programmer in the days before C had prototypes then you are definitely from the old school. If you're working on code that still looks like this in 2012 then maybe it's time to brush up on modern C. ;-)
Re:Why does this C program compile? by jbuzbee on Wednesday December 19, 2012 @01:31PM (#4943937)
Yeah :-) I didn't say I would -write- code like that. I only meant it looked like a lot of code I've run into over the years!
Re:Why does this C program compile? by hnriot on Wednesday December 19, 2012 @01:31PM (#4943935)
He didn't say he still works on code like this, but like me, this program is obviously fine, there's no point in asking why it works because c is backward compatible and it's pretty clear to anyone who knows c that this will work. It doesn't mean we'd write code like that...
Re:Why does this C program compile? by asveikau on Wednesday December 19, 2012 @03:21PM (#4944585)
This is what bothers me about a lot of programmer communities these days. What is a very mundane detail of K&R legacy, that C programmers know very well either from looking at old source trees or making sense of error messages, suddenly turns into something where 116 people (current stack overflow +1 count) are totally amazed and breathless that they've discovered something deep. I suppose it's fair if they just don't know C, so they've not seen it and find it interesting, but could this person's dilemma not be resolved with a simple google search? Or even taking a guess?
Re:Why does this C program compile? by Retric on Wednesday December 19, 2012 @03:59PM (#4944796)
It can be really hard to Google things when you don't know the terms involved. Until, someone posts source code and you get directed to stack overflow etc. Thus, the fact that out of millions of people with some familiarity with C 100 or 1000 chose to up vote an obvious post does not really seem that strange.
Re:Why does this C program compile? by kelnos on Thursday December 20, 2012 @12:51AM (#4946464)
Thing is, the C standard (compared to many other standards) isn't even that complicated. Googling for the C standard doc, and then grepping through for "function declaration" would likely clear that up.

But it's way easier to consult the lazyweb than to do your own research.

Re:Why does this C program compile? by Retric on Thursday December 20, 2012 @08:59AM (#4947692)
Searching for "C standard doc" does not actually work.

"C99 standard doc" let's you find http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf but searching for "function declaration" does not take you anywhere useful.

That's not to say reading the whole thing is a waste of time, just beyond the scope of most new programmers.

Re:Why does this C program compile? by greenyoda on Wednesday December 19, 2012 @11:07PM (#4946231)
You don't even have to Google it. The Wikipedia article on C talks about K&R C vs. modern C:

https://en.wikipedia.org/wiki/C_%28programming_language%29#K...

Re:Why does this C program compile? by vaxdigitalnh on Wednesday December 19, 2012 @05:43PM (#4945234)
I agree with you. Maybe the better question is why does anyone think it shouldn't compile? And then we can ask how much programming history they know.

I think it's useful to know how C compilers, the C specification and C coding styles have evolved over the years. For one, it helps put some of the arguments for/against certain styles and syntax in perspective.

In short, make an effort to learn from history before you make the minimal effort required to learn from a contemporary forum like stackexchange.

Re:Why does this C program compile? by fayden on Wednesday December 19, 2012 @04:03PM (#4944821)
Why do you assume 116 people are "amazed and breathless"? It's an interesting question and I don't believe the answer is obvious unless you've been exposed to legacy C code before, and such users may arguably constitute a large part of SO's audience.

You may be able to solve the question by a simple search, but it still fits in on StackOverflow. The fact that it was upvoted a lot is irrelevant to me, it just means a few people bothered to press a button.

Re:Why does this C program compile? by kelnos on Thursday December 20, 2012 @01:17AM (#4946523)
Maybe "amazed and breathless" is a bit hyperbolic, but I assume the parent is referring to the number of people who had up-voted it at the time of his posting.

Perhaps for people who don't really know C, that's interesting information, but anyone who calls themselves a "C programmer" should not be surprised by that bit of code (well, you should be surprised if anyone still writes code like that, but not surprised that it's valid).

It's not even just an "exposed to legacy code" thing: C is a fairly simple language in both syntax and concepts (esp. when compared to most other languages people are using today), and I don't believe you can call yourself fluent in C without this kind of knowledge.

(I could also argue from the other angle: if you haven't been exposed to that sort of legacy C code, you probably haven't done it enough to call yourself fluent.)

I think it's an interesting question from the perspective of "what are some cool language gotchas that people not familiar with the language would find neat?"... or from the perspective of someone who is new to C, came across some code like that, and is trying to learn what's going on (and is apparently too lazy to google the C specification and spend 2 minutes reading it).

Re:Why does this C program compile? by cube13 on Wednesday December 19, 2012 @01:34PM (#4943952)
It's been a while since I've done this(usually by accident), but I believe that GCC and Visual Studio both throw up a warning along the lines of "Type not defined, assuming int".
Re:Why does this C program compile? by Ogre on Wednesday December 19, 2012 @01:50PM (#4944033)
I see questions online all the time that make me wonder just how old the compiler being used is. Modern versions of GCC, Clang and Visual Studio all have warnings enabled by default for an awful lot of valid, but probably wrong, code. Apparently people learning to code are somehow winding up on old compilers much more often than those of us who have been writing C and C++ for a long time.

Or, I guess, the people having problems learning to code are the people who happen to be on ancient compilers. Maybe it's not so surprising after all.

Re:Why does this C program compile? by Someone on Wednesday December 19, 2012 @03:24PM (#4944597)
I think it is more likely something like this:

Many C programmers cannot make valid judgments as to the severity of a warning ('warning: bla bla may ...' => does it or doesn't it? I wouldn't know)

Also, those programmers typically would not know how to change their code to get rid of warnings. If they do, or if their compiler shoves a solution in their face (for example, a compiler might suggest to do "if((y=f(x)))") to get rid of an 'assignment inside if' warning), they do not see the advantage of the extra work.

The end result is code that produces hundreds of warnings. If a code change brings in a new one, people don't notice and/or don't care.

Some IDEs make things worse by allowing you to filter out warnings and messages from their output, so that you can focus on compiler errors. That is good for good programmers (they will fix errors, compile again, and then study every single warning), but bad for those who are happy to have code that compiles at all, as they will run into 'do not show me warnings' mode.

Re:Why does this C program compile? by npsimons on Wednesday December 19, 2012 @03:57PM (#4944781)
The first reaction to an unfamiliar warning in any language should be to consult the manual (or Google it). In the case of VisualStudio at least, this is trivial (these days) due to the warning numbers you can paste into Google and usually get not just an explanation, but a way to fix it (usually via SO).

Really, if a programmer can't fix warnings (and that includes taking the time to learn why they are there), what are you paying them for? My policy on projects I work on is to enable all warnings, treat them as errors, then only disable warnings after trying to fix them, researching them, and finally discussing them, and usually then only on a case by case basis (only occasionally do I eschew certain warnings altogether (such as -Waggregate-return for g++), and I leave them commented out with the reason they are disbled).

(Edit: a quick tip for gcc, BTW: use -fdiagnostic-show-option for easier search of a warning, and the name of it in case you have to pragma it out).

Re:Why does this C program compile? by marcosdumay on Wednesday December 19, 2012 @05:58PM (#4945291)
I've just tried it. That code compiles without warnings by the default configuration of gcc version 4.4.5 (Debian 4.4.5-8) in my desktop (it's an up-to-date Debian Stable install).

If the default settings of gcc included throuwing out warnings for every time a function is declared without parameters and missing the void, lots of people would complain, and loud. It's a very common construct.

Re:Why does this C program compile? by kelnos on Thursday December 20, 2012 @01:01AM (#4946481)
Wow, that's crazy. I just tried it too (gcc 4.6.3). No warning options generate no warnings at all. -Wall only warns about the lack of return statement in main(). You only get the parameter-type default warning if you add -Wextra.

Interestingly, adding -std=c99 oddly removes the warning about the missing return statement in main().

I can't find a way to warn about the empty param list in the prototype, but it's possible that it's just not considered "bad enough style" to warrant one.

C++ is, of course, more strict. Try to compile that with g++, and you get several errors. The compiler needs to know how to mangle the symbol when it first encounters it, so you can't have "unspecified" parameter lists anywhere.

Re:Why does this C program compile? by cube13 on Wednesday December 19, 2012 @01:56PM (#4944063)
It might be the default settings on the compilers as well. I believe that in our environment, we set our warning level higher than the default, because a lot of the warnings(signed/unsigned mismatch, 32/64-bit pointers, etc.) can be an indication of an actual bug.
Re:Why does this C program compile? by jlgreco on Wednesday December 19, 2012 @01:53PM (#4944053)
Compilers from the CD-ROMs in the back of ancient programming books from libraries maybe?
Re:Why does this C program compile? by kelnos on Thursday December 20, 2012 @12:49AM (#4946460)
Hah, yeah. I looked at it and thought, "well, I'd kick someone for writing code like that nowadays, but looks perfectly valid to me."

(And I'm not even that old! I've only been writing C for 12 years or so!)

Re:Why does this C program compile? by rnadna on Wednesday December 19, 2012 @05:13PM (#4945121)
Me too. And I didn't realize that made me old, until scanning this thread. I guess time flies.