#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0

    Command line arguments


    Code:
    #include<stdio.h>
    
    void main(int argc,char *argv[])
    {
    	int i;
    	printf("%d",argc);
    	for(i = 0;i < argc;i++)
     	{
    		printf("%s",argv[i]);
    		i++;
    	}
    	getchar();
    }
    When I run this, it won't let me type up any words on the command line. It just prints out my location and one command line argument(argc). How can I make it so when I compile it the program waits for me to type up words on the command line and then run the program?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    26
    Rep Power
    0
    That Is Becuase you cant run your program directly you have
    to run it through the cmd or terminal also dont use void main use int main
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    How are you running it? That getchar() kludge at the end of the program suggests to me that you're launching it from within a GUI IDE. That's new-fangled and did not exist when C was developed nor for the first decade and a half of C's existence. Rather, the traditional way, the way that C was designed to use and the way that you were supposed to run programs was from the command line. That's the way that I still run mine; I even compile them from the command line (using MinGW gcc).

    If you insist on running it from the IDE, then you need to set up the command line arguments in the GUI. Some where in one of the menus or under the project options should be something called something like "command line" or "command line arguments" or "arguments". It varies from IDE to IDE. If you cannot find it, then let us know what IDE you're using (you might know your IDE by the misapplied name, "compiler") so that someone who uses that IDE can guide you.

    Another possibility of what you're doing wrong just occurred to me. Are you running the program first and then expect to be able to input the command line arguments in a separate step after the program starts to run? That's not how command line arguments work. Rather, you have to enter them after the program name and then press Enter to start the program. For example, if you have three arguments, file1 and file2 and num, then you would pass those to myprog as:
    C:> myprog file1 file2 num <Enter>

    The value of argc in that example would be 4 and the argv values would be:
    argv[0] myprog -- it may or may not be preceded by the directory path to myprog
    argv[1] file1
    argv[2] file2
    argv[3] num

    I hope I guessed your problem correctly.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    You've also updated i twice each loop, probably not what you intended.

    Code:
    	for(i = 0;i < argc;i++)
     	{
    		printf("%s",argv[i]);
    		i++;
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    dwise_1,

    When I do that through the command prompt I'll say

    c:\users\mineJons\cmdline.c file1 file2 num

    I'll press enter and all it'll do is open cmdline.c in visual studio.

    Why does it do this?
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Are you serious? You've got to be kidding us.

    cmdline.c is a source file. You cannot execute a source file. It has to be compiled and linked into an executable. In Windows/DOS, executables have a .EXE extension.

    Create an executable and then run it with command-line arguments.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    When I do that a message pops up saying:

    "This version of C:\Users\MineJons\cmdline.exe is not compatible of windows you're running."

    I'm running this on windows 7
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    What compiler are you using? If you're running Windows 7 64-bit, then it won't run a 16-bit application. Since you have Visual Studio, which I assume to be post-2003, why don't you build the executable with that? Also, are you creating a console application?

    Time to grab my kilt and go to the Games.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    I'm using Visual Studio 2010, but I'm trying to load the program in the command prompt. Honestly, I really don't know how to build an executable file with visual studio. and yes, I am creating a console application.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    Yes, I am an ametuer. so all the help I can get is awesome to me. Thanks
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    26
    Rep Power
    0
    Do you have to use VisualStudios if you dont i would recommend you use CodeBlocks IDE because it is more simpler and i will pause the program for you so you dont have to use getchar()
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    Oh, haha. You guys are going to like this.

    When I explored the Visual Studio properties I went to:
    Project<hello world1 properties<Debugging and it has a bar that is set for COMMAND LINE ARGUMENTS.

    Thanks for the help and generosity.

IMN logo majestic logo threadwatch logo seochat tools logo