Q:

C program to print the line number in source code with filename and function name

belongs to collection: C Preprocessors Programs

0

C program to print the line number in source code with filename and function name

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will print the line number in the source code using __LINE__ macro on the console screen.

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< C program to print the name of the source code fil...