Common C language Knowledge points

Source: Internet
Author: User

1.How do you refer to a global variable that is already defined in the C language?


Answer:① the way the header file is referenced; ② references the extern keyword.

Note: ※⑴ If a reference to a header file refers to a global variable declared in a header file, an error occurs during compilation if the header file name is written incorrectly.

※⑵ If you refer to the extern keyword, if you write the name incorrectly, you will not get an error during compilation, but the connection times are wrong.



2. Global variables can be defined in multiple. C file contained in the header file? Why?


Answer: Yes. Declare a global variable with the same name in static form in a different C file. Declare a global variable of the same name in a different C file, provided that only one C file is assigned an initial value for this variable, and the connection does not go wrong. In short, the initial assignment can only be made once, and may be declared more than once.



3.What is the difference between a static modified global variable and a normal global variable? What about local variables? What about the function?


Answer:

① the global variable with the static modification, it changes its scope, restricts its scope of use-----> The global variable itself is a static storage method, and static storage is still stored, the storage mode has not changed. Global variables that do not have the static modifier are valid within each source file, and static qualifies only in the source file that defines the variable, which cannot be used in other source files of the same source program.

② local variable plus STATOC modification, it changes the way it is stored, or it changes the lifetime of the variable-----> local variables are stored on the stack, and the current code snippet (scope or function body) is destroyed, while the static modified local variables are stored in the quiescent area. The life cycle is not destroyed until the end of the program. The static modified local variable can only be initialized once, next time based on the last result value. After the ③ function body is modified with a static modifier, the scope changes, only valid in this file-----The >static decorated function has only one copy in memory, while the normal function maintains a copy on each invocation.




4.int a=5;a= (a=3*5,a*4), a+5; Q How much is it?


The problem is simple, but beware of traps. The precedence of the operator is the trap here, the assignment operator takes precedence over the comma operator, and the parentheses operator takes precedence over the assignment operator. So a=15*4=60, the answer is 60, if you add the later 5, then jump into the "pit".




5.What is the difference between a macro and a function in the C language?


Answer:

①. Macros are simply replaced by the compiler when compiling the source code, without any logic detection.

②. Macros are defined without taking into account the type of the parameter, even if the type is specified as a parameter, the macro's parameters are only replaced as is, and the function specifies the parameter type, and the argument is passed only by the type defined.

③. The use of macros will cause blocks of code with the same effect to produce multiple copies in different places, which will increase the size of the target file, and a large number of macros will make the code lengthy and cumbersome, while the function call copies only one copy.

④. Macros run faster than functions, and using macros is a quick and easy way to use a macro if the function call and return consume time that is equivalent to executing the module, or much more than the execution time.

⑤. Because macros do not have any logical detection at compile time, they should be used with parentheses at the corresponding parameter positions to ensure the order of precedence execution.

⑥. Parameters of a function can pass a pointer (address), or it can pass a specific value, whereas a macro is simply a replacement.

⑦. When debugging the program, the function can be debugged, but the macro is not debugging, the compilation phase of the macro will no longer exist, the direct replacement is complete.



The remaining points are in the finishing, I new one, what error also trouble you big God guidance, together explore ... 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0059.gif "alt=" J_0059.gif "/>

This article is from the "Warm Smile" blog, please be sure to keep this source http://10738469.blog.51cto.com/10728469/1738808

Common C language Knowledge points

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.