Passing Command Line Arguments in C?

Passing Command Line Arguments in C?

Post by xTractatorix on Thu Aug 16, 2012 12:04 pm
([msg=68768]see Passing Command Line Arguments in C?[/msg])

Im talking about this:
Code: Select all
int main ( int argc, char *argv[] )

What does that allow you to do? Does it allow you to code from the terminal or cmd? I'm just not understanding what fully declaring main does, i would greatly appreciate a explanation.
xTractatorix
Experienced User
Experienced User
 
Posts: 61
Joined: Sun May 13, 2012 8:42 am
Blog: View Blog (0)


Re: Passing Command Line Arguments in C?

Post by WallShadow on Thu Aug 16, 2012 12:15 pm
([msg=68769]see Re: Passing Command Line Arguments in C?[/msg])

The arguements to the main function help pass additional information to the program. The int passed is the number of arguments you have, and the char * [] is an array of pointers to null-terminated strings.

What is this for? It basically helps give a program immediate information about the way that it should work. for example;

to start notepad with a blank file, you just start it with the command line "notepad.exe", but if you want to immediately open a txt file with notepad, you use the command line "notepad.exe C:/blah/mytxtfile.txt" that 'C:/blah/mytxtfile.txt' will be stored in the second argument to the program ( the first arguement is always the program's name ). Many command line utilities also use command line arguements. such as starting command prompt with "cmd.exe" or "cmd.exe /k dir" will produce different results.
User avatar
WallShadow
Contributor
Contributor
 
Posts: 601
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: Passing Command Line Arguments in C?

Post by tremor77 on Thu Aug 16, 2012 12:49 pm
([msg=68770]see Re: Passing Command Line Arguments in C?[/msg])

This is a great question for HTS forums! And a great answer too... Here's a little sample code I scrounged up to explain further. This is from a C++ tutorial I have bookmarked.. This should be pretty similar for C

Code: Select all
int main ( int argc, char *argv[] )
{
  if ( argc != 2 ) // argc should be 2 for correct execution
    // We print argv[0] assuming it is the program name
    cout<<"usage: "<< argv[0] <<" <filename>\n";
  else {
    // We assume argv[1] is a filename to open
    ifstream the_file ( argv[1] );
    // Always check to see if file opening succeeded
    if ( !the_file.is_open() )
      cout<<"Could not open file\n";
    else {
      char x;
      // the_file.get ( x ) returns false if the end of the file
      //  is reached or an error occurs
      while ( the_file.get ( x ) )
        cout<< x;
    }
    // the_file is closed implicitly here
  }
}
Image
User avatar
tremor77
Contributor
Contributor
 
Posts: 889
Joined: Wed Mar 31, 2010 12:00 pm
Location: New York
Blog: View Blog (0)


Re: Passing Command Line Arguments in C?

Post by xTractatorix on Thu Aug 16, 2012 4:51 pm
([msg=68776]see Re: Passing Command Line Arguments in C?[/msg])

WallShadow wrote:The arguements to the main function help pass additional information to the program. The int passed is the number of arguments you have, and the char * [] is an array of pointers to null-terminated strings.<br><br>What is this for? It basically helps give a program immediate information about the way that it should work. for example;<br><br>to start notepad with a blank file, you just start it with the command line "notepad.exe", but if you want to immediately open a txt file with notepad, you use the command line "notepad.exe C:/blah/mytxtfile.txt" that 'C:/blah/mytxtfile.txt' will be stored in the second argument to the program ( the first arguement is always the program's name ). Many command line utilities also use command line arguements. such as starting command prompt with "cmd.exe" or "cmd.exe /k dir" will produce different results.

I think i understand what your saying.Could you give an example of how allowing a C program to accept command line arguments would allow it to do somenthing that a other program couldnt do without accepting command line arguments.
xTractatorix
Experienced User
Experienced User
 
Posts: 61
Joined: Sun May 13, 2012 8:42 am
Blog: View Blog (0)


Re: Passing Command Line Arguments in C?

Post by LoGiCaL__ on Thu Aug 16, 2012 7:29 pm
([msg=68779]see Re: Passing Command Line Arguments in C?[/msg])

Here's a little bit more explanation from the example tremor gives which is really all you need:
http://www.cprogramming.com/tutorial/lesson14.html

I knew it looked familiar, I had the same one lol. Anyways, chances are if you are having trouble understanding still you may want to go brush up on pointers and arrays.
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1062
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests