C Language interview questions

Source: Internet
Author: User

Content from: C language face large summary of questions

P.S. Only pick up what you think might be asked and not.

    • What is static used for? (Please specify at least two types)

1. Scope of the restriction variable 2. Set the storage domain of the variable

    • What is the difference between a reference and a pointer?

1) The reference must be initialized, and the pointer does not have to. 2) The reference initialization cannot be changed and the pointer can change the object being referred to. 3) There is no reference to a null value, but there is a pointer to a null value.

    • Are global variables and local variables different in memory? If so, what's the difference?

Global variables stored in the static database, local variables on the stack

    • The parameter type that cannot be a switch () is: Switch parameter cannot be a real type.
    • Can local variables have the same names as global variables?

Local variables can have the same name as global variables, and when referenced within a function, local variables of the same name are used instead of global variables. For some compilers, multiple local variables with the same name can be defined within the same function, such as a local variable with the same name defined in the two loop body, and the scope of that local variable is within that loop body

    • How do I reference a global variable that has already been defined?

extern
You can refer to the header file, you can also use the extern keyword, if you use a reference header file to refer to a global variable declared in the header file, assuming that you write the error, then the compilation will be an error, if you use the extern way to reference, assuming you made the same mistake, Then no error during compilation, and error during connection

    • Global variables can be defined in multiple. C file included in the header file? Why?

It is possible to declare global variables of the same name in static form in different C files.
You can declare a global variable of the same name in a different C file, provided that only one of the C files has the initial value assigned to the variable, and the connection is not faulted at this time

  • What is the difference between a static global variable and a normal global variable: the static global variable is only initialized once, preventing it from being referenced in other file units;
    What is the difference between a static local variable and a normal local variable: the static local variable is initialized only once, the next time based on the last result value;
    What is the difference between a static function and a normal function: The static function has only one copy in memory, and the normal function maintains a copy of each call
  • The local variables of the program exist in (the stack), the global variables exist in the (static zone), and the dynamic requisition data exists in the (heap).
  • The difference between heap and stack.
    The heap is heap, and stack is the stack.
    Stack space is automatically allocated/freed by the operating system, and space on the heap is allocated/released manually.
    Stack space is limited, heap is a large free storage area
  • A 32-bit machine with a pointer to how many bits
    The pointer is the number of bits just look at the address bus of the digits on the line. 80386 after the machine are 32 of the data bus. So the number of pointers is 4 bytes.
  • What is the role of the keyword static?
    Defining static variables
  • What is the meaning of the keyword const?
    A variable that represents a constant that cannot be modified.
  • What is the meaning of the keyword volatile?
    Tip the value of the compiler object may change without the compiler being monitored.
  • What is the difference between a struct in C and C + +?
    The main difference between structs in C and C + + is that structs in C can not contain member functions, whereas structs in C + + can. The main difference between struct and class in C + + is that the default access permissions are different, struct defaults to public, and class defaults to private
  • What is precompilation?

    Pre-compilation, also known as preprocessing, is the first task of the whole compilation process, that is, some pre-processing work before program execution. The main processing #开头的指令. such as copy #include包含的文件代码, replace #define定义的宏, conditional compilation #if等.

    When to precompile:

    1. Always use a large code body that does not change frequently.

    2, the program consists of multiple modules, all modules use a set of standard include files and the same compilation options. In this case, you can precompile all the include files into a precompiled header.

  • sizeof keyword? The

    sizeof is processed during the compilation phase and cannot be compiled into machine code. The result of sizeof equals the number of bytes of memory that the object or type occupies. The return value type of sizeof is size_t.

    Variable: int a;  sizeof (a) 4;

    Pointer: int *p;  sizeof (p) 4;

    Array: int b[10]; sizeof (b) Size of the array, 4*10;int c[0]; sizeof (c) equals 0

    struct: struct (int A; char ch;) s1;  sizeof (S1) for 8  is related to struct-byte alignment.

    Note: You cannot use sizeof

    sizeof (void) for bit field members in structs equal to 1

    sizeof (void *) equals 4

  • the difference between Malloc/free and New/delete

    1) malloc and free are standard library functions for the C/s + + language, andNew/delete is a C + + operator. They can all be used to request dynamic memory and free memory.

    2)   for objects of non-intrinsic data types, the light maloc/free cannot meet the requirements of dynamic objects. Objects are automatically executed when they are created, and the object executes the destructor automatically before it dies. Since malloc/free is a library function and not an operator, it is not possible to impose the task of executing constructors and destructors on malloc/free without the compiler controlling permissions. therefore c++ language requires an operator that can perform dynamic memory allocation and initialization work new, and an operator that can perform cleanup and release of memory work delete. Note new/delete is not a library function.
    We do not attempt to use the malloc/free to complete the memory management of dynamic objects, should use new/delete. Because of the internal data type of the "object " there is no process of structuring and destruction, for them malloc/free and new/delete is equivalent.

    3) since the function of new/delete completely covered the malloc/free, why C + + does not put malloc/free out of the elimination? This is because C + + programs often call C functions, whereas C programs can only use Malloc/free to manage dynamic memory.
    If you release the dynamic object created by new with free , the object can cause a program error because it cannot execute the destructor. If you use Delete to release the dynamic memory for malloc requests , the result can also cause the program to fail, but the program is poorly readable. So New/delete must be paired andmalloc/free the same.

C Language interview questions

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.