C formatted and unformatted input and output constructs
GopikaS12
1,569 views
13 slides
Jan 13, 2022
Slide 1 of 13
1
2
3
4
5
6
7
8
9
10
11
12
13
About This Presentation
A detailed explanation of C language - Formatted and unformatted I/O constructs
Size: 239.24 KB
Language: en
Added: Jan 13, 2022
Slides: 13 pages
Slide Content
C- Formatted and Unformatted Input and Output constructs By , Dr. Gopika S, Asst Professor , Department of CS. Kristu Jayanti college Dr. Gopika S , KJC 1
getchar () & putchar () functions The getchar () function reads a character from the terminal and returns it as an integer. This function reads only single character at a time. The putchar () function displays the character passed to it on the screen and returns the same character. #include < stdio.h > void main( ) { int c; printf ("Enter a character"); c = getchar (); putchar (c); } Dr. Gopika S , KJC 2
gets() & puts() functions The gets() function reads a line from stdin (standard input) into the buffer pointed to by str pointer, until either a terminating newline or EOF (end of file) occurs. The puts() function writes the string str and a trailing newline to stdout . #include< stdio.h > void main() { char str [100]; printf ("Enter a string"); gets( str ); puts( str ); getch (); } Dr. Gopika S , KJC 3
getch (): getch () is a nonstandard function and is present in conio.h header file which is mostly used by MS-DOS compilers like Turbo C. // Example for getch () in C #include < stdio.h > #include < conio.h > int main() { printf ("%c", getch ()); return 0; } Dr. Gopika S , KJC 4
getche () Like getch (), this is also a non-standard function present in conio.h . It reads a single character from the keyboard and displays immediately on output screen without waiting for enter key. Syntax:- #include < stdio.h > #include < conio.h > // Example for getche () in C int main() { printf ("%c", getche ()); return 0; } Input: g(without enter key as it is not buffered) Output: Program terminates immediately. But when you use DOS shell in Turbo C, double g, i.e., 'gg' Dr. Gopika S , KJC 5
Difference between scanf () and gets() The main difference between these two functions is that scanf () stops reading characters when it encounters a space, but gets() reads space as character too. If you enter name as ‘Jai Hind’ using scanf () it will only read and store Jai and will leave the part after space. But gets() function will read it completely. Dr. Gopika S , KJC 6
Formatted Input Scanf (<control string>,arg1,arg2,arg3…) Control string specifies the field format in which the data is to be entered arg1, arg2 etc specifies the locations or addresses where the data is to be stored Control string contains field specifications which directs the interpretation of input Control String may include Field or format specs consisting of conversion char (%), a data type specifier, and optional width field Blanks, tabs and newlines % wd for integers, %f for float, %lf for double, % ws or % wc for char or strings Scanf returns number of items read Dr. Gopika S , KJC 7
Dr. Gopika S , KJC 8
Formatted Output printf (“control” string, arg1,arg2…) Control string may contain following Chars to be printed on the screen as it is Format specs for each item to be displayed Escape sequences chars such as \n, \t etc The args should match the specs in number order and type Form of format spec is % w.p type-specs Dr. Gopika S , KJC 9
Format specification for numbers Dr. Gopika S , KJC 10