Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29238 Discussions

__LINE__ directive in a string variable?

OP1
New Contributor III
1,844 Views

Is there a way to have the preprocessor write __LINE__ inside a string variable directly, as shown below:

S = 'This is line __LINE__'

My tests indicate this does not work; and I have to resort to assigning __LINE__ to an integer variable, then perform an internal write of that variable into a string that I can later concatenate with the rest of my text. I would be curious if there is a workaround.

4 Replies
Steven_L_Intel1
Employee
1,844 Views

If there is, I haven't found it myself, and I've tried....

0 Kudos
IanH
Honored Contributor III
1,844 Views
#define THIS_IS_LINE(line_text) 'This is line ' // #line_text
#define DESCRIBE_THIS_LINE(the_line) THIS_IS_LINE(the_line)
  CHARACTER(*), PARAMETER :: s = DESCRIBE_THIS_LINE(__LINE__)
  PRINT *, s
END

 

>ifort /fpp "2015-01-14 preprocessor.F90" && "2015-01-14 preprocessor.exe"
Intel(R) Visual Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 15.0.1.148 Build 20141023
Copyright (C) 1985-2014 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation.  All rights reserved.

"-out:2015-01-14 preprocessor.exe"
-subsystem:console
"2015-01-14 preprocessor.obj"
 This is line 3

 

mecej4
Honored Contributor III
1,844 Views

IanH's solution has the effect of printing the line number of the declaration of s rather than the line number of the PRINT statement or the line in which s is defined before printing. If the user wants the latter, he/she could use "S = 'This is line' // __LINE__".

OP1
New Contributor III
1,844 Views

Thanks IanH!! Very clever solution - it works perfectly.

0 Kudos
Reply