C语言-printf()和scanf()中*的作用

本文深入讲解了printf与scanf函数的功能及用法,特别是在printf中如何使用*作为变量占位符来控制输出宽度,并介绍了scanf中*的作用是跳过相应的输入项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

printf()中*的作用

在printf()中*充当变量的占位符

/*使用变量控制输出字符宽度*/
#include <stdio.h>
int main(void)
{
    unsigned width,precision;
    int number = 256;
    double weight = 243.67;

    printf("Enter a field width:\n");
    scanf("%d",&width);
    printf("The number is :%*d\n",width,number);
    printf("Enter a width and a precision:\n");
    scanf("%d%d",&width,&precision);
    printf("weight = %*.*f\n",width,precision,weight);

    return 0;
}

运行示例
这里写图片描述

scanf()中*的作用

把*放在%和转换字符之间时,会使得scanf()跳过相应的输入项。

\*跳过输入中的整数*\
#include <stdio.h>
int main(void)
{
    int n;

    printf("please input three integers:\n");
    scanf("%*d,%*d,%d",&n);
    printf("the last integer is :%d\n",n);

    return 0;
}

运行示例
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值