12 questions and answers in C Language Learning

Source: Internet
Author: User
Tags password protection

1. gets () method

Q: Can you find the following code hidden?

A: The problem is that the gets () method is used. This method accepts a string type parameter, but does not check whether the value has enough space to copy data. So here we generally use the fgets () method for better future.

2. strcpy () method

Q: password protection is a basic function. Can you fix the following code?

3. Return type of the main () method

Q: Can the following code be compiled? If yes, what are the issues in this code?

A: The answer is that the code can be compiled, but a warning will be given for the return type of the main () method. The true return type of the main () method should be 'int' rather than 'void '. This is because the 'int' return type allows the program to return Status values. This status value is especially important when this program is used as a subsidiary of other applications.

4. Memory leakage

Q: Is there any memory leakage in the following code?

A: Although the above Code does not release the pointer ptr memory, in fact, even if the program ends, it will not cause memory leakage, when the program ends, all the memory occupied at the beginning is cleared. However, if the above Code is in the while loop, it will cause serious problems.

5. free () method

Q: The following code will crash when the user inputs 'freeze '. If the user inputs 'zebra', it will run normally. Why?

A: the root cause is that the Code changes the ptr pointer address in the while loop. When the input is 'zebra ', the while loop even ends before the first execution. Therefore, the memory address released by free () is the address allocated by malloc () at the beginning. However, when 'freeze 'is input, the address of the ptr record is changed in the while LOOP, because it will be an incorrect address passed to the free () method, causing a crash.

6. atexit with _ exit

Q: In the following code, the atexit () method is not called. Do you know why?

A: This is because the _ exit () method is used. This method does not call methods related to data clearing, such as atexit.

7. void * and C struct

Q: Can I design a method to accept any type of parameter and return an integer? Is there a way to pass multiple such parameters?

A: A method that can accept any type of parameters is as follows:

If multiple parameters need to be passed, we can pass a struct containing these parameters.

8. * And ++ Operators

Q: What will the following code output? Why?

A: The above output will be:

Because ++ and * have the same priority, * ptr ++ will operate from right to left. According to this logic, ptr ++ will first execute and then execute * ptr. So the first result is 'l '. Because ++ is executed, the next printf () result is 'I '.

9. Making changes in Code segment

Q: The following code will surely crash during running. Can you tell the reason?

A: This is because, through * ptr = 'T', this line of code tries to change the character string 'linux 'of the read-only memory storage '. Of course, this operation does not work, so it will cause a crash.

10. Process that changes its own name

Q: Can you write a program to modify its name when it runs?

A: The following code can be completed:

11. Return address of a local variable

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

A: Although the above Code sometimes runs well, there are serious risks in method inc. When the inc () method is executed, using the address of the local variable again results in immeasurable results. The solution is to pass the address of variable a to main ().

12. process the printf () parameter

Q: What is the output of the following code?

A: The output will be

This is because the parameters are all processed from right to left, and then printed, but 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.