Lecture 5
Lecture 5
Introduction
2. Binary Representation
Lecture 5 3.
4.
HardwareandSoftware
HighLevel Languages
Standard Input and Output
5. Standard input and output
14. Files
keyboard or a file and be printed to the
14. DataStructures screen or a file
16. Casestudy: lottery number generator
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 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);
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 Ö
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.
}
#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;
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 }
getchar getchar.c
#include <stdio.h>
main()
{
char key;
• 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
• The solution is to “flush the buffer” before printf("Press another key: ");
fflush(stdin); /* flush the keyboard buffer */