C Output Functions

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

  • printf()
  • putchar()
  • puts()
  • fprintf()
printf() function
 
The printf() function is used to print string or data values or a combination of string and data values on the output screen. The printf() function is built-in function defined in a header file called "stdio.h". 
The printf() function has the following syntax...
printf("message!");
Example 
 
#include<stdio.h>
#include<conio.h>
void main(){

   printf("Hello! Welcome to my tutorial!!");
   getch(); 
 
Output
 


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 displayed.

 printf("format string",variableName);
Example
 
#include<stdio.h>
#include<conio.h>

void main(){
   int i = 30;
   float j = 2.5;
   printf("%d \t %f",i, j);   
  getch();
}
 
Output 
 

 
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 requirement.
 


putchar() function
 
The putchar() function is used to display a single character on the output screen. The putchar() functions prints the character which is passed as a parameter to it and returns the same character as a return value. This function is used to print only a single character. To print multiple characters we need to write multiple times or use a looping statement.
 
Example
 
#include<stdio.h>
#include<conio.h>

void main(){
   char ch = 'Z';
   putchar(ch);   
   getch();
 
Output 




puts() function
 
The puts() function is used to display a string on the output screen.
 
Example
 
#include<stdio.h>
#include<conio.h>

void main(){
   char name[30];
   printf("\nEnter your name: ");
   gets(name);
   puts(name);
   getch();
}
 
Output 
 

 

  
fprintf() function
 
The fprintf() function is used with the concept of files. The fprintf() function is used to print a line into the file. 
 
 
 

No comments:

Post a Comment