Thread: Command line argument

  1. #1
    Registered User Max's Avatar
    Join Date
    Jul 2002

    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;


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


    return 0;

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    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).
    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc,char *argv[])
    	int k, num = 0;
    	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
    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, 09:45 PM