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?

はじめに

この記事について

「C言語の基礎を学ぼう」をテーマに、自身の知識 + α をアドベントカレンダーにまとめます。
25日間でC言語をマスターしよう - Qiita Advent Calendar 2025 - Qiita

こんな方を対象としています

  • コンピュータがプログラムをどのように動かしているか知りたい/知らない方

  • プログラミングをしてみたい方

  • C言語初心者の方

キーワード

  • コマンドライン引数

説明

Code Runnerがしていること

プログラムを作成した後、実行するために必要なことは2つあります。

  1. プログラムをコンパイルし、実行ファイルを作成する
  2. 実行ファイルを実行する

これらはコマンドで実行され、それぞれ次のようなコマンドです。

gcc ファイル名 -o 実行ファイル名
実行ファイル名

1日目の環境構築 + powershellを使用しているしている場合、下記のようなコマンドをCode Runnerが実行していると思います。
-fexec-charset=CP932 は文字コードのオプションです)
(コマンドプロンプトであれば .\example ではなく example で実行可能です)

gcc -fexec-charset=CP932 example.c -o example
.\example

Code Runnerは2つのコマンドをワンクリックで実行してくれています。
ありがとう Code Runner。

コマンドライン引数

実行ファイルの実行時( .\example 実行時 )に値を渡すことができます。

今までmain関数の引数に void を指定していました。
これを変更します。

int main(int argc, char *argv[])

argc argv のことを コマンドライン引数 といいます。
コマンドライン引数の変数名は argc argv が広く使われています。
argc argv 以外でも動きます)

それぞれ下記のような値が入ります。

  • argc : 渡された引数の数
  • argv : 渡された引数

実際に見てみましょう。

#include <stdio.h>
int main(int argc, char *argv[]) {
    int i = 0;
    printf("argc: %d\n", argc);
    for (i = 0; i < argc; i++) {
        printf("argv[%d]: %s\n", i, argv[i]);
    }
    return 0;
}

下記コマンドでコンパイルします。

gcc example.c -o example

下記コマンドで実行します。

.\example

結果は下記のようになりました。

argc: 1
argv[0]: C:\workspace\c\example.exe

もう一度実行します。今度はパラメータを追加します。

.\example apple banana coffee

結果は下記のようになりました。

argc: 4
argv[0]: C:\workspace\c\example.exe
argv[1]: apple
argv[2]: banana
argv[3]: coffee

argc argv どちらもパラメータだけでなく、実行ファイル名が含まれることに注意が必要です。

練習

1. コマンドを作ろう

ランダムな英単語を作成するコマンド randword を作成しよう。

  • 形式: randword [-U] <数値 (1-100)>
  • 1~10文字のランダムな英小文字列を出力する。
  • -U :任意。指定された場合大文字で出力する。
  • 数値 (1-100):必須。指定された数だけ英単語を出力する。
  • 正しくない形式での実行は考慮しなくてもよい。
PS C:\workspace\c> .\example 3
dgl
ldfhrqrfd
xvawfx
PS C:\workspace\c> .\example -U 4
MZFINTHZU
JJNTRPW
G
M

ポイント

コマンドライン引数を受け取ります。
乱数は今までの記事と同様に rand() を使用します。
文字列から数値への変換には stdlib.hatoi() が便利です。

#include <stdio.h>
#include <stdlib.h>
int main(void) {
    char *str = "100";
    int num = atoi(str);
    printf("%d", num);
    return 0;
}
100

解答例

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[]) {
    char base_char = 'a';
    int word_cnt = 0, i = 0, j = 0;
    srand((unsigned int)time(NULL));
    // オプションの確認
    if (argv[1][0] == '-' && argv[1][1] == 'U') {
        base_char = 'A';
        word_cnt = atoi(argv[2]);
    } else {
        word_cnt = atoi(argv[1]);
    }
    // 出力処理
    for (i = 0; i < word_cnt; i++) {
        int length = rand() % 10 + 1;
        // 英単語生成
        for (j = 0; j < length; j++) {
            printf("%c", base_char + rand() % 26);
        }
        printf("\n");
    }
    return 0;
}
PS C:\workspace\c> .\example 3
dgl
ldfhrqrfd
xvawfx
PS C:\workspace\c> .\example -U 4
MZFINTHZU
JJNTRPW
G
M

おわりに

コマンドライン引数を使うことでコマンドを自作できます。
こういった身近なものを作れるようになると 嬉C && 楽C ですね。

参考文献

↓↓↓ はじめてプログラミングを学んだときに読んだ本です ↓↓↓
詳細(プログラミング入門 C言語)|プログラミング|情報|実教出版

0
0
0

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?