Monday, June 22, 2015

How to print the current line and the file name in a c program?

'C' compilers usually define preprocessor macros along with our header file inclusion macro.  There are many different useful preprocessor macros available, two of them are __FILE__ & __LINE__. 

__FILE__ -> will be replaced by the executing filename.

__LINE__ -> will be replaced by the current line of execution.

Example program - test.c:

1 #include

3 int main()
4 {
5  printf("\n%s\t%d",__FILE__, __LINE__);
6  return 0;
7 }

while running the above program outputs:
tst.c        5


No comments: