Showing posts with label CMD Line Arguments. Show all posts
Showing posts with label CMD Line Arguments. Show all posts

Friday, September 23, 2011

WAP to accessing command line arguments


/*
  Name: WAP to accessing command line arguments 
  Author: Parveen Malik
  Date: 23/09/11 02:09
*/


#include <stdio.h>
#include <conio.h>
#define plural_text(n) &"s"[(1 == (n))]
#define plural_text2(n) &"es"[(1 == (n)) << 1]


main(int argc, char *argv[])
{
      int i, n = argc - 1;


      printf("You passed %d argument%s on the command line.",
            n, plural_text(n));


      if (argc > 1)
      {
            puts(" They are:");
            for (i = 1; i < argc; ++i)
            {
                  printf("\nArgument #%d:\n  Text: \"%s\"\n  Value: %d\n",
                        i, argv[i], atoi(argv[i]));
            }
      }
      else  putchar('\n');
      getch();
      return 0;
}


OUTPUT :



WAP to retrieve command line arguments


/*
  Name: WAP to retrieve command line arguments
  Copyright:  Copyright Paul Griffiths 2000
  Author:  Parveen Malik
  Date: 23/09/11 01:40

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

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

    while ( n < argc ) {
	printf("Command line argument %d is %s\n", (n+1), argv[n]);
	++n;
    }

    return EXIT_SUCCESS;
}

WAP to find first n numbers of the fibonacci series


/*
  Name: WAP to find first n numbers of the fibonacci series
  Copyright: Paul Griffiths 2000
  Author: Parveen Malik
  Date: 23/09/11 01:56
  Description: Finds the first 'n' numbers in the Fibonacci sequence.
  'n' must be supplied as the sole command line argument,
  and must be between 3 and 47.
*/


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


int ParseCmdLine(int argc, char *argv[]);


int main(int argc, char * argv[]) {
    unsigned long f1 = 1, f2 = 1;
    int temp, i = 3;
    int n = ParseCmdLine(argc, argv);


    printf("First %d numbers in the Fibonacci sequence:\n\n", n);


    printf("%11lu%11lu", f1, f2);


    for ( i = 3; i <= n; ++i ) {
        temp = f2;
        f2 += f1;
        f1 = temp;


        printf("%11lu", f2);


        if ( i % 5 == 0 )
            putchar('\n');
    }
    putchar('\n');
    return EXIT_SUCCESS;
}




/*  Returns the integer specified on the command line  */


int ParseCmdLine(int argc, char *argv[]) {
    int n;
    char * endptr;


    if ( argc < 2 ) {
        fprintf(stderr, "You must supply an argument\n");
        exit(EXIT_FAILURE);
    }
    else if ( argc > 2 ) {
        fprintf(stderr, "You must only supply one argument\n");
        exit(EXIT_FAILURE);
    }


    n = strtol(argv[1], &endptr, 0);
    if ( *endptr ) {
        fprintf(stderr, "You must supply a whole number as an argument\n");
        exit(EXIT_FAILURE);
    }


    if ( n < 3 ) {
        fprintf(stderr, "You must supply a number greater than 2\n");
        exit(EXIT_FAILURE);
    }
    else if ( n > 47 ) {
        fprintf(stderr, "You must supply a number less than 48\n");
        exit(EXIT_FAILURE);
    }


    return n;


}