Last active
April 20, 2022 16:01
-
-
Save gonzafernan/220c573c20dbd023bb27709dd0693b79 to your computer and use it in GitHub Desktop.
Program to obtain month name from the corresponding index.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
============================================================================ | |
Name : getmonth.c | |
Author : Gonzalo G. Fernandez | |
Email : [email protected] | |
Institution : Universidad El Bosque | |
Year : 2022 | |
Version : 1.0 | |
Copyright : License MIT | |
Description : Program to obtain month name from the corresponding index. | |
============================================================================ | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
//! Month list | |
const char *month_list[12] = { | |
"January", "February", "March", "April", "May", "June", "July", | |
"August", "September", "October", "November", "December"}; | |
int main(int argc, char *argv[]){ | |
printf("Convert from month index to month name:\n"); | |
// Check for arguments | |
if (argc < 2){ | |
printf("ERROR: One argument exected (month number).\n"); | |
return 1; | |
} | |
int month_index; // Store input month index | |
for (int i=1; i<argc; i++){ | |
// Convert argument string to integer | |
month_index = atoi(argv[i]); | |
// Check for valid integer (between 1 and 12) | |
if (month_index <= 0 || month_index > 12){ | |
printf("ERROR: Argument number %d (%d) is not a valid month.\n", i, month_index); | |
return 1; | |
} else { | |
// Output the corresponding month name | |
printf("%d: %s\n", month_index, *(month_list + month_index-1)); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment