Output Functions in C



C programming language provides built-in functions to perform output operation. The output opearations are used to display data on user screen (output screen) or printer or any file. C programming language provides the following built-in output functions...

  1. printf()
  2. putchar()
  3. puts()
  4. fprintf()

printf() function

The printf() function is used to print string or data values or combination of string and data values on the output screen (User screen). The printf() function is built-in function defined in a header file called "stdio.h". When we want to use printf() function in our program we need to include the respective header file (stdio.h) using #include statement. The printf() function has the following syntax...

Syntax:

printf("message to be display!!!");

Example Program

#include <stdio.h>
void main(){

   printf("Hello! Welcome to btechsmartclass!!!");   

}
Output:

Hello! Welcome to btechsmartclass!!!

In the above example program, we used the printf() function to print a string on to the output screen.

The printf() function is also used to display data values. When we want to display data values we use format string of the data value to be display.

Syntax:

printf("format string",variableName);

Example Program

#include <stdio.h>
void main(){
   int i = 10;
   float x = 5.5;
   printf("%d %f",i, x);   

}
Output:

10 5.5

In the above example program, we used the printf() function to print data values of variables i and x on to the output screen. Here i is a integer variable so we have used format string %d and x is a float variable so we have used format string %f.

The printf() function can also used to display string along with data values.

Syntax:

printf("String format string",variableName);

Example Program

#include <stdio.h>
void main(){
   int i = 10;
   float x = 5.5;
   printf("Integer value = %d, float value = %f",i, x);   

}
Output:

Integer value = 10, float value = 5.5

In the above program we are displaying string along with data values.

Every function in C programming language must have a return value. The printf() function also have integer as return value. The printf() function returns an integer value equalent to the total number of characters it has printed.

Example Program

#include <stdio.h>
void main(){
   int i;
   i = printf("btechsmartclass");
   printf(" is %d number of characters.",i);   

}
Output:

btechsmartclass is 15 number of characters.

In the above program, first printf() function printing "btechsmartclass" which is of 15 characters. So it returns integer value 15 to variable "i". The value of "i" is printed in the second printf() function.

Formatted printf() function

Generally, when we write multiple printf() statements the result is displayed in single line because the printf() function displays the output in a single line. Consider the following example program...

#include <stdio.h>
void main(){
   printf("Welcome to ");
   printf("btechsmartclass ");
   printf("the perfect website for learning");   

}
Output:

Welcome to btechsmartclass the perfect website for learning

In the above program, there are 3 printf() statements written in different lines but the output is displayed in single line only.

To display the output in different lines or as we wish, we use some special characters called escape sequences. Escape sequences are special characters with special functionality used in printf() function to format the output according to the user requirment. In C programming language, we have the following escape sequences...

Escape sequence Meaning
\n New line
\t Horizontal Tab
\v Vertical Tab
\a Beep sound
\b Backspace
\\ Backward slash
\? Question mark
\' Single quotation mark
\" Double quotation mark

Consider the following example program...

#include <stdio.h>
void main(){
   printf("Welcome to\n");
   printf("btechsmartclass\n");
   printf("the perfect website for learning");   

}
Output:

Welcome to
btechsmartclass
the perfect website for learning

putchar() function

The putchar() function is used to display single character on the output screen. The putchar() functions prints the character which is passed as parameter to it and returns the same character as return value. This function is used to print only single charater. To print multiple characters we need to write multiple times or use a looping statement. Consiider the following example program...

#include <stdio.h>
void main(){
   char ch = 'A';
   putchar(ch);   

}
Output:

A

puts() function

The puts() function is used to display string on the output screen. The puts() functions prints a string or sequence of characters till the newline. Consiider the following example program...

#include <stdio.h>
void main(){
   char name[30];
   printf("\nEnter your favourite website: "); 
   gets(name);
   puts(name);
}
Output:

Enter your favourite website: www.btechsmartclass.com
www.btechsmartclass.com

fprintf() function

The fprintf() function is used with the concept of files. The fprintf() function is used to print a line into the file. When you want to use fprintf() function the file must be opened in writting mode.