Thread: program arguments

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    program arguments

    Ok, I'm stuck again.

    I'm want my program to accept different arguments from the command line, but haven't been able to get it to work. I want something like . . .

    Code:
    #include <stdio.h>
    int main( int argc, char *argv[] )  
    {
    	if( argv[1] == "-h" )
            {
    		printf("This is the help message");
            }
            return 0;
    
    }
    I also tried using a switch statement, but no luck so far. Any help would be appreciated. Thanks.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use strcmp to compare strings:
    if (strcmp(argv[1], "-h") == 0)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    And check argc first before you blast through argv.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    Thanks.
    Here's what I finally got to work!

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	switch(argc)
    	{
    		case 2: 
    			if (strcmp(argv[1], "-h") == 0)
    			{
    				printf("This is the help message!\n");
    			}
    			break;
    	}
    	return 0; 
     }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Replies: 5
    Last Post: 06-24-2003, 01:13 AM
  4. running program with arguments (linux)
    By N8760 in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2002, 02:08 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM