C Language Learning 12 Quiz

Source: Internet
Author: User
Tags printf password protection

1, gets () method

Q: The following code has a hidden problem, can you find it?

A: This unobtrusive problem is the use of the Gets () method. This method accepts a string type parameter, but does not detect if the value has enough space to copy the data. So here we generally use the fgets () method to be better in the future.

2, strcpy () method

Q: Password protection is a very basic function to see if you can handle the following code?

3, the return type of the main () method

Q: Can the following code be compiled? If so, what is the implication of this code?

A: The answer is that the code compiles, but it leaves a warning about the return type of the main () method. The real return type of the main () method should be ' int ' rather than ' void '. This is because the ' int ' return type enables the program to return the status value. This state value is especially important when this program is used as a satellite program for other applications.

4. Memory leak

Q: Does the following code have memory leaks?

A: Although the above code does not release memory for pointer ptr, it does not actually cause memory leaks even when the program ends, because all the memory that was initially occupied at the end of the program is emptied. But if the above code is inside the while loop, it can cause serious problems.

5. Free () method

Q: The following code will crash when the user enters ' freeze ', and if the input ' zebra ' is running normally, why?

A: The root cause of the problem is that the code changes the address of the PTR pointer in the while loop. When entered as ' zebra ', the while loop ends even before the first pass, so the memory address freed by Free () is the address assigned at the beginning malloc (). However, when you enter ' freeze ', the address of the PTR record is changed in the while loop because the wrong address is passed to the free () method to cause a crash.

6, atexit with _exit

Q: In the following code, the Atexit () method is not invoked, do you know why?

Answer: This is because the _exit () method is used. This method does not invoke methods related to purging data, such as atexit ().

7, void* and C structure body

Q: Can you design a method that accepts arguments of any type and then returns an integer? Is there a way to pass multiple such parameters?

A: A method that accepts any type of parameter can be as follows:

If you need to pass multiple arguments, then we can pass a struct that contains these arguments

8, * and + + operator

Q: What does the following code output?

A: The above output will be:

Because + + is the same priority as *, *ptr++ will operate from right to left. According to this logic, ptr++ executes the *ptr first and then executes it. So the first result is ' L '. Also because + + was executed, the next printf () result is ' I '.

9, making changes in Code segment

Q: The following code is bound to crash when it runs, can you tell me why?

A: This is because, through *ptr = ' T ', this line of code tries to change the string ' Linux ' of read-only memory storage. This is not going to work, so it can cause a crash.

10, Process that changes its own name

Q: Can you write a program that modifies its name when it is run?

Answer: The following code can be completed

11, the return address of local variables

Q: Is there a problem with the following code? If so, how do I modify it?

A: Although the above code sometimes works well, there are serious pitfalls in the Method Inc (). Once the Inc () method is executed, the use of the address of the local variable again can cause incalculable results. The solution is to pass the address of variable A to main ().

12, processing printf () parameters

Q: What is the following code output?

Answer: The output will be

This is because the parameters are processed from right to left, and then printed out from left to right.

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.