C language input (scanf) output (printf)

output printf

  • puts(): Only strings can be output, and the output will automatically wrap after the end of the output
  • putchar(): can only output a single character, no newline
  • printf(): It can output various types of data.
    printf() is the most flexible, complex and commonly used output function, which can replace the above two:
    insert image description here

decimal output

insert image description here

#include <stdio.h>
#include <stdlib.h>
int main()
{
    
    
    float a = 0.00001;
    float b = 3000000;
    float c = 12345678.84;
    float d = 11.229338455;
    printf("a=%g \nb=%g \nc=%g \nd=%g\n", a, b, c, d);
    printf("c=%e\n", c);
    /*
        %g以最短的方式来输出小数,让输出结果更加简练。所谓最短,就是输出结果占用最少的字符。
        但是比较长时有损失数据的问题出现

        %e是科学计数法输出数字,最多八位底数,多余8位会损失数据
    */

    /*
        小数赋值给整数类型时,会丢掉小数部分
        整型赋值给小数会在,小数点后加0
        %f 和 %lf 默认保留六位小数,不足六位以 0 补齐,超过六位按四舍五入截断。
    */
    int f = 123.0123f;
    printf("%d\n", f);
    double g = f;
    printf("%lf\n", g);
    return 0;
}

Advanced usage of printf()

%[flag][width][.precision]type
  • type indicates the output type, must have
    insert image description here

  • .precision indicates the output precision, that is, the number of decimal places

  • width represents the minimum output width, how many digits

  • flag is a flag character.
    insert image description here

#include <stdio.h>
int main()
{
    
    
  int a1 = 20, a2 = 345, a3 = 700, a4 = 22;
  int b1 = 56720, b2 = 9999, b3 = 20098, b4 = 2;
  int c1 = 233, c2 = 205, c3 = 1, c4 = 6666;
  int d1 = 34, d2 = 0, d3 = 23, d4 = 23006783;

  printf("%-9d %-9d %-9d %-9d\n", a1, a2, a3, a4);
  printf("%-9d %-9d %-9d %-9d\n", b1, b2, b3, b4);
  printf("%-9d %-9d %-9d %-9d\n", c1, c2, c3, c4);
  printf("%-9d %-9d %-9d %-9d\n", d1, d2, d3, d4);

  printf("\n");
  printf("%9d %9d %9d %9d\n", a1, a2, a3, a4);
  printf("%9d %9d %9d %9d\n", b1, b2, b3, b4);
  printf("%9d %9d %9d %9d\n", c1, c2, c3, c4);
  printf("%9d %9d %9d %9d\n", d1, d2, d3, d4);

  return 0;
}

In %-9d, d means to output in decimal, 9 means to occupy a width of at least 9 characters, and the width is not enough to fill with spaces, and - means to left-align. Taken together, %-9d means output in decimal, left-aligned
insert image description here

type scanf

  • scanf(): Similar to printf(), scanf() can input multiple types of data.
  • getchar(), getche(), getch(): All three functions are used to enter a single character.
  • gets(): Get a row of data and process it as a string.

  • scanf is the abbreviation of scan format, which means format scan, that is, to obtain user input from the keyboard, and the function of printf is just the opposite.
  • scanf() will not skip data that does not meet the requirements, and will fail to read when it encounters data that does not meet the requirements, instead of continuing to wait for user input.
  • For scanf(), the format of the input data must be consistent with the format of the control string.
  • Scanf variables are preceded by an & symbol. & is called the address symbol, which means to get the address of the variable in memory.

insert image description here

#include <stdio.h>
int main()
{
    
    
  char letter;
  int age;
  char url[30];
  float price;

  scanf("%c", &letter);
  scanf("%d", &age);
  scanf("%s", url); //可以加&也可以不加&
  scanf("%f", &price);

  printf("26个英文字母的最后一个是 %c。\n", letter);
  printf("C语言中文网已经成立%d年了,网址是 %s,开通VIP会员的价格是%g。\n", age, url, price);

  char author[30], lang[30], url[30];
  scanf("%s %s", author, lang);
  printf("author:%s \nlang: %s\n", author, lang);

  scanf("%s", url);
  printf("url: %s\n", url);
  return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46372074/article/details/126695928