7 ms·
Why does this C program compile?
- SparrowOS 14y agoFucken retard -- told you I made a compiler from scratch. Everything from scratch, all 138,000 lines.
- jbuzbee 14y agoGuess 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?"
- gburt 14y agoYeah, I definitely expected code with missing braces/brackets or something. This is pretty clear why it compiles.
- minimax 14y agoIf 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. ;-)
- hnriot 14y agoHe 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...
- asveikau 14y agoThis 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?
- Retric 14y agoIt 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.
- greenyoda 14y agoYou 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.26R_C https://en.wikipedia.org/wiki/C_%28programming_language%29#K...
- kelnos 14y agoThing 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.
- Retric 14y agoSearching 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 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.
- fayden 14y agoWhy 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.
- jbuzbee 14y agoYeah :-) 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!
- cube13 14y agoIt'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".
- Ogre 14y agoI 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.
- jlgreco 14y agoCompilers from the CD-ROMs in the back of ancient programming books from libraries maybe?
- cube13 14y agoIt 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.
- Someone 14y agoI 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.
- rnadna 14y agoMe too. And I didn't realize that made me old, until scanning this thread. I guess time flies.
- kelnos 14y agoHah, 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!)
- minimax 14y agoC++ programmers will be familiar with a related (and probably more commonplace) problem: struct Foo {int a;}; int main(int argc, char **argv) { Foo foo(); foo.a = 42; return 0; } Looks ok, right? Wrong. error: request for member 'a' in 'foo', which is of non-class type 'Foo()'
- hnriot 14y agono, it doesn't look ok. You need to remove the () in your declaration. You need to take a look at how constructors work in c++
- Dylan16807 14y ago>you need to take a look I think you missed the point of the post. http://en.wikipedia.org/wiki/Most_vexing_parse http://en.wikipedia.org/wiki/Most_vexing_parse
- cube13 14y agofoo()-with the parenthesis- is a function. foo-without the parenthesis-is a variable. That means that: Foo foo(); is a function pointer declaration within the scope of the main function. Since it's a function, which does not have member types, a is not within the scope of Foo(). Now, if there was a 0 argument constructor for struct Foo, this would be valid: Foo foo = Foo();
- kelnos 14y agoFunction declaration, not function pointer declaration. The latter would be Foo (*foo)();
- jfaucett 14y agothis is pretty basic, I would hope no actual c programmers would ask this question. Compilers are fun aren't they, how bout this one: #include <stdio.h> #define yell(i,l,o,v,e,u) o##v##i##l #define billy_bob yell(i,n,m,a,t,e) billy_bob() { printf("no main in da house!"); }
- ntumlin 14y agoWould you please explain the code?
- deleted 14y ago[deleted]
- kevinnk 14y ago>#include <stdio.h> >#define yell(i,l,o,v,e,u) o##v##i##l defines a macro called yell. The preprocessor operator ## concatenates it's operands together so something like ma##in gets transformed into main. >#define billy_bob yell(i,n,m,a,t,e) defines a macro called billy_bob that simply expands to the macro yell. When we call yell with the given arguments we get m##a##i##n (look at the order of the arguments for yell) which gets transformed into main. >billy_bob() Expanding the macro turns this line into main(). >{ > printf("no main in da house!"); >} So now we've declared a main function that just prints "no main in da house!".
- dubya 14y agoHint: the 't' and 'e' in yell(i,n,m,a,t,e) are irrelevant.
- cnvogel 14y agoThere's the "##" operator with which you can glue together identifiers with the preprocessor. #define yell(i,l,o,v,e,u) o##v##i##l yell(i,l,o,v,e,u) will turn into the identifier ovil. And billy_bot just uses the yell-macro to produce a "main" identifier. #define billy_bob yell(i,n,m,a,t,e) yell(i,l,o,v,e,u) --> o ## v ## i ## l yell(i,n,m,a,t,e) --> m ## a ## i ## n So billy_bop is main. Why is this features useful at all? When you actually want to programmatically create function names, type names, struct names in preprocessor generated code. #define ENCAPS_STRUCT(type,name) struct encaps_##type { type name; } ENCAPS_STRUCT(int,a); will generate... $ cc -E test.c # 1 "test.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "test.c" struct encaps_int { int a; };
- bitwize 14y agoAn empty parameter list in a C prototype means "parameters are unspecified". In C++ it means no parameters. To declare a C function parameterless you need to say int func(void). Also, C will assume int if you leave off param or return types. ...I'm surprised I know that.
- bjoe_lewis 14y agoIs it a bad code, or otherwise?
- johnbellone 14y agoI would say that it is ambiguous code. The first C project that I worked on used this a lot. Its definitely not the easiest to understand when coming from a C++ background (because no parameter declaration means no parameters) or from other descendant languages.
- kelnos 14y agoIt's actually not ambiguous code at all. It's perfectly well-formed and unambiguous with respect to the C standard. What it is, however, is hard-to-read and -maintain code.
- debacle 14y agoIt's an older code, but it checks out.
- klibertp 14y agoIt is a bad code. It was quite ok. This is another proof that as programmers we need to learn and improve constantly :)
- jussij 14y agoI think it's a case of not having the compiler warning level set to it's highest level (which it should be) or the bad habit of some programmer's ignoring warning messages.
- Yoni1 14y agoThis is kind of an encouraging question. To me it screams "people don't know C anymore, capitalize on your skill while you still can".
- spudlyo 14y ago/* Pentium killer */ long main = 0xc8c70ff0;
- TeMPOraL 14y agoThat's why I always tell people that gcc is mostly useless without --ansi --pedantic -Wall.