Microsoft PowerPoint – 5_C_Input_Output
O
SU
C
SE
2
42
1
J.E.Jones
Required Reading:
Pointers on C, Section 15.8, 15.8.1,15.10 through 15.10.4 inclusive
O
SU
C
SE
2
42
1
J. E. Jones
Chalk Talk
◦ Simplified hardware architecture
◦ In your mind’s eye…
PVC Pipe
Tennis Balls
O
SU
C
SE
2
42
1
J. E. Jones
O
SU
C
SE
2
42
1
J. E. Jones
Linux/Unix systems use the ASCII character encoding
format
Most IBM products use the EBCDIC character encoding
format
There is also a character encoding format called Unicode
For this class we will only work with ASCII
If you are interested in this type of thing, I suggest
https://en.wikipedia.org/wiki/Character_encoding
as a short tutorial/history lesson.
O
SU
C
SE
2
42
1
J. E. Jones
Reminder: Before a function in a C program is invoked, or
called, it must be declared. Otherwise, the compiler will
output an error message when you compile. Remember
also that a definition of a function includes a declaration.
When we want to use I/O functions in the C standard
library, we will include stdio.h in our source file, which
contains declarations of these library functions, so that the
compiler can do type-checking of our calls to these
functions.
We will only look at some of the most commonly used of
these functions here.
◦ They are all you need for the labs in this course (and all you
are allowed to use in your lab programs).
O
SU
C
SE
2
42
1
J. E. Jones
• There is no input or output defined in C itself; functions have been written and incorporated
into the standard library for input and output (stdio).
• Two basic types of I/O:
• Character based (no format specifiers) – character by character I/O
• int getchar() – input
• int putchar(c) – output
• Formatted – zero or more characters, with a requirement to specify the format of the
input or output (how the sequence of characters read or written is to be interpreted):
• int scanf(parameters go in here) – input – white space is important!!!
• int printf(parameters go in here) – output
• These functions use format strings to specify interpretation (% before string)
• Two functions of a very few in C that can have a differing number of parameters
each time they are called.
• Example:
#include
int main()
{ /* check1.c – shows calls to scanf() and printf() */
int x;
scanf(“%d\n”, &x); /* Notice address operator & */
printf(“x=%d\n”, x);
}
O
SU
C
SE
2
42
1
J. E. Jones
These functions can be used to input and output one character at a time.
Declarations (interfaces):
◦ int getchar(void);
◦ int putchar(int); /* the int value returned can be ignored */
Notice that both of these functions use the integer (4-byte) values which
correspond to a given 1-byte ASCII character. The integer must be cast
before being assigned to a char variable or must be printed with a %c
format code to be output as a char with printf().
An integer is returned, rather than a char, because certain text file control
values (EOF, or end of file, for example) do not have one-byte ASCII
encodings. Check out the ASCII char represented by 0b 1111 1111.
Now would be a good time to review the ASCII character chart on piazza
Take note of the values for ‘0’-’9’, ‘a’-’z’, ‘A’-’Z’ and their relationship.
O
SU
C
SE
2
42
1
J. E. Jones
/* assume #include
…
int code;
…
printf(“Please enter the appropriate single letter code, followed by enter: \n”);
code = getchar();
if (code != EOF) || ((char) code != ‘\n’)
printf(“The code entered was: %c \n”, code); /* outputs int as a char */
——————————————————————
/* another way to do it – again, assume #include
…
char code;
…
printf(“Please enter the appropriate single letter code, followed by enter: \n”);
code = (char) getchar(); /* cast int value read from input to char */
if (code != EOF) || (code != ‘\n’)
printf(“The code entered was: %c \n”, code);
What happens to EOF in the 2nd example???
O
SU
C
SE
2
42
1
J. E. Jones
• Both format I/O
• Both manipulate “standard I/O” location – the keyboard
for input, and the terminal display for output (although we
can use something called redirection on the command line
to change this – more below)
• printf – output
• Converts values to a character form, according to the format
string, and prints them to “standard out” (stdout)
• scanf – input
• Converts characters from “standard in” (stdin), according to the
format string, and followed by pointer arguments (i.e.,
addresses), indicating where the resulting values are to be stored
O
SU
C
SE
2
42
1
J. E. Jones
Input:
◦ scanf(“format specification string”, ¶m1, ¶m2, ¶m3, …);
◦ A format specification string is a string literal defining the format from which input
should be read
◦ scanf can have an indeterminate number of parameters. The 2nd through the nth
parameters are the addresses of variables into which values are placed (pass by reference)
◦ There is a % format substring within the first parameter that represents the format to be
used for each of the next n-1 parameters
Output:
◦ printf(“format specification string”, param1, param2, param3, …);
◦ A format specification string is a string literal defining the format in which output should
be formatted
◦ printf can have an indeterminate number of parameters. The 2nd through the nth
parameters are values to be printed (pass by value)
◦ There is a % format substring within the first parameter that represents the format to be
used for each of the next n-1 parameters
O
SU
C
SE
2
42
1
J. E. Jones
When programming in C, you use format strings — the percentage sign and a conversion
code, for the most part (although some other characters between % and the conversion
character are optional – see below) — as placeholders for variables you want to read from
input or display. The above table shows the format strings and what they display…
Conversion Character Displays Argument (Variable’s Contents) As
%c Single character
%d, %i Signed decimal integer (int)
%e Signed floating-point value in E notation
%f Signed floating-point value (float)
%g
Signed value in %e or %f format, whichever is
shorter
%o Unsigned octal (base 8) integer (int)
%s String of text (NULL terminated)
%u Unsigned decimal integer (int)
%x Unsigned hexadecimal (base 16) integer (int)
%% (percent character)
O
SU
C
SE
2
42
1
J. E. Jones
O
SU
C
SE
2
42
1
J. E. Jones
/* NOTE: The codes for formatted I/O from above are used */
printf(“%d\t%d\n”, fahr, celsius);
Causes the values of the two integers fahr and celsius to be printed, with a tab
(\t) between them, and followed by a newline(\n).
printf(“%3d\t%6d\n”, fahr, celsius);
To print the first number to three digits wide, and the second to six digits wide
NOTE: Each % construction in the first argument of printf is paired with the
corresponding second argument, third argument, etc.; they must match up
properly by number and type, or you will get wrong answers or a compile
time error.
printf(“\na=%f\nb=%f\nc=%f\nd=%f”,a,b,c,d);
What does this statement print?
O
SU
C
SE
2
42
1
J. E. Jones
printf(“\na=%f\nb=%f\nc=%f\nd=%f\n”,a,b,c,d);
◦ If a=6.2, b=5.455, c=3.1415, d=6.0, then output is:
a=6.200000
b=5.455000
c=3.141500
d=6.000000
printf(“\na=%4.2f\nb=%6.4f\nc=%5f\nd=%3f\n”,a,b,c,d);
◦ If a=6.2, b=5.455, c=3.1415, d=6.0, then output is:
a=6.20
b=5.4550
c=3.141500
d=6.000000
O
SU
C
SE
2
42
1
J. E. Jones
float x = 3.14159;
printf(“output:”);
printf(“%4.3f\n”, x);
printf(“%4f\n”, x);
printf(“%.3f\n”, x);
output:
3.142
3.141590
3.142
minimum field width is 4! Notice it rounds up.
minimum field width is 4, but no precision specified –
6 digits is default
Explicitly says 3 numerals after decimal point
O
SU
C
SE
2
42
1
J. E. Jones
int y = 25;
printf(“%d\n”, y);
printf(“%i\n”, y);
printf(“%4d\n”, y);
printf(“%1d\n”, y);
printf(“%05d\n”, y);
output:
25
25
25
25
00025
two spaces before two-digit value
minimum field width specified is too small to print the value of the data, so printf ignores it!
leading zeroes used to fill minimum field width
O
SU
C
SE
2
42
1
J. E. Jones
int day, month, year;
scanf(“%d/%d/%d”, &month, &day, &year);
Input:
01/29/13
month has the value 1
day has the value 29
year has the value 13
The / characters are not saved.
Input:
111/222/2018
month has the value 111
day has the value 222
year has the value 2018
The / characters are not saved.
int anInt;
scanf(“%i%%”, &anInt);
Input:
23%
anInt has the value 23
The % character is not
saved.
Input:
152%
anInt has the value 152
The % character is not
saved.
O
SU
C
SE
2
42
1
J. E. Jones
int int_1; long long_1;
scanf(“%d %ld”, &int_1, &long_1);
Input:
-23 200
int_1 has the value -23 /*stored as 4 bytes */
/*on stdlinux*/
long_1 has the value 200 /*stored as 8 bytes */
/*on stdlinux*/
double d;
scanf(“%lf”, &d);
Input:
3.14
d has the value 3.14 /*stored as 8 bytes */
/*on stdlinux*/
O
SU
C
SE
2
42
1
J. E. Jones
IMPORTANT: scanf ignores leading (but not following)
white space characters when it reads numeric values from
input:
int i;
printf(“Enter an integer:\n”);
scanf(“%d”, &i);
/*scanf will ignore any leading white space characters */
O
SU
C
SE
2
42
1
J. E. Jones
char string1[10]; /* IMPORTANT: string must hold 9 chars + null */
scanf(“%9s”, string1); /*array identifier is */
/*address of 1st element*/
Input:
VeryLongString
string1==“VeryLongS”
int int1;
scanf(“%*s %i”, &int1); /*read arbitrary length*/
/*string of non-numeric*/
/*chars before int1 */
Input:
Age: 29
int1 has the value 29
“Age: “ is not saved
NOTE: pressing the enter key causes characters that are being held in the input buffer
to be transferred to standard in (stdin)
O
SU
C
SE
2
42
1
J. E. Jones
int int1, int2;
scanf(“%2i”, &int1);
scanf(“%2i”, &int2);
Input:
2345
Then:
int1 has the value 23
int2 has the value 45
NOTE: pressing the enter key causes characters being held in the input
buffer to be transferred to standard in (stdin)
O
SU
C
SE
2
42
1
J. E. Jones
Letter Type of Matching Argument
Auto-skip;
Leading
White-Space
Example
Sample
Matching Input
%% % (a literal, matched but not
converted or assigned)
no int anInt;
scanf(“%i%%”, &anInt);
23%
d int yes int anInt; long l;
scanf(“%d %ld”, &anInt, &l);
-23 200
i int yes int anInt;
scanf(“%i”, &anInt);
0x23
o unsigned int yes unsigned int aUInt;
scanf(“%o”, &aUInt);
023
u unsigned int yes unsigned int aUInt;
scanf(“%u”, &aUInt);
23
x unsigned int yes unsigned int aUInt;
scanf(“%x”, &aUInt);
1A
a, e, f, g float or double yes float f; double d;
scanf(“%f %lf”, &f, &d);
1.2 3.4
c char no char ch;
scanf(” %c”, &ch);
Q
s array of char no char s[30];
scanf(“%29s”, s);
hello
n int no int x, cnt;
scanf(“X: %d%n”, &x, &cnt);
X: 123 (cnt==6)
[ array of char no char s1[64], s2[64];
scanf(” %[^\n]”, s1);
scanf(“%[^\t] %[^\t]”, s1, s2);
Hello World
field1
O
SU
C
SE
2
42
1
J. E. Jones
Letter Type of Matching Argument
Auto-skip;
Leading
White-Space
Example
Sample
Matching Input
%% % (a literal, matched but not
converted or assigned)
no int anInt;
scanf(“%i%%”, &anInt);
23%
d int yes int anInt; long l;
scanf(“%d %ld”, &anInt, &l);
-23 200
i int yes int anInt;
scanf(“%i”, &anInt);
0x23
o unsigned int yes unsigned int aUInt;
scanf(“%o”, &aUInt);
023
u unsigned int yes unsigned int aUInt;
scanf(“%u”, &aUInt);
23
x unsigned int yes unsigned int aUInt;
scanf(“%x”, &aUInt);
1A
a, e, f, g float or double yes float f; double d;
scanf(“%f %lf”, &f, &d);
1.2 3.4
c char no char ch;
scanf(” %c”, &ch);
Q
s array of char no char s[30];
scanf(“%29s”, s);
hello
n int no int x, cnt;
scanf(“X: %d%n”, &x, &cnt);
X: 123 (cnt==6)
[ array of char no char s1[64], s2[64];
scanf(” %[^\n]”, s1);
scanf(“%[^\t] %[^\t]”, s1, s2);
Hello World
field1
Note space between “ and %
O
SU
C
SE
2
42
1
J. E. Jones
What is the difference between %i and %d? The table
says they both print signed decimal integers.
◦ https://www.geeksforgeeks.org/difference-d-format-specifier-c-
language/
I really can’t explain it any better than this link does.
◦ printf: There is no difference between the %i and %d format
specifiers
◦ scanf: %d and %i behave differently
%d assume base 10 while %i auto detects the base.
For example, 012 would be 1210 with %d but 1010 (128)with %i.