473,322 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Error using '__attribute__'

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

May 18 '06 #1
5 2386

ritesh wrote:
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);
}

May 18 '06 #2
ritesh schrieb:
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 <stdio.h>
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.

--
Marc Thrun
http://www.tekwarrior.de/
May 18 '06 #3
<Marc Thrun>
__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

May 18 '06 #4
ritesh wrote:
<Marc Thrun>
__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?


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!
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 18 '06 #5
On 2006-05-18, Flash Gordon <sp**@flash-gordon.me.uk> wrote:
ritesh wrote:
<Marc Thrun>
__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?


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.


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.
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!

May 18 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Krzysztof Kolago | last post by:
Hello! I've download some header files from Internet and I've tried to use them in my program, but Borland C++ Builder is stopping, when it reach this structure: struct fann_neuron {...
3
by: Per Johansson | last post by:
Compiling code using GCC 3.2.2 with the following function declaration give this error. extern void APIENTRY glutDisplayFunc (void (* APIENTRY)(void)); Changing to: extern void APIENTRY...
1
by: DiskMan | last post by:
System: Redhat 7.2 Kernel-2.6.11.8 GCC-3.4.3 CCC-6.5.9 Binutils-2.15 Make-3.80 GTK/GLIB-2.6.7 For some reason my Linux box is suddenly having issues trying to read ;
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
3
by: SpreadTooThin | last post by:
I am trying to figure out why I am getting this error: I define a type in a header file. I include that file. typedef struct x * xref; I include it in my class class myclass { xref xr;
4
by: ...vagrahb | last post by:
Hi, I have the following structure struct Format { char x; unsigned char a; unsigned char b; unsigned char c; char y;
7
by: singhPrabhat | last post by:
Hi, I am using gcc (GCC) 4.2.1 (SUSE Linux). SUSE 10.3 While compiling a .c file I get following error :::: CCWebConfiguration.h:70: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or...
2
by: akhilesh.noida | last post by:
I am trying to compile glibc-2.5 for ARM based board. But I am getting errors while configuring it. Please check and give your inputs for resolving this. configure command : $...
10
by: anju1401 | last post by:
Hi I am using WINAVR compiler for ATMEGA32. While compiling c progam , I am getting the error error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before '{' token before evrey function. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.