By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

Stringification of the __LINE__ macro

P: n/a
Hi,

I am trying the use the current line number in my logging and I want
to stringify it so that I know the size of the buffer required to
output the log string. I would have though I could use #__LINE__ in a
macro like:

#define LOG_ERROR(err) LogError(err, __FILE__, #__LINE__)

but all i get in the output is #<linenumber>. Why should this be?
And is there another way to accomplish what i am trying to do.

Thanks

Feb 25 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a

pi********@googlemail.com wrote:
Hi,

I am trying the use the current line number in my logging and I want
to stringify it so that I know the size of the buffer required to
output the log string. I would have though I could use #__LINE__ in a
macro like:

#define LOG_ERROR(err) LogError(err, __FILE__, #__LINE__)

but all i get in the output is #<linenumber>. Why should this be?
And is there another way to accomplish what i am trying to do.

Thanks
Try this:

#include <stdio.h>

#define strfy(line) #line
#define LOG_ERROR(err, file, line) LogError(err, file, (strfy(line)))

void LogError(int, char *, char *);

int main(void) {
LOG_ERROR(2, __FILE__, __LINE__);
return 0;
}

void LogError(int err, char *f, char *l) {
fprintf(stderr, "err = %d\tfile = %s\tline = %s\n", err, f, l);
return;
}

Feb 25 '07 #2

P: n/a
On 25 Feb, 19:29, "santosh" <santosh....@gmail.comwrote:
pistmas...@googlemail.com wrote:
Hi,
I am trying the use the current line number in my logging and I want
to stringify it so that I know the size of the buffer required to
output the log string. I would have though I could use #__LINE__ in a
macro like:
#define LOG_ERROR(err) LogError(err, __FILE__, #__LINE__)
but all i get in the output is #<linenumber>. Why should this be?
And is there another way to accomplish what i am trying to do.
Thanks

Try this:

#include <stdio.h>

#define strfy(line) #line
#define LOG_ERROR(err, file, line) LogError(err, file, (strfy(line)))

void LogError(int, char *, char *);

int main(void) {
LOG_ERROR(2, __FILE__, __LINE__);
return 0;

}

void LogError(int err, char *f, char *l) {
fprintf(stderr, "err = %d\tfile = %s\tline = %s\n", err, f, l);
return;

}
Yes that works thanks :-)

Feb 25 '07 #3

P: n/a
On Feb 25, 1:52 pm, pistmas...@googlemail.com wrote:
Hi,

I am trying the use the current line number in my logging and I want
to stringify it so that I know the size of the buffer required to
output the log string. I would have though I could use #__LINE__ in a
macro like:

#define LOG_ERROR(err) LogError(err, __FILE__, #__LINE__)

but all i get in the output is #<linenumber>. Why should this be?
And is there another way to accomplish what i am trying to do.
The # operator can only be applied to a macro parameter, what you have
is not valid. Try this:

#define STRINGIFY(x) #x
#define LOG_ERROR(err) LOG_ERROR2(err, __FILE__, __LINE__)
#define LOG_ERROR2(err, file, line) LogError(err, file,
STRINGIFY(line))

Robert Gamble
Feb 25 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.