C语言入门第三天结束书籍第四章内容,学习字符串和格式输入输出等操作。
笔记代码如下所示:
主要包括:
1.如何打印长字符输出
2.scanf()输入格式转换
3.scanf()读取操作及返回值
4.输入输出*修饰符
#include <stdio.h>
/************************1********************************/
//打印较长字符串输出表示方式
/* int main(void)
{
int num = 8;
//使用换行对齐方式输出或者多个printf语句组合
printf("hello world! i love number %d\n",
num);
//利用反斜线符号(\)和回车键组合来结束第一行,千万不要加空格键
printf("here is one way to printf a \
long string.\n");
return 0;
} */
/************************2********************************/
//scanf()把输入的字符串转换成各种形式:整数,浮点数,字符和C的字符串,它是printf()的逆操作
//printf()把整数,浮点数,字符串转换成要在屏幕上要显示的文本。
//如果使用scanf()来读取前面讨论过的某种基本变量(整数,浮点数,char)类型的值,请在变量名之前加一个&
//如果使用scanf()来把一个字符串读进一个字符数组中,请不要使用&
/* 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,%f,%s\n",age,assets,pet);//25,100.250000,songzuer
return 0;
} */
/************************3********************************/
//scanf()函数每次读取一个输入字符,它会跳过空白字符(空格,制表符和换行符)直到遇到一个非空白字符。
//情景练习:1.输入空格加数字输出结果会怎么样;2.如果字母加数字组合会怎么样?3.如果age类型为char又会怎么样?
/* int main(void)
{
// int age;
// printf("please enter your age: \n");
// scanf("%d",&age);
// printf("your age is %d",age);
char age[40];
printf("please enter your age: \n");
scanf("%s",age);//good morning
printf("your age is %s",age);//your age is good
return 0;
}
*/
/************************3********************************/
//printf()和scanf()的*说明符
//printf()假设不想事先指定字段宽度,而是希望由程序来指定该值,那么可以在字段宽度部分使用*代替数字达到目的。
//printf()即%*d,但是参数列表中必须包含一个*的值的一个d的值
//printf()该技术可以和浮点值一起使用来指定精度和字段宽度
//scanf()中把*修饰符放在%和说明符字母之间时,它使函数跳过相应的输入项目
/* int main(void)
{
// unsigned int width,precision;
// int num = 256;
// float weight = 256.5;
// //printf()
// //指定足够大的固定字段宽度,使输出更加整齐,例如printf("%9d %9d %9d\n",n1,n2,n3)
// printf("what field width?\n");
// scanf("%d",&width);
// printf("the number is : %*d\n",width,num);
// printf("now enter a width and a precision: \n");
// scanf("%d %d",&width,&precision);
// printf("weight = %*.*f\n",width,precision,weight);
//scanf()
int n;
printf("please enter three integers: \n");
scanf("%*d %*d %d",&n);
printf("the last integer was %d\n",n);
return 0;
} */
/*
int main(void)
{
float percent = 80.0;
//两个百分号才能打印出%,不然结果出错
printf("this is %2.0f% of list\n",percent);//this is 8035246060f list
printf("this is %2.0f%% of list\n",percent);//this is 80% of list
return 0;
} */