Error using '__attribute__'

R

ritesh

Hi,

I'm compiling the following code on linux/cc and keep getting a
compiler error
"attrib.c:4: syntax error before '{' token"

I'm using the following options to compile -
cc -Wall -c attrib.c

#include "stdio.h"

void myprintf(const char *format, ...) __attribute__((format(printf,
1, 2)))
{
printf(format);
}

int main()
{
myprintf("s=%s \n", "hello");
return 0;
}

I had just learned about this __attribute__ mechanism and was trying it
out for the first time. Is there a problem with the way the
__attribute__ portion of the code is written?

Regards,
Ritesh Kapoor
 
B

Bill Pursell

ritesh said:
I'm compiling the following code on linux/cc and keep getting a
compiler error
"attrib.c:4: syntax error before '{' token"

I'm using the following options to compile -
cc -Wall -c attrib.c

#include "stdio.h"

void myprintf(const char *format, ...) __attribute__((format(printf,
1, 2)))
{
printf(format);
}


You need to use the attribute line in the declaration only:

void myprintf(const char *format, ...) __attribute__((format(printf, 1,
2)));

void
myprintf(const char *format,...)
{
printf(format);
}
 
M

Marc Thrun

ritesh said:
Hi,

I'm compiling the following code on linux/cc and keep getting a
compiler error
"attrib.c:4: syntax error before '{' token"

I'm using the following options to compile -
cc -Wall -c attrib.c

#include "stdio.h"
#include said:
void myprintf(const char *format, ...) __attribute__((format(printf,
1, 2)))
{
printf(format);
}
__attribute__ is not part of standard C and therefore offtopic in this
group. Try to find a group about your particular implementation.

When ignoring the __attribute__ extension of your compiler, still there
is an error in your code. You can't just call printf() from your
myprintf() function as there is no portable way to pass the parameters
your function got from the caller, what you want is vprintf():

void myprintf(const char *format, ...)
{
va_list argp;
va_start(argp, format);
vprintf(format, argp);
va_end(argp);
}
int main()
Better be explicit about the parameters:

int main(void)
{
myprintf("s=%s \n", "hello");
return 0;
}

I had just learned about this __attribute__ mechanism and was trying it
out for the first time. Is there a problem with the way the
__attribute__ portion of the code is written?

Regards,
Ritesh Kapoor

I don't know if there is an error with your use of __attribute__, except
that it's non-standard.
 
R

ritesh

__attribute__ is not part of standard C and therefore offtopic in this
group. Try to find a group about your particular implementation.

Thanks for reminding me that, i forgot this was gcc specific

However is there anything in the standard that allows this kind of
checking for format string and the parameters passed?

Suppose I wanted to write my own printf-like function and that using
vprintf or any other v*** fn was not an option. How can I perform this
check?

Keeping the problem simple lets assume that I want to use the %d, %s,
%f and the rest of the standard argument specifiers only and their
meanings remain the same.

Regards,
Ritesh Kapoor
 
F

Flash Gordon

ritesh said:
Thanks for reminding me that, i forgot this was gcc specific

However is there anything in the standard that allows this kind of
checking for format string and the parameters passed?

No. The C standard does not even suggest that it should be done for printf!
Suppose I wanted to write my own printf-like function and that using
vprintf or any other v*** fn was not an option. How can I perform this
check?

You can't in standard C.
Keeping the problem simple lets assume that I want to use the %d, %s,
%f and the rest of the standard argument specifiers only and their
meanings remain the same.

The only way is using a compiler specific extension. Of course, another
compiler might accept the extension but do something completely
different with it!
 
J

Jordan Abel

No. The C standard does not even suggest that it should be done for printf!


You can't in standard C.

If you meant writing a printf-like function without using v...., I think
he meant he wanted to write his own printf-like function that interprets
a subset of format specifiers manually.

If you meant he can't do the argument check, you're right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top