Monday, July 9, 2007

How do you get the line numbers in C?

Use the following Macros


__FILE__ Source file name (string constant) format "patx.c"
__LINE__ Current source line number (integer)
__DATE__ Date compiled (string constant)format "Dec 14 1985"
__TIME__ Time compiled (string constant) format "15:24:26"
__TIMESTAMP__ Compile date/time (string constant)format "Tue Nov 19 11:39:12 1997"


Usage example


static char stamp[] = "***\nmodule " __FILE__ "\ncompiled " __TIMESTAMP__ "\n***";

...

int main()
{
...

if ( (fp = fopen(fl,"r")) == NULL )
{
printf( "open failed, line %d\n%s\n",__LINE__, stamp );
exit( 4 );
}

...
}


And the output is something like


*** open failed, line 67
******
module myfile.c
compiled Mon Jan 15 11:15:56 1999
***

5 comments:

Anonymous said...

Additionally, there is __FUNCTION__ for function name.

Anonymous said...

Yes! Finally someone writes about compiled.

Take a look at my page; registry cleaner

lee woo said...

A wise man can learn more from a foolish question than a fool can learn from a wise answer. See the link below for more info.

#answer
www.mocsbar.com

andrea chiu said...


I understand that every person has the passion in any aspects or things. If you love something and it came in front of you it completes your day and your mood turns into something you won't expected. I love your work and I want to read more about it. Visit my site if you have time . Thank you.

triciajoy.com

www.triciajoy.com

Cindy Dy said...


Very interesting story. I didn't find it boring to read. In fact, I really had a lot of fun reading your post. Thanks.

Bob
www.gofastek.com