Monday, 29 January 2018

Input / Output in C

            C library provides inbuilt functions for taking user input and displaying the program output. These functions are declared in stdio.h header file. We will illustrate the use of these functions using sample codes.


printf ( ) function
              
           printf is a function which is used to display integers, characters, strings, etc. on the output screen.

Consider the following C statements :

--------------------------------------------------------------------------------------------------------------------------------




--------------------------------------------------------------------------------------------------------------------------------

             Above printf ( ) function would print " Value of x : 20 " on the screen.We have used %d format specifier in the printf statement to specify that the value to be printed is an integer. Basically, %d is replaced by 20 in the statement. Similarly, for displaying a character, we have to use %c format specifier.

 Following table shows the list of format specifiers :



            We will illustrate all the data types mentioned in the second column later in the tutorial. In the above printf statement, we have used an escape sequence i.e \n which is a newline character. The use of \n causes the next characters to be printed in the next line.

Following table shows the list of escape sequences : 

 

Following program illustrates the use of format specifiers and escape sequences in printf function.

-----------------------------------------------------------------------------------------------------------------------------



-----------------------------------------------------------------------------------------------------------------------------
Output :

Integer : 17 Float : 9.324000
Character : R Double : 3.127845
String : csegeek

Octal Equivalent of 17 is 21
Hexadecimal Equivalent of 17 is 11
-----------------------------------------------------------------------------------------------------------------------------

scanf ( ) function
           
             scanf is a function which is used to display integers, characters, strings, etc. from the keyboard. The use of format specifiers are applicable to scanf ( ) function as well. While taking input in a variable, we must use ampersand ( & ) character before the variable as shown in the program below :

-----------------------------------------------------------------------------------------------------------------------------



-----------------------------------------------------------------------------------------------------------------------------

 String Input / Output
       
            A string is an array of characters. We will discuss more about strings later in the tutorial. Right now we just focus on reading a string from keyboard and displaying it on screen. We have seen how to input a string using scanf function but it has a drawback. It stops reading characters once a space character is encountered i.e if you try to input a string "cse geek", scanf will read only "cse". The C library provides the following functions to input a string and display it.



Following program illustrates the use of string input / output functions :

-----------------------------------------------------------------------------------------------------------------------------




-----------------------------------------------------------------------------------------------------------------------------

sscanf ( ) , sprintf ( ) and snprintf ( )
             
            sprintf doesn't display the output on the screen, rather it stores the output in a buffer. snprintf is same as sprintf but it has an additional argument n which specifies the number of characters to be stored in the buffer. sscanf reads data from a buffer instead of keyboard. Following program illustrates the use of these functions.

-----------------------------------------------------------------------------------------------------------------------------



-----------------------------------------------------------------------------------------------------------------------------
Output:

Value of x = 20
Value o
Value

-----------------------------------------------------------------------------------------------------------------------------

             Similarly, we have fscanf and fprintf functions to read from and write to a file respectively. We will discuss these later in the module on file handling.

No comments:

Post a Comment