Get the App
SLTechnology News&Howtos  ›  Development  › 

Input and output methods of C++ format control characters scanf and printf

Shulou Source: shulou.com Published: 2022-05-31 19:44:38 09月15日 Update

This article mainly introduces "C++ format control character scanf and printf input and output methods", in daily operation, I believe many people in C++ format control character scanf and printf input and output methods have doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "C++ format control character scanf and printf input and output methods" doubts helpful! Next, please follow the small series to learn together!

Use Scanf input and Printf output

These are two functions in C.

1. Use of Scanf

Format:

scanf("format control", variable address);scanf("%d",&n);

scanf formatter for common data type variables

int%dlong %lldfloat%fchar%cdouble%lf String (char array)%s

Like 13:45:20, you can type that.

scanf("%d:%d:%d",&h,&m,&s);

Except for %c, scanf inputs other formatting characters with blank characters (spaces, tabs) as the end judgment flag, character arrays with %s read with spaces and newlines as the end of reading flags, %c can read spaces and newlines.

printf("format control", variable name);

Format control character only double becomes %f

If you want to print % , just add a % \to the front.

Common output format 1. %md

Int-type variables with less than m bits can be right-aligned output with m bits, and the high bits are filled with spaces. If they exceed m bits, they remain unchanged.

#includeusing namespace std;int main(){ int a=123,b=123456; printf("%5d\n",a); printf("%5d\n",b); return 0;}

123

123456

2、%0md

Different from %md, when the variable is less than m bits, complement 0.

#includeusing namespace std;int main(){ int a=123,b=123456; printf("%5d\n",a); printf("%5d\n",b); printf("d\n",a); return 0;}

123

123456

00123

3、%.mf

Floating-point numbers can be output with m decimal places

#includeusing namespace std;int main(){ double d=3.14159; printf("%.0f\n",d); printf("%.1f\n",d); printf("%.2f",d); return 0;}

3

3.1

3.14

Third, use getchar input putchar output a single character

Can recognize newline characters

char c1,c2;c1=getchar();c2=getchar();putchar(c1);putchar(c2); At this point, the study of "C++ format control character scanf and printf input and output methods" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Tags: Format output input control control character method variable cantilever space learning character array more flag help different practical next two function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MySQL Shulou Technology Shulou Information Linux