Parsing command line arguments in C

  • Thread starter TheSourceCode
  • Start date
  • Tags
    Line
In summary, the conversation discusses an assignment to write a code that parses command line arguments and displays the user's first and last name, as well as their age if given. The -age key indicates that the following parameter is the age. The program works as long as the arguments are inputted correctly, but crashes if any other time. The conversation also includes a discussion on valid inputs and the importance of avoiding accessing array elements that might not exist.
  • #1
TheSourceCode
14
0
Here is the given assignment:

Write a code that would
parse the command line
and display the user’s first /
last name and (if given)
his/her age. The ‘-age’ key,
if present, could be the first
or, alternatively, the third
argument and indicates that
the following parameter is
age, as in the example
shown. If the command
line arguments are not
valid, an error message to
that effect should be
displayed. (You may use
string functions as needed.)

My program works as long as the user is inputting arguments with the -age key but crashes if any other time. Could I get a hint as to what is wrong?

Code:
#include <stdio.h>
int main(int argc, char *argv[]){ 
  if(argc < 3 || argc > 5 || argc == 4)
    printf("Invalid command line arguements.\n");
  if(!strcmp(argv[2], "-age") || !strcmp(argv[4], "-age"))
    printf("Invalid command line arguements.\n");
  if(argc == 3){
    printf("Your first name is: %s\n", argv[1]);
    printf("Your last name is: %s\n", argv[2]);
  }   
  if(!strcmp(argv[1], "-age")){
    printf("Your first name is: %s\n", argv[3]);
    printf("Your last name is: %s\n", argv[4]);
    printf("Your age is: %s\n", argv[2]);
  }
  if(!strcmp(argv[3], "-age")){
    printf("Your first name is: %s\n", argv[1]);
    printf("Your last name is: %s\n", argv[2]);
    printf("Your age is: %s\n", argv[4]);
  }
  system("PAUSE");	
  return 0;
}
 
Physics news on Phys.org
  • #2
Can you provide an example valid arguments? It said the age argument could be first or third, but in your program, it looks like it's the third or fifth...so I'm a little lost.
 
  • #3
"if(argc < 3 || argc > 5 || argc == 4)"

You said age if given, so that means age input is not necessarily expected right?
 
  • #4
Sorry, let me clarify a little. Valid input would look something like this:

Code:
program.exe John Doe -age 43
or
Code:
program.exe -age 56 Jane Doe
or
Code:
program.exe Bob Smith
Where program.exe is argument 0 and so on...
 
  • #5
If the age is not suppled, your program would attempt to read or write past the end of the argv array. Read your source code and attempt to find out where.

This is something that is very important to avoid. Every time you access an array element that might not exist, check the array size (or bounds) first!
 
  • #6
Thanks for the feedback everyone, I got it to work! I really should have noticed the issue before.
 

Related to Parsing command line arguments in C

What is parsing command line arguments in C?

Parsing command line arguments in C is the process of extracting specific inputs from the command line that a user enters when running a C program. These arguments can be used to modify the behavior of the program or provide additional information.

Why is parsing command line arguments important in C?

Parsing command line arguments allows for more flexibility and customization in C programs. It allows users to provide specific inputs without having to recompile the program, making it more user-friendly and efficient.

How do you parse command line arguments in C?

In C, command line arguments are stored in the argv and argc variables. The argv variable is an array of strings that contains each command line argument, and argc is the number of arguments entered. These can then be accessed and processed using loops and string manipulation functions.

What is the difference between parsing command line arguments in C and other programming languages?

The process of parsing command line arguments is similar across programming languages, but the specific syntax and methods may vary. In C, the argv and argc variables are used to store and access the arguments, while other languages may use different methods or built-in functions.

What are some common errors when parsing command line arguments in C?

Some common errors when parsing command line arguments in C include not properly checking for the correct number of arguments, not handling invalid inputs or arguments, and not properly converting the arguments to the correct data type. It is important to thoroughly test and validate the inputs to avoid these errors.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
794
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top