说明:以下内容摘自《C程序设计语言》,自己编写等
(《The C Programming Language》
Brian W.Kernighan,Dennis M.Ritchie)
1)
for(p = head; p != NULL; p = q){
q = p->next;
free(p);
}
2)Union的对齐使用:
typedef long Align;
union header{
struct{
union header *ptr;
unsigned size;
}s;
Align x;
}
3)-(char)((unsigned char)~0) >> 1)
4)
while((c = getchar()) != EOF) putchar(c);
5)strcpy:
void strcpy(char *s, char *t)
{
while(*s++ = *t++);
}
6)strcmp:
int strcmp(char *s, char *t)
{
for(; *s == *t; s++, t++)
if(*s == '\0') return 0;
return *s - *t;
}
7)进栈标准用法
8)strcat:
void strcat(char *s, char *t)
{
while(*s) s++;
while(*s++ = *t++);
}
9)squeeze: delete all c from s
squeeze(char s[], int c)
{
int i, j;
for(i = j = 0; s[i] != '\0'; i++)
if(s[i] != c) s[j++] = s[i];
s[j] = '\0';
}
11)bitcount: count 1 bits in x
int bitcount(unsigned x)
{
int b = 0;
while(x){
x &= x - 1;
++b;
}
return b;
}
12)
for(i = 0; i < n; i++)
printf("m%c", a[i], (i % 10 == 9 || i == n - 1) ? '\n' : ' ');
printf("You have %d item%s.\n", n, n == 1 ? "" : "s");
13)printd: print n in Decimal
void printd(int n)
{
if(n < 0){
putchar('-');
n = -n;
}
if(n / 10) printd(n /10);
putchar(n % 10 + '0');
}