Friday, April 29, 2016

Simple Input/Output Function in C

Input/Output Function in C

There exist several functions in C which can be used to perform input/output operations. These functions are collectively known as the standard input-output library and are predefined in special files called header files.

A) The Printf() function :

This function is used to display formatted output on the screen. It is defined in the header file <stdio.h> and can be used in two ways:
                                      printf ("Any text messages");
                                  And printf ("format specifiers", variable list);
Example: #include<stdio.h>
                 main ()
                 {printf ("welcome to C program");
                    }
Here in above program,"stdio.'h" header files store the meaning of printf and main () is point where program start. Whenever we execute program, it start from main end after closed middle brackets. This program displays result as:
                     Output: welcome to C program
However we can’t see result in compile because we miss one function that i describe later.

B) The Scanf () Function:

This function is used to receive input through the keyword. It is also defined in the header file <stdio.h>. The general syntax of scanf () function is:
                          scanf ("format specifiers",variable list);
Example: #include <stdio.h>
                #include <conio.h>
                main ()
                {int x;
                 scanf (%d",&x);
                 printf ("%d", x);
                 getch ();
                }
Here in above program we take input through user using "scanf ()" function and display result using printf () function as done earlier. The rest of content is same except getch() function and <conio.h> header file. Here, getch() display results in compiler and view able to user until he wants for next program or until he want to terminate it. The meaning of getch() is store in "conio.h" header file so when program execute over getch() function it takes its meaning form "conio.h" automatically. 

Example 1: Program to input two numbers and find their sum

Solution:

Logic: In this case, we take two numbers from user individually and add them and finally store in new place to show result.
                      --> Input A
                      --> Input B
                      --> Add A+B
                      --> Store in Sum
                      --> Display Result in Sum
Program:
          #include <stdio.h>
          #include <conio.h>
          void main ()
           {int a, b, Sum;
               clrscr ()
              printf ("Input any two number");
              scanf ("%d%d", &a,&b);
                Sum=a+b;
               printf ("%d", sum);
              Or printf ("Sum of two numbers=%d", Sum);
              getch ();
           }
 
Here clrscr() stands for clear the screen. It clears garbage value of integer, float and character data type we declared. By default, value of a,b,c have randomly values assigned. So when we use clrscr() function it clear all its default value to zero.

Output: Input any two number.....5 10
             Sum of two number=15

No comments:

Post a Comment

Popular Posts