1.
Introduction
2. Binary Representation
Lecture 5 3.
4.
HardwareandSoftware
HighLevel Languages
Standard Input and Output
5. Standard input and output
6. Operators, expression and statements • Many programs have the form:
7. MakingDecisions
8. Looping Input Data
9. Arrays Process It
10. Basics of pointers Output Data
11. Strings
12. Basics of functions
• Where data can be read from the
13. Moreabout functions
14. Files
keyboard or a file and be printed to the
14. DataStructures screen or a file
16. Casestudy: lottery number generator
Standard Input and Output Standard Input and Output
• Luckily some nice person has written a whole library
• In C, the standard input (stdin) is generally connected to
of C calls which allow use to easily perform stdio
the keyboard and the standard output (stdout) to the
i.e. <stdio.h>
screen.
• However, in UNIX we can redirect the either or both at
run time from the command line • In order to use these routines we need to include the
• If we have a simple program a.out which takes some text appropriate header file
#include <stdio.h>
from the keyboard and prints the results to the screen,
then
a.out > outfile.txt - sends output to file • We will study some of these more common routines,
a.out < infile.txt - reads input from file namely
a.out < infile.txt > outfile.txt - does both – OUTPUT: puts, printsf and putchar
prog1 | prog2 - sends output of prog1 to input of prog2
– INPUT: scanf, getchar
hello.c stdout
/* Hello world program */ • The first program we will look at uses puts to put a
string to standard out
#include <stdio.h> – a newline is automatically added to the end of the string
– see puts.c (available on web)
void main() /* Example: outputting strings using puts */
#include <stdio.h>
{ void main()
{
puts("To be or not to be: that is the question:");
printf("Hello world"); puts("Whether 'tis nobler in the mind to suffer");
puts("The slings and arrows of outrageous fortune,");
} puts("Or to take arms against a sea of troubles,");
puts("And by opposing end them? To die: to sleep;");
puts("No more; and by a sleep to say we end");
puts("The heart-ache and the thousand natural shocks");
puts("That flesh is heir to, 'tis a consummation");
puts("Devoutly to be wished. To die, to sleep;");
puts("To sleep: perchance to dream: ay, there's the rub.");
puts("For in that sleep of death what dreams may come");
puts("When we have shuffled off this mortal coil,");
puts("Must give us pause.");
/* Hamlet */
}
1
stdout printf1.c
/* Example: outputting numeric data using printf */ The value of j is 5
• Often we need to print data that the program has #include <stdio.h>
void main() The value of x is 123.456787
calculated and we need to control the formatting of {
int j = 5;
...or 1.234568E+002
...or 123.456787109375000000000000000
this data. float x = 123.4567890123456789;
double z = 123.4567890123456789; The value of z is 123.456789
char c = 'A'; ...or 123.456789012345680000000000000
– e.g. the value 30 could be printed as 30 or 30.00 or +3E+01 printf("The value of j is %i\n\n", j);
– this can all be done with printf which prints formatted printf("The value of x is %f\n", x);
The value of c is A or 65
The new value of c is B or 66
printf("...or %E\n", x);
output to standard out printf("...or %30.27f\n\n", x); 0.05 is equivalent to 5%
– note: printf does not automatically add a new line for us printf("The value of z is %f\n", z); Hello world!
printf("...or %30.27f\n\n", z);
• Basic printing printf("\nThe value of c is %c or %i\n", c, c);
c++;
– for an int j: printf(“%i”,j); printf("The new value of c is %c or %i\n\n", c, c);
printf("0.05 is equivalent to %i%%\n\n", j);
– for a float x: printf(“%f”,x);
printf("Hello ");
• The %-string is a format conversion string }
printf("world!\n");
/* Example: formatting float/double data using printf */
printf2.c #include <stdio.h>
main()
printf3.c
{
double a = 123.4; /* These could all be floats */
/* Example: formatting integer data using printf */ double b = -567.8;
#include <stdio.h> double c = 987654321;
double d = -0.00008765; a = 123.4
main() b = -567.8
{ puts("a = 123.4"); c = 987654321
int j = 1234; j = 1234 puts("b = -567.8"); d = -0.00008765
int k = -5678; k = -5678 puts("c = 987654321");
puts("d = -0.00008765"); |...| is used to show the field width.
puts("j = 1234");
|...| is used to show the field width.
puts("k = -5678"); puts("\n|...| is used to show the field width.\n");
Using %f, a = |123.400000|
puts("\n|...| is used to show the field width.\n"); Using %i, j = |1234| printf("Using %%f, a = |%f|\n", a); b = |-567.800000|
k = |-5678| printf(" b = |%f|\n", b); c = |987654321.000000|
printf("Using %%i, j = |%i|\n", j); printf(" c = |%f|\n", c); d = |-0.000088|
printf(" k = |%i|\n\n", k); Using %+i, j = |+1234| printf(" d = |%f|\n\n", d);
k = |-5678| Using %8.3f, a = | 123.400|
printf("Using %%+i, j = |%+i|\n", j); printf("Using %%8.3f, a = |%8.3f|\n", a); b = |-567.800|
printf(" k = |%+i|\n\n", k); Using % i, j = | 1234| printf(" b = |%8.3f|\n", b); c = |987654321.000|
k = |-5678| printf(" c = |%8.3f|\n", c);
printf("Using %% i, j = |% i|\n", j); d = | -0.000|
printf(" d = |%8.3f|\n\n", d);
printf(" k = |% i|\n\n", k);
Using %8i, j = | 1234| printf("Using %%E, a = |%E|\n", a); Using %E, a = |1.234000E+002|
printf("Using %%8i, j = |%8i|\n", j); k = | -5678| printf(" b = |%E|\n", b); b = |-5.678000E+002|
printf(" k = |%8i|\n\n", k); printf(" c = |%E|\n", c); c = |9.876543E+008|
Using %-8i, j = |1234 | printf(" d = |%E|\n\n", d); d = |-8.765000E-005|
printf("Using %%-8i, j = |%-8i|\n", j); k = |-5678 |
printf(" k = |%-8i|\n\n", k); printf("Using %%12.3E, a = |%12.3E|\n", a); Using %12.3E, a = | 1.234E+002|
Using %- 8i, j = | 1234 | printf(" b = |%12.3E|\n", b); b = | -5.678E+002|
printf("Using %%- 8i, j = |%- 8i|\n", j); printf(" c = |%12.3E|\n", c);
printf(" k = |%- 8i|\n\n", k);
k = |-5678 | c = | 9.877E+008|
printf(" d = |%12.3E|\n\n", d);
d = | -8.765E-005|
printf("Using %%08i, j = |%08i|\n", j); Using %08i, j = |00001234| printf("Using %%G, a = |%G|\n", a);
printf(" k = |%08i|\n\n", k); k = |-0005678| printf(" b = |%G|\n", b); Using %G, a = |123.4|
printf(" c = |%G|\n", c); b = |-567.8|
printf("Using %%8.6i, j = |%8.6i|\n", j); Using %8.6i, j = | 001234| printf(" d = |%G|\n\n", d); c = |9.87654E+008|
printf(" k = |%8.6i|\n\n", k); k = | -005678| d = |-8.765E-005|
} }
printf.bug puchar
/* BUG ZONE!!! The value of j is 0.000000
Example: outputting numeric data using printf */
The value of x is 0
• putchar : put a character (to stdout)
#include <stdio.h> The value of z is Ö
main() The value of c is 0.000000 or
– Remember a char can be treated as a character
{
int j = 5;
0.05 is equivalent to
or as a number so putchar(‘A’); and
float x = 123.4567890123456789;
double z = 123.4567890123456789;
char c = 'A';
putchar(65); are equivalent
printf("The value of j is %f\n\n", j); /* BUG */ /* Example: outputting characters using putchar */ Hello! *
printf("The value of x is %i\n", x); /* BUG */ #include <stdio.h>
printf("The value of z is %c\n", z); /* BUG */ main()
{
printf("\nThe value of c is %f or %s\n", c, c); /* 2 BUGS */ putchar('H');
putchar('e');
printf("0.05 is equivalent to 5%"); /* BUG */ putchar(108);
} putchar(108);
putchar('o');
putchar('!');
putchar('\t');
putchar('*');
putchar('\n');
}
2
Can you spot the bugs in this program ? stdin
/* BUG ZONE!!!
Example: standard output */
• scanf : scan formatted (from stdin)
#include (studio.h) /* 2 BUGS! */ • Basic use
Main() /* BUG! */
– for an int j : scanf(“%i”,&j);
{ – for a float x : scanf(“%f”,&x);
puts(Multiplication); /* BUG! */
• the & is very important and must not be
printf("9 times 7 = %c", 9 * 7); /* BUG! */
omitted (an easy mistake to make)!
putchar("\n"); /* BUG */
– &x means “the address of x”, well see why later
print("9 times 8 = %i", 9 * 8); /* BUG */
• Again the %-string is a format conversion
printf("/n"); /* BUG */ string.
}
/* Example: inputting numeric data using scanf */
#include <stdio.h>
#include <limits.h> /* defines INT_MIN, INT_MAX, LONG_MIN, LONG_MAX */
main()
{
int j;
long int k;
scanf.bug
float x;
scanf.c
double z;
printf("Enter an integer (between %i and %i): ", INT_MIN, INT_MAX);
scanf("%i", &j);
printf("You entered %i\n\n", j);
/* BUG ZONE!!!
printf("Enter a long integer (between %li and %li): ", LONG_MIN, LONG_MAX); Example: inputting numeric data using scanf */
scanf("%li", &k);
printf("You entered %li\n\n", k); #include <stdio.h>
#include <limits.h> /* defines INT_MIN, INT_MAX */
printf("Enter a floating point number: ");
scanf("%f", &x); main()
printf("You entered %20.10E\n\n", x);
{
int j;
printf("Enter a double precision floating point number: ");
double z;
scanf("%lf", &z);
printf("You entered %20.10E\n\n", z);
Enter an integer (between -2147483648 and 2147483647): 10 printf("Enter an integer (between %i and %i): ", INT_MIN, INT_MAX);
You entered 10
puts("\n\nTry again: enter invalid data and see what happens!"); scanf("%i", j); /* BUG */
} Enter a long integer (between -2147483648 and 2147483647): 10 printf("You entered %i\n\n", j);
You entered 10
Enter a floating point number: 1.1 printf("Enter a double precision floating point number: ");
You entered 1.1000000238E+000 scanf("%i", &z); /* BUG */
printf("You entered %20.10E\n\n", z);
Enter a double precision floating point number: 1.1
You entered 1.1000000000E+000 }
Try again: enter invalid data and see what happens!
/* Example: getting a single character from the keyboard, using getchar */
getchar getchar.c
#include <stdio.h>
main()
{
char key;
printf("Press a key (then ENTER): ");
• getchar : get a character (from stdin) key = getchar();
printf("You pressed %c\n", key);
Press a key (then ENTER): p
You pressed p
puts("-------------"); -------------
Press another key: You pressed
• The input is buffered - this means you have to printf("Press another key: ");
key = getchar();
-------------
Oops! What went wrong?
press ENTER after the character. printf("You pressed %c\n", key);
puts("-------------");
Let's try again...
• It can also cause problems because this puts("Oops! What went wrong?");
puts("Let's try again...\n\n");
Press a key (then ENTER): p
You pressed p
-------------
ENTER (=‘\n’) will be read next time getchar printf("Press a key (then ENTER): ");
fflush(stdin); /* flush the keyboard buffer */
Press another key: r
You pressed r
is called. key = getchar();
printf("You pressed %c\n", key);
puts("-------------");
-------------
Ah, that's more like it!
• The solution is to “flush the buffer” before printf("Press another key: ");
fflush(stdin); /* flush the keyboard buffer */
reading it using fflush(stdin); key = getchar();
printf("You pressed %c\n", key);
puts("-------------");
puts("Ah, that's more like it!");
/* getchar is both echoed and buffered */
}