> July 2011 ~ Online tutorial

realloc



 realloc<ALLOC.H>

   realloc reallocates main memory

 Declaration:
 
   void far *farrealloc(void far *oldblock, unsigned long nbytes);

 Remarks:

realloc adjusts the size of the allocated block to size, copying the
contents to a new location if necessary.


 Return Value:


   On success, both functions return the address of the reallocated block,
    which might be different than the address of the original block.
   On failure (if the block can't be reallocated, or if size == 0 for
    realloc), the functions return null.



#include <stdio.h>
#include <alloc.h>
#include <string.h>

int main(void)
{
   char *str;

   /* allocate memory for string */
   str = (char *) malloc(10);

   /* copy "Hello" into string */
   strcpy(str, "Hello");

   printf("String is %s\n  Address is %p\n", str, str);
   str = (char *) realloc(str, 20);
   printf("String is %s\n  New address is %p\n", str, str);

   /* free memory */
   free(str);

   return 0;
}

farrealloc



 farrealloc<ALLOC.H>
 
   farrealloc adjusts allocated block in far heap

 Declaration:


  void *realloc(void *block, size_t size);

 Remarks:


farrealloc adjusts the size of the allocated block to nbytes, copying the
contents to a new location, if necessary.

 Return Value:


   On success, both functions return the address of the reallocated block,
    which might be different than the address of the original block.
 
Program.


#include <stdio.h>
#include <alloc.h>

int main(void)
{
   char far *fptr;
   char far *newptr;

   fptr = (char far *) farmalloc(16);
   printf("First address: %Fp\n", fptr);

   /*  we use a second pointer, newptr, so that in the case of */
   /*  farrealloc() returning NULL, our original pointer is    */
   /*  not set to NULL.                                        */

   newptr = (char far *) farrealloc(fptr,64);
   printf("New address  : %Fp\n", newptr);
   if (newptr != NULL)
      farfree(newptr);
   return 0;
}



farcoreleft



 farcoreleft<ALLOC.H>


  farcoreleft returns a measure of unused memory in the far heap

 Declaration:


    All except tiny models:          unsigned long farcoreleft(void);

 Remarks:

farcoreleft returns a measure of the amount of unused memory in the far heap
beyond the highest allocated block.

A tiny model program can't use farcoreleft.

 Return Value:

 farcoreleft: returns the total amount of space left in the far heap,
    between the highest allocated block and the end of available memory.

coreleft



 coreleft<ALLOC.H>

   coreleft returns a measure of unused memory


 Declaration:


   Tiny, small, and medium models:  unsigned coreleft(void);
  Compact, large, and huge models: unsigned long coreleft(void);

 Remarks:


coreleft returns a measure of RAM memory not in use.

The value coreleft gives depends on whether the memory model is of the small
data group or the large data group.

 Return Value:


   coreleft:
      Small data models: returns the amount of unused memory between the
       top of the heap and the stack.
      Large data models: returns the amount of memory between the highest
       allocated block and the end of available memory.

farmalloc() in c



 farmalloc()
 Allocates from far heap

 Declaration:

 void far *farmalloc(unsigned long nbytes);

 Remarks:

farmalloc allocates a block of memory nbytes bytes long from the far heap.



 Return Value:

  On success, farmalloc returns a pointer
    to the newly allocated block
   On failure (not enough space exists for
    the new block), farmalloc returns null




 Example:

 #include <stdio.h>
 #include <alloc.h>
 #include <string.h>
 #include <dos.h>

 int main(void)
 {
    char far *fptr;
    char *str = "Hello";

    /* allocate memory for the far pointer */
    fptr = (char far *) farmalloc(10);

    /* copy "Hello" into allocated memory */
    /*
       Note: movedata is used because we might be in a small data model,
       in which case a normal string copy routine can not be used since it
       assumes the pointer size is near.
    */
    movedata(FP_SEG(str), FP_OFF(str),
             FP_SEG(fptr), FP_OFF(fptr),
             strlen(str) + 1);

    /* display string (note the F modifier)
*/
    printf("Far string is: %Fs\n", fptr);

    /* free the memory */
    farfree(fptr);

    return 0;
 }

clearerr



clearerr<STDIO.H>

 Resets error indication

 Declaration: 

 void clearerr(FILE *stream);

 Remarks:


clearerr resets the named stream's error and end-of-file indicators to 0.

Once the error indicator is set, stream operations continue to return error
status until a call is made to clearerr or rewind.

The end-of-file indicator is reset with each input operation.

 Return Value:
 None



 Example:
 #include <stdio.h>

 int main(void)
 {
    FILE *fp;
    char ch;

    /* open a file for writing */
    fp = fopen("DUMMY.FIL", "w");

    /* force an error condition by attempting to read */
    ch = fgetc(fp);
    printf("%c\n",ch);

    if (ferror(fp))
    {
       /* display an error message */
       printf("Error reading from DUMMY.FIL\n");

       /* reset the error and EOF indicators */
       clearerr(fp);
    }

    fclose(fp);
    return 0;
 }

fdopen



  fdopen <STDIO.H>


   fdopen associates a stream with a file handle

 Declaration:


   FILE *fdopen(int handle, char *type);

      #include <stdio.h>
      #include <share.h>
      FILE *_fsopen(const char *filename, const char *mode, int shflg);

 Remarks:


 fdopen associates a stream with a file handle obtained from creat, dup,
dup2, or open.


 Return Value:
   On success,
      fdopen, fopen, and _fsopen return a
       pointer to the newly opened stream
      freopen returns the argument stream
   On error, these functions return null
Program
#include <sys\stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>

int main(void)
{
   int handle;
   FILE *stream;

   /* open a file */
   handle = open("DUMMY.FIL", O_CREAT,
                  S_IREAD | S_IWRITE);

   /* now turn the handle into a stream */
   stream = fdopen(handle, "w");

   if (stream == NULL)
      printf("fdopen failed\n");
   else
   {
      fprintf(stream, "Hello world\n");
      fclose(stream);
   }
   return 0;
}

fflush



fflush                           <STDIO.H>

 Flushes a stream

 Declaration:

int fflush(FILE *stream);

 Remarks:


If the given stream has buffered output, fflush writes the output for stream
to the associated file.

The stream remains open after fflush has executed. fflush has no effect on
an unbuffered stream.

 Return Value


 On success, returns 0
On error, returns EOF



 Example:

 #include <string.h>
 #include <stdio.h>
 #include <conio.h>
 #include <io.h>

 void flush(FILE *stream);

 int main(void)
 {
    FILE *stream;
    char msg[] = "This is a test";

    /* create a file */
    stream = fopen("DUMMY.FIL", "w");

    /* write some data to the file */
    fwrite(msg, strlen(msg), 1, stream);

    clrscr();
    printf("Press any key to flush DUMMY.FIL:");
    getch();

    /* flush the data to DUMMY.FIL without closing it */
    flush(stream);

    printf("\nFile was flushed, Press any key to quit:");
    getch();
    return 0;
 }

 void flush(FILE *stream)
 {
      int duphandle;

      /* flush the stream's internal buffer */
      fflush(stream);

      /* make a duplicate file handle */
      duphandle = dup(fileno(stream));

      /* close the duplicate handle to flush the DOS buffer */
      close(duphandle);
 }

clearerr



clearerr                    <STDIO.H>

 Resets error indication

 Declaration:

void clearerr(FILE *stream);

 Remarks:


clearerr resets the named stream's error and end-of-file indicators to 0.

Once the error indicator is set, stream operations continue to return error
status until a call is made to clearerr or rewind.

The end-of-file indicator is reset with each input operation.

 Return Value:
 None


 Example:

 #include <stdio.h>

 int main(void)
 {
    FILE *fp;
    char ch;

    /* open a file for writing */
    fp = fopen("DUMMY.FIL", "w");

    /* force an error condition by attempting to read */
    ch = fgetc(fp);
    printf("%c\n",ch);

    if (ferror(fp))
    {
       /* display an error message */
       printf("Error reading from DUMMY.FIL\n");

       /* reset the error and EOF indicators */
       clearerr(fp);
    }

    fclose(fp);
    return 0;
 }

ferror



ferror  <STDIO.H>

 Macro that tests if an error has occurred on a stream

 Declaration:

int ferror(FILE *stream);


 Remarks:


ferror is a macro that tests the given stream for a read or write error.

If the stream's error indicator has been set, it remains set until clearerr
or rewind is called, or until the stream is closed.

 Return Value:


ferror returns non-zero if an error was detected on the named stream.


 Example:

 #include <stdio.h>

 int main(void)
 {
    FILE *stream;

    /* open a file for writing */
    stream = fopen("DUMMY.FIL", "w");

    /* force an error condition by attempting to read */
    (void) getc(stream);

    if (ferror(stream))  /* test for an error on the stream */
    {
       /* display an error message */
       printf("Error reading from DUMMY.FIL\n");

       /* reset the error and EOF indicators */
       clearerr(stream);
    }

    fclose(stream);
    return 0;
 }

rewind



rewind                          <STDIO.H>

 Repositions file pointer to stream's beginning

 Declaration:  


void rewind(FILE *stream);


 Remarks:


rewind(stream) is equivalent to

  fseek(stream, 0L, SEEK_SET)

except that rewind clears the end-of-file and error indicators, while fseek
only clears the end-of-file indicator.

After rewind, the next operation on an update file can be either input or
output.

 Return Value:
  None


 Example:

 #include <stdio.h>
 #include <dir.h>

 int main(void)
 {
    FILE *fp;
    char *fname = "TXXXXXX", *newname, first;

    newname = mktemp(fname);
    fp = fopen(newname,"w+");
    fprintf(fp,"abcdefghijklmnopqrstuvwxyz");
    rewind(fp);
    fscanf(fp,"%c",&first);
    printf("The first character is: %c\n",first);
    fclose(fp);
    remove(newname);

    return 0;
}

putchar


 putchar           <STDIO.H>

  putchar is a macro that outputs a character on stdout

 Declaration:

   int putchar(int c);

 Remarks:

putchar is a macro defined as putc(c, stdout) putchar puts the character
given by c on the output stream stdout.

 Return Value:

putchar returns the character given by c.
  On error (and on end-of-file for getchar), both macros return EOF.


Program

#include <stdio.h>

/* define some box-drawing characters */
#define LEFT_TOP  0xDA
#define RIGHT_TOP 0xBF
#define HORIZ     0xC4
#define VERT      0xB3
#define LEFT_BOT  0xC0
#define RIGHT_BOT 0xD9

int main(void)
{
   char i, j;

   /* draw the top of the box */
   putchar(LEFT_TOP);
   for (i=0; i<10; i++)
      putchar(HORIZ);
   putchar(RIGHT_TOP);
   putchar('\n');

   /* draw the middle */
   for (i=0; i<4; i++)
   {
      putchar(VERT);
      for (j=0; j<10; j++)
         putchar(' ');
      putchar(VERT);
      putchar('\n');
   }

   /* draw the bottom */
   putchar(LEFT_BOT);
   for (i=0; i<10; i++)
      putchar(HORIZ);
   putchar(RIGHT_BOT);
   putchar('\n');

   return 0;
}

getchar



getchar  <STDIO.H>

   getchar is a macro that gets a character from stdin
 
 Declaration:
   int getchar(void);
 
 Remarks:


getchar is a macro defined as getc(stdin) getchar returns the next character
on the input stream stdin.

 Return Value:
 On success,
     getchar returns the character read, after converting it to an int
       without sign extension.
   
 On error (and on end-of-file for getchar), both macros return EOF.

Program


#include <stdio.h>

int main(void)
{
   int c;

   /* Note that getchar reads from stdin and
      is line buffered; this means it will
      not return until you press ENTER. */

   while ((c = getchar()) != '\n')
      printf("%c", c);

   return 0;
}



feof



feof                     <STDIO.H>

 Macro that tests if end-of-file has been reached on a stream.


 Declaration:

 int feof(FILE *stream);

 Remarks:


feof is a macro that tests the given stream for an end-of-file indicator.

Once the indicator is set, read operations on the file return the indicator
until rewind is called, or the file is closed.

The end-of-file indicator is reset with each input operation.

 Return Value:


  Returns non-zero if an end-of-file
    indicator was detected on the last input
    operation on the named stream.
 Returns 0 if end-of-file has not been
    reached.




 Example:

 #include <stdio.h>

 int main(void)
 {
    FILE *stream;

    /* open a file for reading */
    stream = fopen("DUMMY.FIL", "r");

    /* read a character from the file */
    fgetc(stream);

    /* check for EOF */
    if (feof(stream))
       printf("We have reached end-of-file\n");

    /* close the file */
    fclose(stream);
    return 0;
 }

putw




 putw            <STDIO.H>

  putw outputs an integer on a stream

 Declaration:
 
 int putw(int w, FILE *stream);

 Remarks:

 putw outputs the integer w to the given stream. It does not expect (and
does not cause) special alignment in the file.

 Return Value:


   On success,
    putw returns the integer w.
  On error,
      putw returns EOF
   On end-of-file, getw returns EOF

Because EOF is a legitimate value for getw to return, use feof to detect
end-of-file or ferror to detect error.

Because EOF is a legitimate integer, use ferror to detect errors with putw.

Program

#include <stdio.h>
#include <stdlib.h>

#define FNAME "test.$$$"

int main(void)
{
   FILE *fp;
   int word;

   /* place the word in a file */
   fp = fopen(FNAME, "wb");
   if (fp == NULL)
   {
      printf("Error opening file %s\n", FNAME);
      exit(1);
   }

   word = 94;
   putw(word,fp);
   if (ferror(fp))
       printf("Error writing to file\n");
   else
       printf("Successful write\n");
   fclose(fp);

   /* reopen the file */
   fp = fopen(FNAME, "rb");
   if (fp == NULL)
   {
      printf("Error opening file %s\n", FNAME);
      exit(1);
   }

   /* extract the word */
   word = getw(fp);
   if (ferror(fp))
       printf("Error reading file\n");
   else
       printf("Successful read: word = %d\n", word);

   /* clean up */
   fclose(fp);
   unlink(FNAME);

   return 0;
}

getw




 getw                    <STDIO.H>

 getw gets an integer from stream



 Declaration:


   int getw(FILE *stream);



 Remarks:


 getw returns the next integer in the named input stream. It assumes no
special alignment in the file. getw should not be used when the stream is
opened in text mode.

 Return Value:
 On success,
      getw returns the next integer on the input stream.
  .
 On error,
      getw returns EOF
     On end-of-file, getw returns EOF

Because EOF is a legitimate value for getw to return, use feof to detect
end-of-file or ferror to detect error.

Because EOF is a legitimate integer, use ferror to detect errors with putw.

Program
#include <stdio.h>
#include <stdlib.h>

#define FNAME "test.$$$"

int main(void)
{
   FILE *fp;
   int word;

   /* place the word in a file */
   fp = fopen(FNAME, "wb");
   if (fp == NULL)
   {
      printf("Error opening file %s\n", FNAME);
      exit(1);
   }

   word = 94;
   putw(word,fp);
   if (ferror(fp))
       printf("Error writing to file\n");
   else
       printf("Successful write\n");
   fclose(fp);

   /* reopen the file */
   fp = fopen(FNAME, "rb");
   if (fp == NULL)
   {
      printf("Error opening file %s\n", FNAME);
      exit(1);
   }

   /* extract the word */
   word = getw(fp);

   if (ferror(fp))
       printf("Error reading file\n");
   else
       printf("Successful read: word = %d\n", word);

   /* clean up */
   fclose(fp);
   unlink(FNAME);

   return 0;
}


fseek



fseek          <STDIO.H>

 Repositions the file pointer of a stream

 Declaration:

  int fseek(FILE *stream, long offset, int whence);

 Remarks:


fseek sets the file pointer associated with a stream to a new position.

fseek discards any character pushed back using ungetc.

fseek is used with stream I/O. For file handle I/O, use lseek.

After fseek, the next operation on an update file can be either input or
output.

fseek can return a 0 (indicating that the pointer has been moved
successfully), when it has not been. This is because DOS, which actually
resets the pointer, does not verify the setting.

 Return Value:


   On success (the pointer is successfully moved), fseek returns 0.
   On failure, fseek returns a non-zero value. fseek returns an error code
    only on an unopened file or device.


 Example:

 #include <stdio.h>

 long filesize(FILE *stream);

 int main(void)
 {
    FILE *stream;

    stream = fopen("MYFILE.TXT", "w+");
    fprintf(stream, "This is a test");
    printf("Filesize of MYFILE.TXT is %ld bytes\n", filesize(stream));
    fclose(stream);
    return 0;
 }

 long filesize(FILE *stream)
 {
    long curpos, length;

    curpos = ftell(stream);
    fseek(stream, 0L, SEEK_END);
    length = ftell(stream);
    fseek(stream, curpos, SEEK_SET);
    return length;
 }

brk


Brk
Changes data-segment space allocation
 Declaration:
   int brk(void *addr);
 Remarks:
brk dynamically changes the amount of space allocated to the calling
program's heap by resetting the program's break value to addr.
brk and sbrk will fail without making any change in the allocated space if
such a change would allocate more space than is allowable.

 Return Value:
On success,
   brk returns 0
On error, both functions return -1 and
    set errno to ENOMEM (not enough memory).
Program
#include <stdio.h>
#include <alloc.h>

int main(void)
{
   char *ptr;

   printf("Changing allocation with brk()\n");
   ptr = (char *) malloc(1);
   printf("Before brk() call: %lu bytes free\n", coreleft());
   brk(ptr+1000);
   printf(" After brk() call: %lu bytes free\n", coreleft());
   return 0;
}

sbrk


 sbrk

                  <ALLOC.H>
     
Changes data-segment space allocation
Declaration:
   void *sbrk(int incr);
Remarks:
sbrk changes the allocated space by adding incr bytes to the break value.
The amount of allocated space increases as the break value increases.
With sbrk, incr can be negative, which decreases the amount of allocated
space.

Return Value:

sbrk returns the old break value
On error, both functions return -1 and
    set errno to ENOMEM (not enough memory).
Progarm
#include <stdio.h>
#include <alloc.h>

int main(void)
{
   printf("Changing allocation with sbrk()\n");
   printf("Before sbrk() call: %lu bytes free\n",
          (unsigned long) coreleft());
   sbrk(1000);
   printf(" After sbrk() call: %lu bytes free\n",
           (unsigned long) coreleft());
   return 0;
}

mcalloc


 malloc                <ALLOC.H>
 Allocates memory

 Declaration:
 void *malloc(size_t size);

 Remarks:
malloc allocates a block of size bytes from the memory heap. It allows a
program to allocate memory explicitly as it's needed, and in the exact
amounts needed.

The heap is used for dynamic allocation of variable-sized blocks of memory.
Many data structures, such as trees and lists, naturally employ heap memory
allocation.

 Return Value:
On success, malloc returns a pointer to
    the newly allocated block of memory.
On error (if not enough space exists for
    the new block), malloc returns null. The
    contents of the block are left unchanged.
If the argument size == 0, malloc returns
    null.
 Example:

 #include <stdio.h>
 #include <string.h>
 #include <alloc.h>
 #include <process.h>

 int main(void)
 {
    char *str;

    /* allocate memory for string */
    if ((str = (char *) malloc(10)) == NULL)
    {
       printf("Not enough memory to allocate buffer\n");
       exit(1);  /* terminate program if out of memory */
    }

    /* copy "Hello" into string */
    strcpy(str, "Hello");

    /* display string */
    printf("String is %s\n", str);

    /* free memory */
    free(str);

    return 0;
 }

calloc


 calloc                < ALLOC.H)

 Allocates main memory

 Declaration:
 void *calloc(size_t nitems, size_t size);

 Remarks:
calloc provides access to the C memory heap, which is available for dynamic
allocation of variable-sized blocks of memory.

Many data structures, such as trees and lists, naturally employ heap memory
allocation.

calloc allocates a block (nitems * size) bytes and clears it to 0. To
allocate a block larger than 64K, use farcalloc.
 Return Value:
On success, returns a pointer to the newly allocated block.
On failure (not enough space exists for the new block, or nitems or
    size is 0), returns null.
 Example:
 #include <stdio.h>
 #include <alloc.h>
 #include <string.h>

 int main(void)
 {
    char *str = NULL;

    /* allocate memory for string */
    str = (char *) calloc(10, sizeof(char));

    /* copy "Hello" into string */
    strcpy(str, "Hello");

    /* display string */
    printf("String is %s\n", str);

    /* free memory */
    free(str);

    return 0;
 }

ifndef in c example

#ifndef, Conditional compilation directives
Syntax:
#if
#else
#endif

#if
#elif
#endif

1.The compiler only compiles the lines that follow the #if directive when evaluates to non-zero.
2.Otherwise, the compiler skips the lines that follow until it encounters the matching #else or #endif.
3.If the expression evaluates to false and there is a matching #else, the lines between the #else and the #endif are compiled.
4.#if directives can be nested, but matching #else and #endif directives must be in the same file as the #if.
5.#elif is like #else except the else block is only compiled if the expression after #elif is non-zero.

#ifndef
evaluates to 1 if the symbol specified by has not been defined.
Example




#ifndef Quiet
PrintDiagnostics();
#endif

ifdef in c

#ifdef, Conditional compilation directives

Syntax:

#if
#else
#endif

#if
#elif
#endif


1)The compiler only compiles the lines that follow the #if directive when evaluates to non-zero.
2)Otherwise, the compiler skips the lines that follow until it encounters the matching #else or #endif.
3)If the expression evaluates to false and there is a matching #else, the lines between the #else and the #endif are compiled.
4)#if directives can be nested, but matching #else and #endif directives must be in the same file as the #if.
5)#elif is like #else except the else block is only compiled if the expression after #elif is non-zero.


#ifdef  evaluates to 1 if the symbol specified by has been previously
defined with a #define directive.


Example

#ifdef DEBUG
printf("Total space %d\n", space);
#endif

if directives

#if directive 
Conditional compilation directives
Syntax:
#if
#else
#endif

1)The compiler only compiles the lines that follow the #if directive when
evaluates to non-zero.
2)Otherwise, the compiler skips the lines that follow until it encounters the
matching #else or #endif.
3)If the expression evaluates to false and there is a matching #else, the
lines between the #else and the #endif are compiled.
4)#if directives can be nested, but matching #else and #endif directives must
be in the same file as the #if.

Example

#if defined(NEARPOINTERS)
space = farcoreleft();
#elif defined(FARPOINTERS)
space = coreleft();
#else
#error Unsupported memory model
#endif

#include

  #include (directive)

Treats text in the file specified by filename as if it appeared in the
current file.

 Syntax:

   #include "filename"
   #include <filename>

 #include "filename" searches the source path first, then the include path.

 #include <filename> does not search the source directory.

 Examples:
  #include <stdio.h>
  #include "main.h"

#line


#line (directive)
Causes the compiler to think that the line number of the next source line is
given by <constant>, and the current input file is given by <identifier>.

 Syntax: 

#line <constant> [ <identifier> ]

If <identifier> is missing, the current file name remains unchanged.

 Example:

  #line 55 main.cpp;

#error in c

#error (directive)
Issues error message
Syntax:
#error

If this line of code is compiled by the compiler, a fatal error message will be issued for this line and include the text defined by .
Example:

#if !defined(MODEL)
#error Building model not defined
#endif

#define


  #define (directive)
  Defines a macro

 Syntax: 
  #define <id1>[ ( <id2>, ... ) ] <token string>

The #define directive defines a macro.

Macros provide a mechanism for token replacement with or without a set of
formal, function-line parameters.

All subsequent instances of the identifier <id1> in the source text will be
replaced by the text defined by <token string>.


 Example:
  #define MAXINT 32767
  #define ctrl(x) ((x) \
 - 64) /* continued from preceding line */

It is legal but ill-advised to use Turbo C++ keywords as macro identifiers.
For example,

  #define int long

is legal, but possibly catastrophic.

Switch Programs



Switch Programs


#include <stdio.h>

#define CODE 0


main ()

{ short digit;

printf ("Enter any digit in the range 0..9");

scanf ("%h",&digit);

if ((digit < 0) || (digit > 9))
{
printf ("Number was not in range 0..9");
return (CODE);
}

printf ("The Morse code of that digit is ");
Morse (digit);
}

/************************************************/

Morse (digit) /* print out Morse code */

short digit;

{
switch (digit)
{
case 0 : printf ("-----");
break;
case 1 : printf (".----");
break;
case 2 : printf ("..---");
break;
case 3 : printf ("...--");
break;
case 4 : printf ("....-");
break;
case 5 : printf (".....");
break;
case 6 : printf ("-....");
break;
case 7 : printf ("--...");
break;
case 8 : printf ("---..");
break;
case 9 : printf ("----.");
}
}



sqrt

sqrt
sqrt, sqrtl
 Calculates square root

 Declaration:
Real:    
double sqrt(double x);
              long double sqrtl(long double @E(x));
Complex: 
complex sqrt(complex x);

 Remarks:
sqrt calculates the positive square root of the input value.
For complex numbers x, sqrt(x) gives the complex root whose arg is arg(x) /

2.The complex square root is defined by
  sqrt(z) = sqrt(abs(z)) * ( cos(arg(z)/2) + i * sin(arg(z)/2) )

 Return Value:
Real sqrt and sqrtl return the square root of x.
If x is real and positive, the result is positive.
If x is real and negative, sqrt sets errno to EDOM (domain error).

Error handling for real sqrt can be modified via matherr; for sqrtl, via
Example:

 #include <math.h>
 #include <stdio.h>

 int main(void)
 {
    double x = 4.0, result;

    result = sqrt(x);
    printf("The square root of %lf is %lf\n", x, result);
    return 0;
}

scanf


scanf function
 
  cscanf   CONIO.H  The console
  fscanf   STDIO.H  A stream
  scanf    STDIO.H  stdin
  sscanf   STDIO.H  A string
  vfscanf  STDIO.H  A stream, using an argument list
  vscanf   STDIO.H  stdin, using an argument list
  vsscanf  STDIO.H  A string, using an argument list

 Declaration:
int cscanf (                          char *format [, address, ...]);
int fscanf (FILE *stream,       const char *format [, address, ...]);
int scanf  (                    const char *format [, address, ...]);
int sscanf (const char *buffer, const char *format [, address, ...]);
int vfscanf(FILE *stream,       const char *format, va_list arglist);
int vscanf (                    const char *format, va_list arglist);
int vsscanf(const char *buffer, const char *format, va_list arglist);

 Remarks:
The ...scanf functions do the following:
Scan a series of input fields one character at a time
Format each field according to a corresponding format specifier passed
   in the format string *format.
Store the formatted input at an address passed as an argument following
   *format (cscanf also echoes the input directly to the screen)

   Return Value:
On success,
...scanf functions return the number of input fields  successfully scanned, converted, and stored.
    The return value does not include scanned fields that were not stored.

Return value = 0 if no fields were stored.
Return value = EOF if
   cscanf, fscanf, scanf, vfscanf, or vscanf attempts to read at
       end-of-file, or
   sscanf or vsscanf attempts to read at end-of-string

pow()


    Pow()
Power function, x to the y (x**y)

 Declaration:
Real:      
             double pow(double x, double y);
              long double pow(long double (x), long double (y));

Complex:   
              complex pow(complex x, complex y);
              complex pow(complex x, double y);
              complex pow(double x, double y);


 Return Value:
On success,
 pow and powl return the value   calculated, x**y.
If x and y are both 0, they return 1.
If x is real and < 0, and y is not a  whole number, these functions set errno   to EDOM (domain error).

program
#include <math.h>
#include <stdio.h>

int main(void)
{
   double x = 2.0, y = 3.0;

   printf("%lf raised to %lf is %lf\n", x, y, pow(x, y));
   return 0;
}


 <MATH.H>
 log and logl     = natural logarithm function
log10 and log10l = common logarithm function

 Declaration:

   Real:     double log(double x);
              double log10(double x);
              long double logl(long double (x));
              long double log10l(long double (x));

  Complex:  complex log(complex x);
              complex log10(complex x);

 Remarks:

 Real versions:
 log and logl calculate the natural logarithm of x.
 log10 and log10l calculate the base 10 logarithm of x.

 Complex versions:
The complex natural logarithm is defined by
  log(z) = log(abs(z)) + i arg(z)

The complex common logarithm is defined by
  log10(z) = log(z) / log(10)

 Return Value:
   On success,

   log and logl return the natural log of x
   log10 and log10l the return log (base 10) of x
   On error,
      if x = 0, these functions set errno to ERANGE
      log and log10 return negative HUGE_VAL
       logl and log10l return negative _LHUGE_VAL

 Program

#include <math.h>
#include <stdio.h>

int main(void)
{
   double result;
   double x = 8.6872;

   result = log(x);
   printf("The natural log of %lf is %lf\n", x, result);

   return 0;
}

atan() in c

atan Arc cosine, arc sine, and arc tangent functions
Declaration:
Real:
double atan(double x);
double atan2(double y, double x);
Remarks:
atan and atanl calculate the arc tangent of the input value
atan2 and atan2l also calculate the arc tangent of the input value
Return Value:
atan and atanl return the arc tangent of the input value (in the range -pi/2 to pi/2)
atan2 and atan2l return the arc tangent of y/x (in the range -pi to pi).

Program



#include <stdio.h>
#include <math.h>

int main(void)
{
   double result;
   double x = 0.5;

   result = atan(x);
   printf("The arc tangent of %lf is %lf\n", x, result);
   return(0);
}


asin

 Arc cosine, arc sine, and arc tangent functions

 Declaration:
 Real:                   
              double atan(double x);
              double atan2(double y, double x);


Remarks:

                     atan and atanl calculate the arc tangent of the input value
                    atan2 and atan2l also calculate the arc tangent of the input value

Return Value:

   atan and atanl return the arc tangent of the   input value (in the range -pi/2 to pi/2)
   atan2 and atan2l return the arc tangent of y/x      (in the range -pi to pi).

Program

#include <stdio.h>
#include <math.h>

int main(void)
{
   double result;
   double x = 0.5;

   result = atan(x);
   printf("The arc tangent of %lf is %lf\n", x, result);
   return(0);
}


asin

 Arc cosine, arc sine, and arc tangent functions

 Declaration:
 Real:               
                                double asin(double x);
                                long double asinl(long double (x));

Remarks:

                  asin and asinl of a real value compute the arc sine of that value

Return Value:

   asin and asinl return the arc sine of the input value (in the range -pi/2 to pi/2)

Program
#include <stdio.h>
#include <math.h>

int main(void)
{
   double result;
   double x = 0.5;

   result = asin(x);
   printf("The arc sin of %lf is %lf\n", x, result);
   return(0);
}


 acos

 Arc cosine, arc sine, and arc tangent functions

 Declaration:
 Real:   
            double acos(double x);
long double acosl(long double (x));
complex acos(complex z);

Remarks:

                acos and acosl of a real value compute the arc cosine of that value

Return Value:

   On success,
   acos and acosl return the arc cosine of the input value (in the range 0 to pi)

Program

#include <stdio.h>
#include <math.h>

int main(void)
{
  double result;
  double x = 0.5;

  result = acos(x);
  printf("The arc cosine of %lf is %lf\n", x, result);
  return 0;
}

 struct (keyword)

 Groups variables into a single record

 Syntax:

  struct [<struct type name>] {
    [<type> <variable-name[, variable-name, ...]>] ;
    [<type> <variable-name[, variable-name, ...]>] ;
    ...
  } [<structure variables>] ;

A struct, like a union, groups variables into a single record.
             


Though both <struct type name> and <structure variables> are optional, one
of the two must appear.

Elements in the record are defined by naming a <type>, followed by one or
more <variable-name> (separated by commas).

Different variable types can be separated by a semicolon.

 Example:

  struct my_struct {
    char name[80], phone_number[80];
    int  age, height;
  } my_friend;

This struct declares an array of records containing two strings (name and
phone_number) and two integers (age and height).

To access elements in a structure, you use a record selector (.). For
example,

  strcpy(my_friend.name,"Mr. Wizard");