Thread: Command Line Arguments Passing String and Comparing strings

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    2

    Command Line Arguments Passing String and Comparing strings

    i want to compile a c program to run it in my command line.

    but i am unable to do this i am facing many errors. main thing i want to do is running this command in linux like :
    Code:
    mycommand yum upgrade
    here mycommand is a C compiled file and yum upgrade are arguments. if it's detected argument yum then it will check next argument which is upgrade then it will execute a linux command yum update anybody can do this for me ?
    Last edited by monsterlegend; 02-05-2021 at 10:35 AM.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    794
    You need to post your code in Code blocks so we can see why you are "facing many errors".

    Make sure your warnings are turned on and set to highest warning level. Don't know what compier you are using.

    Are you defining your main() function as:
    Code:
    int main(int argc, char *argv[])
    {
       // ...   
       return 0;
    }
    This will allow Command Line Arguments to be passed to your program.
    Last edited by rstanley; 02-05-2021 at 10:59 AM.

  3. #3
    Registered User
    Join Date
    Feb 2021
    Posts
    2
    Quote Originally Posted by rstanley View Post
    You need to post your code in Code blocks so we can see why you are "facing many errors".

    Make sure your warnings are turned on and set to highest warning level. Don't know what compier you are using.

    Are you defining your main() function as:
    Code:
    int main(int argc, char *argv[])
    {
       // ...   
       return 0;
    }
    This will allow Command Line Arguments to be passed to your program.
    yes but i am saying that i want to pass string arguments.

    like someone enter two arguments :
    yum upgrade
    then first it will take yum then take upgrade then it will go to yum function and then give it upgrade argument then if argument upgrade is detected it will run system("yum upgrade")

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    794
    Quote Originally Posted by monsterlegend View Post
    yes but i am saying that i want to pass string arguments.

    like someone enter two arguments :
    yum upgrade
    then first it will take yum then take upgrade then it will go to yum function and then give it upgrade argument then if argument upgrade is detected it will run system("yum upgrade")
    All command line arguments ARE pass as "String" arguments, even numbers.

    int argc contains the number of strings on the command line. One for the program name (Usually) and one for each argument to the program.
    "mycommand yum upgrade" argc would be 3, and argv is an array of pointers to the strings on the command line, program name and each argument. At least under gcc and most hosted environment compilers. Again, I don't know your O/S or compiler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command line arguments and passing them
    By somebody in forum C Programming
    Replies: 13
    Last Post: 04-07-2012, 10:33 AM
  2. C ...String replacement... command line arguments
    By andrew1400 in forum C Programming
    Replies: 11
    Last Post: 04-18-2010, 10:42 PM
  3. passing arguments using "Command Line Arguments"
    By Madshan in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2006, 03:46 PM
  4. Comparing command line arguements to a string.
    By SlyMaelstrom in forum C++ Programming
    Replies: 14
    Last Post: 10-30-2005, 04:56 AM
  5. String Search with command line arguments
    By goron350 in forum C Programming
    Replies: 5
    Last Post: 11-29-2004, 05:56 PM

Tags for this Thread