#define min_t(type, x, y) ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
#define min(x,y) ((x) < (y) ? (x) : (y))
#include <stdio.h>
#define MIN(type, x, y) ({ type __x = (x); type __y = (y); __x < __y ? __x: __y;})
int main ()
{
int i = 10;
int a[5] = {8,9,11,10,13};
int *p = a;
int j;
j = MIN(int, *p++, i);
printf("%d\n", j);
printf("%d\n", *p);
return (0);
}
beyes@linux-beyes:~/C/micro> ./micro2.exe
8
9
#include <stdio.h>
#define MIN(x, y) ({ const typeof(x) _x = (x); const typeof(y) _y = (y); _x < _y ? _x:_y;})
int main ()
{
int i = 10;
float a[5] = {8.1,9.1,11.2,10.3,13.4};
float *p = a;
float j;
j = MIN(*p++, i);
printf("%f\n", j);
printf("%f\n", *p);
return (0);
}
beyes@linux-beyes:~/C/micro> ./micro3.exe
8.100000
9.100000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct user_def {
char *name;
int length;
char bytes[0];
} user_def_t;
int main()
{
int length = 10;
user_def_t *p;
p = (user_def_t *)malloc (sizeof(user_def_t) + length);
if (p == NULL) {
printf("malloc failed\\n");
exit(1);
}
p->name = "good";
p->length = length;
memset(p->bytes, 0, length);
p->bytes[0] = 'a';
p->bytes[1] = 'b';
p->bytes[2] = 'c';
printf("%s\\n", p->bytes);
free(p);
return 0;
}
beyes@linux-beyes:~/C/GNU_C_EXT> ./entry.exe
abc
#define pr_debug(fmt, arg...)
printk(KERN_DEBUG fmt, ##arg);
#define debug(...) printf(__VA_ARGS__)
#include <stdio.h>
#define debug(...) printf(__VA_ARGS__)
int main()
{
char a[20] = "hello world\\n";
int i = 10;
debug("i = %d, %s", i, a);
return 0;
}
beyes@linux-beyes:~/C/micro> ./mic.exe
i = 10, hello world
#define debug(format, args...) fprintf(stderr, format, args)
#include <stdio.h>
#define debug(format, args...) fprintf(stderr, format, args)
int main()
{
char a[20] = "hello world\\n";
int i = 10;
debug("i = %d, %s", i, a);
return 0;
}
beyes@linux-beyes:~/C/micro> ./mic.exe
i = 10, hello world
#include <stdio.h>
#define debug(format, args...) fprintf(stderr, format, args)
int main()
{
debug("hello world\\n");
return 0;
}
beyes@linux-beyes:~/C/micro> gcc -g mic.c -o mic.exe
mic.c: In function ‘main’:
mic.c:10: error: expected expression before ‘)’ token
#include <stdio.h>
#define debug(format, args...) fprintf(stderr, format, ##args)
int main()
{
debug("hello world\\n");
return 0;
}
beyes@linux-beyes:~/C/micro> gcc -g mic.c -o mic.exe
beyes@linux-beyes:~/C/micro> ./mic.exe
hello world
#define debug(format, args...) fprintf(stderr, format, ##__VA_ARGS__)
beyes@linux-beyes:~/C/micro> gcc -g mic.c -o mic.exe
mic.c:3:58: warning: __VA_ARGS__ can only appear in the expansion of a C99 variadic macro
mic.c:9:1: error: pasting "," and "__VA_ARGS__" does not give a valid preprocessing token
mic.c: In function ‘main’:
mic.c:9: error: ‘__VA_ARGS__’ undeclared (first use in this function)
mic.c:9: error: (Each undeclared identifier is reported only once
mic.c:9: error: for each function it appears in.)
#define debug(format, ...) fprintf(stderr, format, ##__VA_ARGS__)
#define DEBUG(args) (printf("DEBUG: "), printf args)
if(n != 0) DEBUG(("n is %d\\n", n));
#include <stdio.h>
#define SIZE 10
int main()
{
unsigned long array[SIZE] = {[2 ... SIZE-1] = 8};
int i;
for (i = 0; i < 10; i++)
printf("%d ", array [i]);
printf("\\n");
return 0;
}
beyes@linux-beyes:~/C/GNU_C_EXT> ./sign.exe
0 0 8 8 8 8 8 8 8 8
#include <stdio.h>
void test (char code)
{
switch (code) {
case '0' ... '9':
printf("code in 0~9\\n");
break;
case 'a' ... 'f':
printf("code in a~f\\n");
break;
case 'A' ... 'F':
printf("code in A~F\\n");
break;
default:
printf("no right code\\n");
}
}
int main()
{
test('9');
test('f');
test('z');
test('C');
return (0);
}
beyes@linux-beyes:~/C/GNU_C_EXT> ./case_more.exe
code in 0~9
code in a~f
no right code
code in A~F
void func_test ()
{
printf(" func_test() ");
/* 其他代码 */
}
static const char __func__[] = "functon-name"; |
#include <stdio.h>
void show_name (const char *name)
{
printf("%s\\n", name);
}
void fun_test ()
{
show_name (__FUNCTION__);
printf ("\\n");
}
void fun_test2 ()
{
printf (__func__);
printf ("\\n");
}
int main()
{
fun_test();
fun_test2();
return 0;
}
beyes@linux-beyes:~/C/GNU_C_EXT> ./FUNCTION.exe
fun_test
fun_test2
#include <stdio.h>
int *address;
int *builtin_func ()
{
address = __builtin_return_address(0);
return address;
}
int main()
{
builtin_func();
printf("%p\\n", address);
return (0);
}
beyes@linux-beyes:~/C/GNU_C_EXT> ./builtin.exe
0x804844c
08048436 <main>:
8048436: 8d 4c 24 04 lea 0x4(%esp),%ecx
804843a: 83 e4 f0 and $0xfffffff0,%esp
804843d: ff 71 fc pushl -0x4(%ecx)
8048440: 55 push %ebp
8048441: 89 e5 mov %esp,%ebp
8048443: 51 push %ecx
8048444: 83 ec 14 sub $0x14,%esp
8048447: e8 d8 ff ff ff call 8048424 <builtin_func>
804844c: a1 1c a0 04 08 mov 0x804a01c,%eax
8048451: 89 44 24 04 mov %eax,0x4(%esp)
8048455: c7 04 24 30 85 04 08 movl $0x8048530,(%esp)
804845c: e8 f3 fe ff ff call 8048354 <printf@plt>
8048461: b8 00 00 00 00 mov $0x0,%eax
8048466: 83 c4 14 add $0x14,%esp
8048469: 59 pop %ecx
804846a: 5d pop %ebp
804846b: 8d 61 fc lea -0x4(%ecx),%esp
804846e: c3 ret
804846f: 90 nop
#include <stdio.h>
int *address;
int *builtin_func ()
{
address = __builtin_return_address(1);
return address;
}
void call_builtinfunc(void)
{
builtin_func();
}
void level_one_call(void)
{
call_builtinfunc();
printf ("return address at hear\\n");
}
int main()
{
level_one_call();
printf("%p\\n", address);
return (0);
}
beyes@linux-kd1q:~/C> ./test
return address at hear
0x804847e
08048454 <builtin_func>:
8048454: 55 push %ebp
8048455: 89 e5 mov %esp,%ebp
8048457: 8b 45 00 mov 0x0(%ebp),%eax
804845a: 8b 40 04 mov 0x4(%eax),%eax
804845d: a3 20 a0 04 08 mov %eax,0x804a020
8048462: a1 20 a0 04 08 mov 0x804a020,%eax
8048467: 5d pop %ebp
8048468: c3 ret
08048469 <call_builtinfunc>:
8048469: 55 push %ebp
804846a: 89 e5 mov %esp,%ebp
804846c: e8 e3 ff ff ff call 8048454 <builtin_func>
8048471: 5d pop %ebp
8048472: c3 ret
08048473 <level_one_call>:
8048473: 55 push %ebp
8048474: 89 e5 mov %esp,%ebp
8048476: 83 ec 18 sub $0x18,%esp
8048479: e8 eb ff ff ff call 8048469 <call_builtinfunc>
804847e: c7 04 24 90 85 04 08 movl $0x8048590,(%esp)
8048485: e8 fe fe ff ff call 8048388 <puts@plt>
804848a: c9 leave
804848b: c3 ret
0804848c <main>:
804848c: 55 push %ebp
804848d: 89 e5 mov %esp,%ebp
804848f: 83 e4 f0 and $0xfffffff0,%esp
8048492: 83 ec 10 sub $0x10,%esp
8048495: e8 d9 ff ff ff call 8048473 <level_one_call>
804849a: 8b 15 20 a0 04 08 mov 0x804a020,%edx
80484a0: b8 a7 85 04 08 mov $0x80485a7,%eax
80484a5: 89 54 24 04 mov %edx,0x4(%esp)
80484a9: 89 04 24 mov %eax,(%esp)
80484ac: e8 c7 fe ff ff call 8048378 <printf@plt>
80484b1: b8 00 00 00 00 mov $0x0,%eax
80484b6: c9 leave
#include <stdio.h>
#define SIZE 100
int main()
{
int k;
k = __builtin_constant_p (SIZE);
if (k == 1) {
printf("SIZE is constant\\n");
return 0;
} else {
printf("SIZE is not constant\\n");
return 0;
}
return 0;
}
beyes@linux-beyes:~/C/GNU_C_EXT> ./builtin_const.exe
SIZE is constant
#include <stdio.h>
#define TEST 10
#define TST 16
int expect (int a, int b)
{
return (a + b);
}
int main()
{
int a = 8;
int b = 2;
if ( __builtin_expect((expect(a, b)), TEST)) {
printf ("expected TEST\\n");
return 0;
}
b = 8;
if ( __builtin_expect((expect(a, b)), TST)) {
printf ("expected TST\\n");
return 0;
}
printf ("none expected\\n");
return 0;
}
beyes@linux-beyes:~/C/GNU_C_EXT> ./builtin_const.exe
SIZE is constant
#include <stdio.h>
#include <stdarg.h>
void self_printf(const char *format, ...) __attribute__ ((format(printf,1,2)));
int main()
{
int a = 10;
int b = 8;
int c;
char buf [20] = "hello world";
c = 10;
self_printf("%d %d %s\\n", a, b, buf);
return 0;
}
void self_printf(const char *format, ...)
{
int i;
char c, *p;
va_list ap;
va_start (ap, format);
while (*format)
switch (*format++) {
case 's':
p = va_arg (ap, char *);
printf ("%s", p);
break;
case 'd':
i = va_arg (ap, int);
printf ("%d ", i);
break;
case 'c':
c = (char)va_arg (ap, int);
printf ("%c ", c);
break;
case '\\n':
c = (char)va_arg (ap, int);
printf("\\n");
break;
}
va_end (ap);
}
beyes@linux-beyes:~/C/GNU_C_EXT> ./attribute.exe
10 8 hello world
beyes@linux-beyes:~/C/GNU_C_EXT> gcc -Wall -g attribute.c -o attribute.exe
attribute.c: In function ‘main’:
attribute.c:14: warning: too many arguments for format
extern void exit(int) __attribute__((noreturn));
extern void abort(void) __attribute__((noreturn));
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
/*__attribute__((noreturn))*/ void exitnow ()
{
exit(1);
}
int foo (int n)
{
if (n > 0) {
exitnow();
printf("hello world\\n");
} else return 0;
}
int main ()
{
int n = 10;
foo (n);
return 0;
}
beyes@linux-beyes:~/C/GNU_C_EXT> gcc -g -Wall attr_noreturn.c -o attr_noreturn.exe
attr_noreturn.c: In function ‘foo’:
attr_noreturn.c:17: warning: control reaches end of non-void function
#include <stdio.h>
__attribute__((const)) int square(int n)
{
return (n * n);
}
int main()
{
int i;
int total = 0;
for (i = 0; i < 100; i++)
total += square(5) + i;
printf ("total = %d\\n", total);
return 0;
}
beyes@linux-beyes:~/C/GNU_C_EXT> ./attr_const.exe
total = 7450
#include <stdio.h>
#include <time.h>
__attribute__((const)) time_t time_test(time_t *t)
{
return (time (t));
}
int main()
{
int i;
time_t t = 0;
for (i = 0; i < 5; i++) {
printf ("%d\\n", (time_test(&t) + i));
sleep(1);
}
return 0;
}
beyes@linux-beyes:~/C/GNU_C_EXT> ./attr_const.exe
1250417600
1250417602
1250417604
1250417606
1250417608
beyes@linux-beyes:~/C/GNU_C_EXT> ./attr_const.exe
1250417612
1250417614
1250417616
1250417618
1250417620
#include <stdio.h>
struct demo {
char i;
char j;
int k;
int l;
double m;
}__attribute__((packed));
typedef struct demo1 {
char i;
char j;
int k;
int l;
double m;
}__attribute__((packed)) test;
typedef struct demo2 {
char i;
char j;
int k;
int l;
double m;
} demo_nopacked;
typedef struct demo3 {
char i;
char j;
int k;
int l;
double m;
} demo_temp __attribute((packed));
int main()
{
printf("sizeof demo is : %d\\n", sizeof(struct demo));
printf("sizeof demo is : %d\\n", sizeof(test));
printf("sizeof demo is : %d\\n", sizeof(demo_nopacked));
printf("sizeof demo is : %d\\n", sizeof(demo_temp));
return 0;
}
beyes@linux-beyes:~/C/base> gcc -g attr_pack.c -o attr_pack.exe
attr_pack.c:34: warning: ‘packed’ attribute ignored
beyes@linux-beyes:~/C/base> ./attr_pack.exe
sizeof demo is : 18
sizeof demo is : 18
sizeof demo is : 20
sizeof demo is : 20
__attribute__ ((section("section_name")))
#include <stdio.h>
int main()
{
int var __attribute__ ((section(".xxdata"))) = 9;
printf ("%d\\n", var);
return 0;
}
beyes@linux-beyes:~/C/ELF> gcc -c test.c -o test.o
test.c: In function ‘main’:
test.c:7: error: section attribute cannot be specified for local variables
#include <stdio.h>
int var __attribute__ ((section(".xxdata"))) = 9;
int main()
{
printf ("%d\\n", var);
return 0;
}
beyes@linux-beyes:~/C/ELF> objdump -x test.o
test.o: file format elf32-i386
test.o
architecture: i386, flags 0x00000011:
HAS_RELOC, HAS_SYMS
start address 0x00000000
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 00000034 00000000 00000000 00000034 2**2
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
1 .data 00000000 00000000 00000000 00000068 2**2
CONTENTS, ALLOC, LOAD, DATA
2 .bss 00000000 00000000 00000000 00000068 2**2
ALLOC
3 .xxdata 00000004 00000000 00000000 00000068 2**2
CONTENTS, ALLOC, LOAD, DATA
4 .rodata 00000004 00000000 00000000 0000006c 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
5 .comment 0000003a 00000000 00000000 00000070 2**0
CONTENTS, READONLY
6 .comment.SUSE.OPTs 00000005 00000000 00000000 000000aa 2**0
CONTENTS, READONLY
7 .note.GNU-stack 00000000 00000000 00000000 000000af 2**0
CONTENTS, READONLY
SYMBOL TABLE:
00000000 l df *ABS* 00000000 test.c
00000000 l d .text 00000000 .text
00000000 l d .data 00000000 .data
00000000 l d .bss 00000000 .bss
00000000 l d .xxdata 00000000 .xxdata
00000000 l d .rodata 00000000 .rodata
00000000 l d .comment.SUSE.OPTs 00000000 .comment.SUSE.OPTs
00000000 l d .note.GNU-stack 00000000 .note.GNU-stack
00000000 l d .comment 00000000 .comment
00000000 g O .xxdata 00000004 var
00000000 g F .text 00000034 main
00000000 *UND* 00000000 printf
RELOCATION RECORDS FOR [.text]:
OFFSET TYPE VALUE
00000012 R_386_32 var
0000001d R_386_32 .rodata
00000022 R_386_PC32 printf
#define __init __attribute__ ((__section__(".init.text")))
#include <stdio.h>
int var __attribute__ ((section(".xdata.text"))) = 9;
int __attribute__ ((section(".xxdata"))) func (int var)
{
printf ("%d\\n", var);
return 0;
}
int main()
{
func (var);
return 0;
}
beyes@linux-beyes:~/C/ELF> objdump -x test.o
test.o: file format elf32-i386
test.o
architecture: i386, flags 0x00000011:
HAS_RELOC, HAS_SYMS
start address 0x00000000
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 0000002c 00000000 00000000 00000034 2**2
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
1 .data 00000000 00000000 00000000 00000060 2**2
CONTENTS, ALLOC, LOAD, DATA
2 .bss 00000000 00000000 00000000 00000060 2**2
ALLOC
3 .xdata.text 00000004 00000000 00000000 00000060 2**2
CONTENTS, ALLOC, LOAD, DATA
4 .rodata 00000004 00000000 00000000 00000064 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
5 .xxdata 00000020 00000000 00000000 00000068 2**0
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
6 .comment 0000003a 00000000 00000000 00000088 2**0
CONTENTS, READONLY
7 .comment.SUSE.OPTs 00000005 00000000 00000000 000000c2 2**0
CONTENTS, READONLY
8 .note.GNU-stack 00000000 00000000 00000000 000000c7 2**0
CONTENTS, READONLY
SYMBOL TABLE:
00000000 l df *ABS* 00000000 test.c
00000000 l d .text 00000000 .text
00000000 l d .data 00000000 .data
00000000 l d .bss 00000000 .bss
00000000 l d .xdata.text 00000000 .xdata.text
00000000 l d .rodata 00000000 .rodata
00000000 l d .xxdata 00000000 .xxdata
00000000 l d .comment.SUSE.OPTs 00000000 .comment.SUSE.OPTs
00000000 l d .note.GNU-stack 00000000 .note.GNU-stack
00000000 l d .comment 00000000 .comment
00000000 g O .xdata.text 00000004 var
00000000 g F .xxdata 00000020 func
00000000 *UND* 00000000 printf
00000000 g F .text 0000002c main
RELOCATION RECORDS FOR [.text]:
OFFSET TYPE VALUE
00000012 R_386_32 var
0000001a R_386_PC32 func
RELOCATION RECORDS FOR [.xxdata]:
OFFSET TYPE VALUE
00000010 R_386_32 .rodata
00000015 R_386_PC32 printf
#define __init __attribute__ ((__section__ (".init.text"))) __cold
#define __initdata __attribute__ (( __section__ (".init.data")))
#define __exitdata __attribute__ (( __section__ (".exit.data")))
#define __exit_call __attribute_used__ __attribute__ (( __section__ (".exitcall.exit")))
#define __init_refok oninline __attribute__ ((__section__ (".text.init.refok")))
#define __initdata_refok __attribute__ ((__section__ (".data.init.refok")))
#define __exit_refok noinline __attribute__ ((__section__ (".exit.text.refok")))
.........
#ifdef MODULE
#define __exit __attribute__ (( __section__ (".exit.text"))) __cold
#else
#define __exit __attribute_used__ __attribute__ ((__section__ (".exit.text"))) __cold
#endif
#define __define_initcall(level,fn,id) static initcall_t __initcall_##fn##id __used __attribute__((__section__(".initcall" level ".init"))) = fn
typedef int (*initcall_t)(void);
#define __used __attribute__((__used__)
#define core_initcall(fn) __define_initcall("1",fn,1)
#define core_initcall_sync(fn) __define_initcall("1s",fn,1s)
#define postcore_initcall(fn) __define_initcall("2",fn,2)
#define postcore_initcall_sync(fn) __define_initcall("2s",fn,2s)
#define arch_initcall(fn) __define_initcall("3",fn,3)
#define arch_initcall_sync(fn) __define_initcall("3s",fn,3s)
#define subsys_initcall(fn) __define_initcall("4",fn,4)
#define subsys_initcall_sync(fn) __define_initcall("4s",fn,4s)
#define fs_initcall(fn) __define_initcall("5",fn,5)
#define fs_initcall_sync(fn) __define_initcall("5s",fn,5s)
#define rootfs_initcall(fn) __define_initcall("rootfs",fn,rootfs)
#define device_initcall(fn) __define_initcall("6",fn,6)
#define device_initcall_sync(fn) __define_initcall("6s",fn,6s)
#define late_initcall(fn) __define_initcall("7",fn,7)
#define late_initcall_sync(fn) __define_initcall("7s",fn,7s)
#include <stdio.h>
int main()
{
int ai = 10;
int bi = 11;
printf("%d\\n", bi);
return 0;
}
beyes@linux-beyes:~/C/GNU_C_EXT> gcc -g -Wall attr_unused.c -o attr_unused.exe
attr_unused.c: In function ‘main’:
attr_unused.c:6: warning: unused variable ‘ai’
#include <stdio.h>
int main()
{
int __attribute__((unused)) ai = 10;
int bi = 11;
printf("%d\\n", bi);
return 0;
}
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |