Argv and strcmp help

This is a discussion on Argv and strcmp help within the C Programming forums, part of the General Programming Boards category; I am trying to find a way to essentially say if argv[4] is nothing, in other words if there was ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    13

    Argv and strcmp help

    I am trying to find a way to essentially say if argv[4] is nothing, in other words if there was not argv [4] given, then do so and so. this is what i have so far

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include<string.h>
    
    int main(int argc, char *argv[]){
       if ((strcmp (argv[4], "dec") == 0 )|| (strcmp (argv[4], "????????") == 0))
        printf("first times a charm\n");
    
    return 0;
    }
    what do i put in for the ????? to say if there is no fourth argument do the printf


    mind you i will have another if later on that states if argv[4] is anything other than dec, oct, or hex, or no argument at all print out an error

    hope my jibberish is clear enough

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    If less then 5 arguments are passed, then argv[4] is nothing.
    Less then 5 arguments are passed; therefor argv[4] is nothing.

    First, check to see how many arguments were given on the command line. If this check is passed, check to see if argv[4] is a valid value. A switch statement seems like a good way to do it.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laxkrzy View Post
    I am trying to find a way to essentially say if argv[4] is nothing, in other words if there was not argv [4] given, then do so and so. this is what i have so far
    In the C entry procedure (main) argc tells you how many command line arguments there are... Your program can test it to see if there are 5 arguments and govern itself accordingly. (Remember argv[4] is the 5th argument)

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    14
    so simply put something like:


    Code:
    if(len(argc)<5)
    {
          printf("charming");
    }

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    if( argc>=5 )
    puts("argv[0] is valid"),
    puts("argv[1] is valid"),
    puts("argv[2] is valid"),
    puts("argv[3] is valid"),
    puts("argv[4] is valid");

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    671
    Quote Originally Posted by CommonTater View Post
    In the C entry procedure (main) argc tells you how many command line arguments there are... Your program can test it to see if there are 5 arguments and govern itself accordingly. (Remember argv[4] is the 5th argument)
    Actually the argv[4] IS the 4th argument, since argv[0] is the program name ( which in fact can be an empty string, if it is not available in the current environment - (?) )

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    14
    Quote Originally Posted by kmdv View Post
    Actually the argv[4] IS the 4th argument, since argv[0] is the program name ( which in fact can be an empty string, if it is not available in the current environment - (?) )
    argv[0] is the first argument and in this case the called program. this must not be NULL(at least on *nix). you need an argument to the execve system call, no?

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    671
    AFAIK the argc can be 0. And I just wanted to point out that it is not really an argument (for the program), as laxkrzy might understand it this way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with strcmp!
    By Darkobra in forum C Programming
    Replies: 3
    Last Post: 07-14-2010, 11:36 AM
  2. argv change
    By dracayr in forum C Programming
    Replies: 9
    Last Post: 04-10-2009, 02:49 AM
  3. 3x syntax errors :S, Help a student finalise an assignment
    By Chickenhawk in forum C Programming
    Replies: 14
    Last Post: 07-27-2006, 05:14 AM
  4. File I/O
    By bliss in forum C++ Programming
    Replies: 11
    Last Post: 06-30-2005, 03:38 AM
  5. Trouble with argv[]
    By Seron in forum C++ Programming
    Replies: 4
    Last Post: 12-25-2001, 02:57 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21