Input Output function in C

Input/Output Function

Standard o/p function         Console o/p function    
(stdio.h)                               (conio.h)            
printf()                                 cprintf()
putc()
putchar()

printf v/s cprintf

  1. printf is a predefine o/p function of stdio.h header file. It is used to print the content on the standard o/p device(Monitor).
  2. If we apply some properties like text colour then it will not be reflected in the text printed by printf function.
  3. cprintf is a predefine o/p function of conio.h header file it is used to print the content on the console screen.
  4. If we apply some property like text colour then it gets reflected in the text printed by cprintf function.
Example:-
#include <stdio.h>
#include<conio.h>
void main()
{
   clrscr(); 
   textcolor(Red);
   cprintf("Hello\n");
   printf("Hi"):
   getch():
}

Both printf and cprintf function is used to display formatted output.

#include <stdio.h>
#include<conio.h>
void main()
{
   int x=1;
   float y=4.5;
   char z='A';
   clrscr();
   printf("%d %f %c",x,y,z);
   getch();
}

putc()  :-

putc is a predefined standard output function of stdio.h header file it is used to write the character at the specified output device.

Ex:--
#include <stdio.h>
#include<conio.h>
void main()
{ 
   clrscr();
   putc('A',stdout);
   getch();
}


putchar() :-

It is a predefined function of stdio.h header file uses to print the character on a standard output device (Monitor).
It internally uses putc with stdout.

Ex:-
#include <stdio.h>
#include<conio.h>
void main()
   clrscr();
   putchar('A');                      //internally     putchar('A');                          getch();                             //             =>  putc('A',stdout);
}  


Input function  

Standard i/p function         Console i/p function       (stdio.h)                             (conio.h)                             scanf()                               cscanf()
getchar()                            getche()
                                           getch()
                                           getchc()


scanf() :-

 scanf is predefined input function of stdio.h header file. It is used to read formatted input from the keyboard.

Ex:-
#include <stdio.h>
#include<conio.h>
void main()
{ 
   int x; 
   clrscr();
   printf("Enter a number");
   scanf("\n%d",&x);
   printf("\n%d",x);
   getch();
}

Ex:-
#include <stdio.h>
#include<conio.h>
void main()
{ 
   float x;
   clrscr();
   printf("Enter a number");
   scanf("\n%f",&x);
   printf("\n%f",x);
   getch();
}

Ex:-
#include <stdio.h>
#include<conio.h>
void main()
{ 
   char x; 
   clrscr();
   printf("Enter a character");
   scanf("\n%c",&x);
   printf("\n%c",x);
   getch();
}

cscanf():-
csnaf is a predefined i/p function of conio.h header file. It is used to read formatted input from the console screen.

NOTE:- If we are reading the i/p from the keyboard then after providing i/p we have to press enter key.
If we are reading the i/p from the console screen then after providing i/p we do not have to press enter key.

Ex:-
#include <stdio.h>
#include<conio.h>
void main()
{ 
   char x; 
   clrscr();
   printf("Enter a character");
   cscanf("\n%c",&x);
   printf("\n%c",x);
   getch();
}

Ex:-
#include <stdio.h>
#include<conio.h>
void main()
{ 
   int x; 
   clrscr();
   printf("Enter a number");
   cscanf("\n%d",&x);
   printf("\n%d",x);
   getch();
   getch();
}

getc() :-
getc is a predefined i/p function of stdio.h header file. It is used to read character input from the specified i/p device.

Ex:-
#include <stdio.h>
#include<conio.h>
void main()
{ 
   char x; 
   clrscr();
   printf("Enter a character");
   x=getc(stdin);
   printf("\n%c",x);
   getch();
}

getchar() :-
getchar is a predefined i/p function of stdio.h header file. It is used to read character input from the keyboard.
Internally uses getc with stdin.

Ex:-
#include <stdio.h>
#include<conio.h>
void main()
{ 
   char x; 
   clrscr();
   printf("Enter a character");
   getchar();
   printf("\n%c",x);
   getch();
}

gete() :-
gete is a predefined i/p function of conio.h header file. It is used to read character input from the console.

Ex:-
#include <stdio.h>
#include<conio.h>
void main()
{ 
   char x; 
   clrscr();
   printf("Enter a character");
   x=getche();
   printf("\n%c",x);
   getch();
}

getch() :-
getch is a predefined function of conio.h header file. It is used to read character input from the console without choosing the character on the console. 

Ex:-
#include <stdio.h>
#include<conio.h>
void main()
{ 
   char x; 
   clrscr();
   printf("Enter a character");
   x=getch();
   printf("\n%c",x);
   getch();
}






  



Comments

Popular posts from this blog

C program that contains a string XOR each character in this string with 0 ,127

Queue in Data Structure(DS)

Implementation of stack Using Array