0
votes

Why do we need to declare global variables before function defining and declaring since we call this function after this variable declaration? Isn't it that compiler read line by line? I mean while calling the function compiler should know what's x.

void function()
{
    x += 3
}


int x = 3;

int main(void)
{
   function();
   return 0;
}

And one more question. I know that we can define function after main function provided we declared this function before main. Then how does main function see these functions after main function? Does the compiler first read the whole file and then run main() or sth?

2
Are you confusing a compiler and an interpreter? - Sourav Ghosh
You answered you own question. The compiler reads line by line. If it encounters x and it hasn't be declared or defined, it'll be an error. - Fiddling Bits
If the compiler encounters something that isn't defined, the compiler has to know about it with a forward declaration, or it's an error. - Fiddling Bits
You mix up C with some script languages. Variables are not defined at runtime but at compile time. The function function must be compilable without knowing anything about the caller or any code that might be parsed afterwards. - Gerhardh
@Gerhardh I thought so, in Python this works... I feel so confused - Jakub

2 Answers

1
votes

You can think at the job of the compiler like this; it reads the source file token by token (not exactly line by line) and when sufficient tokens are read, it outputs the translation. It repeats that, until the source file is (correctly) finished: the job of the compiler is done.

For every token the compiler reads, it needs to know what the token represents. If it doesn't know, an error is generated.

So, while compiling your function

void function()
{
    x += 3
}

it encounters an "x" but does not know what it represents (an integer? A float? Something else?). -error-.

Why do we need to declare global variables before function defining and declaring

Declaration and Definition are two different things. The compiler needs a declaration in order to know how to manage an identifier; the real definition can be somewhere else, even in another source (or already compiled) file.

And one more question. I know that we can define function after main function provided we declared this function before main. Then how does main function see these functions after main function? Does the compiler first read the whole file and then run main() or sth?

As explained before, all the compiler needs is a declaration, so it can output correct (object) code. If you declare function(), then define main(), then define function(), the compiler has enough to generate correct output, which will consist of code for main() and code for function() (we can say "in this order"). The next step, the linker, will take care to connect these two functions.

The definition of function() could also be absent: the compiler still would generate correct code for main(); the linker would instead complain, unless you tell it where to find the definition/implementation of function().

Also note that a definition is also a declaration. So if in your source you declare function() and then main(), you don't need forward declaration.

In the comments I've read that perhaps you are confusing interpreters with compilers - this is true, if you try to compare Python with C: very different beasts. A big difference is compiler vs interpreter, the compiler generates data (object code) but does not link it (and neither runs it). An interpreter instead is a compiler+linker+runtime, all packed together. Normally a compiler generates code that is much faster than the equivalent interpreted program, but to do this it needs accurate informations (precise types and declarations) and often (always?) is less versatile. The interpreter is often more versatile but it can not exploit all the optimizazions a good compiler can do.

0
votes

Why do we need to declare global variables before function defining and declaring since we call this variablefunction after this declaration?

The c language is a strictly typed language. When the compiler processes an identifier it needs to determine its type to generate correct object code.

It is not necessary that a global variable used in a function shall be declared exactly before the function definition. But in any case it shall be declared before its usage in a function.

Here is a demonstrative program.

#include <stdio.h>

void function( void )
{
    extern int x;    
    x += 3;
}

int x = 3;

int main( void ) 
{
    function();
    printf( "x = %d\n",  x );

}

The program output is

x = 6

Here the variable x declared within the function refers to the global variable x defined after the function.

Then how does main function see these functions after main function? Does the compiler first read the whole file and then run main() or sth?

C is a compilation language. It does not run programs. It generates object code that then after some processing by the linker can be run.

In the point where a function is used what the compiler is need is the type of the function that to check whether the function is used correctly. If the function is an inline function then it can substitute its call for the function body It can rebuild the object code when the inline definition in the translation unit will be known.