> Online tutorial

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;
}