Talk C chestnuts together (124th back: C language instance-built-in macro)

Source: Internet
Author: User

Talk C chestnuts together (124th back: C language instance-built-in macro)

Hello, everyone. In the previous session, we talked about the example of displaying variables and function addresses. This example is as follows:Built-in macros. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

When compiling a program, if there is a syntax error, the compiler will prompt that there is an error in a row of a file. How does this happen? In fact, the built-in Macros in C language are used.

Speaking of macros, I think everyone knows that the so-called built-in macros are similar to the keywords of the C language. They are defined by the C language itself. Therefore, we can directly use them without defining macros. Common built-in macros"_ LINE _And_ FILE _". If you put them in the code, you can enter the row number and file name of their location. When the compiler outputs incorrect file names and row numbers, they are used.

In fact, they are often used to add debugging information to files. Because the program we write will inevitably have errors, we need to debug the program. After adding a built-in macro to the file, we can quickly find the row in which the error occurred through debugging. In this way, errors in the program can be quickly modified to improve work efficiency.

Let's take a simple example to illustrate the use of built-in macros. The following is a detailed code for your reference:

#include
  
   int show(){     printf("hello \n");    printf("file:%s line:%d  \n",__FILE__,__LINE__);    return 0;}int main(){    printf("file:%s line:%d  \n",__FILE__,__LINE__);    show();    return 0;}
  

In the code, we use a built-in macro to output the current file name and row number. The following is the running result of the program. For details, refer:

File: test. c line: 13 // This is the content in the main function. hello file: test. c line: 7 // This is the content in the show function.

Let's talk about the example of built-in macros. I want to know what examples will be provided later, and I will try again.

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.