c语言中没有返回值的函数和不含形参的函数

 

001、没有返回值的函数

复制代码
[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c         ## 测试c程序
#include <stdio.h>

void put_star(int a)                // 定义不含返回值的函数
{
        while(a-- > 0)
                putchar('*');       // 函数中没有return语句
}

int main(void)
{
        int x;
        printf("x = "); scanf("%d", &x);

        put_star(x);
        putchar('\n');

        return 0;
}
[root@PC1 test]# gcc test.c -o kkk      ## 编译测试
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk
x = 3
***
[root@PC1 test]# ./kkk
x = 7
*******
复制代码

 

002、没有形参的函数

复制代码
[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c                      ## 测试c程序
#include <stdio.h>

int get_pos_num(void)                           // 定义没有形参的函数
{
        int a;

        do
        {
                printf("please input an positive number: "); scanf("%d", &a);

                if(a <= 0)
                {
                        puts("the value of a should be positive.");
                }
        }
        while( a <= 0);

        return a;
}

int main(void)
{
        int x;

        printf("x = %d\n", get_pos_num());            // 没有形参的函数调用, 函数调用运算符中不需要任务内return 0;
}
[root@PC1 test]# gcc test.c -o kkk                    ## 编译
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk                                ## 测试
please input an positive number: -86
the value of a should be positive.
please input an positive number: 0
the value of a should be positive.
please input an positive number: 533
x = 533
复制代码

 。

 

posted @   小鲨鱼2018  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2023-11-09 非root 用户安装perl模块
2023-11-09 Can't locate CPAN.pm in @INC (@INC contains: /usr/local/lib64/perl5
2023-11-09 Warning: prerequisite Test::More 0 not found.
2022-11-09 使用 gff2bed 将 gff文件转换为bed格式
2022-11-09 /usr/bin/ld: cannot find -lm
2022-11-09 File "/usr/bin/yum", line 30
2020-11-09 linux系统中磁盘容量配额
点击右上角即可分享
微信分享提示