Common output functions include: * `printf()`: formatted output to the standard output device (usually the screen). * `fprintf()`: Format output to the specified file stream. * `sprintf()`: Formatted output into a string. Common input functions include: * `scanf()`: Format input from the standard input device. * `fscanf()`: Format input from the specified file stream. * `sscanf()`: Format input from a string.
In C language, functions for input and output are mainly provided by the standard input and output library
Output function:
For example:
c复制代码printf("Hello, world!\n");
For example:
c复制代码FILE *fp = fopen("output.txt", "w");if (fp != NULL) {fprintf(fp, "Hello, file!\n");fclose(fp);}
For example:
c复制代码char buffer[50];int a = 10;sprintf(buffer, "The value of a is %d", a);printf("%s\n", buffer);
Input function:
For example:
c复制代码int a;printf("Enter a number: ");scanf("%d", &a);printf("You entered: %d\n", a);
It should be noted that these functions all involve format strings, which contain various format specifiers (such as %d for integers, %f for floating point numbers, %s for for strings, etc.). You need to choose the appropriate format specifier based on the type of data you want to input or output.
In addition, for more complex input and output requirements, C language also provides other functions and tools, such as file operation functions (fopen(), fclose(), fread(), fwrite(), etc.), character operation functions (getchar(), putchar(), gets(), puts(), etc.) etc. You can choose the appropriate function to use according to your specific needs.
The above is the detailed content of Introduction to several output and input functions in C language. For more information, please follow other related articles on the PHP Chinese website!