Input/Output in C: Streams, Formatted Output with printf, and Formatted Input with scanf, Slides of Computer Engineering and Programming

An overview of input/output (i/o) in c programming language. It explains that c does not have built-in statements for i/o and introduces the concept of streams, which are sequences of characters organized into lines. The standard input, output, and error streams and demonstrates how to use formatted output with printf and formatted input with scanf. It also covers the use of various format conversion specifiers and provides examples of using getchar, putchar, getc, and putc functions.

Typology: Slides

2012/2013

Uploaded on 05/06/2013

apsara
apsara 🇮🇳

4.5

(2)

86 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
I/O in C
Lecture 6
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Input/Output in C: Streams, Formatted Output with printf, and Formatted Input with scanf and more Slides Computer Engineering and Programming in PDF only on Docsity!

I/O in C

Lecture 6

Input/Output in C

  • C has no built-in statements for input or output.
  • A library of functions is supplied to perform these operations. The I/O library functions are listed the “header” file <stdio.h>.
  • You do not need to memorize them, just be familiar with them.

Types of Streams in C

  • Standard input stream is called "stdin" and is normally connected to the keyboard
  • Standard output stream is called "stdout" and is normally connected to the display screen.
  • Standard error stream is called "stderr" and is also normally connected to the screen.

Formatted Output with printf

printf ( ) ;

  • This function provides for formatted output to the screen. The syntax is: printf ( “format”, var1, var2, … ) ;
  • The “format” includes a listing of the data types of the variables to be output and, optionally, some text and control character(s).
  • Example: float a ; int b ; scanf ( “%f%d”, &a, &b ) ; printf ( “You entered %f and %d \n”, a, b ) ;

Input/Output in C

scanf ( ) ;

  • This function provides for formatted input from the keyboard. The syntax is: scanf ( “format” , &var1, &var2, …) ;
  • The “format” is a listing of the data types of the variables to be input and the & in front of each variable name tells the system WHERE to store the value that is input. It provides the address for the variable.
  • Example: float a; int b; scanf (“%f%d”, &a, &b);

Input/Output in C

getchar ( ) ;

  • This function provides for getting exactly one character from the keyboard.
  • Example:

char ch; ch = getchar ( ) ;

Input/Output in C

getc ( *file ) ;

  • This function is similar to getchar( ) except the input can be from the keyboard or a file.
  • Example:

char ch; ch = getc (stdin) ; /* input from keyboard / ch = getc (fileptr) ; / input from a file */

Input/Output in C

putc ( char, *file ) ;

  • This function is similar to putchar ( ) except the output can be to the screen or a file.
  • Example:

char ch; ch = getc (stdin) ; /* input from keyboard / putc (ch, stdout) ; / output to the Docsity.com