IIT Delhi CS 123 - Formatted I/O Functions

Unformatted text preview:

Formatted I O Functions Formatted I O functions are used to take various inputs from the user and display multiple outputs to the user These types of I O functions can help to display the output to the user in different formats using the format specifiers These I O supports all data types like int float char and many more Why they are called formatted I O These functions are called formatted I O functions because we can use format specifiers in these functions and hence we can format these functions according to our needs List of some format specifiers S NO Format Specifier Type Description int signed int used for I O signed integer value char Used for I O character value 3 f float Used for I O decimal floating point value 6 u Used for I O unsigned integer value Used for I O string group of characters string long int Used for I O long signed integer value unsigned int unsigned int used for the I O integer value double Used for I O fractional or floating data 1 2 4 5 7 8 d c s ld i lf 9 n prints prints nothing The following formatted I O functions will be discussed in this section 1 printf 2 scanf printf printf function is used in a C program to display any value like float integer character string etc on the console screen It is a pre defined function that is already declared in the stdio h header file Syntax 1 To display any variable value printf Format Specifier var1 var2 varn include stdio h int main int a a 20 printf d a return 0 scanf scanf function is used in the C program for reading or taking any value from the keyboard by the user these values can be of any data type like integer float character string and many more This function is declared in stdio h header file that s why it is also a pre defined function In scanf function we use address of operator which is used to store the variable value on the memory location of that variable Syntax scanf Format Specifier var1 var2 varn Unformatted Input Output functions Unformatted I O functions are used only for character data type or character array string and cannot be used for any other datatype These functions are used to read single input from the user at the console and it allows to display the value at the console Why they are called unformatted I O These functions are called unformatted I O functions because we cannot use format specifiers in these functions and hence cannot format these functions according to our needs The following unformatted I O functions will be discussed in this section 1 getch 2 getche 3 getchar 4 putchar 5 gets 6 puts 7 putch getch getch function reads a single character from the keyboard by the user but doesn t display that character on the console screen and immediately returned without pressing enter key This function is declared in conio h header file getch is also used for hold the screen Syntax getch or getche variable name getch getche function reads a single character from the keyboard by the user and displays it on the console screen and immediately returns without pressing the enter key This function is declared in conio h header file Syntax getche or variable name getche include conio h include stdio h int main printf Enter any character getche return 0 getchar The getchar function is used to read only a first single character from the keyboard whether multiple characters is typed by the user and this function reads one character at one time until and unless the enter key is pressed This function is declared in stdio h header file Syntax Variable name getchar include conio h include stdio h int main char ch printf Enter the character ch getchar printf c ch return 0 putchar The putchar function is used to display a single character at a time by passing that character directly to it or by passing a variable that has already stored a character This function is declared in stdio h header file Syntax putchar variable name gets gets function reads a group of characters or strings from the keyboard by the user and these characters get stored in a character array This function allows us to write space separated texts or strings This function is declared in stdio h header file Syntax char str length of string in number Declare a char type variable of any length gets str include conio h include stdio h int main char name 50 printf Please enter some texts printf You have entered s gets name name return 0 puts In C programming puts function is used to display a group of characters or strings which is already stored in a character array This function is declared in stdio h header file Syntax puts identifier name putch putch function is used to display a single character which is given by the user and that character prints at the current cursor location This function is declared in conio h header file Syntax putch variable name Formatted I O vs Unformatted I O S No Formatted I O functions Unformatted I O functions These functions allow us to take input or display output in the user s desired format These functions do not allow to take input or display output in user desired format These functions support format specifiers These functions do not support format specifiers These are used for storing data more user friendly These functions are not more user friendly Here we can use all data types Here we can use only character and string data types 1 2 3 4 CONTROL STRUCTURES Control structures are used to write accurate error resistant and maintainable code It makes program easy to trace the flow of execution of statements and also in the review and maintenance of the program later There are three main control structures 1 Sequence structure In this structure program execution starts from top to bottom i e it executes statement one first then statement two and so on up to last statement Here each statement executes only once as shown below Entry Action 1 Action 2 2 Selection structure In this structure program execution starts from top to bottom But when condition is encountered flow of execution changed If condition is true then action 1 will perform and then action 3 and if condition is false action 2 will perform and then action3 as shown in figure Exit Action 3 3 Loop structure In this structure program execution starts from top to bottom But here single statement or group of statements can be executed more than once for a finite time till given condition is true Action 3 Exit Entry Condition True False Action 1 Action 2 Entry Loop True Condition Action1 False Action 2 There are 3 different types


View Full Document

IIT Delhi CS 123 - Formatted I/O Functions

Download Formatted I/O Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Formatted I/O Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Formatted I/O Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?