Lecture 9-2 simple i/o with scanf( ) and printf( )
2
Objectives
At the end of this lecture, you will be able to get data input and output using
• scanf( ) for input • printf( ) for output
Reading & watching
• •
7.2 and 7.4 in English text book
C programming tutorial video clips
– 11-gettinginputwithscanf
–
https://www.youtube.com/watch?v=hSHFjPvqFjw&in dex=11&list=PL6gx4Cwl9DGAKIXv8Yr6nhGJ9Vlcjyy mq
4- print text on the screen
https://www.youtube.com/watch?v=oSpmApiUsHw&i ndex=4&list=PL6gx4Cwl9DGAKIXv8Yr6nhGJ9Vlcjyy mq
•
Youtube video clip
– inputandoutputwithscanfandprintf
3
4
Simple input and output
• It is necessary to know how to get data into computer and to print data on the sceen
• In this short lecture, we learn how to use – printf for output
– scanf for input
• They are defined/implemented in standard input/output library, so you need to include the header file stdio.h
5
•
Formatted output with printf( )
Print a text string
– put the string within double quotations printf ( “hello world\n” );
Print a decimal integer
– using conversion specifier %d
printf( “%d” ,10); // only print the decimal integer 10
printf ( “i = %d\n” ,10); // print the text and integer
– using conversion specifier %d with width
%6d print a decimal integer, at least 6 character width
•
6
•
Formatted output with printf( )
Print a floating point ( real number)
– using conversion specifier %f
printf( “%f” ,3.14159); // print 3.14159
printf ( “pi = %f\n” ,3.14159); // print the text and the floating point
– using conversion specifier with width
%6f print a floating point, at least 6 character wide
%.2fprint a floating point, only 2 digits after decimal point
%6.2fprint at least 6 digits wide and only 2 after the decimal point
7
Formatted output with printf( )
• Print a character
– using conversion specifier %c
printf( “%c” , ‘G’); // print G
– using conversion specifier with width
%2c print a character, 2 characters wide
• Print a string – Using %s
printf( “%s” , “Hej!”);
printf format specifiers
• Form: %[flags][width][.precision][length]
•
Type (read Table B-1 page 244, English textbook)
8
9
More on printf format specifiers
• Form: %[flags][width][.precision][length]
10
printf format specifiers
• Form: %[flags][width][.precision][length]
• flags ( +, – and 0, in the following examples)
• Try out the following examples to understand the effect of the flags:
printf(“%d,%+d,%+d\n”, 10,10,-10); printf(“%04d\n”, 10);
printf(“%7s\n”, “hello”); printf(“%-7s\n”, “hello”);
printf format specifiers
• Form: %[flags][width][.precision][length]
• type (modifier)
11
12
Formatted input with scanf( )
• scanf() reads characters from standard input, interpreting them according to format specification
• The format specification is the same as that for printf()
• When multiple items are to be read, each item is assumed to be separated by white space.
• It returns the number of items read or EOF. – EOF end of file; a special value (-1)
• Important: scanf() ignores white spaces.
• Important: Arguments have to be addresses of variables
13
Examples with input
• Read in an integer and a float point int a; // define an int variable
float f; // define a float point variable scanf(“%d %f”, &a, &f);
// read in an int and save it in a
// read in a float point and save it in f
• You have to use operator & ( address-of) before the variabletell the value read should be placed in the specified address
Formatted input with scanf( )
int scanf(char∗ format ,…) is the input analog of printf. (read Table B-3 page 246)
14
15
Examples printf.c and scanf.c
• Read, run and study the example programs
– printf.c – scanf.c