Bcaguru

C Output Functions

As we know that ‘C’ is a programming language. It is well known and very popular entire world, That is why because, it provides built-in functions to perform output operation.

 The output operations are used to display data on output screen or printer or any file where we want.

These are the following built-in output functions provided by C language…

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

     1*)  printf() function

What is printf() function ?

 Ans: As we have seen earlier, the printf() function is an output function. Here we shall know some primary features of printf() function:—  

  • printf() function is used to print string or data values on the output screen.
  • It is also used to print combination of strings and data values on the output screen (User screen).
  •  The printf() function is defined in a header file called “stdio.h“.
  •   For using printf() function in our program we must have to include the respective header file (stdio.h) using #include statement.

      Syntax used for printf() function:

Syntax:

printf(“message to be display as output on the user screen!!!”);

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

   printf(“Hello friends! Welcome to bcaguru!!!”);  

}

Output:

            Hello friends ! Welcome to bcaguru! ! !

  • In the above example program, we used the printf() function to print a string as an output.
  • The printf() function is also used to display data values. If we want to display data values we use format string of the data value which has to be displayed.

Syntax:

printf(“format string”,variableName);

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

   int i = 10;

   float x = 5.5;

   printf(“%d %f”,i, x);  

}

Output:

            10    5.500000

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 an integer variable so we have used format string %d and x is a float variable so we used format string %f.

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

Syntax:

printf(“String format string”,variableName);

Let us see with an example:

Example Program(3)

#include<stdio.h>

#include<conio.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.500000

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

In C programming language, every function must have a return value.

 The printf() function can also have integer as return value.

 The printf() function returns an integer value equaling to the total number of characters it has printed.

Let us see with an example:

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

   int i;

   i = printf(“bcaguru”);

   printf(“has %d number of characters.”,i);  

}

Output:

            bcaguru has 7 number of characters.

In the above program, first printf() function printing “bcaguru” which contains 7 characters. So it returns integer value 7 to variable “i”. The value of “i” is printed in the another printf() function used in the above example program.

Formatted printf() function

 The printf() function can displays the output only in a single line. So if we want several output in a single line, we have to use multiple printf() statements for every output but in a single line.

Let us consider the following example program:-

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

   printf(“Welcome to “);

   printf(“bcaguru your online guru,”);

   printf(“the perfect website for learning”);  

}

Output:

            Welcome to bcaguru your online guru, the perfect website for learning

As we can see in the above program, there are 3 printf() statements written in three(3) different lines but the output is displayed in single line only.

We use some special characters called ‘escape sequences’ to display the output in several lines on the output screen.

 Escape sequences are the special characters which performs special function used in printf() function to built (format) the output according to the user requirement.

     Let us see the basic and important ‘escape sequences’ used in C programming language.

        The following escape sequences are:

Escape sequence Meaning
\n                     Used to move the cursor to New Line
\t Used to insert Horizontal Tab (5 characters space)
\v  Used to inserts Vertical Tab (5 lines space)
\a Used to originate beep sound
\b Backspace (removes previous character from its current position)
\\ Used to inserts Backward slash symbol
\? Used to inserts Question mark symbol
\’ Used toinserts Single quotation mark symbol
\” Used to inserts Double quotation mark symbol

 Let us consider the following example program…

Example Program

#include<stdio.h>

#include<conio.h>

void main()

{

   printf(“Welcome to\n”);

   printf(“bcaguru,\n”);

   printf(“the perfect website for learning”);  

}

Output:

            Welcome to

            bcaguru,

            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…

Example Program

#include<stdio.h>

#include<conio.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…

Example Program

#include<stdio.h>

#include<conio.h>

void main()

{

   char name[20];

   printf(“\nEnter your favorite online guru website: “);

   gets(name);

   puts(name);

}

Output:

            Enter your favorite online guru website:bcaguru

            bcaguru

fprintf() function

The fprintf() function is used whenever we go through the concept of files. The fprintf() function is used to print a line into the file. When we want to use fprintf() function the file must be opened in written mode.

Leave a Reply

Close Menu
error: Content is protected !!