字符串与格式化输入/输出

//主要是数组的scanf用法和strlen sizeof 的区别
#include<stdio.h>
#define DENSITY 62.4
int main(void)
{
float weight ,volume;
int size ,letters;
char name[40];

printf("Hi!What's your first name?\n");
scanf("%s",name);
printf("%s,what's your weight in pounds?\n",name);
scanf("%f",&weight);
size = sizeof name;
letters = strlen(name);
volume = weight /DENSITY;
printf("Well,%s,your volume is %2.2f cubic feet.\n",
name,volume);
printf("Also,your first name has %d letters,\n",
letters);
printf("and we have %d bytes to store it in.\n",size);
return 0;

}

 


/*
scanf()开始读取输入以后,会在遇到的第一个空白字符(bank)、制表符(tab)或者换行符(newline)处停止读取。
%s只会把一个单词而不是整个语句作为字符串读入,其他会存入内存缓冲区内
*/


#include<stdio.h>
#define PRAISE "What a super marvelous name!"
int main(void)
{
char name[40];
while(1)
{
printf("What's your name?\n");
scanf("%s",name);
printf("Hello,%s.%s\n",name,PRAISE);
}

}

 

 


//strlen()会比用sizeof()少一位'\0'


#include<stdio.h>
#include<string.h>
#define PRAISE "What a super marvelous name!"
int main(void)
{
char name[40];

printf("What's your name?\n");
scanf("%s",name);
printf("Hello,%s. %s\n",name,PRAISE);
printf("Your name of %d letters occupies %d memory cells.\n",
strlen(name),sizeof name);
printf("The phrase of praise has %d letters",
strlen(PRAISE));
printf("and occupies %d memory cells.\n",sizeof PRAISE);
return 0;

}

 

 


/* 符号常量所用的名字必须满足变量的命名规则,可以使用大写和小写字母、数字和下划线字符,第一个字符不能是数字
printf()语句中的%1.2f使输出结果四舍五入为保留两位小数 */


#include<stdio.h>
#define PI 3.14159
int main(void)
{
float area,circum,radius;

printf("What is 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 follow: \n");
printf("circumference = %1.2f,area = %1.2f\n",circum,
area);
return 0;

}

 


//系统定义的明显常量--------------以下程序使用limit.h和float.h中定义的常量

#include<stdio.h>
#include<limits.h>
#include<float.h>

int main(void)
{
printf("Some number limits for this system: \n");
printf("Biggest int :%d\n",INT_MAX);
// printf("Smallest unsigned long: %lld\n",LLONG_MIN); //我的系统不支持
printf("One byte = %d bits on this system.\n",CHAR_BIT);
printf("Largest double: %e\n",DBL_MAX);
printf("Smallest normal float: %e\n",FLT_MIN);
printf("float precision = %d digits\n",FLT_DIG);
printf("float epsilon = %e\n",FLT_EPSILON);
return 0;
}

 

 

关于%g在群里的一些讨论:

%.2g不是一共两位的,要分情况的,比如是整数,会打印06,指的是两位有效数字,此时可以它决定位数为2;
而7.982会打印7.98 ,指的是保留两位小数 ,位数是由.前面点前面的数决定的,比如%6.2d是显示6位。

c中又有一个规定:如果你指定的位数小于实际的位数,那么字段自动拓展以适应数字的长度,指的是明明3位,你指定2位,系统自动变为3位。p71


%f是四舍五入,小数点后面6位为有效数字
%g根据数值的不同自动选择%f或%e。%e格式在指数小于-4或者大于等于精度时使用
%g不显示无效的0,不管精度时多少,它的有效位数包括整数 如3.2的格式说明符就是%.2g。33.141434234用%.2g会输出33

题目中第一个不行,我猜想:0.0000009979被指定用%f,它先化成0.000001(因为系统%f有效位数6位--四舍五入了)之后变为科学计数法,即1e-006,
至于为什么不是1.00e-006不清楚

我发几个代码图片,你可以研究下

 


//使用转换说明符,printf()中第二项可以是变量,常量,表达式
#include<stdio.h>
#define PI 3.141593
int main(void)
{
int number = 5;
float expresso = 13.5;
int cost = 3100;
printf("The %d CEOs drank %f cups of expresso.\n",number,
expresso);
printf("The value of PI is %f.\n",PI);
printf("Farewell!thou art too dear for my possessing,\n");
printf("%c%d\n",'$',2 * cost);
return 0 ;

}

 

/* 字段宽度
第二个转换说明符是%2d,它指示应该产生宽度为2的字段,但是由于该整数有3位数字,所以字段应该自动拓展以适应数字的长度*/

#include<stdio.h>
#define PAGES 931
int main()
{
printf("*%d*\n",PAGES);
printf("*%2d*\n",PAGES);
printf("*%10d*\n",PAGES);
printf("*%-10d*\n",PAGES);
return 0;


}

 

//float.c--一些浮点数的组合

#include<stdio.h>
int main(void)
{
const double RENT = 3852.99;

printf("*%f*\n",RENT);
printf("*%e*\n",RENT);
printf("*%4.2f*\n",RENT);
printf("*%3.1f*\n",RENT);
printf("*%10.3f*\n",RENT);
printf("*%10.3e*\n",RENT);
printf("*%+4.2f*\n",RENT);
printf("*%010.2f*\n",RENT);
return 0;
}

 


/*
第二行说明符中使用空格以在正值之前产生一个前导空格(在负值前不产生前导空格),这将使有效位相同的正值和负值以相同的宽度打印输出,因此结果看起来会舒服一些。
第三行产生足够的前导零以填满要求的最小数字(这里是3);而使用0标志将会用前导零填满整个字符宽度;最后,如果0标志和精度说明符同时出现,则0标志将会被忽略
*/

#include<stdio.h>
int main(void)
{
printf("%x %X %#x\n",31,31,31);
printf("**%d**% d**% d**\n",42,42,-42);
printf("**%5d**%5.3d**%05d**%05.3d**\n",6,6,6,6);
return 0;

}

 

//字符串的格式化--格式说明符中的.5告诉printf()只打印5个字符
#include<stdio.h>
#define BLURB "Authentic imitation! "
int main(void)
{
printf("/%2s/\n",BLURB);
printf("/%24s/\n",BLURB);
printf("/%24.5s/\n",BLURB);
printf("/%-24.5s/\n",BLURB);
return 0;
}

 


不匹配的浮点数转换
/*
n1~n4的值先放到堆栈中,放的时候是他们类型字节,long四字节,double八字节。再根据说明符读取%ld四字节,说明符不对读取的时候就不对了
float在print被用作参数时会进行转变为double */



#include<stdio.h>

int main(void)
{
float n1 = 3.0;
double n2 = 3.0;
long n3 = 2000000000;
long n4 = 1234567890;

printf("%.1e %.1e %.1e %.1e\n",n1,n2,n3,n4);
printf("%ld %ld\n",n3,n4);
printf("%ld %ld %ld %ld\n",n1,n2,n3,n4);
return 0;

}

 

printf函数的返回值
/*
printf()返回所打印字符的个数,如果有输出错误,返回一个负数 */


#include<stdio.h>
int main(void)
{
int bph2o = 212;
int rv;

rv = printf("%d F is water's boiling point.\n",bph2o);
printf("The printf() function printed %d characters.\n",
rv);

return 0;
}

 

 

//打印较长的字符串--三种方法
#include<stdio.h>
int main(void)
{
printf("Here's one way to print a ");
printf("long string.\n");
printf("Here's another way to print a \
long string.\n");
printf("Here's the newest way to print a"
"long string.\n");
return 0;

}

 

什么情况下使用&和scanf()的一些注意点
/*
如果少参数的话,会出错。
scanf()使用空格(换行、制表符、空格)来决定怎样把输入分为几个字段,%c除外。double要用%lf。
char数组不需要使用&
scanf(“%d,%d”,&n,&m);运行输入时要用88,121的形式,格式说明符中的空格意味着跳过下一个输入项之前的任何空格
scanf("%d%d",&n,&m);与scanf("%d %d",&n,&m);的行为是相同的。%c会有区别,scanf(" %c",&ch);会读取遇到的第一个非空白字符


*/


#include<stdio.h>
int main(void)
{
int age;
float assets;
char pet[30];

printf("Enter your age,assets,and favourite pet.\n");
scanf("%d %f",&age,&assets);
scanf("%s",pet);
printf("%d $%.2f %s\n",age,assets,pet);
return 0;

}

 


//使用可变宽度的输出字段

#include<stdio.h>
int main(void)
{
unsigned width,precision;
int number = 256;
double weight = 242.5;

printf("What field width?\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);
return 0;

}

 


/*
跳过输入的头两个整数--下面程序会把第三个整数复制给n
如果程序需要读取文件中特定的列(该文件的数据以统一的列排列),那么该功能将非常有用 */

#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;
}
小技巧:%9d等格式使输出对齐,指定一个和期望的数字宽度同样小或者更小的字段宽度会使数字的宽度正合适。

 

转载于:https://www.cnblogs.com/zxj-262410/p/6691221.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值