Chap4字符串的格式化输入与输出
#include <stdio.h>
int main(void)
{
int age;
float assets;
char pet[30]; //用于储存字符串
printf("Enter your age,assets,and favorite pet.\n");
scanf("%d%f",&age,&assets); //这里使用&
scanf("%s",pet); //字符组不使用&
printf("%d$%.2f%s\n",age,assets,pet);
return 0;
}
在使用scanf时当前面为%s即要填入字符串时不使用地址符号&
//
// Created by 卢志远 on 2024/3/21.
//
#include <stdio.h>
int main(void)
{
unsigned width, precision;
int number=256;
double weight=242.5;
printf("Enter a field widrh:\n");
scanf("%d",&width);
printf("The number is:%*d:\n",width,number);
printf("Now enter a width and a precision:\n");
scanf("%d%d",&width,&precision);
printf("Weight=%*.*f\n",width,precision,weight);//这里将格式化的参赛放在后面,时的可以控制对格式化输出的格式
printf("Done!\n");
return 0;
}
单词意思:field是字段的意思 precision是精确度
printf中*. *f可以看成三个参数 * * f
#include <stdio.h>
int main(void)
{
int n;
printf("Please enter three integers:\n");
scanf("%*d%*d%d",&n);
printf("The last integer was %d\n",n);
return 0;
}
在scanf函数中*放在%和d之间就会在输入时被忽略
//可以用define来定义一个字符常量,通常用#define NAME value 这个代码我们叫他c预处理器
//变量名大写方便记忆
#include <stdio.h>
#define PI 3.14159
int main(void)
{
float area,circum,radius;
printf("What's the radius of your pizza?\n");
scanf("%f",&radius);
area = PI*radius*radius;
circum = 2.0*PI*radius;
printf("Your basic pizza parameters are as follows:\n");
printf("circumference=%1.2f,area=%1.2f\n",circum,area);
return 0;
}
//%1.2f 表示以四舍五入输出小数
标签:记录,int,scanf,precision,学习,width,radius,printf From: https://www.cnblogs.com/hbdxlzy/p/18087497