Command line argument

This is a discussion on Command line argument within the C Programming forums, part of the General Programming Boards category; I am a beginner C. How can I use the value of a Command line argument in a formula. Example: ...

  1. #1
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    Command line argument

    I am a beginner C.

    How can I use the value of a Command line argument in a formula.

    Example: I want to use the user's input such the 1000 in the
    Command line argument intro 1000

    I used atoi to convert 1000 into an integer..but I don't know whether it is necessary or even correct to use atoi!!!!!


    This what I have so far:


    int main(int argc,char*argv[])
    {
    int k, num;

    if(argc==1)
    {
    strcpy(num,argv[1]);
    num=atoi(argv[1]);
    }

    k=num+10 //I want num to be = 1000//

    printf("%d",k);

    return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This:
    >if(argc==1)
    >{
    >strcpy(num,argv[1]);
    is incorrect. argc will always be 1 as the first entry in argv array is the program name.

    Here's a working version for you to study (it's pretty much the same).
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc,char *argv[])
    {
    	int k, num = 0;
    
    	if(argc==2)
    	{
    		num=atoi(argv[1]);
    	}
    
    	k=num+10;
    
    	printf("num:%d k:%d",num, k);
    
    	return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Unregistered
    Guest
    Thank you hammer, so i guess I was right to use atoi
    but all i needed was to initialize num and argv is 2

    Thanks a lot!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. member as default argument
    By MarkZWEERS in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2009, 08:09 AM
  2. template argument
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2008, 03:01 AM
  3. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 08:45 PM

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