当前位置:网站首页>C language input and output (printf and scanf functions, putchar and getchar functions)

C language input and output (printf and scanf functions, putchar and getchar functions)

2022-04-23 17:57:00 Chshyz

printf and scanf function 、putchar and getchar function

Input and output operations are performed by C Functions in the standard function library , Use preprocessing instructions at the beginning of the program file #include Put relevant documents in this procedure #include<stdio.h>

One 、printf() Output details :printf( Format control , Output table columns )

  • Format control is a string enclosed in double quotation marks . Abbreviation format string , for example :“%d”.
  • The output list is some data that the program needs to output , It can be a constant 、 Variable or table , for example a.
#include<stdio.h>
int main(){
    
    printf("ddddd\n");
}
~
[[email protected] c]# ./printf
ddddd


#include<stdio.h>
int main(){
    
    int a = 100;
    printf("%d\tdddd\n",a); // \t There are four spaces 
}
~
[[email protected] c]# ./printf
100     dddd


#include<stdio.h>
int main(){
    
    int a = 100;
    printf("%d,%d,%d\n",a,a,a++);
}
[[email protected] c]# ./printf
101,101,100


#include<stdio.h>
int main(){
    
    int a = 100;
    printf("%d,%d,%d\n",a,a++,a++);
}
[[email protected] c]# ./printf
102,101,100


#include<stdio.h>
int main(){
    
    int a = 100;
    printf("%d,%d,%d\n",a,a,++a);
}
[[email protected] c]# ./printf
101,101,101

Two 、scanf() Enter details :scanf(% Format characters , Variable address )

#include<stdio.h>
int main(){
    
    int a;
    scanf("%d",&a);
}
[[email protected] c]# ./scanf
2


#include<stdio.h>
int main(){
    
    int a, b;
    scanf("%d,%d",&a,&b);
    printf("%d,%d\n",a,b);
}
[[email protected] c]# ./scanf
2,3
2,3

3、 ... and 、getchar() and putchar(), Input and output single characters .

  • putchar() Output ( Anyone who ASCLL You can put any size ).
#include<stdio.h>
int main(){
    
    char chshyz = 97;
    putchar(chshyz);
    return 0;
}
~
[[email protected] c]# ./putchar
a
  • getchar() Input , It has no parameters .
#include<stdio.h>
int main(){
    
    char chshyz;
    chshyz = getchar();
    printf("%c\n",chshyz);
    return 0;
}
~
[[email protected] c]# ./getchar
chshyz
c

PS: Count the number of upper and lower case letters of the input string ( One letter circulates once )

#include<stdio.h>
int main(){
    
    char chshyz;
    int small = 0;
    int big = 0;
    int i = 0;
    for(chshyz = getchar(); chshyz!='\n';){
    
       if (chshyz >= 'a' && chshyz <= 'z') // The comparison is ASCLL code 
          small++;
       else if (chshyz >= 'A' && chshyz <= 'Z')
           big++;
       chshyz = getchar();
           i++;
       printf(" The first %d Secondary cycle \n", i);
    }
    printf(" A lowercase letter :%d individual \t Capitalization :%d individual \n", small, big);
    return 0;
}
~

[[email protected] c]# ./getchar
Chshyz
 The first 1 Secondary cycle 
 The first 2 Secondary cycle 
 The first 3 Secondary cycle 
 The first 4 Secondary cycle 
 The first 5 Secondary cycle 
 The first 6 Secondary cycle 
 A lowercase letter :5 individual          Capitalization :1 individual 

版权声明
本文为[Chshyz]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230547413515.html