Skip to content

Instantly share code, notes, and snippets.

@shivergard
Created December 19, 2013 12:22
Show Gist options
  • Save shivergard/8038342 to your computer and use it in GitHub Desktop.
Save shivergard/8038342 to your computer and use it in GitHub Desktop.
C Arguments
/* Hello World program */
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "getopt32.c"
main(int argc, char **argv){
int opt;
while ((opt = getopt(argc, argv, "ilw") != -1)){
printf("Hello World [Method 1] %d \n " , opt );
}
int i;
int quiet = 0; /* Value for the "-q" optional argument. */
for (i = 1; i < argc; i++) /* Skip argv[0] (program name). */
{
/*
* Use the 'strcmp' function to compare the argv values
* to a string of your choice (here, it's the optional
* argument "-q"). When strcmp returns 0, it means that the
* two strings are identical.
*/
if (strcmp(argv[i], "-q") == 0) /* Process optional arguments. */
{
quiet = 1; /* This is used as a boolean value. */
printf("Hello World %s [method 2] \n " , argv[i + 1]);
}
else
{
/* Process non-optional arguments here. */
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment