Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C and C++ Question

Name: Anonymous 2008-10-09 18:17

Why must you declare functions twice?

Name: Anonymous 2008-10-09 18:27

bcuz ur dum

Name: Anonymous 2008-10-09 19:44

because 1 is the loneliest number that you'll ever do.

Name: Anonymous 2008-10-09 21:32

>>1
Because those languages are so ambiguous that the compiler has to be extra sure you are actually attempting to declare a function.

Name: Anonymous 2008-10-09 22:19

linkan gaems

Name: Anonymous 2008-10-09 22:39

>>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: Anonymous 2008-10-09 22:54

>>6 is right, but the distinction is pointless and quite a regrettable design decision.

Name: Anonymous 2008-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: Anonymous 2008-10-09 23:55

>>6
Declarations go in header files, definitions in source files.
You mean either one goes wherever you like.

Name: Anonymous 2008-10-10 0:08

>>9
...as long as they're read in the proper order when needed.

Name: Anonymous 2008-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.

Name: HMA FAN 2008-10-10 2:23

>>11
Fucking seconded.

Name: Anonymous 2008-10-10 2:38

>>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: Anonymous 2008-10-10 9:05

>>11
sed -n 's/^\([A-Za-z0-9_].*\)(\(.*\)) {$/\1(\2);/p'

Name: Anonymous 2008-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: Anonymous 2008-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.

Name: Anonymous 2008-10-10 11:49

>>16
wrong. learn C faggot

Name: Anonymous 2008-10-10 15:03

>>17
Try it if you don't believe me.

Name: Anonymous 2008-10-10 15:24

I'm kind of surprised that the D language hasn't taken off for these reasons. It's similar enough to c/c++, but without decades worth of cruft.

Name: Anonymous 2008-10-10 15:51

>>19
It doesn't offer anything over C.

C++ is popular because of that faggot that kept promoting it.

Name: Anonymous 2008-10-10 16:02

>>17
$ cat > noproto.c
main() { return !printf("%d\n", times2(16)); }
times2(a) { return mul(a, 2); }
mul(a, b) { return a * b; }
$ gcc -w -o noproto noproto.c && ./noproto
32
$

Name: Anonymous 2008-10-10 16:15

>>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.

Name: Anonymous 2008-10-11 12:18

>>22
D is an improved version of Sepples

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: Anonymous 2008-10-11 13:59

>>23
I think >>22 means there's not enough bullshit taken out.

Name: Anonymous 2008-10-11 18:31

>>23
Wouldn't a fixed version be an improved version? Not that Sepples is fixable.

Name: Anonymous 2008-10-12 11:49

>>23
I need everything Sepples
not even fucking sensible

Name: Anonymous 2008-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: Anonymous 2008-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!

Name: Anonymous 2008-10-13 11:36

>>28
Good luck parsing Sepples.

Name: Anonymous 2008-10-13 12:24

>>29
Thanks.

Name: Anonymous 2008-10-13 14:15

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: Anonymous 2008-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!"

Name: Anonymous 2008-10-13 14:36

>>32
you sir have been trolled

Name: Anonymous 2008-10-13 15:25

>>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: Anonymous 2008-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: Anonymous 2008-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: Anonymous 2008-11-05 21:28

Fucking finally found a few tools that will make your headers for you.

Preprocess - A preprocessor for C and C++ modules
http://os.inf.tu-dresden.de/~hohmuth/prj/preprocess/

Lzz: The Lazy C++ Programmer's Tool
http://www.lazycplusplus.com/

Makeheaders
http://www.hwaci.com/sw/mkhdr/

Stupid shit C/C++

Name: Anonymous 2008-11-05 21:43

Name: Anonymous 2008-11-06 2:01

Name: Anonymous 2008-11-06 6:47

>>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?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List