Input And Output In C

In here we will learn about Input and Output in c programming language . What is input and Output in c programming ? Use of scanf() , print(),getchar(), putchar() ,.gets() and puts() functions in c program . We provide a lots of example please see this....

Input and Output:

Input: It means feed some data into a program . It can be from a file or from command line. C program have a set of build-in function to read/input from a file or command line and feed to the program.


Output: It means display the program data on the screen, comman line or in any file. C program have a set of build-in function to display the data on the screen, comman line or in any file.

[Note: C program counts all the devices as files.]

Standard File File Pointer From
Standard Input stdin keyboard
Standard Output stdout Screen

For that the basic library of c is <stdio.h> 'std' is for Standard 'i' for input 'o' for output and 'h' for head .

Lets have a look some of build-in Input & Output function:

1.The scanf() and printf() function:

scanf(): This function scanf() function can takes sevaral types of data from keyboard.

printf(): This function printf() function can display avobe data of data from keyboard.

Use of scanf() and printf():
  
  scanf("format_specifier", &variable;_name);
  printf("format_specifier", variable_name);

Let's see an example:

  #include<stdio.h>
   int main() {
    int a;
    char b;
    double g;
    scanf("%d", &a;);
    scanf("%c", &b;); 
    scanf("%lf", &g;);
    printf("%d", a);
    printf("%c", b);
    printf("%lf", g); 
   }

Instead of taking and printing sevaral lines you can take multiple input and output in single line.See the following example:


  #include<stdio.h>
  int main() {
    int a;
    char b;
    double g;
    scanf("%d%c%lf", &a;, &b;, &g;);
    printf("%d %c %lf", a, b, g);
  } 

2.getchar() and putchar() Functions:

getchar() and putchar() functions: The getchar() can take a single character input and putchar() function can display a single character at a time.

  
  #include<stdio.h>
  int main() {
    variable_name = getchar();//for input a single character
    putchar(variable_name);//display a single character
    }
  

2.gets() and puts() functions:

gets() and puts() functions: The gets() can take a line input or more than one character and puts() function can display a line input or more than one character at a time.

  
  #include<stdio.h>
  int main() {
    gets(variable_name);//for taking a line input or more than one character
    puts(variable_name);//display a line input or more than one character
  }