🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Input Output Statements.docx

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

**Input Output Statements** [ **Introduction**] All input/output operations are carried out through function calls such as printf and scanf. There exist several functions that have more or less become standard for input and output operations in C. These functions are collectively known as the *sta...

**Input Output Statements** [ **Introduction**] All input/output operations are carried out through function calls such as printf and scanf. There exist several functions that have more or less become standard for input and output operations in C. These functions are collectively known as the *standard I/O library.* **[Reading a character]** The simplest of all input/output operations is reading a character from the standard input unit ( the keyboard) and writing it to the standard output unit ( the screen). **Reading a single character can be done by using the function getchar**. The general form: *variable\_name* = **getchar(**); *variable\_name* is a valid C name that has been declared as char type. When this statement is encountered, the computer waits until a key is pressed and then assigns this character as a value to **getchar(**) function. Since getchar**(**) is used on the right-hand side of an assignment statement, the character value of getchar**(**) is in turn assigned to the variable name on the left. For example char c; c = getchar(); will assign the character a to the variable c' when we press the key a on the keyboard. **[Program 1]** \#Include \ \#Include\ main( ) { char character; printf(\"Press any key \\in\"); character = getchar( ); if (isalpha(character) \> 0) printf(\"The character is a letter\"); else if (Isdlglt (character) \> 0) printf(\"The character is a digit\"); else printf(\"The character is not alphanumeric.\"); } **[Character Test Functions]** **isalpha( character) isdigit(character) functions.** isalpha assumes a value non-zero (TRUE) if the argument character contains an alphabet; otherwise it assumes 0 (FALSE). Similar is the case with the function isdigit. C supports many other similar functions which are given in Table below. To use these functions one must include \#include \< ctype.h\> in the program. +-----------------------------------+-----------------------------------+ | ***Function*** | **Purpose** | +===================================+===================================+ | isalnum(c) | Is c an alphanumeric character? | | | | | isalpha(c) | Is c an alphabetic character? | | | | | iSdigit(c) | Is c a digit? | | | | | islower(c) | Is c a lower case letter? | | | | | isprint(c) | Is c a printable character? | | | | | ispunct(c) | Is c a punctuation mark? | | | | | isspace(c) | Is c a *white space* character? I | | | | | isupper(c) | s c an upper case letter? | +-----------------------------------+-----------------------------------+ **[Writing a character]** Like getchar, there is a function putchar for writing characters one at a time to the screen. The general form is : ­ **putchar(variable *\_name);*** where variable-name is a type char variable containing a character. This statement displays the character contained in the variable-name at the terminal. For example, the statements anawer = \'X\'; putchar(anawer); This will display the character X on the screen. **[Program-2]** \#include \ \#include \ main( ) { char alphabet; printf(\"Enter an alphabet\"); alphabet = getchar( ); if (islower(alphabet)); putchar(toupper( alphabet)); else putchar(tolower( alphabet)); } **[Inputting Data using scanf function ]** The scanf function can be used to input data of all types.The general format of scanf function is ***scanf(\"control string\", arg* 1, *arg2,..argn);*** The control string specifies the field format in which the data is to be entered and the arguments argl, arg2,.....arg n specify the address of locations where the data is stored. - Control string and arguments are separated by commas. - Control string contains field specifications which direct the interpretation of input data. - It may include: format specifications, consisting of the conversion character %, a data type character (or type specifier ), and an optional number, specifying the field width. - Formatted input refers to an input data that has been arranged in a particular format. The table 2 below gives scanf field or format specifications for various data types +-----------------------------------+-----------------------------------+ | Format code | Meaning | +===================================+===================================+ | \%c | read a single character | | | | | \%d | read a decimal integer | | | | | \%e | read a floating point value | | | | | \%f | read a floating point value | | | | | \%g | read a floating point value | | | | | \%h | read a short integer | | | | | \%i | read a decimal, hexadecimal, | | | oroctal integer | | \%0 | | | | read an octal integer | | \%s | | | | read a string | | \%u | | | | read an unsigned decimal Integer | | \%x | | | | read a hexadecimal integer | | \%\[..\] | | | | read a string of word(s) | +-----------------------------------+-----------------------------------+ **[Inputting integer numbers]** The field specification for reading integer number is \%wd The percent sign (%) indicates that a conversion specification follows. w is an integer number that specifies the field width of the number to be read and d, known as data type character, indicates that the number to be read is in integer mode. Consider the following example: scanf(\"%2d %5d\", &num1, &num2); If the data line is : 50 31426 The value 50 is assigned to numl and 31426 to num2. Suppose the input data is as follows: 31426 50 The variable num1 will be assigned 31 (because of %2d) and num2 will be assigned 426 (unread part of 31426). The value 50 that is unread will be assigned to the first variable in the next scanf call. This kind of errors may be eliminated if we use the field specifications without the field width specifications. That is, the statement scanf{\"%d %d\", &num1, &num2); will read the data 31426 50 correctly and assign 31426 to num1 and 50 to num2. Input data items must be separated by spaces, tabs or newlines. Punctuation marks do not count as separators. An input field may be skipped by specifying \* in the place of field width. For example, the statement scanf{\"%d %\*d %d", &a, &b) will assign the data 123 456 789 as follows: 123 to a 456 skipped (because of \*) 789 to b **[Inputting Real Numbers]** Unlike integer numbers, the field width of real numbers is not to be specified and therefore scanf reads real numbers using the simple specification %f for both the notations, namely, decimal point notation and exponential notation. For example, the statement. scanf(\"%f %f %f\", &x, &y, &z); with the input data 475.89 43.21E-1 678 will assign the value 475.89 to x, 4.321 to y, and 678.0 to z. The input field specifications may be separated by any arbitrary blank spaces. **[Inputting Character Strings]** We have already seen how a single character can be read from the terminal using the getchar() function. The same can be achieved using the scanf function also. In addition, a scanf function can input strings containing more than one character. Following are the specifications. reading character strings: \%ws or %wc The corresponding argument should be a pointer to a character array. However, %c may used to read a single character when the argument is a pointer to a char variable. Some versions of scanf support the following conversion specifications for strings: **%\[characters\] and %\[ \^ characters\]** The specification %\[characters\] means that only the characters specified within the bracket are permissible in the input string. If the input string contains any other character, the scanf will be, terminated at the first encounter of such a character. The specification %\[\^characters\] exactly does exactly the reverse. That is, the characters specified after the circumflex () are not permitted in the input string. The reading of the string will be terminated at the encounter of one of these characters. We have just seen that %s specifier cannot be used to read strings with blank spaces. But, this can be done with the help of %\[ \] specification. Blank spaces may be included within the brackets, thus enabling the scanf to read strings with spaces. Remember that the lowercase and uppercase letters are distinct. **[Reading Mixed Data Types]** It is possible to use one scanf statement to input a data line containing mixed mode data. The statement: **scanf(\"%d %c %f %s\", &count, &code, &ratlo, name);** will read the data 15 P 1.575 coffee correctly and assign the values to the variables in the order in which they appear. **Given below are some of the general points to keep in mind while writing a scanf statement.** I. All function arguments, except the control string, must be pointers to variables. 2\. Format specification contained in the control string should match the arguments in order. 3\. Input data items must be separated by spaces and must match the variables receiving the input in the same order. 4\. The reading will be terminated, when scanf encounters an \'invalid mismatch\' of data or a character that is not valid for the value being read. 5\. When searching for a value, scanf ignores line boundaries and simply looks for the next appropriate character. 6\. Any unread data items in a line will be considered as a part of the data input line to the next scanf call. #### [Outputting Data Using printf function] The printf statement provides way to display data on the screen in the desired way. The general form of printf statement is **printfl(\"control string\", arg1, arg2,...... argn);** Control string consists of three types of items: 1\. Characters that will be printed on the screen as they appear. 2, Format specifications that define the output format for display of each item, 3\. Escape sequence characters such as \\n, \\t, and \\b. The control string indicates how many arguments follow and what their types are. The arguments argl, arg2, argn are the variables whose values are formatted and printed according to the specifications of the control string. The arguments should match in number, order and type with the format specifications. **A simple format specification has the following form:** **% w.p type-specifier** where w is an integer number that specifies the total number of columns for the output value and p is another integer number that specifies the number of digits to the right of the decimal point (of a real number) or the number of characters to be printed from a string. Both w\' and p are optional. Some examples of printf statement are: printf(\"Programming in C\"); printf(\" \"); printf(\" \\n\"); printf(\"%d\", x); printf(\"a = %d\\n b = %f\", a, b); printf(\"sum = %d\", 1234); printf(\"\\n\\n\"); [Output of Integer Numbers] ======================================= The format specification for printing an integer number is \%wd where w specifies the minimum field width for the output. However. if a number is greater than the specified field width. it will be printed in full. overriding the minimum specification, d specifies that the value to be printed is an integer. --- --- --- --- 1 2 3 4 --- --- --- --- printf("%6d",1234) -- -- --- --- --- --- 1 2 3 4 -- -- --- --- --- --- --- --- --- --- 1 2 3 4 --- --- --- --- --- --- --- --- -- -- 1 2 3 4 --- --- --- --- -- -- --- --- --- --- --- --- 0 0 1 2 3 4 --- --- --- --- --- --- **Output of Real Numbers** The output of a real number may be displayed in decimal notation using the following format specification: The integer w indicates the minimum number of positions that are to be used for the display of the value and the integer p indicates the number of digits to be displayed after the decimal point (precision). The value, when displayed, is rounded to p decimal places and printed right justified in the field of w columns. The following examples illustrate the output of the number y = 98.7654 under different format specifications: printf(\"% 7.4f\",y) --- --- --- --- --- --- --- 9 8. 7 6 5 4 --- --- --- --- --- --- --- printf(\"% 7.2f\",y) -- -- --- --- --- --- --- 9 8. 7 7 -- -- --- --- --- --- --- printf(\"% -7.2f\",y) --- --- --- --- --- -- -- 9 8. 7 7 --- --- --- --- --- -- -- printf(\"%f\",y) --- --- --- --- --- --- --- 9 8. 7 6 5 4 --- --- --- --- --- --- --- [Printing Single Character] ======================================= A single character can be displayed in a desired position using the format specification \%wc The characters will be right justified in the given field width of w columns. To left justify the output put --sign before the integer w. Printing Of Strings The format specification for outputting string is \%w.ps Where w specifies total filed width for display and p specifies that only first p characters of string should be displayed. **It is permitted to mix data types in one printf statement. for examples the statement like** **printf( "%d%f%c%d" ,a,b,c,d); is valid one.** Following table gives printf format codes +-----------------------------------+-----------------------------------+ | **Format code** | **Meaning** | +===================================+===================================+ | \%c | print a single character | | | | | \%d | print a decimal integer | | | | | \%e | print a floating point value in | | | exponent form | | \%f | | | | print a floating point value | | \%g | | | | print a floating point value | | \%i | either in e-type or f-type | | | depending on value. | | \%0 | | | | Print a singed decimal integer | | \%s | | | | print an octal integer without | | \%u | leading zeros | | | | | \%x | print a string | | | | | | print an unsigned decimal Integer | | | | | | print a hexadecimal integer | | | without leading ox | +-----------------------------------+-----------------------------------+

Use Quizgecko on...
Browser
Browser