Talk C Chestnut Bar (169th time: C Language Example-------the built-in macro)

Source: Internet
Author: User

Ladies and gentlemen, crossing, the last time we were talking about the example of a Boolean –bool type in C, this time we're talking about a built-in macro . Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, we introduced the built-in macros in the C language in the 124th time, introducing the commonly used built-in macro _line_ and _file_. It also describes how to use them to output the line number and the file name where they are located. As you know, these two built-in macros are often used to add debugging information to a file. Because we write the program will inevitably have errors, so we need to debug the program. After adding a built-in macro to the file, we can quickly find out which line of the file the error occurred in by debugging. This makes it possible to quickly modify errors in the program, thereby increasing productivity. However, some crossing are not satisfied with the file name and the number of rows when debugging, and they also want to know the date and time associated with the error, which is why we'll talk about the built-in macros.

In this chapter, we will look at the other two built-in macros _date_ and _time_. They represent the date and time when the program was compiled. Next we use specific code to illustrate the use of the two built-in macros, the following is the detailed code please refer to:

#include <stdio.h>void Func () {intlen=2; while(len-->0)    {printf("Sleeping ... \ n");Sleep(1); }}intMain () {printf("File:%s line:%d date:%s, time:%s \ n", __file__, __line__, __date__, __time__); Func ();printf("File:%s line:%d date:%s, time:%s \ n", __file__, __line__, __date__, __time__);return 0;}

We in the code through the built-in macro to output the current file name and line number and compile the date and time of the files, the following is the operation of the program results, please refer to:

file :test.c line:17302016,time:20:45:39...... file :test.c line:19302016,time:20:45:39

You can see the program's file name, compile date, and time from the results of the program running above. However, the output was two times, and the reason for this is to emphasize that the _TIME_ macro represents the compile time of the program, not the run time of the program. As you can see, when we use the built-in macro output time, the first time is before the Func () function is called, the second time after the Func () function is called, and the Func () function in the code keeps the program dormant for 2 seconds. Normally, the output time should be delayed by two seconds, but the time we output two times is the same, indicating that the method output time is not the program run time, but the program compile time. It's kind of a contradiction, haha.

This explains so much, what does this built-in macro for date and time have to do? Crossing, and listen to me give you a word. In the actual work, we can not complete a large program one day, so there will be more than one version of the program. If the program is wrong, we can know the date and time of the program compile, can be based on the release date and time of the program version of the wrong version, in a specific version to find errors, more than one version of the error to find more convenient, because this can narrow the scope of the lookup error, And then quickly find out why the error occurred. Given this principle, we often use the built-in macros of date and time to confirm the version of the program.

Crossing, let's talk about the example of built-in macros. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (169th time: C Language Example-------the built-in macro)

Related Article

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.