Command Line Arguments in C Language:
It is a procedure of passing the arguments to the main function from the command prompt. By using command-line arguments, we can create user-defined commands. In the implementation, when we were required to develop an application for the DOS operating system then recommended going for command-line arguments. DOS is a character-based or command-based OS i.e. if we are required to perform any task, we need to use the specific commands only. When we are developing the program in command-line arguments, then the main function will take 2 arguments i.e. argc & argv i.e. main (int argc, char *argv[])
The argc is a variable of type an integer, it maintains the total number of arguments count value. The argv is a variable of type char*, which maintains actual argument values that are passed to the main() function. In C and C++, by default total number of arguments is 1, i.e. programname.exe
Program to understand Command Line Arguments in C Language:
#include <stdio.h> int main (int argc, char *argv[]) { int i; printf ("\n total no. of arguments:%d", argc); for (i = 0; i < argc; i++) printf ("\n %d arguments: %s", i + 1, argv[i]); return 0; }
Output:
Command Line Arguments Example in C Language:
#include <stdio.h> int main (int argc, char *argv[]) { int i; printf ("\n total no. of arguments:%d", argc); for (i = 0; i < argc; i++) printf ("\n %d arguments: %s", i + 1, argv[i]); return 0; }
//save the file as mytest.c
//compile mytest.c
//Build or rebuild mytest.c
Load the command prompt & change the directory to a specific location where the application file is available. To execute the program, just we required to use the program name or programname.exe. Command-line arguments related programs are not recommended to execute by IDE because we can’t pass the arguments to the main function.
argc & argv are local variable to main() so command prompt related data outside of main(), then recommended to go for aargc & aargv variables, which is defined in dos.h
Program to understand Command Line Arguments in C Language:
#include <stdio.h> #include<dos.h> void abc () { int i, aargc; char *aargv[i]; printf ("\n data in abc:"); printf ("\n total no. of arguments:%d", aargc); for (i = 0; i < aargc; i++) printf ("\n %d arguments: %s", i + 1, aargv[i]); } int main (int argc, const char *argv[]) { int i; printf ("\n data in main:"); printf ("\n total no. of arguments:%d", argc); for (i = 0; i < argc; i++) printf ("\n %d arguments: %s", i + 1, argv[i]); abc (); return 0; }
Output:
By using command-line arguments, it is possible to pass any type of data but all are treated like strings only. When we are working with command line arguments, always recommended to convert string type to numeric type properly before the execution of the logic. Conversion related all predefined functions are declared in stdlib.
Stdlib related predefined functions are:
atoi(), atof(), atol(), _atold(), itoa(), ltoa(), abort(), exit(), _exit(), _c_exit(), atexit(), raise(), signal(), min(), max(), random(), randomize(), rand(), srand(), abs(), labs(), strtod(), strtol(), strtold(), strtoul(), wctomb(), wcstombs(), fcvt(), gcvt(), lfind(), lsearch(), _lrotl(), _lrotr(), div(), ldiv()
Constants: EXIT_SUCCESS, EXIT_FAILURE
atoi(): By using this, we can convert a string into an integer type. atoi() functions requires 1 arguments of type char* & returns int type.
Syntax: int atoi (const char*str);
atol(): By using this, we can convert a string into a long integer type.
Syntax: long atol (const char*str);
atof(): By using this predefined function, we can convert a string into a double data type.
Syntax: double atof (const char*str);
_atold(): By using this predefined function, we can convert a string into a long double type.
Syntax: long double _atold (const char*str);
By using fcvt(), gcvt(), ecvt(), we can convert float to string type.
Program:
#include <stdio.h> int myatoi (char *str) { int res = 0; for (int i = 0; str[i] != '\0'; ++i) res = res * 10 + str[i] - '0'; return res; } int main () { char str[] = "89789"; int val = myatoi (str); printf ("%d ", val); return 0; }
Output: 89789
<ctype.h>:
Character datatype specific functions are available in ctype.h. ctype.h related predefined functions are:
- isalpha(): checking alphabet or not.
- isascii(): checking input data within the ASCII range or not.
- iscntrl(): if control button from keyboard.
- isspace(): if space button from keyboard.
- isupper(): required to use from A to Z.
- isdigit(): checking whether the numeric type or not.
- islower(): to check if it is a-z.
- tolower(): convert uppercase to lowercase.
- toupper(): convert lowercase to uppercase.
- toascii(): convert to ASCII value.
When the function prefix ‘is’ available, then it returns Boolean type i.e. true or false. If ‘to’ is available, then it returns an integer value.
Program:
#include <stdio.h> #include <ctype.h> void identify_alpha_numeric (char a[]) { int count_alpha = 0, count_digit = 0; for (int i = 0; a[i] != '\0'; i++) { if (isalpha (a[i])) count_alpha++; if (isdigit (a[i])) count_digit++; } printf ("The number of alphabets are %d\n", count_alpha); printf ("The number of digits are %d", count_digit); } int main () { char a[] = "Hi 1996, " " How Are You?"; identify_alpha_numeric (a); }