Application of C and Common Errors

Source: Internet
Author: User

Application of C and Common Errors
All of the following are suffixed with ". c. ". C" is the suffix of the c source file, and ". cpp" is the suffix of the c ++ source file. C ++ inherits some features of the c language, so some bugs can be passed in ". cpp. 1. scanf () Do you really understand it ?; Scanf () has a returned value, and the returned value type is int. Do you know this? It returns the number of input messages that are successfully read. So what can we do with it? Non-zero is true. You must have heard of this sentence more than once. Let's look at the code below. Int a; while (scanf ("% d", & a) = 0) {printf ("Please enter a valid character:");} is it better to write code like this, then, the system checks whether the input is correct. But this will bring you into an endless loop. Why? During input, our key value requires a buffer to store the key value. If the Input key value is not accepted by scanf (), it will remain in the buffer, so take a closer look at the above Code, do you understand. It is not difficult to solve this problem. As long as the input key value is accepted, it will not affect your program in the buffer. Check out the following code. Is it clear that scanf () can be used like this! Int a; while (scanf ("% d", & a) = 0) {getchar (); printf ("Enter valid characters :");} 2. You are still struggling with while () and for (); beginners may struggle with the scenarios where while () and for () are used (). In my opinion, this is not worth my consideration. We should think about which one can make the program simpler and easier for others to understand. While (), for () is essentially a loop, but the form is different. Int I = 0; int I; while (I <10) for (I = 0; I <10; I ++) {I ++ ;}} the above code is of course for (), which is more concise and concise, but 1. The example in uses while (). For () is more than while () in the statement. Is this what you need? You cannot use for () or while () in a unified manner based on the Code style of each person (). It is recommended that beginners try both of them and write beautiful code in different ways. 3. Will you use switch? Switch (n) {case 1: break; case 2: break; default:} Have you always used this? We all know that without a break after case, the following code will be executed until the code is not executed completely or the break is encountered. With this feature, can we do other things, such as addition, which has 12 months in a year? I want to figure out that April 6 is the day of this year. How do you think of it? Let's look at the code below. Switch (month-1) {case 11: sumMonth + = 30; case 10: sumMonth + = 31; case 9: sumMonth + = 30; case 8: sumMonth + = 31; case 7: sumMonth + = 31; case 6: sumMonth + = 30; case 5: sumMonth + = 31; case 4: sumMonth + = 30; case 3: sumMonth + = 31; case 2: if (I % 4 = 0 & amp; I % 100! = 0) | I % 400 = 0) {sumMonth + = 29;} else {sumMonth + = 28;} case 1: sumMonth + = 31 ;} have you understood this? It just provides an idea to open our minds and you will see another blank sky. Have you Defined variables in case? Why can't variables be defined in case? Int a; switch (a) {case 1: int B; break; case 2: B = 0; break;} What should the compiler do if you are so capricious! Why do we not stipulate that the variables defined in case 1 can only be used in case 1? 6. You will find the answer in the variable scope. Of course, we should not be limited to switch (). if () else can also complete multiple selection tasks. However, switch () has 3 to built-in labels and uses switch () the program running speed may be slightly faster, and the code is more concise. 4. We all know the use of if () for judgment. So how do you write when you judge that two characters are equal? Int a; if (a = 5) if it is me, I will write it as int a; if (5 = a) functions are the same, why bother? It was impossible for us to debug ten lines of code for half an hour when we were working on a project. When we thought about flying on the keyboard, I realized the joy of a programmer. When we are experiencing this kind of happiness, hand mistakes are inevitable. if we write it as if (a = 5), this error is enough for two days. But if (5 = a), the compiler can help you find it. If you want to determine whether two strings are equal, will you write them like this? Char c_c1 [10] = "acdefg"; char c_c2 [10] = "qwerty"; if (c_c1 = c_c2) or if (c_c1 [10] = c_c2 [10]) is always incorrect. Strings, arrays, and pointers will be involved later. To compare two strings, we use the strcmp () function provided by the C function library, which is included in the header file <string. h>. Strcmp () returns an int type value,-, 1. 0 indicates that two strings are equal.-is determined by the comparison between characters (ASCII. Here is an example. Char c_ch [10] = "qwertyui" // a maximum of 9 characters can be stored, and the last character is "\ 0" char * p_ch = "adgf" strcmp (c_ch, p_ch ); // The two parameters in strcmp () are the addresses of the two strings strcmp ("qwertyui", "adfg5"); // This is also possible, and the strings are also the addresses of the first letters, it will be mentioned later. 5. Do you know the role of header files? We get used to writing # include <stdio. h> first, but do you know its function? # Include: file contains when the Preprocessor finds the # include command, it will find the file name in <> and include the file to the program code you write, replace # include in the source file (the file you wrote. Although you only write one line of code, the compiler adds a lot of code to you. <Stido. h> it contains the input and output functions, so it is indispensable in the debugging phase of the program. # Have you seen headers such as include "gaozy? What is the difference between it and # include <stdio. h>? <> The included header file is searched by the compiler in the system directory. The "" header file, the compiler will first search in the current working directory, if not, and then search in the system directory. You can also simply understand the functions in the C function library in the header file, while the functions we write are placed in the "" file. 6. Does the variable scope int I; while (1) {int I = 0; printf ("% d", I);} return an error? Of course not, because two int I; s are not in the same scope. So what is scope? How can we identify scope? We usually regard the code in {} as a scope. This is why variables cannot be defined in case. Can the scope be distinguished only? Of course not while (1) int I; printf ("hellow world! "); Int I; in a single scope, printf () is executed only once. Of course, this is an endless loop and will not be executed to printf (). Do you understand a little bit now? Like while (), there is also for () and if (). Have you ever thought about it? The first loop defines an I, and the second loop defines an I. Isn't that repeated? Of course not. The variables in the scope have lifecycles. That is to say, when the first cycle ends, the memory space of your defined I has been released. 7. Have you ever written such a program? Int I; while (1) {int I = 0; printf ("% d", I); int j;} the compiler reports the following error: \ c. c (49): error C2143: syntax error: missing '; 'before' type' I am not good at English. I don't understand what this sentence means, but this code does not exist;. I stick the code to vs2010 and it can run. I once suspected that the compiler had an error. But in fact, the compiler has very few errors and has been entangled for a long time. I suddenly remembered that the C language stipulates that variables must be defined at the beginning of the scope. In vs210, the file suffix is. cpp, that is, c ++ allows variables to be defined after other functions, while c does not. The code you wrote is not running normally before. You just modified a little bit and there is no error in syntax. But does the compiler report an error? LINK: fatal error LNK1168: cannot open Debug/SMIS.exe for writing was a long time ago. Why can I compile and wait for a while? This is not a compiler error. Instead, you have executed the program, not closed, modified the code, and executed the program again. If the compiler can talk: "Hey, you have fun with me. Can you play together ." When you use malloc, remember free, and it is necessary to keep up with a judgment after malloc. P_head = (P_STUDENT) malloc (sizeof (STUDENT); if (NULL = p_head) {printf ("dynamic memory allocation failed, program ended! "); Exit (-1);} if it is not determined by if (), the running program will fail when the memory allocation fails, and it is difficult to find the cause of the error. 8. When talking about strings, arrays, and pointers, we have to talk about memory. We can think of memory as a long and long house, which is a linear structure. There are different families in the house. Each house has a different number. People correspond to variables defined in the computer, and the house number corresponds to the address of the variable. Therefore, every variable we define occupies a house. When the House is fully occupied, your computer crashes. Of course, the memory on our computer is still very large. Let's take a look at the relationship between strings, arrays, and pointers. The string ends with "/0". Therefore, the actual length of the string must be + 1. The array name is the address of the first element of the array. char ch [10] = "asdfg "; // ch [10] is a fixed-length array char ch [] = "asdfg"; // ch [] has no specified length, however, you must initialize char * p_ch = "asdfg" during definition; // The Pointer Points to the first address of the string. Both ch and p_ch are addresses. The array definition cannot be changed, and the pointer can point to other addresses, but pay attention to memory leakage. 9. pointers and addresses some books say that pointers are addresses. In fact, this statement is not accurate. So what are pointers and addresses? First, the address is the address, physical memory number. A pointer is a variable that stores the physical address of the memory. Just like int I; I is a variable that stores an I value. The pointer variable also stores a pointer value, which is the address of a physical memory. In fact, whether the pointer is an address or a variable does not affect our usage of the pointer. 10. Have you used Macros? # Define MAX 60 means MAX = 60; in the subsequent code, MAX is equal to 60. Macros make program maintenance easier and easier to understand. 11. malloc and free some people say that new and delete are more powerful, but I think that new and delete are operators in c ++ and provide operations on objects. Malloc and free are used for dynamic memory allocation in C language and are not comparable. C language is a process-oriented program design without the concept of objects.

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.