This lecture covers input and output functions in C programming, explaining how to manage data input from the keyboard or files and output to screens or files. Standard functions such as getchar, putchar, scanf, printf, gets, and puts are detailed for their specific roles in reading and displaying data. The session concludes with an emphasis on applying these concepts in program development.
Lecture on computer programming focusing on Input/Output functions in C. Instructor is K.A.S.H. Kulathilake from Rajarata University.
Students will understand C standard functions for input/output and how to apply them in programming.
Input means feeding data into a program (from files/command line) and output involves displaying data (on screen/printer). C offers built-in functions for both.
Standard C functions exist for formatted/unformatted input/output operations essential for program interaction.
C treats devices as files with three standard files opened by default: stdin (keyboard), stdout (screen), and stderr (error messages).
Key functions include getchar, putchar, scanf, printf, gets, and puts; these facilitate data transfer between the program and devices.
Detailed explanation of getchar() for reading a single character and putchar() for displaying characters, with loop usage for multiple characters.
Example code demonstrating the usage of getchar() and putchar() functions for basic input/output.
Another example using getchar() to obtain user input with conditional output based on the response.
Demonstrates using putchar() in a loop to print an array of characters.
Explains scanf() format string usage for reading various data types including characters, integers, and strings.
Discusses printf() for formatted output, moving data from memory to output devices using various format specifiers.
Illustrates scanf() and printf() through code where user input is read and processed before output.
Introduces gets() for reading a line of input with spaces, and puts() for outputting strings with a newline.
Example code showing the usage of gets() to read input and puts() to print it.
Recap of lecture objectives: defining C standard I/O functions and application in writing programs.
Preview of the next lecture focusing on pointers in C programming.
COM1407 Computer Programming Lecture 10 Input/Output Functions K.A.S.H. Kulathilake B.Sc. (Hons) IT, MCS , M.Phil., SEDA(UK) Rajarata University of Sri Lanka Department of Physical Sciences 1
2.
Objectives • At theend of this lecture students should be able to; ▫ Define the C standard functions for managing input output. ▫ Apply taught concepts for writing programs. 2
3.
Input/ Output Methods •When we say Input, it means to feed some data into a program. • An input can be given in the form of a file or from the command line. • C programming provides a set of built-in functions to read the given input and feed it to the program as per requirement. • When we say Output, it means to display some data on screen, printer, or in any file. • C programming provides a set of built-in functions to output the data on the computer screen as well as to save it in text or binary files. 3
4.
Input/ Output Methods(Cont…) • In order to perform input output functions, there are some standard C functions. • Some of the input/ output functions are used to format the input/ output and the others for unformatted input/ output. 4
5.
Input/ Output Methods(Cont…) • The standard files ▫ C programming treats all the devices as files. ▫ So devices such as the display are addressed in the same way as files and the following three files are automatically opened when a program executes to provide access to the keyboard and screen. 5 Standard File File Pointer Device Purpose Standard input stdin Keyboard Console input from the user Standard output stdout Screen Message output to the user Standard error stderr Your screen System error message output to the user
6.
Input/ Output Methods(Cont…) • Some standard input output functions are; ▫ getchar ▫ putchar ▫ scanf ▫ printf ▫ gets ▫ puts • These functions permits the transfer of information between the computer and the standard input/output devices. 6
7.
The getchar() andputchar() functions • The int getchar(void) function reads the next available character from the screen and returns it as an integer. • This function reads only single character at a time. • You can use this method in the loop in case you want to read more than one character from the screen. • The int putchar(int c) function puts the passed character on the screen and returns the same character. • This function puts only single character at a time. • You can use this method in the loop in case you want to display more than one character on the screen. 7
8.
The getchar() andputchar() functions (Cont…) #include <stdio.h> int main( ) { int c; printf( "Enter a value :"); c = getchar( ); printf( "nYou entered: "); putchar( c ); return 0; } 8
9.
The getchar() andputchar() functions (Cont…) #include <stdio.h> int main( ) { int response; printf( "Do you wish to learn C language (y/n) : ?"); response = getchar(); if (response == 'y') printf("nRead the referencen"); else if (response == 'n') printf("nFollow language you liken"); else printf("nSorry wrong answern"); return 0; } 9
10.
The getchar() andputchar() functions (Cont…) #include <stdio.h> int main( ) { int i; char let[20] = {'C', ' ', 'P', 'R', 'O', 'G', 'R', 'A', 'M'}; printf ("Your name is : "); for ( i = 0; i<20; i++) putchar(let[i]); return 0; } 10
11.
The scanf andprintf Functions • scanf(format string, arg1, arg2, ..., argn) ▫ Reads the input from the standard input stream stdin and scans that input according to the format provided. ▫ Conversion characters %c single character %d decimal integer %e, %f, %g floating point value %h short integer %i decimal, hexadecimal or octal integer %o octal integer %s string of characters %u unsigned decimal integer %x hexadecimal integer ▫ Each variable name must be preceded by an ampersand (&). ▫ However, array names should not begin with an &. 11
12.
The scanf andprintf Functions (Cont…) • printf(format string, arg1, arg2,…., argn) ▫ function writes the output to the standard output stream stdout and produces the output according to the format provided. ▫ printf function moves data from the computers memory to the standard output device. ▫ The format can be a simple constant string, but you can specify %s, %d, %c, %f, etc., to print or read strings, integer, character or float respectively. ▫ There are many other formatting options available which can be used based on requirements. 12
13.
The scanf andprintf Functions (Cont…) #include <stdio.h> int main( ) { char str[100]; int i; printf( "Enter a value :"); scanf("%s %d", str, &i); i = i * 2; printf( "nYou entered: %s %d ", str, i); return 0; } 13
14.
The gets() andputs() Functions • The char *gets(char *s) function reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF (End of File). • Unlike the scanf fucntion, the gets function allows to include spaces and other characters. • The int puts(const char *s) function writes the string 's' and 'a' trailing newline to stdout. 14
15.
The gets() andputs() Functions (Cont…) #include <stdio.h> int main( ) { char str[100]; printf( "Enter a value :"); gets( str ); printf( "nYou entered: "); puts( str ); return 0; } 15
16.
Objective Re-cap • Nowyou should be able to: ▫ Define the C standard functions for managing input output. ▫ Apply taught concepts for writing programs. 16