Can I assign values to the main function in C language?

Source: Internet
Author: User

In the past two days, we used the structure to simulate the object-oriented method and wrote the hardware driver in the C language.Program. As follows:

A structure is defined in ds1302.h:

Typedef struct {

Void (* fun1 );

Void (* fun2 );

} Ds1302_t;

Extern ds1302_t ds1302;

A global variable is declared to be called in the main. c file where the main function is located, and the variable is defined in ds1032.c:

Ds1302_t ds1302;

Then initialize in ds1032.c:

Ds1302 = {fun1, fun2}; // among them, fun1 and fun2 is the previously defined function as a structure member. However, before initialization, the error message is "repeated definition. Think twice about it.

Finally, a simple program (test. c) is created with VC)

# Include <stdio. h>

Typedef struct {

Unsigned char;

} Student;

Student S1;

S1.a = 0;

Void main ()

{

}

The above files cannot be compiled, and the prompt is the same. If you repeat the definition, you will find that you have previously assigned a value to the variable before the main function. The main function is used to declare and define global variables and functions, and is pre-processed by the compiler to allocate memory and addresses to those global variables and defined functions. It cannot be designed to be executable.Code.

In the compilation phase, the compiler allocates memory for the global variables before the main function, generates executable code for the defined function, and retains the address of the function for the main function to call, the value assignment statement operation is an executable code, and there is no such thing as a function pointer for the main function to call, so it can only be executed in sequence within the main function.

In multi-file organization, only the main function in the source file containing the main function can assign values to variables, other source files that do not contain main cannot assign values to variables. They can only declare variables.

But it does not mean that you cannot initialize and assign values to variables before the main function: You can initialize and assign values to variables when defining variables. As follows:

Int A = 10;

Student S1 = {10 };

These are all possible.

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.