字符串和格式化输入输出
#include
#include
#define DENSITY 62.4
int main(void)
{
float weight, volume;
int size, letters;
char name[];//数组
printf("Hi!What's your first name?");
gets(name);//get(sth.)取得地址
printf("%s,What's your weight in pounds?\n", name);
scanf_s("%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 fist name has %d letters\n", letters);
printf("We have %d bytes to store it in.\n", size);
return ;
}
字符串
用双引号表示,且C语言没有专门的字符串变量类型,而是把它储存在char数组里面。数组的最后一个位置显示空字符\0,用于标记字符串的结束。如 "The weather is so well!"
#include
#define PRAISE "You are so good!"
int main(void)
{
char name[];
printf("What's your name?");
gets(name);
printf("Hello,%s,%s", name, PRAISE);
getchar();
return ;
}
字符串和字符的区别
'x'是基本类型,而"x"是派生类型(char数组);
"x"实际上是由'x'和空字符两部分组成的。
strlen函数
#include
#define PRAISE "You are so good!"
int main(void)
{
char name[];
printf("What's your name?");
gets(name);
printf("Hello,%s,%s", name, PRAISE);
printf("Your name of %zd letters occupies %zd memory cells.\n", strlen(name), sizeof name);
/*strlen函数给出字符数,sizeof为给出所占内存数量;但是两者都需要使用"%zd"转换符来打印。另外sizeof(特定量),如sizeof(char),而一般的类型,不使用圆括号也可以。*/
printf("The phraze of PRAISE has %zd letters", strlen(PRAISE));
printf(" and occupies %zd memory cells.\n", sizeof PRAISE);
getchar();
return ;
}
这样在程序运行时,所有的NAME将会被value替代,这样定义的常量也称为明示常量。
#include
#define PI 3.14
int main(void)
{
float area, circum, radius;
printf("What's the radius of your pizza?\n");
scanf_s("%f", &radius);
area = PI * radius*radius;
circum = * PI*radius;
printf("Your basic pizza parameters are as follows:\n ");
printf("circumference = %1.2f,area = %1.2f\n", circum, area);
system("pause");
return ;
}
const限定符
const int = OLD_YEAR;//OLD_YEAR在程序里面不可修改
明示常量
#include
#include
int main(void)
{
printf("%d\n", INT_MAX);
system("pause");
return ;
}
limits.h
明示常量
含义
CHAR_BIT
char类型的位数
CHAR_MAX
char类型的最大值
CHAR_MIN
char类型的最小值
SCHAR_MAX
signed char类型的最大值
SCHAR_MIN
signed char类型的最小值
UCHAR_MAX
unsiged char类型的最大值
SHRT_MAX
short类型的最大值
SHRT_MIN
short类型的最小值
USHRT_MAX
unsigned short类型的最大值
INT_MAX
int类型的最大值
INT_MIN
int类型的最小值
UINT_MAX
unsiged int的最大值
LONG_MAX
long类型的最大值
LING_MIN
long类型的最小值
ULONG_MAX
unsigned long类型的最大值
LLONG_MAX
long long类型的最大值
LLONG_MIN
long long类型的最小值
ULLONG_MAX
unsigned long long类型的最大值
float.h
明示常量
含义
FLT_MANT_DIG
float类型的尾数位数
FLT_DIG
float类型的最少有效数字位数(十进制)
FLT_MIN_10_EXP
带全部有效数字的float类型的最小负指数(以10为底)
FLT_MAX_10_EXP
float类型的最大正指数(以10为底)
FLT_MIN
保留全部精度的float类型最小正数
FLT_MAX
float类型的最大正数
FLT_EPSILON
1.00和比1.00大的最小float类型值之间的差值
把明示常量名中的FLT分别替代成DBL和LDBL,即可分别表示double和long double类型对应的明示常量。
printf()和scanf()和*修饰符
如果不想预先指定字段宽度,希望通过程序来指定,那么可以用*修饰符代替字段宽度;如果转换符%*d,那么参数列表中应包含*和d对应的值
#include
int main(void)
{
unsigned width, precision;
int number = ;
double weight = 242.5;
printf("Enter a field width:\n");
scanf_s("%d", &width);
printf("The number is:%*d:\n", width, number);
printf("Now enter a width and a precision:\n");
scanf_s("%d %d", &width, &precision);
printf("Weight = %*.*f\n", width, precision, weight);
printf("Done!\n");
system("pause");
return ;
}
scanf()中*的用法与此不同,把*放在%和转换符之间时,会使得scanf()跳过相应的输入项。
#include
int main(void)
{
int n;
printf("Please enter three integers:\n");
scanf_s("%*d %*d %d", &n);
printf("The last integer was %d\n", n);
system("pause");
return ;
}
result:
Please enter three integers:
The last integer was
printf()用法提示
#include
int main(void)
{
int val_1 = , val_2 = , val_3 = ;
printf("%9d %9d %9d\n", val_1, val_2, val_3);//%nd设置字段宽度
system("pause");
return ;
}
result:
C Primer Plus_第四章_字符串和格式化输入输出_编程练习
Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...
C语言入门教程-(5)格式化输入输出
1.输入和输出 在程序的使用中,我们经常可以看的这么一个场景:用户需要输入数据,经过程序运算,得到结果后输出.在C语言中,输入数据和输出数据都是由库函数完成的,通过语句来输入/输出. 2.格式化输出— ...
C语言中字符串的格式化
本文整理转载自:http://wenku.baidu.com/view/065d62fff705cc1755270989.html C语言中格式字符串的一般形式为: %[标志][输出最小宽度][.精度 ...
【CPP】字符串和格式化输入输出
前导:数组(array),字符串转换说明符%s,定义符号常量,,strlen()获取字符串长度,. [字符串] 没有专门的字符串类型,是吧他存储在字符型数组中,数组最后一个字符为空字符'\0',c用他 ...
重学C语言---04字符串和格式化输入/输出
1.程序示例 //talkback.c一个能为你提供一些信息的对话框 #include #include //提供strlen函数原型 ...
C:指针、数据类型、格式化输入输出、输入函数的坑点
指针.数据类型.格式化输入输出.输入函数的坑点 有时候我们迷茫的时候,坚持就是最好的选择. 1.指针的分类为什么很重要? 参考 答:因为指针会根据相应的类型取对应长度的数据,类型决定所取数据的长度.如 ...
C语言printf()函数:格式化输出函数
C语言printf()函数:格式化输出函数 头文件:#include printf()函数是最常用的格式化输出函数,其原型为: int printf( char ...
C++格式化输入输出
要实现格式化输入输出,程序需要包含 iostreams 标准标头 以定义几个各自采用单个参数的操控器. 备注: 其中每个操控器都返回重载 basic_istream&l ...
iOS开发-OC语言 (三)字符串
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; text-align: center; font: 24.0px "PingFang SC" } p ...
随机推荐
通过扩展让ASP.NET Web API支持W3C的CORS规范(转载)
转载地址:http://www.cnblogs.com/artech/p/cors-4-asp-net-web-api-04.html CORS(Cross-Origin Resource Shari ...
poj2749
万变不离其宗 只要搞清楚题目的基本模型 搞清楚边是一种推导出的关系 搞清楚里面的逻辑关系 那就没什么难的了…… 二分+sat,没什么好说的 ; type node=record point,next: ...
HDU-4689 Derangement
太洗脑了: 题目意思:初始队列是1,2, 3.......n :在打乱这个队列切保证每个数字都不在原来的位置上的情况下给出一个具有+,- 的队列: 被打乱的队列 和 原来队列 对应位置的大小的关系是那 ...
[leetcode-553-Optimal Division]
Given a list of positive integers, the adjacent integers will perform the float division. For exampl ...
spring boot整合Thymeleaf的那些坑(spring boot 学习笔记之四)
这里简单记录一下Thymeleaf配置和使用的步骤 1.修改pom文件,添加依赖 org.springframework.boot& ...
如何配置Open Live Writer程序以便更好的为博客服务
Open Live Writer的前身是Windows live Writer 即(WLW)是一个免费的桌面应用程序,您可以使用它轻松发布丰富的内容到您的网络日志.WLW最终版为Windows Liv ...
基于.net EF6 MVC5+WEB Api 的Web系统框架总结(1)-Web前端页面
本 Web 系统框架基于C# EF6+MVC+WebApi的快速应用开发平台.本节主要介绍Web前端页面设计与实现.Web前端页面主要分为普通列表页面.树状导航列表页面.普通编辑页面.数据导入页面.向 ...
ref、out参数
ref和out都是表示按引用传递.与指针类似,直接指向同一内存. 按值传递参数的方法永远不可能改变方法外的变量,需要改变方法外的变量就必须按引用传递参数. 传递参数的方法,在C语言里,用指针.在C#里 ...
高精度POJ1001
今天看到这道题了 poj1001 题目地址是http://bailian.openjudge.cn/practice/1001/ 英文看得懂,可是算法不明白,所以转别人的文章,留着给学生看看:乔高建( ...
firewall 和 iptables 常用命令
[参考文章]:Centos7 关闭防火墙 [参考文章]:Centos7 firewall防火墙常用配置 CentOS 7.0默认使用的是firewall作为防火墙,使用iptables必须重新设置一下 ...