# C语言考古报告: 从K&R C到C99的函数声明

DONG Yuxuan

## K&R C

double square();	/* 声明 */
...
...
...
double square(x)	/* 定义 */
double x;
{
return x * x;
}


#include <stdio.h>

double square();

main()
{
square(1, 2, 3, 4, 5);
return 0;
}

double square(x)
double x;
{
return x * x;
}


## C89/C99

double square(double);


int icmp(int *a,int *b) { return *a - *b; }


#include <stdlib.h>
#include <stdio.h>

int icmp(int *a, int *b) { return *a - *b; }

int main(void) {
int i ,buff[] = {3 ,7 ,4 ,1 ,0 ,9 ,6};
qsort(buff ,sizeof buff / sizeof *buff, sizeof *buff, icmp);
for (i = 0; i < sizeof buff / sizeof *buff; ++i) printf("%d\n", buff[i]);
return 0;
}


qsort(
buff,
sizeof buff / sizeof *buff,
sizeof *buff,
(int (*)(const void *, const void *))icmp
);


int icmp(const void *a, const void *b) { return *(int *)a - *(int *)b; }


qsort(buff, sizeof buff / sizeof *buff, sizeof *buff, (int (*)())icmp);


#include <stdlib.h>
#include <stdio.h>

int icmp();

int main(void)
{
int i, buff[] = {3, 7, 4, 1, 0, 9, 6};
qsort(buff, sizeof buff / sizeof *buff, sizeof *buff, icmp);
for (i = 0; i < sizeof buff / sizeof *buff; ++i) printf("%d\n", buff[i]);
return 0;
}

int icmp(int *a, int *b) { return *a - *b; }


## 默认参数提升

#include <stdio.h>

double square();

main()
{
printf("%f\n", square(1));
return 0;
}

double square(x)
double x;
{
return x * x;
}


printf("%f\n", square(1.0));


printf("%f\n", square(1.0f));


1. 如果传入的参数是[unsigned] char[unsigned] short类型，则它会被转化为[unsigned] int类型入栈
2. 如果传入的参数是float类型，则它会被转化为double类型入栈

float square();	/* 声明 */
...
...
...
float square(float x)	/* 定义 */
{
return x * x;
}


float square(double x)
{
float y = (float)x;
return y * y;
}


square(1.0f);
square(1.0);


float square(float);	/* 声明 */
...
...
...
float square(float x)	/* 定义 */
{
return x * x;
}


int printf(char *fmt, ...);