zoukankan      html  css  js  c++  java
  • C 语言字符串和格式化输入与输出

    1.前导程序

     1 #include<stdio.h>
     2 #include<string.h>        //1提供strlen()的函数原型
     3 #define DENSITY   62.4    //2预处理命令
     4 int main(void)
     5 {
     6     float weight,volume;
     7     int size,letters;
     8     char name[40];    //3定义一个长度为40的数组
     9 
    10     printf("Hi! What's your first name?
    ");
    11     scanf("%s",name);
    12     printf("%s,What's your weight in pounds?
    ",name);
    13     scanf("%f",&weight);
    14     size=sizeof  name;   //4  数组name[]的长度
    15     letters=strlen(name);//5  调用函数strlen()
    16     volume=weight/DENSITY;
    17     printf("well,%s,your volume is %2.2f  cubic feet.
    ",name,volume);//%2.2f表示字符宽度为2,精确到小数点后两位
    18     printf("Also,your first name has %d letters,
    ",letters);
    19     printf("and we have %d bytes to store it in.
    ",size);
    20     return 0;
    21 }

    2.关于字符串

    (1)字符串是一个或多个字符的序列。如"I am a student!"。

    (2)C语言用空字符来标记一个字符串的结束。数组的单元数必须至少比要存储的字符数多1。

    (3)字符串和字符。'x'和"x"的区别(后者是一个字符串由'x'和''组成)。

    (4)Sizeof()和strlen()函数。

      • 同一个字符串,sizeof()把标志字符串结尾的空字符计算在内,长度比strlen()大1.
      • strlen()以字符为单位给出字符串的长度。sizeof()给出数组的长度(分配的内存单元)。
      • 获取一个类型大大小~获取一个具体量的大小。sizeof(char)和sizeof (name)=sizeof name。
     1 sizeof()和strlen() 
     2 #include<stdio.h>
     3 #include<string.h>
     4 #define PRAISE  "What a super marvelous name!"
     5 int main(void)
     6 {
     7     char name[40];
     8     printf("What's your name?
    ");
     9     scanf("%s",name);
    10     printf("Hello,%s.%s
    ",name,PRAISE);
    11     printf("Your name of %d letrers occupies %d memory cells.
    ",
    12         strlen(name),sizeof(name));//sizeof name
    13     printf("The phrase of praise has %d letters",
    14         strlen(PRAISE));
    15     printf("and occupies %d memory cells.
    ",sizeof(PRAISE));//sizeof PRAISE
    16     return 0;
    17 }

    3.常量和C预处理器

    (1)常量如0.015。float taxrate=0.015。把常量0.015赋值给变量taxrate,但程序可能意外的改变它的值。

    (2)两种方法const修饰符和#define预处理命令

      • const  int MONTHS=12;
      • #define MONTHS     +12;(#define TEE  'T')(#define OOPS  "Now you have it!")

    4.printf()函数

    (1)printf():(“控制描述"+变量列表)~(变量使用的是值,无论该值是变量、常量、还是表达式)。

    (2)printf()转换说明符:%c--一个字符、%d--有符号十进制整数、%e--浮点数e记数法、%、f--浮点数十进制、%p--指针、%%--打印一个%、%s--字符串...:

      • 请避免不匹配的转换。

    (3)printf()标志符:-(左对齐)、+(带符号)、#(...)、0(对所有数字格式,用前导0填充字段宽度)

      • 打印一个字符串的前8个字符,字段宽度为8字符(%8.8s)
      • 打印双引号"...."
      • 打印一个字段宽度在参数列表中给定的八进制整数(%*0).
      • %5d(00006)
      • 指定固定字段宽度(有效防止溢出)

    (4)用printf()打印较长的字符串

    a.采用多个printf()函数;

    b.在一个printf()中采用()和回车键

    c.采用字符串连接方法("Hello""world")

     1 printf()打印较长字符串
     2 #include<stdio.h>
     3 int main(void)
     4 {
     5     printf("Here's one way to print a ");
     6     printf("long string.
    ");//a
     7     printf("Here's another way to print a 
     8 long string.
    ");//b
     9     printf("Here's the newest way to print a "
    10         "long string.
    ");//c
    11     return 0;
    12 }

    (5)printf()的函数返回值(返回所打印字符的数目,如果输出有误则返回-1,常用于检查输出错误。向文件中而非屏幕)

     1 printf()的返回值
     2 #include<stdio.h>
     3 int main(void)
     4 {
     5     int bph2o=212;
     6     int rv;
     7  
     8     rv=printf("%d F is water's boiling point.
    ",bph2o);
     9     printf("The printf()function printed %d characters.
    ",rv);
    10     return 0;
    11 }

    5.scanf()函数

    (1)scanf()会在遇到第一个空白字符空格、制表符、或者换行符处停止读取。~gets()函数可以用来读取一个字符串。

    (2)读取变量类型的值加&,把字符串读进一个字符数组不使用&。

    (3)scanf("%d,%d",&n,&m)接受输入  1,2   {scanf("%c",&ch)读取在输入中遇到的第一个字符}

    6.关于修饰符*

      • printf()中在字段宽度部分使用*来代替数字
      • scanf()中*使函数跳过相应的输出项目(scanf("%*d%*d%d",&n);前两个跳过)

      

     1 使用可变宽度的输出字段
     2 #include<stdio.h>
     3 int main(void)
     4 {
     5     unsigned width,precision;
     6     int number=256;
     7     double weight=242.5;
     8 
     9     printf("What field width?
    ");
    10     scanf("%d",&width);
    11     printf("The number is :%*d:
    ",width,number);
    12     printf("Now enter a width and a precision:
    ");
    13     scanf("%d%d",&width,&precision);
    14     printf("Weight=%*.*f
    ",width,precision,weight);
    15     return 0;
    16 }
  • 相关阅读:
    Atitit..组件化事件化的编程模型(2)Web datagridview 服务器端控件的实现原理and总结
    Atitit.dwr3 不能显示错误详细信息的解决方案,控件显示错误详细信息的解决方案 java .net php
    Atitit.实现继承的原理and方法java javascript .net c# php ...
    Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php
    Atitit..文件上传组件选型and最佳实践总结(2)断点续传
    atitit.web的动态include 跟变量传递 java .net php
    Atitit. BigConfirmTips 控件 大数据量提示确认控件的原理and总结O9
    Atitit.guice3 ioc 最佳实践 o9o
    Atitit.hybrid混合型应用 浏览器插件,控件的实现方式 浏览器运行本地程序的解决方案大的总结提升用户体验and开发效率..
    atitit.提升开发效率使用服务器控件生命周期 asp.net 11个阶段 java jsf 的6个阶段比较
  • 原文地址:https://www.cnblogs.com/asd5551680/p/4153433.html
Copyright © 2011-2022 走看看