0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【C言語】コマンドライン引数に入力した文字列を一文字ずつ取り出す方法

Last updated at Posted at 2021-06-02

はじめに

コマンドライン引数に入力した文字を一文字ずつ取り出すコードを書きたかった時に、なかなかネットでサンプルコードを見つけられず、3日ほど悩んだ挙句に有識者に教えていただいたので忘れないように記事にします。

コード

qiita.rb
# include <stdio.h>
# include <string.h>

int main(int argc, char const *argv[])
{
    for (int i = 0; i < argc; i++)
    {
        printf("%s\n", argv[i]);
        for (int j = 0; j < strlen(argv[i]); j++)
        {
            char c = *(argv[i] + j);
            printf("%c\n", c); 
        }
    }
    return 0;
}

ターミナルでは、以下のように出力されます。

qiita.rb
% ./test "abc def"  
./test
.
/
t
e
s
t
abc def
a
b
c
 
d
e
f

最後に

もしコードなどで間違っている箇所などがありましたらご指摘ください。
ポインタ配列難しかったです。。

0
0
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?