Example of a simple calculator program:
1# include <stdio.h>//1. header file2 3 //2. Addition function4 intAddintAintb//3. function definition mode5{//4. Function Body6 returnA+b;//5. function return7 } 8 //Subtraction9 intMoveintAintb)Ten { One returnA-b; A } - - //multiplication the intMulintAintb) - { - returnA *b; - } + - //Division + floatDivideintAfloatb) A { at if(b!=0)//6. If Judgment statement - { - returnA/b; - } - return 0; - } in - //7. Main function to intMain () + { - intb;//8, the definition and initialization of variables theprintf"Please enter two numbers: \ n");//9. printf function *scanf"%d%d", &a,&b);//10. scanf function $printf"and =%d, poor =%d, =%d, Shang =%f\n", add (A, B), move (A, B), Mul (A, B), divide (A, b));//11. Output format of printf functionPanax NotoginsengGetChar ();//12. GetChar function - return 0;//13. Return value of main function the}enter two numbers to calculate and print the results
The function of the above program is as follows: Input two number, calculate and, difference, product, quotient, and print out
The above program I commented on each line of code in some of the content, the point of explanation, and explain the learning method:
0. Annotation method
The so-called annotation, is to the program developers to see, prompting code some of the role of information to facilitate development. The compiler will not take care of the comment content, no matter what you write in the comments, will not affect the function of the program, but in order to facilitate the development and reuse of the program, you write comments preferably have a certain relevance. There are two main types of annotation methods available in C, one is:/* Paragraph comment */, two://single-line comment, as the name implies, the first can be multiple lines of comments, the second can only comment on the content behind the parallel bars symbol.
1. header file
Header files are generally used with #include<> symbols, in a project, the general corresponding xxx.h files, placed in the header files directory, beginners may wonder why every time the use of #include<stdio.h> There is no stdio.h in the project, in fact there is, this has been included in the C + + development environment, stdio.h refers to the standard IO is the normal input and output library, see the printf, scanf functions and GetChar and other functions are in this library to achieve good, So we can understand why we don't have to define printf and other functions, so we can use them directly, because this is what the system has already defined. As you learn more, you will learn more about the header file, or you can write a custom header file to include it yourself.
2. Addition function
The functions in mathematics we all know, such as f (x) =2x, a narrow concept of function is a numerical calculation. The function in the C language is a generalized concept, but the essence is actually changed, there is input, there is output, input a thing, output a thing, or do not explicitly output, just inside the function to do some transactions, such as changing the value of a variable, but the function does not explicitly return the number of changes, this is also possible. The function input parameter is called the formal parameter, just a form routine, when you call this function, also must follow this routine to call, like above example, when you call, the input parameter number and the type must be same as the function definition inside, Of course there are exceptions where the type of the variable is automatically converted (which is also to be able to be converted, or the loss of precision will occur), this knowledge will be exposed in later chapters. In addition, each function has a return statement, even if nothing is returned (void-modified function), also return;.
3. Statement block
By a pair of curly braces is called a block of statements, each function body is a large statement block, C language statement block writing format is the example of the various functions in the form of the body. Programming habits for the convenience of the program to read, it is generally required to align the corresponding pairs of curly braces. This is not so much a programmer's obsessive-compulsive disorder, but rather a programmer's programming habits, the good or bad programming habits, can reflect your force lattice, but also improve your learning, especially when beginners. So if your programming basic skills have a good grasp, I also do not believe you can achieve what function and tall on the algorithm, do small things to do big things.
4. Judgment statement
The commonly used branch structure has the order structure, the branch structure, the loop structure, the judgment statement is the branch structure inside the content, second also has the switch statement and so on. The format of if () {}else{} is best represented by a block of statements, even if it is simply a return, as in the example above, which is part of the programming habit, and you will find that it saves you a lot of time to find bugs. If the expression inside the If bracket is true, for example (1+1==2), then the next statement block is executed, otherwise the statement block inside the else is executed. There will also be some extended nesting, such as If+else if +else if+ ... + else, in the middle there are many else if judgments. The meaning of the explanation is that if the a==5,a==5 is wrong, then the a==4, a==4 the wrong words that a==3 .... Otherwise.... The following is an if else nested code fragment.
intA =0;if(A = =5) {printf ("5"); }Else if(A = =4) {printf ("4"); }Else if(A = =3) {printf ("3");}Else{printf ("None of these are right .");}
5. Main function
The main function is the entry of a program, and the program runs from the main function. It should be stated that the definition of variables in C must always be at the head of other statements, and no one will make an error. The structure of the printf and SCANF functions Note that the & entered in the scanf is the meaning of the accessor, indicating that the function wants to get the address of these variables as input, and so on, you can implement the system function on your own, and you can see how the bottom is written, and now use it first. The GetChar function is doing a pause here, and it takes you to enter a character at the command line, so you have to do it again to end the program. In particular, the main function of the return value problem, the previous version seems to be able to use void as the return type of main, but now all with an int, return a value of 0, return 0 for the program to run correctly, return other instructions, program errors.
Learning methods:
Beginners a language, the best way is to more hands to knock code, encounter will not be the problem to find information to solve, the general problem, the Internet has the answer, if not, that is because the problem is too simple on the internet has no one to put forward. In addition, just learned to write code when the grammar is not good grasp, remember to each sentence to hit the upward comment, practice much, the back slowly do not need comments can also be utilized. Learning ability is as you learn more things become more and more excellent, so if you want to learn faster, you need to spend more time than others to practice, than others to learn more things to improve their horizons, than others redouble their efforts to become the eyes of Daniel, go technology This road, this is a must.
C Language Learning _ an explanation of a simple program and a summary of C learning methods