Learn to develop and use open-source software, including WordPress...

Welcome to a new generation of Drupal. It’s a digital experience...

Chapter 7 - Input and Output

Input and output are not part of the C language itself, so we have not emphasized them in our presentation thus far. Nonetheless, programs interact with their environment in much more complicated ways than those we have shown before. In this chapter we will describe the standard library, a set of functions that provide input and output, string handling, storage management, mathematical routines, and a variety of other services for C programs. We will concentrate on input and output

The ANSI standard defines these library functions precisely, so that they can exist in compatible form on any system where C exists. Programs that confine their system interactions to facilities provided by the standard library can be moved from one system to another without change.

The properties of library functions are specified in more than a dozen headers; we have already seen several of these, including <stdio.h>, <string.h>, and <ctype.h>. We will not present the entire library here, since we are more interested in writing C programs that use it. The library is described in detail in Appendix B.

7.1 Standard Input and Output

As we said in Chapter 1, the library implements a simple model of text input and output. A text stream consists of a sequence of lines; each line ends with a newline character. If the system doesn't operate that way, the library does whatever necessary to make it appear as if it does. For instance, the library might convert carriage return and linefeed to newline on input and back again on output.

The simplest input mechanism is to read one character at a time from the standard input, normally the keyboard, with getchar:

   int getchar(void)

getcharreturns the next input character each time it is called, or EOFwhen it encounters end of file. The symbolic constant EOFis defined in <stdio.h>. The value is typically -1, bus tests should be written in terms of EOFso as to be independent of the specific value.

In many environments, a file may be substituted for the keyboard by using the < convention for input redirection: if a program proguses getchar, then the command line

   prog <infile

causes progto read characters from infileinstead. The switching of the input is done in such a way that progitself is oblivious to the change; in particular, the string ``<infile'' is not included in the command-line arguments in argv. Input switching is also invisible if the input comes from another program via a pipe mechanism: on some systems, the command line

   otherprog | prog

runs the two programs otherprogand prog, and pipes the standard output of otherproginto the standard input for prog. The function

   int putchar(int)

is used for output: putchar(c)puts the character con the standard output, which is by default the screen. putcharreturns the character written, or EOFis an error occurs. Again, output can usually be directed to a file with >filename: if proguses putchar,

   prog >outfile

will write the standard output to outfileinstead. If pipes are supported,

   prog | anotherprog

puts the standard output of proginto the standard input of anotherprog.

Output produced by printfalso finds its way to the standard output. Calls to putcharand printfmay be interleaved - output happens in the order in which the calls are made.

Each source file that refers to an input/output library function must contain the line

   #include <stdio.h>

before the first reference. When the name is bracketed by < and > a search is made for the header in a standard set of places (for example, on UNIX systems, typically in the directory /usr/include).

Many programs read only one input stream and write only one output stream; for such programs, input and output with getchar, putchar, and printfmay be entirely adequate, and is certainly enough to get started. This is particularly true if redirection is used to connect the output of one program to the input of the next. For example, consider the program lower, which converts its input to lower case:

   #include <stdio.h>    #include <ctype.h>

   main() /* lower: convert input to lower case*/    {        int c

       while ((c = getchar()) != EOF)            putchar(tolower(c));        return 0;    }

The function toloweris defined in <ctype.h>; it converts an upper case letter to lower case, and returns other characters untouched. As we mentioned earlier, ``functions'' like getcharand putcharin <stdio.h>and tolowerin <ctype.h>are often macros, thus avoiding the overhead of a function call per character. We will show how this is done in Section 8.5. Regardless of how the

<ctype.h>functions are implemented on a given machine, programs that use them are shielded from knowledge of the character set.

Exercise 7-1.Write a program that converts upper case to lower or lower case to upper, depending on the name it is invoked with, as found in argv[0].

7.2 Formatted Output - printf

The output function printftranslates internal values to characters. We have used printfinformally in previous chapters. The description here covers most typical uses but is not complete; for the full story, see Appendix B.

   int printf(char *format, arg1, arg2, ...);

printfconverts, formats, and prints its arguments on the standard output under control of the format. It returns the number of characters printed.

The format string contains two types of objects: ordinary characters, which are copied to the output stream, and conversion specifications, each of which causes conversion and printing of the next successive argument to printf. Each conversion specification begins with a % and ends with a conversion character. Between the % and the conversion character there may be, in order:

  • A minus sign, which specifies left adjustment of the converted argument.
  • A number that specifies the minimum field width. The converted argument will be printed in a field at least this wide. If necessary it will be padded on the left (or right, if left adjustment is called for) to make up the field width.
  • A period, which separates the field width from the precision.
  • A number, the precision, that specifies the maximum number of characters to be printed from a string, or the number of digits after the decimal point of a floating-point value, or the minimum number of digits for an integer.
  • An hif the integer is to be printed as a short, or l(letter ell) if as a long.

Conversion characters are shown in Table 7.1. If the character after the % is not a conversion specification, the behavior is undefined.

Table 7.1Basic Printf Conversions

Character                                                 Argument type; Printed As

d,i      int; decimal number

o                int; unsigned octal number (without a leading zero)

int; unsigned hexadecimal number (without a leading 0xor 0X), using abcdefor

x,X

ABCDEFfor 10, ...,15.

u     int; unsigned decimal number c  int; single character

char *; print characters from the string until a '\0'or the number of characters given

s by the precision.

f    double; [-]m.dddddd, where the number of d's is given by the precision (default 6).

double; [-]m.dddddde+/-xxor [-]m.ddddddE+/-xx, where the number of d's is

e,E given by the precision (default 6).

double; use %eor %Eif the exponent is less than -4 or greater than or equal to the

g,G

precision; otherwise use %f. Trailing zeros and a trailing decimal point are not printed.

p                void *; pointer (implementation-dependent representation).

%                no argument is converted; print a %

A width or precision may be specified as *, in which case the value is computed by converting the next argument (which must be an int). For example, to print at most maxcharacters from a string s,

   printf("%.*s", max, s);

Most of the format conversions have been illustrated in earlier chapters. One exception is the precision as it relates to strings. The following table shows the effect of a variety of specifications in printing ``hello, world'' (12 characters). We have put colons around each field so you can see it extent.

   :%s:          :hello, world:

   :%10s:        :hello, world:

   :%.10s:       :hello, wor:

   :%-10s:       :hello, world:

   :%.15s:       :hello, world:

   :%-15s:       :hello, world   :

   :%15.10s:     :     hello, wor:

   :%-15.10s:    :hello, wor     :

A warning: printfuses its first argument to decide how many arguments follow and what their type is. It will get confused, and you will get wrong answers, if there are not enough arguments of if they are the wrong type. You should also be aware of the difference between these two calls:

   printf(s);         /* FAILS if s contains % */    printf("%s", s);   /* SAFE */

The function sprintfdoes the same conversions as printfdoes, but stores the output in a string:

   int sprintf(char *string, char *format, arg1, arg2, ...);

sprintfformats the arguments in arg1, arg2, etc., according to formatas before, but places the result in stringinstead of the standard output; stringmust be big enough to receive the result.

Exercise 7-2.Write a program that will print arbitrary input in a sensible way. As a minimum, it should print non-graphic characters in octal or hexadecimal according to local custom, and break long text lines.

7.3 Variable-length Argument Lists

This section contains an implementation of a minimal version of printf, to show how to write a function that processes a variable-length argument list in a portable way. Since we are mainly interested in the argument processing, minprintfwill process the format string and arguments but will call the real printfto do the format conversions. The proper declaration for printfis

   int printf(char *fmt, ...)

where the declaration ...means that the number and types of these arguments may vary. The declaration ...can only appear at the end of an argument list. Our minprintfis declared as

   void minprintf(char *fmt, ...)

since we will not return the character count that printfdoes.

The tricky bit is how minprintfwalks along the argument list when the list doesn't even have a name. The standard header <stdarg.h>contains a set of macro definitions that define how to step through an argument list. The implementation of this header will vary from machine to machine, but the interface it presents is uniform.

The type va_listis used to declare a variable that will refer to each argument in turn; in minprintf, this variable is called ap, for ``argument pointer.'' The macro va_startinitializes apto point to the first unnamed argument. It must be called once before apis used. There must be at least one named argument; the final named argument is used by va_startto get started.

Each call of va_argreturns one argument and steps apto the next; va_arguses a type name to determine what type to return and how big a step to take. Finally, va_enddoes whatever cleanup is necessary. It must be called before the program returns.

These properties form the basis of our simplified printf:

   #include <stdarg.h>

   /* minprintf: minimal printf with variable argument list */    void minprintf(char *fmt, ...)    {

       va_list ap; /* points to each unnamed arg in turn */        char *p, *sval;        int ival;        double dval;

       va_start(ap, fmt); /* make ap point to 1st unnamed arg */        for (p = fmt; *p; p++) {            if (*p != '%') {                putchar(*p);                continue;            }

           switch (*++p) {            case 'd':

               ival = va_arg(ap, int);

               printf("%d", ival);                break;            case 'f':

               dval = va_arg(ap, double);                printf("%f", dval);                break;            case 's':

               for (sval = va_arg(ap, char *); *sval; sval++)                    putchar(*sval);                break;            default:                putchar(*p);                break;            }        }        va_end(ap); /* clean up when done */    }

Exercise 7-3.Revise minprintfto handle more of the other facilities of printf.

7.4 Formatted Input - Scanf

The function scanfis the input analog of printf, providing many of the same conversion facilities in the opposite direction.

   int scanf(char *format, ...)

scanfreads characters from the standard input, interprets them according to the specification in format, and stores the results through the remaining arguments. The format argument is described below; the other arguments, each of which must be a pointer, indicate where the corresponding converted input should be stored. As with printf, this section is a summary of the most useful features, not an exhaustive list.

scanfstops when it exhausts its format string, or when some input fails to match the control specification. It returns as its value the number of successfully matched and assigned input items. This can be used to decide how many items were found. On the end of file, EOFis returned; note that this is different from 0, which means that the next input character does not match the first specification in the format string. The next call to scanfresumes searching immediately after the last character already converted.

There is also a function sscanfthat reads from a string instead of the standard input:

   int sscanf(char *string, char *format, arg1, arg2, ...)

It scans the stringaccording to the format in formatand stores the resulting values through arg1, arg2, etc. These arguments must be pointers.

The format string usually contains conversion specifications, which are used to control conversion of input. The format string may contain:

  • Blanks or tabs, which are not ignored.
  • Ordinary characters (not %), which are expected to match the next non-white space character of the input stream.
  • Conversion specifications, consisting of the character %, an optional assignment suppression character *, an optional number specifying a maximum field width, an optional h, lor Lindicating the width of the target, and a conversion character.

A conversion specification directs the conversion of the next input field. Normally the result is places in the variable pointed to by the corresponding argument. If assignment suppression is indicated by the * character, however, the input field is skipped; no assignment is made. An input field is defined as a string of non-white space characters; it extends either to the next white space character or until the field width, is specified, is exhausted. This implies that scanfwill read across boundaries to find its input, since newlines are white space. (White space characters are blank, tab, newline, carriage return, vertical tab, and formfeed.)

The conversion character indicates the interpretation of the input field. The corresponding argument must be a pointer, as required by the call-by-value semantics of C. Conversion characters are shown in Table 7.2.

character string (not quoted); char *, pointing to an array of characters long enough for

s the string and a terminating '\0'that will be added.

floating-point number with optional sign, optional decimal point and optional exponent;

e,f,g float *

%        literal %; no assignment is made.

The conversion characters d, i, o, u, and xmay be preceded by hto indicate that a pointer to shortrather than intappears in the argument list, or by l(letter ell) to indicate that a pointer to longappears in the argument list.

As a first example, the rudimentary calculator of Chapter 4can be written with scanfto do the input conversion:

   #include <stdio.h>

   main()  /* rudimentary calculator */    {        double sum, v;

       sum = 0;        while (scanf("%lf", &v) == 1)            printf("\t%.2f\n", sum += v);        return 0;    }

Suppose we want to read input lines that contain dates of the form

   25 Dec 1988

The scanfstatement is

   int day, year;    char monthname[20];    scanf("%d %s %d", &day, monthname, &year);

No &is used with monthname, since an array name is a pointer.

Literal characters can appear in the scanfformat string; they must match the same characters in the input. So we could read dates of the form mm/dd/yywith the scanfstatement:

   int day, month, year;    scanf("%d/%d/%d", &month, &day, &year);

scanfignores blanks and tabs in its format string. Furthermore, it skips over white space (blanks, tabs, newlines, etc.) as it looks for input values. To read input whose format is not fixed, it is often best to read a line at a time, then pick it apart with scanf. For example, suppose we want to read lines that might contain a date in either of the forms above. Then we could write

   while (getline(line, sizeof(line)) > 0) {

       if (sscanf(line, "%d %s %d", &day, monthname, &year) == 3)            printf("valid: %s\n", line); /* 25 Dec 1988 form */        else if (sscanf(line, "%d/%d/%d", &month, &day, &year) == 3)            printf("valid: %s\n", line); /* mm/dd/yy form */        else            printf("invalid: %s\n", line); /* invalid form */    }

Calls to scanfcan be mixed with calls to other input functions. The next call to any input function will begin by reading the first character not read by scanf.

A final warning: the arguments to scanfand sscanfmust be pointers. By far the most common error is writing

   scanf("%d", n);

instead of

   scanf("%d", &n);

This error is not generally detected at compile time.

Exercise 7-4.Write a private version of scanfanalogous to minprintffrom the previous section.

Exercise 5-5.Rewrite the postfix calculator of Chapter 4to use scanfand/or sscanfto do the input and number conversion.

7.5 File Access

The examples so far have all read the standard input and written the standard output, which are automatically defined for a program by the local operating system.

The next step is to write a program that accesses a file that is not already connected to the program. One program that illustrates the need for such operations is cat, which concatenates a set of named files into the standard output. catis used for printing files on the screen, and as a general-purpose input collector for programs that do not have the capability of accessing files by name. For example, the command

   cat x.c y.c

prints the contents of the files x.cand y.c(and nothing else) on the standard output.

The question is how to arrange for the named files to be read - that is, how to connect the external names that a user thinks of to the statements that read the data.

The rules are simple. Before it can be read or written, a file has to be opened by the library function fopen. fopentakes an external name like x.cor y.c, does some housekeeping and negotiation with the operating system (details of which needn't concern us), and returns a pointer to be used in subsequent reads or writes of the file.

This pointer, called the file pointer, points to a structure that contains information about the file, such as the location of a buffer, the current character position in the buffer, whether the file is being read or written, and whether errors or end of file have occurred. Users don't need to know the details, because the definitions obtained from <stdio.h>include a structure declaration called FILE. The only declaration needed for a file pointer is exemplified by

   FILE *fp;

   FILE *fopen(char *name, char *mode);

This says that fpis a pointer to a FILE, and fopenreturns a pointer to a FILE. Notice that FILEis a type name, like int, not a structure tag; it is defined with a typedef. (Details of how fopencan be implemented on the UNIX system are given in Section 8.5.) The call to fopenin a program is

   fp = fopen(name, mode);

The first argument of fopenis a character string containing the name of the file. The second argument is the mode, also a character string, which indicates how one intends to use the file. Allowable modes include read ("r"), write ("w"), and append ("a"). Some systems distinguish between text and binary files; for the latter, a "b"must be appended to the mode string.

If a file that does not exist is opened for writing or appending, it is created if possible. Opening an existing file for writing causes the old contents to be discarded, while opening for appending preserves them. Trying to read a file that does not exist is an error, and there may be other causes of error as well, like trying to read a file when you don't have permission. If there is any error, fopenwill return NULL.

(The error can be identified more precisely; see the discussion of error-handling functions at the end of Section 1 in Appendix B.)

The next thing needed is a way to read or write the file once it is open. getcreturns the next character from a file; it needs the file pointer to tell it which file.

   int getc(FILE *fp)

getcreturns the next character from the stream referred to by fp; it returns EOFfor end of file or error.

putcis an output function:

   int putc(int c, FILE *fp)

putcwrites the character cto the file fpand returns the character written, or EOF if an error occurs. Like getcharand putchar, getcand putcmay be macros instead of functions.

When a C program is started, the operating system environment is responsible for opening three files and providing pointers for them. These files are the standard input, the standard output, and the standard error; the corresponding file pointers are called stdin, stdout, and stderr, and are declared in

<stdio.h>. Normally stdinis connected to the keyboard and stdoutand stderrare connected to the screen, but stdinand stdoutmay be redirected to files or pipes as described in Section 7.1. getcharand putcharcan be defined in terms of getc, putc, stdin, and stdoutas follows:

   #define getchar()    getc(stdin)

   #define putchar(c)   putc((c), stdout)

For formatted input or output of files, the functions fscanfand fprintfmay be used. These are identical to scanfand printf, except that the first argument is a file pointer that specifies the file to be read or written; the format string is the second argument.

   int fscanf(FILE *fp, char *format, ...)    int fprintf(FILE *fp, char *format, ...)

With these preliminaries out of the way, we are now in a position to write the program catto concatenate files. The design is one that has been found convenient for many programs. If there are command-line arguments, they are interpreted as filenames, and processed in order. If there are no arguments, the standard input is processed.

   #include <stdio.h>

   /* cat:  concatenate files, version 1 */    main(int argc, char *argv[])    {        FILE *fp;

       void filecopy(FILE *, FILE *)

       if (argc == 1) /* no args; copy standard input */            filecopy(stdin, stdout);        else

          while(--argc > 0)

              if ((fp = fopen(*++argv, "r")) == NULL) {                   printf("cat: can't open %s\n, *argv);                   return 1;               } else {                  filecopy(fp, stdout);                  fclose(fp);               }           return 0;    }

    /* filecopy:  copy file ifp to file ofp */     void filecopy(FILE *ifp, FILE *ofp)     {         int c;

        while ((c = getc(ifp)) != EOF)             putc(c, ofp);     }

The file pointers stdinand stdoutare objects of type FILE *. They are constants, however, not variables, so it is not possible to assign to them. The function

   int fclose(FILE *fp)

is the inverse of fopen, it breaks the connection between the file pointer and the external name that was established by fopen, freeing the file pointer for another file. Since most operating systems have some limit on the number of files that a program may have open simultaneously, it's a good idea to free the file pointers when they are no longer needed, as we did in cat. There is also another reason for fcloseon an output file - it flushes the buffer in which putcis collecting output. fcloseis called automatically for each open file when a program terminates normally. (You can close stdinand stdoutif they are not needed. They can also be reassigned by the library function freopen.)

7.6 Error Handling - Stderr and Exit

The treatment of errors in catis not ideal. The trouble is that if one of the files can't be accessed for some reason, the diagnostic is printed at the end of the concatenated output. That might be acceptable if the output is going to a screen, but not if it's going into a file or into another program via a pipeline.

To handle this situation better, a second output stream, called stderr, is assigned to a program in the same way that stdinand stdoutare. Output written on stderrnormally appears on the screen even if the standard output is redirected.

Let us revise catto write its error messages on the standard error.

   #include <stdio.h>

   /* cat:  concatenate files, version 2 */    main(int argc, char *argv[])    {        FILE *fp;        void filecopy(FILE *, FILE *);        char *prog = argv[0];  /* program name for errors */

       if (argc == 1 ) /* no args; copy standard input */            filecopy(stdin, stdout);        else

           while (--argc > 0)

               if ((fp = fopen(*++argv, "r")) == NULL) {                    fprintf(stderr, "%s: can't open %s\n",                            prog, *argv);                    exit(1);                } else {                    filecopy(fp, stdout);                    fclose(fp);                }

       if (ferror(stdout)) {

           fprintf(stderr, "%s: error writing stdout\n", prog);            exit(2);        }        exit(0);

   }

The program signals errors in two ways. First, the diagnostic output produced by fprintfgoes to stderr, so it finds its way to the screen instead of disappearing down a pipeline or into an output file. We included the program name, from argv[0], in the message, so if this program is used with others, the source of an error is identified.

Second, the program uses the standard library function exit, which terminates program execution when it is called. The argument of exitis available to whatever process called this one, so the success or failure of the program can be tested by another program that uses this one as a sub-process.

Conventionally, a return value of 0 signals that all is well; non-zero values usually signal abnormal situations. exitcalls fclosefor each open output file, to flush out any buffered output.

Within main, returnexpr is equivalent to exit(expr). exithas the advantage that it can be called from other functions, and that calls to it can be found with a pattern-searching program like those in Chapter 5.

The function ferrorreturns non-zero if an error occurred on the stream fp.

   int ferror(FILE *fp)

Although output errors are rare, they do occur (for example, if a disk fills up), so a production program should check this as well.

The function feof(FILE *)is analogous to ferror; it returns non-zero if end of file has occurred on the specified file.

   int feof(FILE *fp)

We have generally not worried about exit status in our small illustrative programs, but any serious program should take care to return sensible, useful status values.

7.7 Line Input and Output

The standard library provides an input and output routine fgetsthat is similar to the getlinefunction that we have used in earlier chapters:

   char *fgets(char *line, int maxline, FILE *fp)

fgetsreads the next input line (including the newline) from file fpinto the character array line; at most maxline-1characters will be read. The resulting line is terminated with '\0'. Normally fgetsreturns line; on end of file or error it returns NULL. (Our getlinereturns the line length, which is a more useful value; zero means end of file.)

For output, the function fputswrites a string (which need not contain a newline) to a file:

   int fputs(char *line, FILE *fp)

It returns EOFif an error occurs, and non-negative otherwise.

The library functions getsand putsare similar to fgetsand fputs, but operate on stdinand stdout. Confusingly, getsdeletes the terminating '\n', and putsadds it.

To show that there is nothing special about functions like fgetsand fputs, here they are, copied from the standard library on our system:

   /* fgets:  get at most n chars from iop */    char *fgets(char *s, int n, FILE *iop)    {        register int c;        register char *cs;

       cs = s;        while (--n > 0 && (c = getc(iop)) != EOF)            if ((*cs++ = c) == '\n')                break;        *cs = '\0';        return (c == EOF && cs == s) ? NULL : s;    }

   /* fputs:  put string s on file iop */    int fputs(char *s, FILE *iop)    {        int c;

       while (c = *s++)            putc(c, iop);        return ferror(iop) ? EOF : 0;    }

For no obvious reason, the standard specifies different return values for ferrorand fputs.

It is easy to implement our getlinefrom fgets:

   /* getline:  read a line, return length */    int getline(char *line, int max)    {

       if (fgets(line, max, stdin) == NULL)            return 0;        else

           return strlen(line);    }

Exercise 7-6.Write a program to compare two files, printing the first line where they differ.

Exercise 7-7.Modify the pattern finding program of Chapter 5to take its input from a set of named files or, if no files are named as arguments, from the standard input. Should the file name be printed when a matching line is found?

Exercise 7-8.Write a program to print a set of files, starting each new one on a new page, with a title and a running page count for each file.

7.8 Miscellaneous Functions

The standard library provides a wide variety of functions. This section is a brief synopsis of the most useful. More details and many other functions can be found in Appendix B.

7.8.1 String Operations

We have already mentioned the string functions strlen, strcpy, strcat, and strcmp, found in <string.h>. In the following, sand tare char *'s, and cand nare ints.

                          strcat(s,t)       concatenate tto end of s

strncat(s,t,n) concatenate ncharacters of tto end of s

strcmp(s,t)       return negative, zero, or positive for s < t, s == t, s > t strncmp(s,t,n) same as strcmpbut only in first ncharacters strcpy(s,t)copy tto s

strncpy(s,t,n) copy at most ncharacters of tto s strlen(s)  return length of s

strchr(s,c)       return pointer to first cin s, or NULLif not present strrchr(s,c)     return pointer to last cin s, or NULLif not present

7.8.2 Character Class Testing and Conversion

Several functions from <ctype.h>perform character tests and conversions. In the following, cis an intthat can be represented as an unsigned charor EOF. The function returns int.

isalpha(c) non-zero if cis alphabetic, 0 if not isupper(c) non-zero if cis upper case, 0 if not islower(c) non-zero if cis lower case, 0 if not isdigit(c) non-zero if cis digit, 0 if not isalnum(c) non-zero if isalpha(c)or isdigit(c), 0 if not isspace(c) non-zero if cis blank, tab, newline, return, formfeed, vertical tab toupper(c) return cconverted to upper case tolower(c) return cconverted to lower case

7.8.3 Ungetc

The standard library provides a rather restricted version of the function ungetchthat we wrote in Chapter 4; it is called ungetc.

   int ungetc(int c, FILE *fp)

pushes the character cback onto file fp, and returns either c, or EOFfor an error. Only one character of pushback is guaranteed per file. ungetcmay be used with any of the input functions like scanf, getc, or getchar.

7.8.4 Command Execution

The function system(char *s)executes the command contained in the character string s, then resumes execution of the current program. The contents of sdepend strongly on the local operating system. As a trivial example, on UNIX systems, the statement

   system("date");

causes the program dateto be run; it prints the date and time of day on the standard output. systemreturns a system-dependent integer status from the command executed. In the UNIX system, the status return is the value returned by exit.

7.8.5 Storage Management

The functions mallocand callocobtain blocks of memory dynamically.

   void *malloc(size_t n)

returns a pointer to nbytes of uninitialized storage, or NULLif the request cannot be satisfied.

   void *calloc(size_t n, size_t size)

returns a pointer to enough free space for an array of nobjects of the specified size, or NULLif the request cannot be satisfied. The storage is initialized to zero.

The pointer returned by mallocor callochas the proper alignment for the object in question, but it must be cast into the appropriate type, as in

   int *ip;    ip = (int *) calloc(n, sizeof(int));

free(p)frees the space pointed to by p, where pwas originally obtained by a call to mallocor calloc. There are no restrictions on the order in which space is freed, but it is a ghastly error to free something not obtained by calling mallocor calloc.

It is also an error to use something after it has been freed. A typical but incorrect piece of code is this loop that frees items from a list:

   for (p = head; p != NULL; p = p->next) /* WRONG */        free(p);

The right way is to save whatever is needed before freeing:

   for (p = head; p != NULL; p = q) {        q = p->next;        free(p);    }

Section 8.7shows the implementation of a storage allocator like malloc, in which allocated blocks may be freed in any order.

7.8.6 Mathematical Functions

There are more than twenty mathematical functions declared in <math.h>; here are some of the more frequently used. Each takes one or two doublearguments and returns a double.

sin(x)      sine of x, x in radians cos(x)      cosine of x, x in radians atan2(y,x) arctangent of y/x, in radians

exp(x)

exponential function ex

log(x)

natural (base e) logarithm of x (x>0)

log10(x)

common (base 10) logarithm of x (x>0)

pow(x,y)

xy

sqrt(x)

square root of x (x>0)

fabs(x)

absolute value of x

7.8.7 Random Number generation

The function rand()computes a sequence of pseudo-random integers in the range zero to RAND_MAX, which is defined in <stdlib.h>. One way to produce random floating-point numbers greater than or equal to zero but less than one is

   #define frand() ((double) rand() / (RAND_MAX+1.0))

(If your library already provides a function for floating-point random numbers, it is likely to have better statistical properties than this one.)

The function srand(unsigned)sets the seed for rand. The portable implementation of randand srandsuggested by the standard appears in Section 2.7.

Exercise 7-9.Functions like isuppercan be implemented to save space or to save time. Explore both possibilities.

Computer Courses

Get in touch with us

FIZIKA MIND

Best website developer in bareilly

Contact us

FIZIKA MIND.

Kipps Enclave Shastri Nagar Bareilly, IN

Cont. no. +91-9259436235
[email protected]

Join our mailing list

Signup for Our Free Newsletter
Education - This is a contributing Drupal Theme
Design by WeebPal.