Timeline for Why is *declaration* of data and functions necessary in C language, when the definition is written at the end of the source code?
Current License: CC BY-SA 3.0
14 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Dec 23, 2014 at 17:22 | comment | added | Mooing Duck | @user31782: The compiler must know the type of the variable in order to generate the correct assembly for the processor. When the compiler finds the implicit Func_i(), it immediately generates and saves code for the processor to jump to another location, then to receive some integer, and then continue. When the compiler later finds the Func_i definition, it makes sure the signatures match, and if they do, it places the assembly for Func_i() at that address, and tells it to return some integer. When you run the program, the processor then follows those instructions with the value 3. | |
| Dec 23, 2014 at 15:43 | comment | added | amon | @Carcigenicate C was heavily influenced by the B language, which only had a single type: an word-width integral numeric type that could also be used for pointers. C originally copied this behaviour, but it's now completely outlawed since the C99 standard. Unit makes a nice default type from a type-theory viewpoint, but fails in the practicalities of the close to the metal systems programming that B and then C were designed for. | |
| Dec 23, 2014 at 15:23 | comment | added | Carcigenicate | Why is the default type an int? That seems kind of off. I'd think Unit would be a better assumed type. | |
| Dec 23, 2014 at 15:20 | comment | added | user106313 | Then how does it know at which line the return-value is mentioned? | |
| Dec 23, 2014 at 15:15 | comment | added | amon | @user31782 The job of the compiler is to create a binary file where we can jump efficiently to known locations. But the source code is a text file. When we see a function name Func_i, where do we jump to? Line 12? Line 10123? The compiler doesn't know, so the whole file would have to be loaded into memory at once and the compiler would have to search through everything. (Actually, knowing the line number wouldn't even be enough – computers operate on bytes and it would be even more difficult to find the exact byte where a function definition starts). | |
| Dec 23, 2014 at 15:10 | comment | added | user106313 | Why don't the processor jump to the point where the information of the type of the function is mentioned? If the processor can jump to find the return value then why not jump to find the return type? | |
| Dec 23, 2014 at 15:02 | comment | added | amon | @user31782 This is possible, and some languages do that. C is not one of those languages. With predeclarations, the compiler can go through the file almost line by line and can immediately print out assembly code for that line with all the information currently known. Without predeclarations, the whole file would have to be loaded into memory, and type analysis becomes more complicated. The compiler turns the text of the source file into binary codes, where a function is just a sequence of codes that starts at a known position. A function call is a jump to this position – no need to search. | |
| Dec 23, 2014 at 14:49 | comment | added | user106313 | You say: "The compiler does not find the value returned by Func_i" Then how is the value of Func_i() get printed? Could you explain in simple language. If the return-value of Func_i() can be known at the run-time then can't the same mechanism be used to find the type of Func_i() and Data_i, when no forward declaration is provided for both? | |
| Dec 23, 2014 at 14:38 | comment | added | user106313 | @LightnessRacesinOrbit I know that. I mistakenly wrote compiler in my comment above because I forgot the name processor. | |
| Dec 23, 2014 at 14:18 | comment | added | Lightness Races in Orbit | @user31782: The compiler doesn't print the value. Your compiler doesn't run the program!! | |
| Dec 23, 2014 at 13:38 | comment | added | user106313 | @Blrfl Can the same be happen to Data_i(). That is the processor jump to the memory location where Data_i()'s value is stored. | |
| Dec 23, 2014 at 12:58 | comment | added | Blrfl | @user31782: The order of the code matters at compile time, but not at run time. The compiler is out of the picture when the program runs. By run time, the function will have been assembled and linked, its address will have been resolved and stuck into the call's address placeholder. (It's a little more complex than that, but that's the basic idea.) The processor can branch forward or backward. | |
| Dec 23, 2014 at 12:44 | comment | added | user106313 | If the compiler doesn't jump down to find the returned value of Func_i(), then how does it print that value. Suppose I put a getchar() after the printf(). Now the processing will have to stop until I press something. There is now no way for the computer to go down to find the returned value but it somehow still prints the correct value. | |
| Dec 23, 2014 at 12:35 | history | answered | amon | CC BY-SA 3.0 |