题目

演示使用 typedef 关键字为结构体类型创建别名,对比使用别名前后定义结构体变量的语法区别。

输入格式:一行输入两个浮点数,表示二维坐标。

输出格式:输出坐标点的信息。

数据范围与约定:浮点数范围。

思路与算法

1. typedef 别名简化
未定义别名时,必须写完整的 struct Point p;;使用 typedef struct { double x, y; } Point; 声明后,可直接写 Point p;。

2. 工程实践价值
在复杂工程和库开发中,typedef 能让类型名称简短优雅,消除随处可见的 struct 冗余关键字。

完整程序

#include <stdio.h>
#include <math.h>

// typedef 给结构体起类型别名,后续无需再书写 struct 关键字
typedef struct {
    double x, y;
} Point;

int main(void)
{
    Point a, b;
    scanf("%lf %lf %lf %lf", &a.x, &a.y, &b.x, &b.y);
    double dx = a.x - b.x, dy = a.y - b.y;
    printf("%.2f\n", sqrt(dx * dx + dy * dy));
    return 0;
}

运行示例

输入:

0 0 6 8

输出:

10.00