In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you how to use _ _ FILE__ and _ _ LINE__. The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn how to use _ _ FILE__ and _ _ LINE__.
Foreword:
When we write programs, we always add some statements such as printf to output debugging information, but the inconvenience of printf statements is that when we need to release the program, we have to delete these statements one by one, and once we need to debug again, these statements have to be added one by one, which brings us a lot of inconvenience and wastes a lot of our time. It also results in low efficiency of debugging. Therefore, many people will choose to use macro definition to output debug statements.
For example, define a macro switch:
# define _ _ DEBUG
When debugging is needed, use the statement:
# ifdef _ _ DEBUGprintf (xxx); # endif
This way of debugging, you can tell the compiler not to compile these statements by undef _ _ DEBUG, so that they are no longer output. But the trouble in this way is also obvious, each debugging statement needs to be surrounded by two macro definitions, which is not only inconvenient to write the code, the source code structure is not good-looking, the workload is still not small.
How comfortable it would be if we could program one of these three statements, so we thought of using statements like this:
# ifdef _ DEBUG#define DEBUG (info) printf (info) # else#define DEBUG (info) # endif
In this way, when we write the code, we can use a DEBUG statement, we turn on the macro switch _ _ DEBUG, all the DEBUG (info) macro definition information will be replaced with printf (info), and turn off will be replaced with empty, so it will not be compiled. Well, it's much more convenient this time. One sentence is fine. However, the problem also arises. Printf supports multiple parameters and is variable. When you use a statement like this, you will get an error:
DEBUG ("% s", msg)
This is because the macro definition of DEBUG (info) only supports the replacement of one parameter.
Therefore, we want DEBUG to be able to support multiple parameters like printf, and these parameters just expand into the parameters used by the printf statement itself, for example, we want DEBUG ("% s", msg) to be expanded to printf ("% s", msg).
Text:
After consulting the data on the Internet, it is found that since the C99 specification, the compiler has begun to support the macro definition of variable parameters, just like printf.
Please take a look at this article: http://blog.csdn.net/aobai219/archive/2010/12/22/6092292.aspx
(this link also turns, I can't find out who the original author is, alas, the Internet. )
So, we define something like this:
# define DEBUG (format,...) Printf (format, # # _ VA_ARGS__) / / ('# # 'means that if the variable parameter is ignored or empty, the preprocessor (preprocessor) removes the comma before it. )
As a result, we miraculously found that DEBUG completely replaced printf, all DEBUG (…) All have been replaced by printf (…) I won't worry about that abominable comma anymore.
However, we found that printf alone is not enough, although the debugging information is output, but a lot of debugging information output, we can not all of a sudden know where this information is printed out, so we thought, can we print out the current file name and source line location, so it is clear at a glance, where do you need to think, to find where the debugging information is output? It's all printed out!
So we have the following story.
Compiler built-in macros:
First introduce several compiler built-in macro definitions, these macro definitions can not only help us to complete cross-platform source code writing, flexible use can also skillfully help us output very useful debugging information.
There are several standard predefined macros (also commonly used) in the ANSI C standard:
_ _ LINE__: inserts the current source code line number in the source code
_ _ FILE__: inserts the current source file name in the source file
_ _ DATE__: inserts the current compilation date in the source file
_ _ TIME__: inserts the current compilation time in the source file
_ _ STDC__: this identity is assigned to 1 when the program is required to strictly follow the ANSI C standard
_ _ cplusplus: this identifier is defined when writing C++ programs.
When the compiler compiles the source code, it automatically replaces these macros with the corresponding content.
When you see this, your eyes should light up. Well, yes, _ _ FILE__ and _ _ LINE__ are exactly what we wanted to output before, so each of our statements becomes:
DEBUG ("FILE:% s, LINE:% d …" , _ _ FILE__,__LINE__,...)
In fact, it is not necessary, _ _ FILE__ itself will be replaced by the compiler with character constants, so our statement becomes like this again:
DEBUG ("FILE:" _ _ FILE__ ", LINE:% d …" , _ _ LINE__,...)
However, we are still not satisfied, still find, still hate, why every statement has to write "FILE:" _ _ FILE__ ", LINE:% d and, _ _ LINE, these two parts? isn't this a waste of our time?
Haha, yes, this is the finale, writing DEBUG like this:
DEBUG (format,...) Printf ("FILE:" _ _ FILE__ ", LINE:% d:" format "/ n", _ _ LINE__, # # _ VA_ARGS__)
Yes, that's it! Below, all DEBUG information is output in this way:
FILE: xxx, LINE: xxx, … .
Finally:
Finally, the old rule, the coding test.
/ / = / / Name: debug.cpp / / Author: boyce / / Version: 1.0 / / Copyright: pku / / Description: Hello World in clients, Ansi-style / / = # include # define _ DEBUG__ # ifdef _ DEBUG__ # define DEBUG (format,...) Printf ("File:" _ _ FILE__ ", Line: d:" format "/ n", _ _ LINE__, # # _ VA_ARGS__) # else # define DEBUG (format,...) # endif int main () {char str [] = "Hello World"; DEBUG ("A ha, check me:% s", str); return 0;}
Test results:
The above is all the contents of the article "how to use _ _ FILE__ and _ _ LINE__". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.