3.7 Simple Input Output Function

3.7 Simple Input Output Function

For inputting and outputting data we use library function .the important of these functions are
getch( ), putchar( ), scanf( ), printf( ), gets( ), puts( ).

3.7.1 getchar() functionIt is used to read a single character (char type) from keyboard. The syntax is char variable name = getchar( );
For reading an array of characters or a string we can use getchar( ) function.
  1. #include<stdio.h> 
  2. #include<conio.h> 
  3. void main ( ) 
  4. {
  5. char place[80] ; 
  6. int i ;
  7.  for ( i = 0 ; (place [i] = getchar ( ) ) ! =`\n` , ++i ) ; 
  8. }
3.7.2 putchar() function It is used to display single character. The syntax is putchar(char c);
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main ( )
  4. {
  5. char alphabet ;
  6. printf ( "Enter an alphabet" );
  7. putchar ('\n ') ;
  8. alphabet=getchar ( );
  9. putchar (alphabet); 
  10. }
3.7.3 scanf() function 
This function is generally used to read any data type- int, char, double, float,string.The syntax is scanf (control string, list of arguments) The control string consists of group of characters, each group beginning % sign and a conversion character indicating the data type of the data item. The conversion characters are c,d,e,f,o,s,u,x indicating the type resp. char decimal integer, oating point value in exponent form, oating point value with decimal point, octal integer, string, unsigned integer, hexadecimal integer. ie, \%s", \%d" etc are such group of characters. 

  1. #include<stdio.h> 
  2. #include<conio.h> 
  3. void main ( ) 
  4. {
  5. char name[30] , l i n e ; 
  6. int x ; 7
  7.  float y ; 
  8.  . . . . . . . . 
  9.  . . . . . . . . 
  10. scanf( "%s%d%f " , name , &x , &y ) ; 
  11. sc anf( "%c ", line) ;

3.7.4 printf() function
This is the most commonly used function for outputting a data of any type. The syntax is printf(control string, list of arguments) Here also control string consists of group of characters, each group having % symbol and conversion characters like c, d, o, f, x etc. 
  1. #include<stdio.h> 
  2. #include<conio.h> 
  3. void main ( ) 
  4. int x ; 
  5. scanf ( "%d", &x );  
  6. x=x*x ; 
  7. printf ( "The square of the number is %d", x ) ; 
  8. 9 g 
Note that in this list of arguments the variable names are without & symbol unlike in the case of scanf( ) function. In the conversion string, one can include the message to be displayed. In the above example "The square of the number is" is displayed and is followed by the value of x. 

3.7.5 gets() and puts() function

The C library function gets() reads a line from stdin and stores it into the string pointed to by str. It stops when either the newline character is read or when the end-of- le is reached, whichever comes first.
  1. #include<stdio.h> 
  2. #include<conio.h>
  3. int main ( ) 
  4. charstr[50 ];
  5. printf ( "Enter a string:");
  6. gets(str); 
  7. printf ( "You entered : %s ", str);
  8. return ( 0 ) ;
  9. }
The C library function int puts() writes a string to stdout up to but not including the null character. A newline character is appended to the output. 
  1. #include<stdio.h>
  2. #include <string.h>
  3. int main ( ) 
  4. {
  5. char str1[15] ;
  6. char str2[15] ; 
  7. strcpy(str1, "Nepal "); 
  8. strcpy(str2, "Online"); 
  9. puts(str1); 
  10. puts(str2); 
  11. return(0); 
  12. }


No comments:

Post a Comment