C / C ++的行号 [英] C/C++ line number

查看:170
本文介绍了C / C ++的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调试的目的起见,可我得到的 C / C ++编译器的行号?
(标准方式或某些特定的编译方式)

例如

 如果(!逻辑)
    的printf(在行号%d个\\ N不合乎逻辑的价值,行号);
    //如何不受我的手,写它得到LINENUMBER?(动态编译)


解决方案

您应该使用preprocessor宏 __ LINE __ __ FILE __ 。他们是predefined宏和C / C ++标准的一部分。在preprocessing,它们是由一个常量字符串拿着重新$ P $整数psenting当前的行号和当前文件名分别替换。

其他preprocessor变量:


  • __ __ FUNC :函数名(这是的 C99 ,并不是所有的C ++编译器支持)

  • __ __ DATE :形式嗯月日年的字符串

  • __ __ TIME :形式的字符串HH:MM:SS


您code将是:

 如果(!逻辑)
  的printf(在文件%s \\ n的行号%d个不符合逻辑的价值,__LINE__,__FILE__);

In the sake of debugging purposes, can I get the line number in C/C++ compilers? (standard way or specific ways for certain compilers)

e.g

if(!Logical)
    printf("Not logical value at line number %d \n",LineNumber);
    // How to get LineNumber without writing it by my hand?(dynamic compilation)

解决方案

You should use the preprocessor macro __LINE__ and __FILE__. They are predefined macros and part of the C/C++ standard. During preprocessing, they are replaced respectively by a constant string holding an integer representing the current line number and by the current file name.

Others preprocessor variables :

  • __func__ : function name (this is part of C99, not all C++ compilers support it)
  • __DATE__ : a string of form "Mmm dd yyyy"
  • __TIME__ : a string of form "hh:mm:ss"

Your code will be :

if(!Logical)
  printf("Not logical value at line number %d in file %s\n", __LINE__, __FILE__);

这篇关于C / C ++的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆