C keyboard input – Input Output Functions C Programming

Input and Output in C programming language are accomplished through library functions. C programming language has defined many library functions for input and output.

In C programming, scanf() and printf() functions are most commonly used standard library function for taking input form keyboard and printing output on screen respectively.

Here, when we are say Input that means feeding data into C program using the keyboard and output means printing data on screen. We will discuss file Input and Output in separate section.

Most of the standard library function for Input and Output is defined in stdio.h header file.
C keyboard input: C programming language treats all the I/O devices and files as stream of data. A C program reading data from keyboard is similar to reading data from a file similarly printing data on screen is similar to writing data on a file. Three streams gets automatically attached when a program starts execution.

Stream File Pointer I/O Device
Standard Input stdin Keyboard
Standard Output stdout Screen
Standard Error stderr Screen

Standard Library Functions for Input & Output of a Character

Formatted input and output functions in c: Header file stdio.h provides two in-built functions for reading a character from keyboard and writing/printing a character on screen.

Function Description
getchar() Returns a character from stdin stream(keyboard).
putchar() Writes a character to stdout stream(screen).

C Program for Input and Output of a Character

Input Output Functions C Programming

#include<stdio.h>
#include<conio.h>
  
int main(){
   char c;
  
   printf("Enter a character\n");
   /* Reading a character from keyboard */
   c = getchar();
   printf("You Entered \n");
   /* Printing a character on screen */
   putchar(c);
    
   getch();
   return(0);
}

Program Output

Enter a character
J
You Entered 
J

<h3″>Standard Library Functions for Input & Output of Strings

Function Description
gets() Reads a line from stdin(keyboard) and stores it into given character array.
puts() Writes a string to stdout(screen) stream excluding null terminating character.

C Program for Input and Output of a String

C Program for Input and Output of a String

#include <stdio.h>
#include <conio.h>
  
int main(){
   char string[50];
  
   printf("Enter your name\n");
   gets(string);
  
   printf("You name is : ");
   puts(string);
     
   getch();
   return(0);
}

Output

Enter your name
Jack
You name is : Jack

Standard Library Functions for Formatted Input & Output

The stdio.h Library function printf() and scanf() are used to print and read formatted data in a C program respectively. Function printf and scanf both can be used for Input and output of all in-built data types as well as user defined data types.

Function Description
printf() Print formatted data to stdout(screen).
scanf() Writes a character to stdout stream(screen).

C Program to shows the use of printf function

C Program to shows the use of printf function

#include <stdio.h>
#include <conio.h>
  
int main(){
  
    printf("Printing characters\n");
    printf("%c %c %c %c\n\n", 'a', 'A', '#', '1');
      
    printf("Printing integers\n");
    printf("%d %ld %10d %010d\n\n", 2015, 2015L, 2015, 2015);
      
    printf("Printing floating point numbers\n");
    printf("%f %5.2f %+.0e %E\n\n", 1.41412, 1.41412, 1.41412, 1.41412);
      
    printf("Printing string\n");
    printf("%s\n\n", "TechCrashCourse");
      
    printf("Printing radicals\n");
    printf ("%d %x %o %#x %#o\n\n", 2105, 2015, 2015, 2015, 2015);
      
    getch();
    return 0;
}

Output

Printing characters
a A # 1

Printing integers
2015 2015       2015 0000002015

Printing floating point numbers
1.414120  1.41 +1e+000 1.414120E+000

Printing string
TechCrashCourse

Printing radicals
2105 7df 3737 0x7df 03737

C Program to shows the use of scanf function

C Program to shows the use of scanf function

#include <stdio.h>
#include <conio.h>
  
int main(){
    int a, b, sum;
    printf("Enter to integers to add\n");
    /* Taking formatted input from user using scanf */
    scanf("%d %d", &a, &b);
    sum = a + b;
    /* Printing formatted output */
    printf("%d + %d = %d", a, b, sum);
     
    getch();
    return 0;
}

Output

Enter to integers to add
5 3
5 + 3 = 8