I am trying to understand usage of keyword __attribute in C
I have checked following two pages
http://unixwiz.net/techtips/gnu-c-attributes.html
and this page also
http://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html
but still I could not understand where do I declare them and how I can use __attribute in my program.

To test this I wrote a C program but I am not able to understand how will I use attribute keyword of C in it

#include<stdio.h>
int myfunction (int ,int)
int main ()
{
 int a,b,c;
 printf("Enter 2 numbers \n");
 scanf("%d,%d",&a,&b);
 c = myfunction(a,b);
 printf("the result is %d ",c);
 exit 0;
}

int myfunction (int a,int b)
{
 int c;
return c=a+b;
}

Dani AI

Generated

A few concise, practical points that tie the replies together and clarify how to use GCC/Clang attributes in the small program from .

What they are and where they go
Attributes are compiler extensions (GCC/Clang style) written as __attribute__((...)) and attach extra information to a declaration or definition. For functions and variables the attribute is placed after the declarator; for structs it can follow the closing } (as demonstrated with packed). They do not change language semantics by themselves but let the compiler warn or optimize (format-checking, alignment, purity, noreturn, etc.). is correct: these are nonstandard, so expect portability issues with other compilers (MSVC uses __declspec).

Concrete placement (example)

/* put attribute on the prototype or definition */
int myfunction(int, int) __attribute__((const));

int myfunction(int a, int b) { return a + b; }

For attributes that affect callers (like format(printf,...)) prefer placing them on the prototype so calls are checked.

Useful attributes (brief)

void fatal(const char *msg) __attribute__((noreturn));
void logmsg(const char *fmt, ...) __attribute__((format(printf,1,2)));
int buf __attribute__((aligned(16)));

Troubleshooting & best practices
Fix the basic syntax errors in ’s sample first (missing semicolon after a prototype, exit(0) vs exit 0, include <stdlib.h> or use return 0;). Compile with gcc -Wall -Wextra -std=gnu11 or clang to see attribute-related warnings. If portability is required, wrap attributes in macros that map to __attribute__ for GCC/Clang and to __declspec or nothing on other compilers. Attributes should be applied consistently across declarations and definitions when the information matters to both.

Recommended Answers

All 2 Replies

Here's an example of creating a packed structure using attribute

struct mystr
{
char ca;
unsigned int i;
}__attribute__((packed)) thestr;

I am trying to understand usage of keyword __attribute in C
I have checked following two pages
http://unixwiz.net/techtips/gnu-c-attributes.html
and this page also
http://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html
but still I could not understand where do I declare them and how I can use __attribute in my program.

To test this I wrote a C program but I am not able to understand how will I use attribute keyword of C in it

#include<stdio.h>
int myfunction (int ,int)
int main ()
{
 int a,b,c;
 printf("Enter 2 numbers \n");
 scanf("%d,%d",&a,&b);
 c = myfunction(a,b);
 printf("the result is %d ",c);
 exit 0;
}

int myfunction (int a,int b)
{
 int c;
return c=a+b;
}

Check out the explanations under this link

Remember one thing when you use these: These are compiler dependent and your code might become non portable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.