C printf, scanf, fprintf and sprintf functions
printf and scanf are two standard C programming language functions for input and output. Both are functions in the studio library which means #include <studio. h> is required at the top of your file.
The standard library provides functions related to printf() and scanf() that can be used for dealing with files and strings. The use of these functions is explained. General file input/output is important in applications where data reside in files on disks and tapes. We will show how to open files for processing and how to use pointer into file. Some applications need temporary files.
printf and scanf Functions with Example,
printf Function
The printf() function has two nice properties that allow flexible use at a high level First, a list of arguments of arbitrary length can be printed, and second, the printing is controlled by simple conversion specifications, or formats.
printf function arguments:
int printf(const char * format,..)
printf like scanf, also requires format placeholders
Example,
printf("hello");
Example,
int number = 9; printf("%d", number);
In this example we have declared an integer called number, which takes the value 9. The ” %d” in printf allows the function to recognize the variable number as being of integer data type.
Our output would simply be 9.
scanf Function
The function scanf() has generally two properties that allow flexible use at high level.The first is that list of arguments of arbitrary length can be scanned and the second is that this input is controlled by simple conversion specifications.
scanf Function arguments:
int scanf(const char * format, [varl], [var2],...)
scanf requires format placeholders within the format string to denote what data type we would like the input to be read as by the computer.
Here are a table of some of the most commonly used placeholders:
Example,
int number; scanf ( "%d" , &number ) ;
In this example we have declared an integer called number. The ” %d” in scanf allows the function to recognise user input as being of an integer data type, which matches the data type of our variable number. The ampersand (&) allows us to pass the address of variable number which is the place in memory where we store the information that scanf read.
Example,
char word [ 256 ]; scanf ( "%s" , word) ;
In this example we have declared a string called word.
The ” %s “ in scanf allows the function to recognise user input as being of string data type, which matches the data type of our variable word. since we’re using a string here we require the header file <string . h>, but unlike the example of the integer before, we do not require the ampersand (&) since string is an array of characters.
An array is already a pointer to a character data type in memory as we have already learned before. Using the ampersand, the above example would be equivalent to:
for(int i=0;i<256;11=1){ scanf("%s", &word[i]);
In a past assignment we were required to read strings one at a time without the \n so that we could print them all on one line. Square brackets in scanf allow us to define which characters to read from input.
sprintf Function
Example,
int n = sprintf(storageString,"string"); printf("%s",storage);
This function is used along with strings. There are at least 2 parameters. The first parameter is where the string will be stored; the second parameter is the formatted string that needs to be printed. The formatted string that is defined in the same way that printf is with its respective placeholders. The possible multi-parameters that are needed in the function are defined the same way as printf. On success, this function returns an integer indicating how long the formatted string is. It is also followed by using printf to print the string to stdout.
fprintf Function
Example,
int n = fprintf(storageFile,"string");
This function is used along with file reading and writing. Similar to sprintf, it takes in at least 2 parameters and returns an integer indicating how long the formatted string is. One of the big differences between sprintf and fprintf is that the first parameter is not a string where the formatted string will be stored, but a file where the formatted string will be printed. Instead of printing to stdout, the formatted string will appear in the file, similar to the >out . txt option in shell command line.