c语言之字符串和格式化输入输出
字符串和格式化输入输出
#include<stdio.h>
#include<string.h>
#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<stdio.h>
#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<stdio.h>
#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<stdio.h>
#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<limits.h>
#include<stdio.h> 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<stdio.h> 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<stdio.h>
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<stdio.h>
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语言之字符串和格式化输入输出的更多相关文章
- 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 <stdio.h> #include <string.h> //提供strlen函数原型 ...
- C语言学习笔记---3.字符串格式化输入输出
1.C语言字符串 字符串(character string)是一个或多个字符的序列,例如:"Zing went the strings of my heart!" C语言没有专门用 ...
- C:指针、数据类型、格式化输入输出、输入函数的坑点
指针.数据类型.格式化输入输出.输入函数的坑点 有时候我们迷茫的时候,坚持就是最好的选择. 1.指针的分类为什么很重要? 参考 答:因为指针会根据相应的类型取对应长度的数据,类型决定所取数据的长度.如 ...
- C语言中字符串详解
C语言中字符串详解 字符串时是C语言中非常重要的部分,我们从字符串的性质和字符串的创建.程序中字符串的输入输出和字符串的操作来对字符串进行详细的解析. 什么是字符串? C语言本身没有内置的字符串类型, ...
- C语言printf()函数:格式化输出函数
C语言printf()函数:格式化输出函数 头文件:#include <stdio.h> printf()函数是最常用的格式化输出函数,其原型为: int printf( char ...
随机推荐
- SQL Server 一致性读
我们在Oracle和MySQL数据库中已经对一致性读的概念比较熟悉了,但是在SQL Server中却鲜少提及,但SQL Server自2005版本以来其实也实现了一致性读,几乎所有关系型数据库产品的一 ...
- Hibernate 5 入门指南-基于类注解
首先创建hibernate.cfg.xml配置文件并做简单的配置 <hibernate-configuration> <session-factory> & ...
- PHP的move_uploaded_file()出错解决
今天用的PHP的move_uploaded_file方法保存前端上传的中文名称文件时,方法返回假,调试时错误码为2,错误信息为: move_uploaded_file(D:\ git_prj \ xx ...
- June.19 2018, Week 25th Tuesday
True love is visible not to the eyes but to the heart. 真爱不靠眼睛看,要用心感受. True love is visible not to th ...
- python 基本数据类型--字符串实例详解
字符串(str) :把字符连成串. 在python中⽤', ", ''', """引起来的内容被称为字符串 . 注意:python中没有单一字符说法,统一称叫字 ...
- Teradata数据库访问链条
- Mac中selenium使用出现错误
解决方案是: 首先通过brew 安装 $ brew install geckodriver 然后设置配置文件~/.bash_profile文件 export PATH=$PATH:/path/to/g ...
- Error response from daemon: rpc error: code = Unknown desc = name conflicts with
环境:centos7 执行一下命令时, docker service create --mode global --name logspout gliderlabs/logspout 出现以下报错: ...
- springmvc组件--ViewResolver
无论Controller是何种返回类型最终都会被封装成一个ModelAndView对象,然后交由ViewResolver解析成Vie对象.该接口定义非常简单,根据传入视图的逻辑名(var1)和相应的国 ...
- utc时间转成local时间
public static Date utcToLocal(String utcTime){ SimpleDateFormat sdf = new SimpleDateFormat("yyy ...