argv[]

This is a discussion on argv[] within the C Programming forums, part of the General Programming Boards category; My question is regarding the use of command line arguments. As we know that argc and argv[] can be used ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105

    argv[]

    My question is regarding the use of command line arguments. As we know that argc and argv[] can be used as arguments to be passed to main(), so whether argv[] should only be arrays of pointers to chars or whether can it be simply an integer array???
    So whether main() function like this:
    Code:
    int main(int argc,long int argv[])
    is ok??

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by rakeshkool27 View Post
    My question is regarding the use of command line arguments. As we know that argc and argv[] can be used as arguments to be passed to main(), so whether argv[] should only be arrays of pointers to chars or whether can it be simply an integer array???
    So whether main() function like this:
    Code:
    int main(int argc,long int argv[])
    is ok??
    No..

  3. #3
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by EVOEx View Post
    No..
    so you mean to say main() arguments are good only with character strings.
    So this means we cannot pass an integer in the form of arguments.
    Actually I was just trying out to find the factorial by command line arguments rather than giving the input at runtime,but using "long int argv" still gave an answer.....

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Any kind of alphanumeric data can be in a character string, including integers.
    Code:
    char string[]="666";
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by MK27 View Post
    Any kind of alphanumeric data can be in a character string, including integers.
    Code:
    char string[]="666";
    although the integers can be passed as strings they can't be processed as integers in the program.
    How to pass an integer number to main function via command line,so that i can find the factorial of the integer value???

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    cmd: program_name 63 14 18 hello i am a string

    Code:
    int main (int argc, char *argv[]){
    
    int num = atoi(argv[1]);
    printf("%d",num);
    return 0;
    }
    result : 63

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    A few other ways:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, const char *argv[]) {
    	char string[]="666";
    	int n1, n2;
    
    // option 1
    	n1 = (int)strtol(string,NULL,0);
    // option 2
    	sscanf(string, "%d", &n2);
    
    	printf("%d %d\n",n1,n2);
    
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by mushy View Post
    cmd: program_name 63 14 18 hello i am a string

    Code:
    int main (int argc, char *argv[]){
    
    int num = atoi(argv[1]);
    printf("%d",num);
    return 0;
    }
    result : 63
    hmmmm.....quite a nice idea to work with!!!!! thanks , finally got a way to deal with

  9. #9
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by MK27 View Post
    A few other ways:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, const char *argv[]) {
    	char string[]="666";
    	int n1, n2;
    
    // option 1
    	n1 = (int)strtol(string,NULL,0);
    // option 2
    	sscanf(string, "%d", &n2);
    
    	printf("%d %d\n",n1,n2);
    
    	return 0;
    }
    hey MK27, i got your logic behind it.......
    but couldn't understand strtol() function.....since i never found it before?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should read the manual on strtol, by typing "man strtol" into your terminal, or Google, or your C textbook.

  11. #11
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    A few other ways:
    Surely you cannot forget atoi().

    rakeshkool27, documentation is a wonderful thing.

    strtol - C++ Reference

    Damn, beaten to it.

  12. #12
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Thanks all of you guys, You people are really C-worm totally addicted to C. Everytime I come here I get to learn something here.....its very nice!!!! I hope that someday I shall also become a C expert like you people.....
    hey I also want to tell you people something out of topic....somedays ago I went through a computer magazine in which it mentioned about the replacing of silicon by graphene-a carbon composition substance having multiple states unlike the silicon having two states. So if it replaces silicon whether the computer language shall be affected or shall it be the same??? any idea about it????

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think the only thing that would effect language design is if the nature of memory changed. I think I have heard of ideas and experiments to do with non-electrical "transistors" (they are actually molecular/chemical) that can have non-binary states. I have no idea where that is supposed to go tho.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by MK27 View Post
    I think the only thing that would effect language design is if the nature of memory changed. I think I have heard of ideas and experiments to do with non-electrical "transistors" (they are actually molecular/chemical) that can have non-binary states. I have no idea where that is supposed to go tho.
    So there should not be any great harm to the existence of 'C'....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is argv[] terminated by a NULL string?
    By dwks in forum C Programming
    Replies: 9
    Last Post: 07-24-2005, 11:24 AM
  2. when i would use char *argv[] and when char** argv[] ?
    By blue_gene in forum C Programming
    Replies: 17
    Last Post: 04-14-2004, 08:00 PM
  3. How do we cast an argv[]
    By Abdi in forum C Programming
    Replies: 10
    Last Post: 06-07-2002, 12:39 AM
  4. void main(), int main(), argc, argv[]????
    By Jonny M in forum C Programming
    Replies: 3
    Last Post: 03-06-2002, 09:12 AM
  5. How to use int argc, char* argv[] ?
    By gogo in forum C++ Programming
    Replies: 3
    Last Post: 11-21-2001, 01:21 PM

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