C programs under Linux
1. function with variable arguments
NAME
printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - formatted output conversion
SYNOPSIS
#include <stdio.h>
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);
#include <stdarg.h>
int vprintf(const char *format, va_list ap);
int vfprintf(FILE *stream, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
输出:
hello
world
函数调用规范__cdecl(C语言缺省调用约定)中参数从右至左入栈,first在栈顶,fifth在栈底,根据first的地址即可算出后面参数的地址。函数调用者负责清理堆栈,因此可以定义参数个数可变的函数。(注:以下函数在VC6.0下可以编译运行,gcc下可能会有char类型提升相关的警告。)
输出:a 123456 123.456000 test
这里给出的也是VC6.0下的定义:
2. macro with __VA_ARGS__
3. print 64 bits integer
sizeof(long long): 8
value in dec: 78187493520
value in hex: 1234567890
4. process id, task id and thread id test
> gcc pidtest.c -o pidtest -lpthread
> ./pidtest
enter main() ...
pthread_self() : 3086104256
getpid() : 18773
syscall(__NR_gettid): 18773
enter thread 2 ...
pthread_self() : 3077708688
getpid() : 18773
syscall(__NR_gettid): 18775
exit thread 2 ...
enter thread 1 ...
pthread_self() : 3086101392
getpid() : 18773
syscall(__NR_gettid): 18774
exit thread 1 ...
exit main() ...
5. SIGCHLD
enter main process 28041
child process 28042 created by 28041
total 72
drwxr-xr-x 2 user user 4096 Jan 31 20:01 .
drwxr-xr-x 42 user user 4096 Jan 31 20:01 ..
-rwxr-xr-x 1 user user 9572 Jan 31 18:58 pidtest
-rw-r--r-- 1 user user 1093 Jan 31 18:56 pidtest.c
-rwxr-xr-x 1 user user 9149 Jan 31 12:43 print64
-rw-r--r-- 1 user user 226 Jan 31 12:43 print64.c
-rwxr-xr-x 1 user user 9745 Jan 31 20:01 sigchild
-rw-r--r-- 1 user user 1468 Jan 31 20:00 sigchild.c
-rwxr-xr-x 1 user user 9266 Jan 31 12:24 vargs
-rw-r--r-- 1 user user 489 Jan 31 12:23 vargs.c
SIGCHLD from process 28042
child process 28043 created by 28041
total 72
drwxr-xr-x 2 user user 4096 Jan 31 20:01 .
drwxr-xr-x 42 user user 4096 Jan 31 20:01 ..
-rwxr-xr-x 1 user user 9572 Jan 31 18:58 pidtest
-rw-r--r-- 1 user user 1093 Jan 31 18:56 pidtest.c
-rwxr-xr-x 1 user user 9149 Jan 31 12:43 print64
-rw-r--r-- 1 user user 226 Jan 31 12:43 print64.c
-rwxr-xr-x 1 user user 9745 Jan 31 20:01 sigchild
-rw-r--r-- 1 user user 1468 Jan 31 20:00 sigchild.c
-rwxr-xr-x 1 user user 9266 Jan 31 12:24 vargs
-rw-r--r-- 1 user user 489 Jan 31 12:23 vargs.c
SIGCHLD from process 28043
main process 28041 exited
6. __attribute__((constructor)) and __attribute__((destructor))
this is ctor2()...
this is ctor1()...
this is main()...
this is dtor1()...
this is dtor2()...
7. useful macros
1) compute offset of a field in a struct
2) compute size of a field in a struct
3) compute maximum value of a given integer type
eg. MAX_VALUE(short) = 0x7FFF
MAX_VALUE(unsigned short) = 0xFFFF
MAX_VALUE(int) = 0x7FFFFFFF
MAX_VALUE(unsigned int) = 0xFFFFFFFF
4) exchange two variables without a third variable
浙公网安备 33010602011771号