12 Interestingly, c language interview questions

1.gets () function

Q: Please find the following code in question:

  1. #include
  2. int main(void)
  3. {
  4. char buff[10];
  5. memset(buff,0,sizeof(buff));
  6. gets(buff);
  7. printf("\n The buffer entered is [%s]\n",buff);
  8. return 0;
  9. }

A: The problem is that the above code using the function gets (), this function takes a string without checking its cache volume copy from stdin, which may lead to a buffer overflow. It is recommended to use the standard function fgets () instead.

C language interview questions stronghold

2.strcpy () function

Q: Here's a simple password protection, in case you can not know the password to crack it?

  1. #include
  2. int main(int argc, char *argv[])
  3. {
  4. int flag = 0;
  5. char passwd[10];
  6. memset(passwd,0,sizeof(passwd));
  7. strcpy(passwd, argv[1]);
  8. if(0 == strcmp("LinuxGeek", passwd))
  9. {
  10. flag = 1;
  11. }
  12. if(flag)
  13. {
  14. printf("\n Password cracked \n");
  15. }
  16. else
  17. {
  18. printf("\n Incorrect passwd \n");
  19. }
  20. return 0;
  21. }

A: The key is to crack the encrypted using the break strcpy () function vulnerability. So when users enter cache random password to "passwd" and do not check in advance "passwd" capacity is adequate. So, if the user enters a sufficient cause buffer overflows and memory rewrite long "password" "flag" Variable Default position exists, even if the password can not be verified, flag validation bit has become a non-zero, it can be get the data to be protected. E.g:

  1. $ ./Psswd aaaaaaaaaaaaa
  2. Password cracked

Although the above code is not correct, but we can still buffer overflow to bypass the password security.

To avoid such problems, it is recommended to use strncpy () function.

Author's Note: Recently, the compiler will detect possible stack overflow in the interior, so this is difficult variable to the stack memory stack overflow occurs. The default is one such in my gcc, so I had to use a compiler command '-fno-stack-protector' to achieve the above scheme.

3.main () return type

Q: The following code can compile it? If so, what does it do potential problem?

  1. #include
  2. void main(void)
  3. {
  4. Char * Ptr = (char *) Malloch (10);
  5. if(NULL == ptr)
  6. {
  7. printf("\n Malloc failed \n");
  8. return;
  9. }
  10. else
  11. {
  12. // Do some processing
  13. free(ptr);
  14. }
  15. return;
  16. }

A: Because the return type of main () method, the error code in most compilers in will be treated as a warning. main () return type should be "int" instead of "void". Because "int" return value type will return to the state program. This is very important, especially when the program is part of the script as dependent on the successful running of the program is running.

4. Memory leaks

Q: The following code causes a memory leak it?

  1. #include
  2. void main(void)
  3. {
  4. Char * Ptr = (char *) Malloch (10);
  5. if(NULL == ptr)
  6. {
  7. printf("\n Malloc failed \n");
  8. return;
  9. }
  10. else
  11. {
  12. // Do some processing
  13. }
  14. return;
  15. }

A: Although the above code is not assigned to the release of "ptr" memory, but will not cause a memory leak when the program exits. At the end of the program, this program allocates all memory will automatically be disposed of. However, if the above code in a "while loop" in, it will lead to serious memory leak problem!

Tip: If you want to know more about memory leaks knowledge and memory leak detection tool, you can take a look at our article on the Valgrind.

5.free () function

Q: The following program will cause problems with user input 'freeze' when, while 'zebra' is not, and why?

  1. #include
  2. int main(int argc, char *argv[])
  3. {
  4. Char * Ptr = (char *) Malloch (10);
  5. if(NULL == ptr)
  6. {
  7. printf("\n Malloc failed \n");
  8. return -1;
  9. }
  10. else if(argc == 1)
  11. {
  12. printf("\n Usage \n");
  13. }
  14. else
  15. {
  16. memset(ptr, 0, 10);
  17. strncpy(ptr, argv[1], 9);
  18. while(*ptr != 'z')
  19. {
  20. if(*ptr == '')
  21. break;
  22. else
  23. ++ ptr;
  24. }
  25. if(*ptr == 'z')
  26. {
  27. printf("\n String contains 'z'\n");
  28. // Do some more processing
  29. }
  30. free(ptr);
  31. }
  32. return 0;
  33. }

A: The problem here is that the code will change the address while loop "ptr" stored (by increasing "ptr"). When the input "zebra", while the cycle is terminated before execution, and therefore variable passed to free () is passed to malloc () address. But when the "freeze", "ptr" stored address will be modified in the while loop, thus causing passed to free () the address wrong, it led to a seg-fault or crash.

6. Use _exit exit

Q: In the following code, atexit () is not called, and why?

  1. #include
  2. void func(void)
  3. {
  4. printf("\n Cleanup function called \n");
  5. return;
  6. }
  7. int main(void)
  8. {
  9. int i = 0;
  10. atexit(func);
  11. for(;i<0xffffff;i++);
  12. _exit(0);
  13. }

This is because the use _exit () function, the function does not call atexit () functions such as cleaning. If atexit () should be used exit () or "return" associated with it. # P # 12 C-plane interesting questions Part 2 # e #

7.void * and C structure

Q: Can you design a can accept any type of argument and returns a function interger results (integer) it?

A: follows:

  1. int func (void * ptr)

If this parameter is more than a function, then this function should be called by a structure, this structure may be filled by the need to pass parameters.

8. * ++ and operation

Q: What will output the following actions? why?

  1. #include
  2. int main(void)
  3. {
  4. char *ptr = "Linux";
  5. printf("\n [%c] \n",*ptr++);
  6. printf("\n [%c] \n",*ptr);
  7. return 0;
  8. }

A: The output should look like this:

  1. [L]
  2. [i]

Because the "+" and "*" as a priority, so "* ptr ++" equivalent "* (ptr ++)". That should be the first implementation ptr ++, and only then * ptr, so the result of the operation is "L". The second result is "i".

9. Q: modifying code (or read-only code)

Q: The following code segment is wrong, you can point out to you?

  1. #include
  2. int main(void)
  3. {
  4. char *ptr = "Linux";
  5. *ptr = 'T';
  6. printf("\n [%s] \n", ptr);
  7. return 0;
  8. }

A: This is because, by * ptr, change = 'T' in the code segment of memory (read-only code) "Linux" first letter. This operation is invalid, thus giving seg-fault or crash.

10. change the course of his name

Q: Can you write a process to change their name at runtime it?

A: See the following code:

  1. #include
  2. int main(int argc, char *argv[])
  3. {
  4. int i = 0;
  5. char buff[100];
  6. memset(buff,0,sizeof(buff));
  7. strncpy(buff, argv[0], sizeof(buff));
  8. memset(argv[0],0,strlen(buff));
  9. strncpy(argv[0], "NewName", 7);
  10. // Simulate a wait. Check the process
  11. // name at this point.
  12. for(;i<0xffffffff;i++);
  13. return 0;
  14. }

11. Return the address of a local variable

Q: The following code is the problem? If so, how to modify?

  1. #include
  2. int* inc(int val)
  3. {
  4. int a = val;
  5. a++;
  6. return &a;
  7. }
  8. int main(void)
  9. {
  10. int a = 10;
  11. int *val = inc(a);
  12. printf("\n Incremented value is equal to [%d] \n", *val);
  13. return 0;
  14. }

A: While the above procedures can sometimes run properly, but there is a serious flaw in the "inc ()" in. This function returns the address of the local variable. Because the life cycle of a local variable is the "inc ()" life cycle, so at the end of inc, use local variables bad results occur. This may be an address in main () variable "a" to be avoided, so that later can also modify the address value stored.

12. The process parameters of printf ()

Q: What is the output of the following code?

  1. #include
  2. int main(void)
  3. {
  4. int a = 10, b = 20, c = 30;
  5. printf("\n %d..%d..%d \n", a+b+c, (b = b*2), (c = c*2));
  6. return 0;
  7. }

A: The output is:

  1. 110..40..60

This is because the parameters of the default C language functions is processed from right to left, left to right when the output is.

C language interview questions stronghold

Guess you like

Origin www.cnblogs.com/miansheng/p/11297114.html