Jump to content

use of attribute keyword in C

- - - - -

  • Please log in to reply
13 replies to this topic

#1
onus

onus

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
Hi,
I am not clear with use of __attribute__ keyword in C.I had read the relevant docs of gcc but still I am not able to understand this.Can some one help to understand.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

onus said:

Hi,
I am not clear with use of __attribute__ keyword in C.I had read the relevant docs of gcc but still I am not able to understand this.Can some one help to understand.

You can tell the compiler to apply attributes to functions and optimize it as so, for example defining a function that is not to return anything i.e. a fatal() or exit() function:
void fatal () __attribute__ ((noreturn));
void fatal (char* msg) {
    fprintf(stderr, "%s", msg);
    exit(1);[SIZE=-1]
}[/SIZE]
The documentation within GCC for __attribute__ is here:
Using and Porting the GNU Compiler Collection (GCC): C Extensions

I find it fairly straightforward, what parts do you not get? You may find some simple examples of the different attributes of use to your learning:
Using GNU C __attribute__
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
onus

onus

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts

Nullw0rm said:

I find it fairly straightforward, what parts do you not get? You may find some simple examples of the different attributes of use to your learning:
Using GNU C __attribute__
Ok I think I had looked that link and the GNU C link also before asking here.
Let us deal one by one excerpts from the above page says
This __attribute__ allows assigning printf-like or scanf-like characteristics to the declared function, and this enables the compiler to check the format string against the parameters provided throughout the code. This is exceptionally helpful in tracking down hard-to-find bugs.

There are two flavors: 

So what does the above paragraph means?

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript

Quote

This __attribute__ allows assigning printf-like or scanf-like characteristics to the declared function, and this enables the compiler to check the format string against the parameters provided throughout the code. This is exceptionally helpful in tracking down hard-to-find bugs.
There are two flavors:
__attribute__((format(printf,m,n)))
__attribute__((format(scanf,m,n)))
but in practice we use the first one much more often.
The (m) is the number of the "format string" parameter, and (n) is the number of the first variadic parameter. To see some examples:

Are you familiar with the standard C functions printf and scanf? Unlike Microsoft compilers, GCC makes sure that your format string matches the arguments you pass in. Using the attributes outlined above, you can write your own function whose format string will get checked just like printf and scanf are.
sudo rm -rf /

#5
onus

onus

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
I have used scanf and printf in C programs but still from your reply I could not understand

dargueta said:

sure that your format string matches the arguments you pass in.
What do you mean by format string and the matching arguments?

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
This is what I mean about not matching.
printf("%s", 5);

sudo rm -rf /

#7
onus

onus

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
Here is a C program I wrote but I am not able to understand how will I use attribute in it
#include<stdio.h>

int myfunction (int ,int);

int main ()

{

 int a,b,c;

 c=myfunction(a,b);

 printf("the result is %d ",c);

}


int myfunction (int a,int b)

{

int c;

return c=a+b;

}



#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
What do you want to use the attribute for? There's a lot of things it's capable of doing.
sudo rm -rf /

#9
onus

onus

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
I first want to understand how __attribute__ works so any simple thing would be a good start for me.I looked at the format arguments where on your link it was mentioned scanf or printf like arguments but what is the difference between scanf and print for attribute?

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
printf lets you specify field widths and other things that scanf has no need for. For example:

This code
printf("%#04x", 50)

produces the output 0x0032. If you pass this format string to scanf, it'll choke, because you have no need for forcing the leading zeros and such.
sudo rm -rf /

#11
onus

onus

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
Why does the above give out put 0x0032?

#12
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
# means print out the base prefix for the number you're printing out. For hexadecimal this is a leading 0x, for octal it's a leading 0, and decimal has none.
04 means that the number must take up at least four digits, and be padded with zeros if it's shorter. If I had just put 4 then printf would've padded it with spaces.
x means print out the number in lowercase hexadecimal. X uses uppercase letters.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users