エンジニアとしての市場価値を測りませんか?PR

企業からあなたに合ったオリジナルのスカウトを受け取って、市場価値を測りましょう

0
1

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 5 years have passed since last update.

dockerでさくっとC言語の hello world

Posted at

大学ぶりにC言語をやってみたいな〜と思い、dockerでサックとgccの環境を構築してみました。

gccのイメージが入ったコンテナを起動します。イメージを持っていない場合はdockerが自動でimageをプルしてくれます。

docker run -v "$PWD":/usr/src/myapp -w /usr/src/myapp -it gcc:4.9

# -v ローカルのvolumeをコンテナにマウント
# -w コンテナのworkdirを定義
# -it コンテナにログイン

早速c言語でハローワールド

app.c
# include <stdio.h>
# include <stdlib.h>

int main(int argc, char *argv[])
{
    printf("Hello World!\n");
    printf("ハローワールド!\n");

    return 0;
}

app.cをコンパイルし、実行ファイルを実行

$ gcc app.c -o a.out 
$ ./a.out

# 出力結果
# Hello World!
# ハローワールド!

番外編

stdlib.h + argv を使ったサンプル

app.c
# include <stdio.h>
# include <stdlib.h>

int main(int argc, char *argv[])
{
    int i, result = 0;
    
    for (i = 1; i < argc; ++i)
    {
        printf("argv[%d] = %s\n", i, argv[i]);

        result += atoi(argv[i]);
    }

    printf("Total Sum = %d\n", result);

    return 0;
}

実行結果

$ gcc app.c -o a.out 
$ ./a.out 1 2 3

# 出力結果
# argv[1] = 1
# argv[2] = 2
# argv[3] = 3
# Total Sum = 6
0
1
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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?