How can I get argv[] as int?

前端 未结 5 1867
离开以前
离开以前 2020-12-08 09:31

i have a piece of code like this:

int main (int argc, char *argv[]) 
{
   printf(\"%d\\t\",(int)argv[1]);
   printf(\"%s\\t\",(int)argv[1]);
}
5条回答
  •  盖世英雄少女心
    2020-12-08 10:04

    /*
    
        Input from command line using atoi, and strtol 
    */
    
    #include //printf, scanf
    #include //atoi, strtol 
    
    //strtol - converts a string to a long int 
    //atoi - converts string to an int 
    
    int main(int argc, char *argv[]){
    
        char *p;//used in strtol 
        int i;//used in for loop
    
        long int longN = strtol( argv[1],&p, 10);
        printf("longN = %ld\n",longN);
    
        //cast (int) to strtol
        int N = (int) strtol( argv[1],&p, 10);
        printf("N = %d\n",N);
    
        int atoiN;
        for(i = 0; i < argc; i++)
        {
            //set atoiN equal to the users number in the command line 
            //The C library function int atoi(const char *str) converts the string argument str to an integer (type int).
            atoiN = atoi(argv[i]);
        }
    
        printf("atoiN = %d\n",atoiN);
        //-----------------------------------------------------//
        //Get string input from command line 
        char * charN;
    
        for(i = 0; i < argc; i++)
        {
            charN = argv[i];
        }
    
        printf("charN = %s\n", charN); 
    
    }
    

    Hope this helps. Good luck!

提交回复
热议问题