C language Debugging means: the implementation of locking errors _c language

Source: Internet
Author: User
Tags assert current time
In the project development project, if you can determine which file under the which line under which function is wrong--lock error, that would be good, the article is for this purpose.
First, let's take a look at the file default output information function:
File information functions:
Copy Code code as follows:

printf ("Line:%d\n", __line__); Current number of rows
printf ("FileName:%s\n", __file__); Current file name
printf ("Function:%s\n", __function__); Current function
printf ("Time:%s\n", __time__); Current time
printf ("Date:%s\n", __date__); Current date
Output:
Line:10
Filename:test.c
Function:main.c
Time:14:13:51
Date:oct 13 2012

The theory is enough, let's see how to lock the bug:
First, the source file:
Copy Code code as follows:

[Root@localhost for_test]# Cat erroutput.c
#include <stdio.h>
#include <assert.h>
#define _DEBUG (msg ...)  printf ("[%s,%s,%d]=>", __file__, __function__, __line__); printf (msg);p rintf ("\ r \ n")
#define _ERROR (msg ...) printf ("[ERROR:%s,%d]=>", __file__, __line__);p rintf (msg); printf ("\ r \ n")
#define _ASSERT (exp) \
do {\
if (!) ( EXP)) {\
printf ("[%s]", #exp);p rintf ("\ r \ n"); \
ASSERT (exp); \
}\
} while (0)
int main (void)
{
char *p = NULL;
_DEBUG ("debug!");
_error ("error!");
_assert (NULL!= p);
return 0;
}

Second, the output:
Copy Code code as follows:

[Root@localhost for_test]# gcc erroutput.c
[Root@localhost for_test]#./a.out
[Erroutput.c,main,]=>debug!
[Error:erroutput.c, 18]=>error!
[NULL!= P]
A.out:erroutput.c:19:main:assertion ' ((void *) 0)!= p ' failed.
Has abandoned

ti Processing:
Copy Code code as follows:

#ifdef DEBUG
#define DBG (FMT, args ...) printf ("Debug" FMT, # #args)//# #运算符用于把参数连接到一起. The preprocessor appears in # #两侧的参数合并成一个符号.
#else
#define DBG (FMT, args ...)
#endif
#define ERR (FMT, args ...) printf ("Error" FMT, # #args)
[Root@localhost for_test]# Cat Debug_err.c
#include <stdio.h>
#define DEBUG
int main (void)
{
DBG ("xxxx\n");
ERR ("xxxx\n");
return 0;
}
[Root@localhost for_test]#./a.out
Error xxxx

#ifdef __debug
#define DBG (FMT, args ...) fprintf (stderr, "Encode Debug:" Fmt, # # args)
#else
#define DBG (FMT, args ...)
#endif
#define ERR (FMT, args ...) fprintf (stderr, "Encode Error:" Fmt, # # args)

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.