2

Possible Duplicate:
C/C++ line number

I want to display the line number where the printf is triggered?

It may look like this:

printf("the line number is: %d",SOME_LIBC_MACRO);

How to do it?

Community
  • 1
  • 1
developer
  • 4,744
  • 7
  • 40
  • 55

1 Answers1

12

Use the __LINE__ macro

printf("the line number is: %d", __LINE__);

Lists of other pre-defined macros are available for GCC and Visual Studio

simonc
  • 41,632
  • 12
  • 85
  • 103
  • @unwind Thanks for the link and typo correction. I'd just found that link to pre-defined macros but you beat me to it. – simonc Dec 20 '12 at 15:57
  • also, some compilers support __func__ or __FUNCTION__ however, it's not a standard one. – anishsane Dec 20 '12 at 15:58
  • @anishsane `__func__` is standard; it's defined in [C99](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf) (§6.4.2.2) and [C++11](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf) (§8.4.1). `__FUNCTION__` is non-standard but implemented by many compilers (some require certain flags to enable it). – Brian Campbell Dec 20 '12 at 17:53
  • I think, I should have `back-quoted` `__func__` :-) – anishsane Dec 21 '12 at 05:22