>>1
Wrong. You only have to declare a function one, and define it once. Declarations go in header files, definitions in source files. A prototype is the most common form of declaration, but for functions that are only used in one file, a definition can double as a declaration.
Declaration: void foo();
Definition: void foo() { }
Declaration and Definition (assuming no other conflicting declarations): void foo() { }
Name:
Anonymous2008-10-09 22:54
>>6 is right, but the distinction is pointless and quite a regrettable design decision.
Name:
Anonymous2008-10-09 22:59
>>1
By itself, the c/c++ compiler is pretty dumb in that it has no way of knowing what functions are going to be used in your program. Additionally, it knows nothing about whether a function you've declared and used has even been defined yet, which is why you're able to simply put declarations in header files, #include them and everything just works.
The compiler's job is to turn your source file into an ``object file'' which contains partial machine code and reference stubs whereever a function is used.
These object files are then given to a second program called a linker, whose job is to take a bunch of these object files, resolve all of those reference stubs into their real locations, and produce an executable binary.
Name:
Anonymous2008-10-09 23:55
>>6 Declarations go in header files, definitions in source files.
You mean either one goes wherever you like.
Name:
Anonymous2008-10-10 0:08
>>9
...as long as they're read in the proper order when needed.
Name:
Anonymous2008-10-10 2:07
I absolutely hate trying to keep the declaration and definition in sync, especially during the early stages of writing some new code. I usually keep everything in the header as long as possible until I need to separate it out for some reason.
I have often wished there was a tool that could parse code to pull out the declarations from a c/cpp file and put them in a header file, or pull the function definition out of a header file and put it in a c/cpp file.
>>11
If you use a Visual Studio IDE, the Visual Assist plugin does just that with the Refactor option. Highlight function, ctrl-shift-x, type in new function signature, b'aam.
Name:
Anonymous2008-10-10 9:05
>>11 sed -n 's/^\([A-Za-z0-9_].*\)(\(.*\)) {$/\1(\2);/p'
Name:
Anonymous2008-10-10 9:48
>>11
One of the many reasons that I became a Haskell Nomad. I got tired of doing shit the tired way. It was great back in 70's and 80's and 90's but now, there shouldn't be any need to mess around with that shite anymore.
Name:
Anonymous2008-10-10 10:31
Actually, in C, you only have to declare a function if its return value or one of its arguments is not a word-sized integer. That's a feature inherited from the B programming language (which had the word-sized integer as its only type), I guess for compatibility. That's also why if you don't specify the type of a variable it defaults to int.
>>19
D is an improved version of Sepples, which places it pretty low on the desirability scale. So you've got two kinds of programmers: those who would actually use Sepples, and those who have the sense to pick a decent language. Leaving D out in the cold.
I would have said "fixed", which is exactly the reason I'd use it. I need everything Sepples can do, but without all of the bullshit.
Name:
Anonymous2008-10-11 13:59
>>23
I think >>22 means there's not enough bullshit taken out.
Name:
Anonymous2008-10-11 18:31
>>23
Wouldn't a fixed version be an improved version? Not that Sepples is fixable.
Name:
Anonymous2008-10-12 11:49
>>23 I need everything Sepples
not even fucking sensible
Name:
Anonymous2008-10-13 6:18
>>1
So the compiler can throw out a "conflicting types" error message whenever you decide to change the type of a parameter.
Name:
Anonymous2008-10-13 10:49
>>1
1. Write a script that reads a .c[pp] file and generates a header containing prototypes for all functions in that file.
2. Include a header generation rule in your makefile.
3. ???
4. PROFIT!
The actual reason. Because C++ is an ancient language based off an even more ancient language. They were made for a time when a computer didn't have the resources for a compiler to parse all of an apps code and do something meaningful with it. Why the fuck do you think the syntax is case sensitive. All those extra instructions incurred by the parser when reading a character to make them all lower case to the compiler (or upper if you are a fag) would be a performance nightmare. I mean its obvious that value and Value and VaLue are completley different entities in the same scope. VERY OBVIOUS! Also, the lazy ass holes who shit that would would have had to take 10 minutes and actually write those instructions in ASM.
Those self gratified nig-tards loved C so much the first C full compiler was written in C.
Name:
Anonymous2008-10-13 14:25
>>31
"Why the fuck do you think the syntax is case sensitive."
Because case sensitivity fucking rocks. It ensures that symbols have a consistent appearance. Besides, a case error is probably the easiest syntax error to fix. "'Cannot find "VaLue"'? Oh, shit, I meant 'Value'. *Fixes.* Great, now it compiles fine!"
>>32
Because case sensitivity fucking sucks. It ensures that symbols can have many nearly indistinguishable permutations, while still only having one useful one. Besides, how often do you want "'Cannot find "VaLue"'? Oh, shit, I meant 'Value'"?
Name:
Anonymous2008-10-14 16:08
"Include files. A major cause of slow compiles as each compilation unit must reparse enormous quantities of header files. Include files should be done as importing a symbol table." "Mutual dependence of compiler passes. In C++, successfully parsing the source text relies on having a symbol table, and on the various preprocessor commands. This makes it impossible to preparse C++ source, and makes writing code analyzers and syntax directed editors painfully difficult to do correctly." "It is spectacularly difficult to parse C++ 100% correctly. To do so really requires a full C++ compiler. Third party tools tend to parse only a subset of C++ correctly, so their use will constrain the source code to that subset."
"D's lexical analyzer and parser are totally independent of each other and of the semantic analyzer. This means it is easy to write simple tools to manipulate D source perfectly without having to build a full compiler. It also means that source code can be transmitted in tokenized form for specialized applications."
Name:
Anonymous2008-10-14 16:21
Many of the features of C just exist because it would be easier to write the compiler (long ago, at bell labs). "Features" like: only having constants in case statements, needing function prototypes, not being allowed to do a switch on a string, etc...
Name:
Anonymous2008-11-05 21:28
Fucking finally found a few tools that will make your headers for you.
>>21
Not valid ISO C code. Try -Wall or -pedantic one of these days. You'll notice that a) your screen will be flooded with warning messages and b) your whole source code will be unmaintainable like a steaming pile of shit without proper variable declarations.
But I guess this was just an academic example anyway, right? You wouldn't really write such code, RIGHT?
>>37
Gave all 3 of those a try and they probably work fine for C and simple C++, but blow chunks for things even as barely complex as namespaces (and you can forget nested classes).
fucking hate sepples
Name:
Anonymous2008-12-31 4:09
Enjoy Your Seppled Neurons.
Name:
Anonymous2008-12-31 5:15
/prog/ was giving me a 404 error for a while. Huh.
>>49
I take that back. I tested Lzz more (http://www.lazycplusplus.com/) and found it usually works. You may have to define certain declarations to go in particular files, but say goodbye to repeated declarations. Saved me from having to manually split up all my .h files into .cpp+.h. Now I just convert them to .lzz files and I'm good to go.
Name:
Anonymous2008-12-31 14:43
>>49
I take that back. I tested Lzz more (http://www.lazycplusplus.com/) and found it usually works. You may have to define certain declarations to go in particular files, but say goodbye to repeated declarations. Saved me from having to manually split up all my .h files into .cpp+.h. Now I just convert them to .lzz files and I'm good to go.
Name:
Anonymous2008-12-31 16:06
>>67,68
Perhaps you could use another language instead?
its called prototyping. the first time u basicly just say, this function is somewhere in this file. Otherwise your compiler will bump into functions he doensn 't know yet and will just not compile. U can do without prototyping, its not needed, however it makes the order in wich the functions are ordered in tour code-file irrelevant.