When solving problems on a computer using a programming language, it is necessary to display the result of the solution in a form understandable to the user. In this case, the form of displaying the output data can be different. Often the result of the program's work is displayed on the screen or in an external file as text. The C programming language uses special functions to display on the screen. With their help, any type of data can be easily displayed on the screen or in a file in the desired representation.
Necessary
The programming environment of the C
Instructions
Step 1
To use functions that provide output of a data stream, include a special library at the beginning of the program. To do this, write a line like: #include.
Step 2
Write a program code that solves a given problem. If you use your own functions for calculations, be sure to return all intermediate results from them to the main function main. It is also desirable to output the final result to the screen or to a file from the main body of the program.
Step 3
Use the overloaded printf function to print the result to the screen. Specify the type of output value with a special character in one of the function parameters. If the variable with the final value Result is of type int, then use a notation like: printf ("
The result is displayed and is equal to% d
", Result). Explanatory text before the variable, write the one you need. The special character"% d "indicates that a numeric value of the int type is displayed. The character"
»Produces a carriage return, that is, allows you to display data on a new line. To display a variable of a string type, use the special characters "% s" and "% c".
Step 4
The output of the resulting variables to a file occurs using other functions. First of all, open an existing or create a new file on your hard drive. To do this, enter the variable in the program: FILE * fp. Open the file for writing: fp = fopen ("output.dat", "w"). Here output.txt is the name of the file to output the result, and the "w" character indicates to open the file in write mode. If a file with this name does not exist on the disk, the function will create it when executed.
Step 5
Write the resulting variable to the file. To do this, use the fprintf (fp,"
The result is output to a file and is equal to% d
, Result). The first parameter specifies the file descriptor to write, the rest of the parameters are similar to those described for the printf function.
Step 6
After all the required data is displayed, close the file with the fclose (fp) command. Now, when you run the program, you will see the result on the screen or in a file.