Pergunta

I know that it is always the main () function being executed first, then function calls will direct the program to other functions. What if functions were called *before* the main () function? When will they be executed?\

I had a program (I downloaded from the Internet) and there were function calls before main( ).

Now I don't know what they are for if execution is only done in main () (and the the functions called inside main).

HERE IS THE FRAGMENT OF THE PROGRAM:

static void set_level_indices   (VideoParameters *p_Vid);
static void chroma_mc_setup     (VideoParameters *p_Vid);
static void init_img            (VideoParameters *p_Vid);
static void init_encoder        (VideoParameters *p_Vid, InputParameters *p_Inp);
static int  init_global_buffers (VideoParameters *p_Vid, InputParameters *p_Inp);
static void free_global_buffers (VideoParameters *p_Vid, InputParameters *p_Inp);
static void free_img            (VideoParameters *p_Vid, InputParameters *p_Inp);
static void free_params         (InputParameters *p_Inp);

static void encode_sequence     (VideoParameters *p_Vid, InputParameters *p_Inp);

*(SOME FUNCTION DECLARATIONS OMITTED)*

int main(int argc, char **argv)
{
  init_time();
#if MEMORY_DEBUG
  _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif

  alloc_encoder(&p_Enc);

  Configure (p_Enc->p_Vid, p_Enc->p_Inp, argc, argv);

  // init encoder
  init_encoder(p_Enc->p_Vid, p_Enc->p_Inp);

  // encode sequence
  encode_sequence(p_Enc->p_Vid, p_Enc->p_Inp);

  // terminate sequence
  free_encoder_memory(p_Enc->p_Vid, p_Enc->p_Inp);

  free_params (p_Enc->p_Inp);  
  free_encoder(p_Enc);

  return 0;
}

Now that I thought of it, is the static has something to do with these calls done before main () being okay?

This is the encoder of H.264 in its reference software.

EDIT

Are the codes above main () function calls, function prototypes or a function declaration. You all give different answers. Please choose one and explain why by presenting the format. I really thought these statements are in the form of function calls. Furthermore, can function prototypes be included in a source code and not in the header file? Thank you!

Foi útil?

Solução 3

In the code you listed, the functions before main are function prototypes, also called function declarations (they are synonyms). They are not function calls. You can tell they aren't function calls for two reasons:

  1. Functions cannot be called from the global scope. The global scope is anything outside of a function body. This is the quickest way to recognize it, but it's a little hand wavy.
  2. The real reason is that function calls are not preceded by type specifiers or type qualifiers, like void and static. These keywords specify the type of a symbol, in this case the function being declared. Function calls don't have a type (the return value does, but not the call itself), so you would never specify the type this way for a function call.

To answer some of your other questions:

  • In most implementations (and according to the C standard) the first user defined function to be executed is always main, in which case it is not possible for you to write code that calls any other function before main. In most cases, compilers will generate their own initialization code which is executed before your main function. It does things like set up memory and initialize static variables. In some cases, you can write code that will be used as part of initialization, but that's relatively uncommon and it would be compiler specific how to do that.
  • A function prototype, or function declaration, is just a way of telling the compiler about a function before you actually define/implement it. The declaration/prototype (same thing) tells the compiler: a) this symbol is a function, b) it has such-and-such return type, c) it has such-and-such parameter list. That way, when the compiler sees you try to call the function somewhere else in your code, it knows what you're trying to do.
  • The static keyword is called a storage specifier. On functions, it simply means that the function name is not exported to the linker, so it cannot be called from any other code modules (i.e., from any other C source files). So it's really just a way of limiting the visibility, or the scope, of the function.
  • Remember, a header file is just a way to include the same content in multiple places. Anywhere you #include a file, the compiler will treat it as if the contents of the included file appeared directly in the spot where it is included from. So function prototypes/declarations will very often appear in a header file. This allows the function to be "known" (and therefore called) from any source file where the header is included. Function implementations (also called function definitions) should generally not be put in header files. Doing so would cause the function to be defined every time the file is included, which will generally cause the linker to complain that the name (of the function) has multiple definitions. (The only way to get around that is marking the function static, and then you're really defining a file-local function each time the header is included, as opposed to implementing a single function with global scope which can be called from multiple source files. The latter is usually what you want.)

Outras dicas

If you are asking how this might be accomplished, it is not through a standard feature of the C language.

The C runtime works by providing an entry point for the operating system to execute when you execute the binary. That entry point runs code specific to the compiler implementation. It will set up some initialization code, and then call your main() function (with any command line parameters if provided). Any other code outside of the C runtime proper that is executed before main() is called is a mechanism outside of the C language (or an extension to the C language provided by the compiler).

In C++, global constructors are executed before main().

In C, your compiler implementation may provide an extension that allows a function to be marked to be executed before main() is called. In GCC, this can be done with the constructor attribute.

void foo () __attribute__((constructor));
void foo () { puts(__func__); }
int main () { puts(__func__); return 0; }

The output of the above program (when compiled with GCC) is:

foo
main

In the code that you have posted, the functions are declared before the main and that doesn't mean that these functions are called. They are declared, informing the compiler that those functions are going to be used in the program

I think you may mis-understand function declarations and function calling.

#include <stdio.h>
void foo(void);
int main(void)
{
    printf("main\n");
    foo();
    return 0;
}
void foo(void)
{
    printf("func\n");
}

In the simple program above, since the definition of foo is below main, it has to be declared before main. Either put the declaration in a header file and include it, or, put the declaration before main or any function that calls them.

UPDATE:

functions that are static are just limited their scope in the file only, no other meanings.

Just write a print statement in each function..

printf(" main");

printf("function 1") etc

you will get the sequence of the functions being called. And by default the main function is always called first and at the end of execution the main functions should return back a int, float etc as declared in the main definition.

If functions were called before the main function what would be the point of the main function? main is the starting point for the compiler in most languages.

If you want to run a function before the main() function code just call it before you run any code.

If functions were called before the main function, they would execute before the main function.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top