• Aucun résultat trouvé

Related Topics:

Dans le document Language Reference (Page 127-146)

struct complex value;

double d;

value.x = 3.0;

value.y = 4.0;

d = cabs(value);

printf("Absolute value is %f\n",d);

Related Topics:

abs, labs, hypot, labs

calloc

Purpose:

Reserves storage space for an array and sets the initial value of all elements to

o.

Format:

/* Required for function declarations */

#include <stdlib.h>

void *calloc(n, size)

size_t n; /* Number of elements */

/* Length, in bytes, of each element */

size_t size;

Comments:

The canoe function reserves storage space for an array of

n

ele-ments, each of length size bytes. The canoe function then gives each element an initial value of O.

The canoe function returns a pointer to the reserved space. The storage space to which the return value points is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type, use a type cast on the return value. The return value is NULL if there is not enough storage available.

calloc

Example:

The following example reserves enough space in storage for 40 long integers and gives each integer an initial value of zero.

#include <stdio.h>

#include <stdlib.h>

long *1 all oc;

rnai n () {

lalloc = (long *)calloc(40,sizeof(long));

If (lalloc !=NULL)

printf("Allocation OK \n");

else printf ("ca11oc failed \n");

}

Related Topics:

free, malloc, realloc

ceil

Purpose:

Returns a double value representing the smallest integer that is greater than or equal to x.

Format:

#include <math.h>

double ceil(x)

double x; /* Floating-point value */

Comments:

The ceil function returns a double value representing the smallest integer that is greater than or equal to x.

There is no error return value.

Example:

The following example sets the y to the smallest integer greater than 1.05 and, then, the smallest integer greater than -1.05. The results are 2. and -1., respectively.

#include <math.h>

double y, z;

y = ceil(1.05); /* y = 2.0 */

z = ceil (-1.05); /* Z = -1.0 */

Related Topics:

floor, fmod

cgets

Purpose:

Reads and stores a string of characters directly from the keyboard.

Format:

/* Required for function declarations */

#include <conio.h>

char *cgets(str)

/* Storage location for data */

char *str;

Comments:

The cgets function reads a string of characters directly from the key-board and stores the string and its length in the location to which str points. The str variable must be a pointer to a character array. The first element of the array, str[O], must contain the maximum length, in characters, of the string to be read. The array must have enough ele-ments to hold the string, a final null character (\0), and two additional bytes.

The cgets function continues to read characters until it meets a car-riage returnlline feed combination or reads the specified number of characters. It stores the stri ng starti ng at str[2]. If cgets reads a CR-LF combination, it replaces this combination with a null character (\0) before storing the string. The cgets function then stores the actual length of the string in the second array element, str[1].

The cgets function returns a pointer to the start of the string, which is at str[2]. In case of error in OS/2 mode, cgets returns NULL.

cgets

Example:

This example creates a buffer and initializes the first byte to the size of the buffer-2. Next, the program accepts an input string using cgets and displays the size and text of that string.

#include <conio.h>

char buf[S2];

char *result;

mai nO

{

buf[O] = SO; /* max. number */

printf("Input line of text, follo");

printf("wed by carriage return:\n");

result = cgets(buf);

printf("\nLine length = %d\n",buf[l]);

printf("Text = %s\n",result);

Related Topics:

getch, getche

chdir

Purpose:

Changes the directory.

Format:

/* Required for function declarations */

#include <direct.h>

int chdir(pathname)

/* Path name of new working directory */

char *pathname;

Comments:

The chdir function causes the current working directory to change to the directory specified by pathname. The pathname must refer to an existing directory.

The chdir function returns a value of 0 if the working directory suc-cessfully changes. A return value of -1 shows an error; in this case, chdir sets errno to ENOENT, showing that chdir cannot find the speci-fied pathname. No error occurs if pathname specifies the current working directory.

Example:

The following example changes the current working directory to the root d i recto ry.

#include <direct.h>

chdir("\\");

chdir

Related Topics:

mkdir, rmdir, system

Note: This function can change the current working directory only on the current default drive. It cannot change the current working direc-tory on a different drive. For example, if A:\BIN is the current working directory, the following does not change it:

chdir' ("c:elllp");

Under DOS you can achieve the desired result. In this case, you must first call system to change the current default drive to C: before you can change the current working directory on that drive.

chmod

Purpose:

Changes the permission setting of a file.

Format:

#include <sys\types.h>

#include <sys\stat.h>

/* Required for function declarations */

#include <io.h>

int chmod(pathname, pmode) /* Path name of the existing file * / char *pathname;

/* Permission setting for the file */

int pmode;

Comments:

The chmod function changes the permission setting .of the file speci-fied by pathname. The permission setting controls access to the file for reading or writing. The pmode constant expression contains one or both of the manifest constants SJWRITE and SJREAD, defined in sys\siat.h. The chmod functions ignores any other values for pmode.

When you give both Gonstants, the chmod function joins them with the bitwise operator OR(I). The following list gives the meaning for the val ues of the pmode argument.

Reading and writing both permitted.

If you do not give permission to write to the file, the chmod function makes the file read-only. Under DOS, all files are readable; it is not possible to give write-only permission. Thus, the modes S_IWRITE and S_IREAD I S_IWRITE set the same permission.

chmod

The chmod function returns the value 0 if it successfully changes the permission setting. A return value of -1 shows an error; in this case, the chmod function sets errno to ENOENT, showing either that it cannot find the specified file or in the OS/2 mode the filename was incorrect.

Example:

This program takes file names passed as arguments and sets each to read-only.

#include <sys\types.h>

#include <sys\stat.h>

#include <io.h>

main(argc,argv) int argc;

char **argv;

int result;

register char **p;

if (argc<2) return;

for (p=++argv; argc>l; argc--, p++) /* Make a file read-only */

result = chmod(*p, S IREAD);

if (result == -1) -perror("file not found");

}

Related Topics:

access, creat, Istat, open, stat

chsize

Purpose:

Lengthens or cuts off the file associated with the handle.

Format:

/* Required for function declarations */

#include <io.h>

int chsize(handle, size)

/* A handle referring to open file */

int handle;

/* The new length of the file, in bytes */

long size;

Comments:

The chsize function lengthens or cuts off the file associated with the handle to the length specified by size. You must open the file in a mode that permits writing. The chsize function adds null characters (\0) when it lengthens the file. When chsize cuts off the file, it erases all data from the end of the shortened file to the end of the original file.

The chsize function returns the value 0 if it successfully changes the file size. A return value of -1 shows an error, and chsize sets errno to one of the following values.

Value Meaning

EACCESS The specified file is locked against access

EBADF The file handle is not valid, or the file is not open for writing.

ENOSPC There is no space left on device.

chsize

Example:

This program opens a file named dat and writes data to it. Then it uses chsize to extend the size of dat.

#include <io.h>

#include <fcntl .h>

#include <sys\types.h>

#include <sys\stat.h>

#include <stdio.h>

#define MAXSIZE 32768L i nt fh, result;

char buffer[BUFSIZ] = "Initialize the buffer to this string.\n";

mai n () { i nt i;

unsigned int nbytes = BUFSIZ;

fh = open(ldat",O_RDWRlo_CREAT, S_IREADls_IWRITE);

for (i=O; i<50; i++)

result = write(fh, buffer, nbytes);

result = -1;

/* Make sure the file is no longer */

/* than 32K bytes before closing it */

if (lseek(fh,OL,SEEK END) > MAXSIZE) result = chsize(fh,MAXSIZE);

if (result == 0)

printf ("Size successfully changed");

else

printf ("Problem in changing the size");

Related Topics:

close, creat, open

clear87

Purpose:

Gets and clears the floating-point status word.

Format:

#include <float.h>

unsigned int _clear87( )

Comments:

The _clear87 function gets and clears the floating-point status word.

The floating-point status word is a combination of the numeric coprocessor status word and other conditions that the numeric excep-tion handler detects, such as floating-point stack overflow and under-flow.

The bits in the value returned show the floating-point status. For a complete definition of the bits returned by _clear87, see the float.h include file.

Example:

The following example shows how you can lose significance by assigning a double to a float. It takes a number close to zero as a double and assigns it to a float. The result is a loss of significance and the creation of a denormal number. The _clear87 function gets the floating-point status word, and the printf function prints it as immediate data.

#include <stdio.h>

#include <float.h>

double a = le-40,b;

float x,y;

mai nO {

unsigned int statword;

statword = _clear87();

printf("cleared floating-point status word"

" = %.4x\n", statword);

/* Assignment of the double to the float y is inexact; the underflow bit is also set. */

y=a;

statword = _clear87();

printf("floating-point status = %.4x"

"after underflow\n" , statword);

/* Reassigning the denormal y to the double b causes the denormal bit to be set. */

b = y;

statword = _clear87();

printf("floating-point status = %.4x"

"for denormal\n",statword);

Output:

cleared floating-point status word = 0000 floating-point status = 0030 after underflow floating-point status= 0002 for denormal

Related Topics:

_ control87, _ status87

clear87

clearerr

Purpose:

Resets the error and end-o;-file indicators.

Format:

#include <stdio.h>

void clearerr (stream)

FILE *stream;/* Pointer to file structure */

Comments:

The clearerr function resets the error indicator and end-of-file indi-cator for the specified stream to O. The system does not automatically clear error indicators. When clearerr sets the error indicator for a specified stream, operations on that stream continue to return an error until your program calls clearerr or rewind.

Example:

The following example sends data to a stream, and then checks to make sure that a write error has not occurred. The stream must be open for writing.

#include <stdio.h>

#include <stdlib.h>

FILE *stream;

int c;

mai n () {

stream = fopen("data", Ow");

if ((c=getc(stream)) == EOF) if (ferror(stream)) {

fprintf(stderr,"write error\n");

clearerr(stream);

}

clearerr

Related Topics:

eof, feof, ferror, perror

close

Purpose:

Closes the file associated with the handle.

Format:

/* Required for function declarations */

#inciude <io.h>

int close(handle)

/* Handle referring to open file */

int handle;

Comments:

The close function closes the file associated with the handle.

The close function returns 0 if it successfully closes the file. A return value of ~1 shows an error, and close sets errno to EBADF, showing a incorrect file handle argument.

Example:

The following example closes the file DATA after opening it as a read-only file with the file handle fh.

#inelude <io.h>

#inelude <fentl.h>

int fh;

fh = open("data" ,O_RDONLy);

elose(fh);

Related Topics:

chsize, creal, dup; dup2, open, unlink

Purpose:

Gets and sets the floating-point control word.

Format:

#include <float.h>

/* Get floating-point control word */

unsigned int _controI87(new,mask) /* New control word bit val ues * / unsigned int new;

/* Mask for new control word bits to set * / unsigned int mask;

Comments:

control87

The _control87 function gets and sets the floating-point control word.

The floating-point control word lets the program change the precision, rounding, and infinity modes in the floating-point math package. You can mask or unmask floating-point exceptions using the _control87 function.

If the value for the mask is equal to 0, _control87 gets the floating-point control word. If the mask is non-zero, _control87 sets a new value for the control word in the following manner. For any bit in the mask equal to 1, the corresponding bit in new updates the control word. This is equivalent to the expression

fpcntrl = ((fpcntrl & - mask)

I

(new & mask))

where 'pentrl is the floating-point control word.

The bits in the returned value show the floating-point control state.

For a complete definition of the bits returned by _controI87, see the discussion of the float.h include file.

control87

Example:

This example prints the control word in hexadecimal, then illustrates different representations of 0.01, depending on the precision.

#include <stdio.h>

#include <float.h>

double a = .1;

rnai n () {

printf("control = %.4x\n", /* Get control word */

contro187(CW_DEFAULT,0»;

printfC'a*a = .01 = %.15e\n",a*a);

_contro187(PC_24,MCW_PC);

/* Set precision to 24 bits */

printf("a*a =.01 (rounded to 24 bits) =%.15e\n",a*a);

/* Restore to initial default */

contro187(CW_DEFAULT,Oxffff);

printf("a*a = .01 = %.15e\n",a*a);

Related Topics:

_ clear87,_ status87

Dans le document Language Reference (Page 127-146)