C language-Function

Source: Internet
Author: User

C language-Function
Functions in C language are similar to methods in C # And Java. functions are generally divided into primary functions (generally, a PC program has only one primary function, that is, the entry of the program, this is the convention of the compiler. If you feel uncomfortable, you can write the linker script directly, instead of using main as the program entry.) custom functions (think about all the necessary types of object-oriented, various Helper, Util bar), library functions (printf and scanf functions provided by stadio), simple definition and use of functions, first look at a piece of code: # include <stdio. h> void main () {printf ("sum add % d \ n", sum (100,200); printf ("div evaluate remainder % d", div (300,100 )); getchar ();} int sum (int numberA, int numberB) {return numberA + numberB;} int div (int numberA, int numberB) {return nu MberA/numberB;} the sum and div functions are called to evaluate and obtain the remainder. Let's look at the simple C # code: static void Main (string [] args) {Console. writeLine ("sum: {0}", sum (100,200); Console. readKey () ;}static int sum (int a, int B) {return a + B ;}extremely similar style, however, the above C program won't report errors in VS, but it will report errors in the Standard C language compiler, by default, the defined functions after the execution of the program can call the previously defined functions. In fact, the above C program should define sum, div and the two definitions in advance, you do not need parameters such as sum; # include <stdio. h> int sum (int, int); int div (int numberA, int numberB); Void main () {printf ("sum add % d \ n", sum (100,200); printf ("div evaluate remainder % d", div (300,100 )); getchar ();} int sum (int numberA, int numberB) {return numberA + numberB;} int div (int numberA, int numberB) {return numberA/numberB ;} if the above code needs to be encapsulated according to the object-oriented idea, the normal logic will want to encapsulate the top function declaration and the bottom function, in fact, the C language solves the problem in this way. The solution is to add one. h file and. c file; Let's first look at the implementation, and then specifically talk about the implementation process, respectively define a Calculate. h and Calculate. c file; Calculate. code in h: int sum (int, int); int div (int NumberA, int numberB); Calculate. code in c: int sum (int numberA, int numberB) {return numberA + numberB;} int div (int numberA, int numberB) {return numberA/numberB;} main function call: # include <stdio. h> # include "Calculate. h "void main () {printf (" sum add % d \ n ", sum (100,200); printf (" div evaluate remainder % d ", div (300,100 )); getchar () ;}generally, the compiler compiles the c language in several stages: ① pre-compiles the c program, reads the c source program, and runs pseudo commands on the c program (commands starting with #, such as # include # define) and special symbols. Scan the source code for initial conversion, and generate new source code for the compiler. The Preprocessing process processes the source code before the compiler. ② In the syntax and lexical analysis phase, the work in this phase will determine whether there is a problem with the basic data type definition, and whether the called function has such work; ③ In the compilation phase, first, compile the C file into a pure Assembly statement, and then convert the Assembly statement into a CPU-related binary code to generate each target file (. obj file); inventory file; in the bin directory of the vs project, you can see the generated Demo. obj and Calculate. obj file. In this case, we can look back at include, which is actually a pre-defined sum and div functions. include can be understood as Calculate. copy the code in h to the Demo. in the c file, if we include Calculate directly. what will happen to the c file, because there is Calculate. obj, an error will be reported during the link process, with duplicate identifiers. As for the reason why separate write is encapsulated, you don't need to talk about the object-oriented thinking, finally, let's look at the Directory and obj:

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.