首页 > 编程语言 > 详细

c语言中函数、函数式宏

时间:2021-05-24 16:44:06      阅读:17      评论:0      收藏:0      [点我收藏+]

c语言中函数、函数式宏例子

返回一个数的平方。

1、函数

#include <stdio.h>

int sqr_int(int x)
{
    return x * x;    
} 

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

int main(void)
{
    int a;
    puts("please input an integer.");
    printf("a = "); scanf("%d", &a);
    printf("int  result: %d\n", sqr_int(a));
    
    double b;
    puts("please input an double.");
    printf("b = "); scanf("%lf", &b);
    printf("double result: %f\n", sqr_double(b));
    
    return 0;
}

技术分享图片

 

 

2、函数式宏

#include <stdio.h>

#define sqr(x) ((x) * (x))

int main(void)
{
    int a;
    puts("please input an integer.");
    printf("a = "); scanf("%d", &a);
    printf("int result: %d\n", sqr(a));
    
    double b;
    puts("please input an double");
    printf("b = "); scanf("%lf", &b);
    printf("double result: %f\n", sqr(b));
    
    return 0; 
}

技术分享图片

 

 

3、函数和函数式宏的区别

1、函数式宏是在编译时展开并填入程序的

2、函数更为严格,需要指定返回值类型、形参类型

 

c语言中函数、函数式宏

原文:https://www.cnblogs.com/liujiaxin2018/p/14803919.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!