• Aucun résultat trouvé

cos - cosh

Dans le document Language Reference (Page 146-161)

Purpose:

The cos function returns the cosine. The cosh function returns the hyperbolic cosine.

Format:

#include <math.h>

/* Calculate the cosine of x */

double cos(x)

/* Calculate the hyperbolic cosine of x */

double cosh(x)

double x; /* Angle in radians */

Comments:

The cos function returns the cosine of x. If x is large, a partial loss of significance in the result might occur. In such cases, cos produces a

PLOSS error, but prints no message. If x is so large that a total loss of significance results, cos prints a TLOSS error message to STDERR and returns 0 In both cases, the cos function sets errno to ERANGE.

The cosh function returns the hyperbolic cosine of x. If the result is too large, cosh returns the value HUGE_VAL and sets errno to ERANGE.

You can changes the way that cosh handles errors by using the malherr routine.

For more information about PLOSS, TLOSS, ERANGE, and HUGE_VAL, see

"Math Errors" in Appendix A, "Error Messages."

cos - cosh

Example:

This program samples the level of an oscillator at ten equally-spaced points, one millisecond apart. The phase angle begins at zero, at time zero.

#include <math.h>

#define TWOPI 6.283185307 /* frequency in hertz */

double frequency=120.0;

double amplitude=1.0;

mai n () {

double level, time;

int n;

printf("time (s) level\n");

for (n=O; n<18; n++) {

time=n * 0.001;

1 evel =

amplitude*cos(TWOPI*frequency*time);

printf(" %5.3f % 8.6f\n", time, 1 evel);

}

Related Topics:

acos, asin, atan, atan2, matherr, sin, sinh, tan, tanh

Purpose:

Formats and prints characters directly to the screen.

Format:

/* Required for function declarations */

#include <conio.h>

i nt cpri ntf(format-string[,argument ... ]) /* Format control string */

char *format-string;

Comments:

cprintf

The cprinH function formats and prints a series of characters and values directly to the screen, using the putch function to put each character out to the screen. The cprlnH function converts each argu-ment (if any) and puts it out according to the corresponding format specit'ication in the format-string. The format-string has the same form and function as the format-string argument for the prinH func-tion. See the prinH reference page for a description of the format-string and arguments.

The cprinH function returns the number of characters printed.

Example:

The following example prints:

i=-16, j=Ox1d, k=511 Hinclude <conio.h>

i nt i = -16, j = 29;

unsigned int k = 511;

rnai nO {

cprintf("i=%d, j=%Hx, k=%u\n" , i, j, k);

}

cprintf

Related Topics:

'prinH, prinH, sprinH

Note: Unlike the 'prinH, prinH, and sprinH functions, cprinH does not translate line feed characters into output of carriage returnlline feed combinations.

cputs

Purpose:

Writes a string ending with a null character directly to the screen.

Format:

/* Required for function declarations */

#include <conio.h>

int cputs(str)

char *str; /* Pointer to output string * /

Comments:

The cputs function writes directly to the screen the string to which *str points. The string str must end with a null character (\0). The cputs function does not automatically add a carriage returnlline feed combi-nation to the string after writing.

If the action is successful, cputs returns O. In case of error in OS/2, cputs returns EOF.

Example:

The following statement puts a prompt out to the screen.

#include <conio.h>

char *buffer = "Insert data disk in drive a: \r\n";

cputs(buffer);

Related Topics:

putch

creat

Purpose:

Creates or opens and cuts off a file.

Format:

#include <sys\types.h>

#include <sys\stat.h>

/* Required for function declarations */

#include <io.h>

int creat(pathname, pmode)

char *pathname; /* Pathname of new file */

int pmode; /* Permission setting */

Comments:

The creat tunction either creates a new file or opens and cuts off an existing file. If the file specified by pathname does not exist, creat creates a new file is with the given permission setting and open for writing. If the file already exists and its permission setting allows writing, creat cuts off the file to length 0, destroying the previous con-tents, and opens it for writing.

The permission setting, pmode, applies to newly created files only.

The new file receives the specified permission setting after you close it for the first time. The pmode integer expression contains one or both 91 the manifest constants S_IWRITE and SJREAD, defined in sys\slat.h. When you give both constants, creal joins them with the bitwise operator oR(I).

creat

The following gives the values of the pmode argument and their meaning.

If you do not give permission to write to the file, the file is a read-only file. Under DOS it is not possible to give write-only permission. Thus, the modes SJWRITE and SJREAD ISJWRITE have the same results.

Under DOS, files opened using creat are always open in DOS mode (see sopen in this chapter). The creat function applies the current file permission mask to pmode before setting the permissions (see umask in this chapter). The creat function returns a handle for the created file if the call is successful. A return value of -1 shows an error, and creat sets errno to one of the following values:

Value Meaning

EACCCES The pathname specifies an eXisting read-only file or speci-fies a directory instead of a file.

EMFILE No more file handles are available. There are too many open files.

ENOENT The pathname was not found, or (in OS/2 mode) the filename was incorrect.

Example:

This example creates for reading or writing a file with the file name

DATA. It prints an error message if the creat operation fails.

#include <sys\types.h>

perror("couldn't create data file");

else printf("file \"data\" created\n"); }

creat

Related Topics:

chmod, chsize, close, dup, dup2, open, sopen, umask

Nole: IBM provides the creal function primarily for compatibility with previous libraries. A call to open with the O_CREAT and O_TRUNC

values specified in the of/ag argument has the same results as creal and is preferable for new code.

Purpose:

Reads data directly from the keyboard to locations given by arguments.

Format:

/* Required for function declarations */

#include <conio.h>

i nt cscanf(format-string[,argument. .. ])

char *format-string; /* Format control stri ng * /

Comments:

cscanf

The cscanf function reads data di rectly from the keyboard to the locations given by the arguments, if any. The cscanf function uses the getche function to read characters. Each argument must be a pointer to a variable with a type that corresponds to a type specifier in the format-string. The format-string controls the interpretation of the input fields and has the same form and function as the format-string argument for the scanf function. See the scanf reference page for a description of the format-string.

The cscanf function returns the number of fields that were success-fully converted and assigned. The return value does not include fields that were read but not assigned.

The return value is EOF for an attempt to read at end-of-file. A return value of 0 means that no fields were assigned.

cscanf

Example:

The following example stores string input from the keyboard. The result is the number of correctly matched input fields. If cscanf can match no input fields, the result is zero.

#include <conio.h>

i nt result;

char buffer[20];

cprintf("Please enter file name:");

result = cscanf("%19s", buffer);

Related Topics:

fscanf, scanf, sscanf

Note: Although cscanf normally echoes the input character, it will not do so if the last action was an ungetch.

ctime

Purpose:

Converts time stored as long value to a character string.

Format:

/* Required only for function declarations */

#include <time.h>

char *ctime(time)

const time_t *time; /* Pointer to stored time */

Comments:

The clime function usually obtains the time value from a call to lime, which returns the number of seconds elapsed since 00:00:00

Greenwich Mean Time, January 1,1970.

The string result produced by clime contains exactly 26 characters and has the form of the following example:

Sat Jul 06 02:03:55 1985\n\0

The clime function uses a 24-hour clock format. All fields have a con-stant width. The newline character (\n) and the null character (\0) occupy the last two positions of the string.

Under DOS, clime does not understand dates prior to 1980. If time represents a date before January 1,1980, clime returns NULL.

The clime function returns a pointer to the character string result.

There is no error return value.

clime

Example:

This example gets the number of seconds elapsed since January 1, 197000:00:00 GMT and assigns it to Itime. It then uses the clime func-tion to convert the number of seconds to the current time. It prints a message giving the current date and time.

#include <time.h>

#include <stdio.h>

ma in () {

time(&ltime);

printf("The time is %s\n", ctime(&ltime));

Related Topics:

asclime, ftime, gmlime, locallime, lime

Note: The asctime and clime functions use a single, statically-allocated buffer for holding the return string. Each call to one of these functions destroys the result of the previous call.

cwait

Purpose:

The cwait function delays the completion of a parent process until a particular child process is complete.

Format:

#include <process.h>

int cwait (stat_loc,process_id,action_code) int *stat_loc;

int process_id;

int action_code;

Comments:

The cwait function delays a parent process until the child process specified by process_id ends. You can use this function only under OS/2.

The process_id specifies the child process for which the parent process waits. This value is the process_id value returned by the spawn function or by the call to the OS/2 DOSEXECPGM

function that started the child process. If the specified child process ends before cwait is called, cwait returns immediately. If the

process_id is 0, the parent process waits until all of its child proc-esses end.

cwait

If not NULL, the

stat_/oc

argument points to a location that holds infor-mation about the return status and the return code of the child process. The return status shows whether the child process ended

norm~lIy, using a call to the OS/2 DOSEXIT function. The low-order and high-order bytes of the return status are as follows:

Byte L()w-order High-order

Contents

o

The low-order byte of the resulting code that the child process passed to DOSEXIT. DOSEXIT is called if the child process called exit or _exit, returned from main, or reached the end of main. The low-order byte of the result code is either the low-order byte of the argument to _exit or exit, the low~order byte of the return value from main, or if control from the child process fell through at the end of main, an unpredictable value.

If the child process stopped for any other reason, the low-order and high-order bytes of the return status are as follows:

Byte Low-order

Contents

Return code from OS/2 DOSCWAIT function.

Code Meaning

1 Hard-error abnormal end 2 Trap operation

3 SIGTERM signal not intercepted High-order 0

cwait

The action-code specifies when the parent process starts running again as shown in the following list:

Action Code Meaning

WAIT_CHILD The parent process waits until the specified child process stops.

WAIT_GRANDCHILD

The parent process waits until the specified child process and all of the child processes of that child process stop.

WAIT_CHILD and WAIT_GRANDCHILD are defined in process.h.

If cwait returns after an unexpected end of the child process, it returns -1 to the parent process and sets errno to EINTR.

If cwait returns after a normal end of the child process, it returns the process identifier of the child process to the parent process.

Otherwise, cwait returns immediately with a value of -1. In this case, errno indicates the error, as shown in the following list:

Value Meaning

EINVAL Incorrect action code

ECHILD No child process exists, or incorrect process identifier.

Related Topics:

exit, _exit, spawnl, spawnle, spawnlp, spawnlpe, spawnv, spawnve, spawnvp, spawnvpe, walt

Dans le document Language Reference (Page 146-161)