Check The Programming Section
The formatted Output functions writes data back to the output device or console. The formatted output functions support all basic data types such as, int, char, float, and double. The following list of formatted output functions are defined in stdio (in C) or cstdio (in C++) header files:
printf( ) - writes the output to the console
fprintf( ) - writes the output to a file
sprintf( ) - instead of writing output data to c console or file, it writes data to a character buffer
Standard output functions required a formatted specifier to write data to console or files or character buffer as output.
In C/C++, format specifiers define the type of the data to be read or written. Format specifiers tell the compiler about the type of the data. Each format specifier is defined in C/C++ and that have predefined meaning and started with a % character. The required format specifiers for each data type are listed below:
printf( ) is a standard output function, displayed output into the console or standard output device computer screen. The prototype of the printf( ) function is given below:
int printf (format_string, list_of_expressions);
Details of Arguments
format_string - it represents the layout of what's being printed as string and must be passed inside the double quote (" ")
list_of_expressions - it is a comma separated list of variables or expressions yielding the result to be inserted into the output
Return Value
printf( ) returns an integer value as number characters it's printed into the console
Printing Values
To output an integer value %d is used in the format string, and an integer expression in the list_of_expressions.
int number = 45;
printf(“%d”, number);
It prints 45 to the console. We can also print a message as a string before printing the integer number. For example,
printf(“The Number is = %d”, number);
It prints the output as: The Number is = 45. To print an unsigned integer (positive values) %u format specifier can be used instead of %d (used to print negative and positive integer numbers). For example,
unsigned int k = 34;
printf("k = %u",k);
It prints the output as: k = 34. If we pass an negative value as expression and %u is used as format specifier, then negative value will be converted to a known unsigned integer value. For example,
printf("%u",-1);
It prints the output as 4294967295, -1 gets converted to a known unsigned int value. That’s why format specifiers are also called conversion specifiers. To print a character the format specifier %c is used and to print a string as message no format specifier is required. We simply put the message inside the double quote. To print a single floating-point number upto 6th decimal point %f is used. For example,
#include <stdio.h>
int main(){
char ch = ‘A’;
float f = 5.6;
printf(“The Output is ”);
printf(“ch = %c f = %f”, ch, f);
}
sprintf( ) stands for string print. Like printf( ) instead of print the output to the console it prints the output to a character buffer. The prototype of the sprintf( ) function is given below:
int sprintf(char *buffer, format_string, list_of_expressions);
Details of Arguments
*buffer - represents a character array and used to save the output
format_string - it represents the layout of what's being printed as string and must be passed inside the double quote (“ ”)
list_of_expressions - it is a comma separated list of variables or expressions yielding the result to be inserted into the output
Return Value
int - returns the number characters written in the buffer
In C/C++ character buffers are represented using char arrays and also called null terminated strings. For example,
#include<stdio.h>
int main(){
int num1 = 3;
int num2 = 4;
char buffer[20];//declaration of a char buffer as string
sprintf(buffer, "The sum of %d + %d = %d", num1,num2,num1+num2);//line4
printf("%s",buffer);//prints the output from buffer
}
To print the buffer %s format specifier is used. %s is used to print a char buffer as string. The output of code is: The sum of 3 + 4 = 7. This output is written to the char buffer using sprintf( ) formatted function at line4.
It is used to write the output to a file instead of a console like printf( ) function. The prototype of the function is as follows:
int fprintf(FILE *, format_string, list_of_expressions);
FILE * - it is a file pointer pointing to a file stream to write the output.
format_string - it represents the layout of what's being write as string to a file pointed by FILE pointer and must be passed inside the double quote (“ ”)
list_of_expressions - it is a comma separated list of variables or expressions yielding the result to be inserted into the output
Before writing the output to a file stream, the file needs to be created and opened in writing mode ( w ). To do that, a file must be opened first in the project directory or in a specified location. For example,
#include<stdio.h>
#include<stdlib.h>
int main( ){
FILE *ptr;
int year;
char str1[10] = "This is";
char str2[10] = "free";
char str3[10] = "study material";
ptr = fopen("abc.txt","w");
fprintf(ptr, "%s %s %s",str1, str2, str3);
}