C Standard Library
C Standard Library
C Standard Library
A computer treats data as a series of bytes called a stream. It can read data from a hard
disk or from a floppy or the keyboard. Each of these sources of data represents a different
stream. A stream in C language is a variable of the type FILE. You will learn about this
type of variable in detail in the second part of this course (Computer Science B).
§ The standard input stream for reading is stdin. It is normally associated with the
keyboard.
§ The standard output stream for writing is stdout, which is usually the screen.
C language provides many functions to manipulate data reading and writing. The header
file stdio.h contains the declarations for these functions.
The header file stdio.h must be included in any C program that uses Input /
Output.
We have already used the printf() function in the previous tutorials to write text,
characters and numbers onto the screen. An argument of the printf() function consists of
a formatting string that may contain format specifiers, and expressions to be formatted.
Each format specifier must have a corresponding expression. The following format
specifiers can be used in the printf() function:
The format specifiers %d, %f, and %c were introduced in the previous section. The use
of some others will be discussed later. To print long or double value, the letter l is to be
added to the format specifier: %ld, or %lf. You can experiment with format specifiers to
find out the way they output an expression.
The format specifiers allow you to print the output in a particular manner by writing some
additional formatting characters between the percent sign % and the letter.
The minimum field width can be set by writing an integer number. For example
int n1 = 5;
int n2 = 5432;
printf("%d\n", n1);
printf("%d\n", n2);
printf("%4d\n", n1);
printf("%04d\n", n1);
printf("%2d\n", n2);
Output:
5
5432
5
0005
5432
• The first two lines are printed without any minimum field width specifiers.
• The minimum width of 4 is set in the third line for the number n1 (%4d), which
results in 3 white spaces being added in front of the number.
• The specifier %04d in the next line replaces white spaces with zeros.
• Note that the format %2d for n2 in the last line sets the minimum width which is
smaller than the width of the output. In this case the minimum width is ignored
and the number is printed in full.
Aligning output
The output can also be aligned to the left or right. By default the output has right
alignment, as in the example above. To specify left alignment include a minus sign in front
of the minimum field width,
like in (%-4d)
Specifying precision
For a floating point number you can set the precision or number of decimal places to be
printed. The precision specifier consists of a dot and the following integer number. For
example, printf("%.2f", 35.5678); will print 35.56. If you add a number and a sign
before the dot, you will specify the minimum field width and alignment together with the
precision. For example, the following two lines
printf("%-5.2f\n", 32.5678);
printf("%-5.2f\n", 3.333);
printf("%-5.1f\n", 2.0);
will output
32.56
3.33
2.0
Two other functions can be used to output characters onto the screen.
• The function int putc(int c, FILE *stream); takes two arguments, which are an
integer value for a character and a stream. To print the character onto the screen
the stream must be stdout.
• The function int putchar(int c); needs only one argument to represent a
character, because it always prints into the standard output.
Program 4.1.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char c1 = 'H';
char c2 = 'e';
char c3 = 'l', c4;
char c5 = 'o';
c4 = c3;
putc(c1, stdout);
putc(c2, stdout);
putc(c3, stdout);
putc(c4, stdout);
putc(c5, stdout);
putchar(c1);
putchar('\n');
putchar(c2);
putchar('\n');
putchar(c3);
putchar('\n');
putchar(c4);
putchar('\n');
putchar(c5);
putchar('\n');
system(“PAUSE”);
return 0;
}
Output:
Hello
To read a user input from the keyboard (standard input) the following functions can be
used.
• The function int getc(FILE *stream) reads the input from the input stream. In
the case of the reading from the keyboard, stream is stdin. The function returns
an integer value corresponding to the character typed.
• The function int getchar() reads only from the standard input and returns an
integer value corresponding to the character typed. Both functions read one
character at a time.
• The function int scanf() can read an arbitrary number of characters from the
standard input file stdin and store them at the given addresses. The address of a
variable indicates the location of this variable in the computer memory. The
address-of-operator & returns the address of a variable.
The scanf() function has two arguments: control_string and list of addresses. For
example
int n;
float x;
The control_string includes formats similar to the format specifiers in the printf()
function, which convert the input characters into the specified data types.
The scanf() function reads an input stream until the null or new -line character is
entered. The input values can be separated by the [TAB] or [WHITE SPACE]
characters. If the scanf() function concludes successfully it returns the number of
items read from the stdin. If an error occurs, the scanf() function returns a
constant EOF (end-of-file). This constant is defined in the stdio.h header file and
usually equals -1.
Good programming practice is to check the output of the sca nf() function
before using the values that have been read.
Program 4.2
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, count;
float fl_num;
char c, last;
Output:
2 3.5 *
You entered the following items: integer 2, float 3.5 and character *
The number of items read is 3
The last character read from the input stream has int value 10
The last character in the input stream in the program above is nl or new-line character,
which is entered by pressing the [ENTER] key. Its integer value is 10.
NOTE that there are variables' addresses in the argument list of the scanf()
function. Writing, for instance, x instead of &x will result in a run-time
error.
The header file <math.h> contains prototypes of the mathematical functions in the
standard C library. All mathematical functions return double value and take variables of
the type double as arguments. Some of the useful mathematical functions are listed here.
The example of using sqrt() function is given below.
The Program 4.3 reads coordinates of two points on a plane from the keyboard and
calculates the distance between them using the following expression:
Program 4.3
int main(void)
{
double x1, y1, x2, y2; /* coordinates of 2 points */
double distance;
double diff_x, diff_y; /*temporary values used in calculations */
The int rand(void) function generates and returns an integer in the range [0,
RAND_MAX] each time it is called. Repeated calls generate a randomly distributed
sequence. The rand() function and the constant RAND_MAX = 32767 (which is the
maximum value of the 16-bit integer) are defined in the header file stdlib.h. Although the
sequence generated by the function rand() appears random, it will repeat itself each time
you run the program. To generate an actual random sequence use the following
statement:
srand(time(NULL));
The time(NULL) function requires the header file time.h. It returns the current calendar
time as a number of seconds that have elapsed since 1 January 1970. srand() function is
used to pass to the rand() function different seeds (initial values) each time it is called.
The seed is given by the return value of the time(NULL).
To generate a random number in the range other than [0, RAND_MAX], the remainder
operator can be used. For example, the following call will return a random number in the
range [0, 99]:
rand()%100;
To include negative numbers you can subtract half of the range from the result for each
number generated:
rand()%100 - 50;
You can also generate floating-point numbers, as in the following code fragment.
float x, y;
int random_number1, random_number2;
int n1, n2
srand(time(NULL));
Summary: