Error using '__attribute__'

Discussion in 'C Programming' started by ritesh, May 18, 2006.

  1. ritesh

    ritesh Guest

    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
     
    ritesh, May 18, 2006
    #1
    1. Advertising

  2. ritesh

    Bill Pursell Guest

    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);
    }
     
    Bill Pursell, May 18, 2006
    #2
    1. Advertising

  3. ritesh

    Marc Thrun Guest

    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/
     
    Marc Thrun, May 18, 2006
    #3
  4. ritesh

    ritesh Guest

    <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
     
    ritesh, May 18, 2006
    #4
  5. ritesh

    Flash Gordon Guest

    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
     
    Flash Gordon, May 18, 2006
    #5
  6. ritesh

    Jordan Abel Guest

    On 2006-05-18, Flash Gordon <> 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!
     
    Jordan Abel, May 18, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krzysztof Kolago

    Problem with __attribute__

    Krzysztof Kolago, Nov 19, 2003, in forum: C++
    Replies:
    1
    Views:
    400
    Victor Bazarov
    Nov 19, 2003
  2. Replies:
    1
    Views:
    335
    Ian Collins
    Feb 28, 2007
  3. __attribute__ ((mode(__byte__)))

    , Oct 9, 2007, in forum: C Programming
    Replies:
    4
    Views:
    621
    Keith Thompson
    Oct 9, 2007
  4. nagaraj
    Replies:
    0
    Views:
    510
    nagaraj
    Apr 14, 2008
  5. parag
    Replies:
    0
    Views:
    218
    parag
    Jun 13, 2011
Loading...

Share This Page