2012-03-17 46 views
18

は、私はこのようなコードの一部を持っているargv []をintとしてどのように取得できますか?私はこれを行う</p> <pre><code>int main (int argc, char *argv[]) { printf("%dt",(int)argv[1]); printf("%st",(int)argv[1]); } </code></pre> <p>とシェルで:

./test 7

が、最初のprintfの結果は、私が[] ARGVを得ることができますどのように、7ではありませんintとして?多くのおかげで

+3

私はcnicutarがすでに答えを出したことを知っていますが、何が起こっているのか理解するのに役立ちます。 argv [1]が正確に何であるか知っていますか?つまり、あなたが得た成果を得た理由を説明できますか? –

+0

私の意見では、argv [0]はコマンドそのものであり、argv [1]は最初に入力するパラメータです。この場合は7です。 – Michael

+0

はい、そうですが、それは何ですか?どんなタイプ?あなたはそれで何ができますか?上記の例で得た成果はどうして得られますか?あなたはそれを加えたり乗算したりすることができますか?ユーザーが数字を入力していないのに './テスト7 'なんかか? –

答えて

23

argv[1]は、文字列へのポインタです。

あなたは使用して印刷することができます。printf("%s\n", argv[1]);

はあなたがそれを変換するために、最初の必要があり、文字列から整数を取得するには。文字列をintに変換するには、strtolを使用します。

#include <errno.h> // for errno 
#include <limits.h> // for INT_MAX 
#include <stdlib.h> // for strtol 

char *p; 
int num; 

errno = 0; 
long conv = strtol(argv[1], &p, 10); 

// Check for errors: e.g., the string does not represent an integer 
// or the integer is larger than int 
if (errno != 0 || *p != '\0' || conv > INT_MAX) { 
    // Put here the handling of the error, like exiting the program with 
    // an error message 
} else { 
    // No error 
    num = conv;  
    printf("%d\n", num); 
} 
+0

多くのありがとう。私はstrtol関数を使用してビルドに成功しましたが、EXC_BAD_ACCESSの問題が発生しました。プロジェクトビルド時にargv [1]が存在しないためです。 – Michael

+0

strtol関数の10はどういう意味ですか? –

+4

@AlejandroSazoこれは変換の基底ですので、変換はベース '10'で行われます。 – ouah

12

は、あなたはそのためstrtolを使用することができます。また

long x; 
if (argc < 2) 
    /* handle error */ 

x = strtol(argv[1], NULL, 10); 

、あなたはC99以上を使用している場合は、strtoimaxを探ることができます。

+0

ありがとうございます、それは非常に便利です。 – Michael

6

"string to long"(strtol)関数がこれの標準です。基本的な使い方:我々は十進法を使用しているので

#include <stdlib.h> 

int arg = strtol(argv[1], NULL, 10); 
// string to long(string, endptr, base) 

、ベースは10です。endptr引数は、「最初の無効な文字」、すなわち、最初の非数字に設定されます。気にしない場合は、ポインターを渡す代わりに、引数をNULLに設定します。

#include <stdlib.h> 

char* p; 
int arg = strtol(argv[1], &p, 10); 
if (*p != '\0') // an invalid character was found before the end of the string 

man pageは述べているように、あなたがerrnoにを使用することができます:あなたは数字以外が発生しない場合、あなたは確かにそれは(\0はCの文字列を終了)「ヌルターミネータ」に設定されますすることができますエラーが発生していないことを確認します(この場合、オーバーフローまたはアンダーフロー)。

#include <stdlib.h> 
#include <errno.h> 

char* p; 
errno = 0; 
int arg = strtol(argv[1], &p, 10); 
if (*p != '\0' || errno != 0) return 1; 

// Everything went well 
printf("%d", arg); 

これ以外にも、カスタムチェックを実装できます。ユーザーが引数を渡したかどうかをテストします。その数が許容範囲内であるかどうかをテストする。

+0

受け入れられた答えよりよく説明されました。少なくとも、私のような初心者のために。 – Dinei

3

機能int atoi (const char * str);を使用できます。必要であれば、ここではより多くの情報を
int x = atoi(argv[1]);
:あなたは#include <stdlib.h>が含まれており、このように機能を使用する必要が
atoi - C++ Reference

0
/* 

    Input from command line using atoi, and strtol 
*/ 

#include <stdio.h>//printf, scanf 
#include <stdlib.h>//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); 

} 

は、この情報がお役に立てば幸いです。がんばろう!

関連する問題