Check if a string has only numerals and do conversion if it is true

$ls
ascii-table     avg-with-garbage.txt  env-var.c      functions_ver1    mph-to-kph
ascii-table.c   case-changer          env-var-set    functions_ver1.c  mph-to-kph.c
ascii-table.md  case-changer.c        env-var-set.c  functions_ver2    mph-to-kph_v2.c
avg.txt         env-var               exist.sh       functions_ver2.c  output.c
$./mph-to-kph 
564
907.7
$cat mph-to-kph.c 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char mph[10] = { 0 };

    while(fgets(mph, sizeof(mph), stdin) != NULL)
    {
        /* Check if mph is numeric 
         * (and do conversion) */
        if( strspn(mph, "0123456789.-\n") == 
            strlen(mph) )
        {
            printf("%.1f\n", (atof(mph)*1.60934) );
        }
        /* If mph is NOT numeric, print error 
         * and return */
        else
        {
            fprintf(stderr, "Found non-numeric" 
                " value\n");
            return 1;
        }
    }
    return 0;
}
$

Exiting a program using exit() function

$ls
ascii-table     avg.txt               case-changer.c  env-var-set    functions_ver1    mph-to-kph.c
ascii-table.c   avg-with-garbage.txt  env-var         env-var-set.c  functions_ver1.c  mph-to-kph_v2.c
ascii-table.md  case-changer          env-var.c       exist.sh       functions_ver2.c  output.c
$zile functions_ver2.c 
$gcc functions_ver2.c -o functions_ver2
$./functions_ver2
Inside main
Calling function one
Inside function one
Calling function two
Inside function two
Returning with (error) from function two
$cat functions_ver2.c
#include <stdio.h>
#include <stdlib.h>
int func1(void);
int func2(void);

int main(int argc, char *argv[])
{
   printf("Inside main\n");
   printf("Calling function one\n");
   if (func1())
   {
      printf("Everything ok from function one\n");
      printf("Return with 0 from main - all ok\n");
      return 0;
   }
   else
   {
      printf("Caught an error from funtcion one\n");
      printf("Return with 1 from main - error\n");
      return 1;
   }
   return 0; /* We shouldn't reach this, but just 
                in case */
}

int func1(void)
{
   printf("Inside function one\n");
   printf("Calling function two\n");
   if (func2())
   {
      printf("Everything ok from function two\n");
      exit(0);
   }
   else
   {
      printf("Caught an error from function two\n");
      exit(1);
   }
}

int func2(void)
{
   printf("Inside function two\n");
   printf("Returning with (error) from " 
      "function two\n");
   exit(1);
}
$

Exiting a program with relevant return value

$ls
ascii-table     avg.txt               case-changer.c  env-var-set    functions_ver1.c  mph-to-kph_v2.c
ascii-table.c   avg-with-garbage.txt  env-var         env-var-set.c  functions_ver2.c  output.c
ascii-table.md  case-changer          env-var.c       exist.sh       mph-to-kph.c
$cat functions_ver1.c
#include <stdio.h>
int func1(void);
int func2(void);

int main(int argc, char *argv[])
{
   printf("Inside main\n");
   printf("Calling function one\n");
   if (func1())
   {
      printf("Everything ok from function one\n");
      printf("Return with 0 from main - all ok\n");
      return 0;
   }
   else
   {
      printf("Caught an error from function one\n");
      printf("Return with 1 from main - error\n");
      return 1;
   }
   return 0; /* We shouldn't reach this, but 
                just in case */
}

int func1(void)
{
   printf("Inside function one\n");
   printf("Calling function two\n");
   if (func2())
   {
      printf("Everything ok from function two\n");
      return 1;
   }
   else
   {
      printf("Caught an error from function two\n");
      return 0;
   }
}

int func2(void)
{
   printf("Inside function two\n");
   printf("Returning with 0 (error) from "
      "function two\n");
   return 0;
}
$gcc functions_ver1.c -o functions_ver1.c 
gcc: fatal error: input file ‘functions_ver1.c’ is the same as output file
compilation terminated.
$gcc functions_ver1.c -o functions_ver1
$./functions_ver1 
Inside main
Calling function one
Inside function one
Calling function two
Inside function two
Returning with 0 (error) from function two
Caught an error from function two
Caught an error from function one
Return with 1 from main - error
$

Setting value to an environment variable

$ls
ascii-table     avg.txt               case-changer.c  env-var-set.c     functions_ver2.c  output.c
ascii-table.c   avg-with-garbage.txt  env-var         exist.sh          mph-to-kph.c
ascii-table.md  case-changer          env-var.c       functions_ver1.c  mph-to-kph_v2.c
$cat env-var-set.c 
#define _POSIX_C_SOURCE 200112L
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    setenv("FULLNAME", "Jack-Benny", 1);
    printf("Your full name is %s\n", getenv("FULLNAME"));
    return 0;
}
$gcc env-var-set.c -o env-var-set

$
$./env-var-set 
Your full name is Jack-Benny
$

Fetching values of environment variables

$ls
ascii-table     avg.txt               case-changer.c  exist.sh          mph-to-kph.c
ascii-table.c   avg-with-garbage.txt  env-var.c       functions_ver1.c  mph-to-kph_v2.c
ascii-table.md  case-changer          env-var-set.c   functions_ver2.c  output.c
$cat env-var.c 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
   /* Using getenv() to fetch env. variables */
   printf("Your username is %s\n", getenv("USER"));
   printf("Your home directory is %s\n", 
      getenv("HOME"));
   printf("Your preferred editor is %s\n", 
      getenv("EDITOR"));
   printf("Your shell is %s\n", getenv("SHELL"));

   /* Check if the current terminal support colors*/
   if ( strstr(getenv("TERM"), "256color")  )
   {
      /* Color the output with \033 + colorcode */
      printf("\033[0;31mYour \033[0;32mterminal "
         "\033[0;35msupport "
         "\033[0;33mcolors\033[0m\n");
   }
   else
   {
      printf("Your terminal doesn't support" 
         " colors\n");
   }
   return 0;
}
$gcc env-var.c -o env-var.c 
gcc: fatal error: input file ‘env-var.c’ is the same as output file
compilation terminated.
$gcc env-var.c -o env-var
$./env-var 
Your username is jeffrin
Your home directory is /home/jeffrin
Your preferred editor is (null)
Your shell is /bin/bash
Your terminal support colors
$

Program for changing case

$ls
ascii-table     avg.txt               env-var.c      functions_ver1.c  mph-to-kph_v2.c
ascii-table.c   avg-with-garbage.txt  env-var-set.c  functions_ver2.c  output.c
ascii-table.md  case-changer.c        exist.sh       mph-to-kph.c
$gcc case-changer.c -o case-changer
$./case-changer 
g
G
v
V
FF
ff
$cat case-changer.c 
#include <stdio.h>
#include <string.h>

int main(void)
{
    char c[20] = { 0 };
    char newcase[20] = { 0 };
    int i;
    while(fgets(c, sizeof(c), stdin) != NULL)
    {
        for(i=0; i<=sizeof(c); i++)
        {
            /* Upper case to lower case */
            if ( (c[i] >= 65) && (c[i] <= 90) )
            {
                newcase[i] = c[i] + 32;
            }
            /* Lower case to upper case */
            if ( (c[i] >= 97 && c[i] <= 122) )
            {
                newcase[i] = c[i] - 32;
            }
        }
        printf("%s\n", newcase);
        /* zero out the arrays so there are no
           left-overs in the next run */
        memset(c, 0, sizeof(c));
        memset(newcase, 0, sizeof(newcase));
    }
    return 0;
}
$ 

The ascii table program

$ls
ascii-table.c   avg-with-garbage.txt  env-var-set.c     functions_ver2.c  output.c
ascii-table.md  case-changer.c        exist.sh          mph-to-kph.c
avg.txt         env-var.c             functions_ver1.c  mph-to-kph_v2.c
$cat ascii-table.c 
#include <stdio.h>

int main(void)
{
    char c;
    for (c = 65; c<=90; c++)
    {
        printf("%c = %d    ", c, c); /* upper case */
        printf("%c = %d\n", c+32, c+32); /* lower case */
    }
    return 0;
}
$gcc ascii-table.c -o ascii-table
$./ascii-table 
A = 65    a = 97
B = 66    b = 98
C = 67    c = 99
D = 68    d = 100
E = 69    e = 101
F = 70    f = 102
G = 71    g = 103
H = 72    h = 104
I = 73    i = 105
J = 74    j = 106
K = 75    k = 107
L = 76    l = 108
M = 77    m = 109
N = 78    n = 110
O = 79    o = 111
P = 80    p = 112
Q = 81    q = 113
R = 82    r = 114
S = 83    s = 115
T = 84    t = 116
U = 85    u = 117
V = 86    v = 118
W = 87    w = 119
X = 88    x = 120
Y = 89    y = 121
Z = 90    z = 122
$

Sum and multiplication

$ls
first-example  first-example.c  new-sum.c  sum  sum.c
$cat new-sum.c 
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void printhelp(char progname[]);

int main(int argc, char *argv[])
{
    int i, opt, sum;

    /* Simple sanity check */
    if (argc == 1)
    {
        printhelp(argv[0]);
        return 1;
    }

    /* Parse command-line options */
    while ((opt = getopt(argc, argv, "smh")) != -1)
    {
        switch (opt)
        {
            case 's': /* sum the integers */
                sum = 0;
                for (i=2; i<argc; i++)
                    sum = sum + atoi(argv[i]);
                break;
            case 'm': /* multiply the integers */
                sum = 1;
                for (i=2; i<argc; i++)
                    sum = sum * atoi(argv[i]);
                break;
            case 'h': /* -h for help */
                printhelp(argv[0]);
                return 0;
            default: /* in case of invalid options*/
                printhelp(argv[0]);
                return 1;
        }
    }
    printf("Total: %i\n", sum);
    return 0;
}

void printhelp(char progname[])
{
    printf("%s [-s] [-m] integer ...\n", progname);
    printf("-s sums all the integers\n"
        "-m multiplies all the integers\n"
        "This program takes any number of integer "
        "values and either add or multiply them.\n"
        "For example: %s -m 5 5 5\n", progname);
}
$ls
first-example  first-example.c  new-sum.c  sum  sum.c
$gcc new-sum.c 
$rm a.out 
$gcc new-sum.c -o new-sum
$./new-sum
./new-sum [-s] [-m] integer ...
-s sums all the integers
-m multiplies all the integers
This program takes any number of integer values and either add or multiply them.
For example: ./new-sum -m 5 5 5
$./new-sum -m 3 3
Total: 9
$./new-sum -s 3 3
Total: 6
$

Sum of numbers

$ls
first-example first-example.c new-sum.c sum.c
$gcc sum.c -o sum
$cat sum.c
#include <stdio.h>
#include <stdlib.h>
void printhelp(char progname[]);

int main(int argc, char *argv[])
{
int i;
int sum = 0;

/* Simple sanity check */
if (argc == 1)
{
printhelp(argv[0]);
return 1;
}

for (i=1; i<argc; i++)
{
sum = sum + atoi(argv[i]);
}
printf(“Total sum: %i\n”, sum);
return 0;
}

void printhelp(char progname[])
{
printf(“%s integer …\n”, progname);
printf(“This program takes any number of ”
“integer values and sums them up\n”);
}
$./sum 1 2 3
Total sum: 6
$