Home »
C programs »
C preprocessors programs
C program to print the line number in source code with filename and function name
Here, we are going to learn how to print the line number in source code with filename and function name using C program?
By Nidhi Last updated : March 10, 2024
Problem Solution
In this program, we will print the line number in the source code using __LINE__ macro on the console screen.
Macros Used
Here, we will use three macros, __LINE__, __FUNCTION__, and __FILE__.
- __LINE__ Macro: __LINE__ macro is a preprocessor in C language and it is used to get the current line number of the source file, as an integer.
- __FUNCTION__ Macro: __FUNCTION__ macro is a preprocessor in C language and it is used to get the current function name. It is useful for debugging i.e., to generate the logs.
- __FILE__ Macro: __FILE__ is a preprocessor macro in C language that is used to get the full path to the current file. It is useful for debugging to generate the log statements, error messages intended for programmers, when throwing exceptions, or when writing debugging code.
Program
The source code to print the line number in the source code with filename and function name is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.
// C program to print the line number in source code
// with filename and function name
#include <stdio.h>
int main()
{
printf("Line Number %s->%s:%d\n", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
Output
Line Number main.c->main:8
Explanation
In the main() function, we printed the line number with filename and function name using __LINE__, __FILE__, and __FUNCTION__ macros on the console screen.
C Preprocessors Programs »