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言語初心者の方

キーワード

  • 変数

  • 演算子

説明

変数と型

変数 : 値を記憶しておく入れ物です。値を変更することができます。

: 変数を作るときにどのような値を格納するかあらかじめ指定します。それを型と呼びます。

#include <stdio.h>
int main(void) {
    int a = 100; // 型はintの変数aを定義し、初期値として100を代入
    printf("a = %d", a);
    return 0;
}
a = 100

主な数値型は下記のとおりです。

バイト数 用途
int 4バイト(または2バイト) 整数
float 4バイト 小数
double 8バイト 小数
#include <stdio.h>
int main(void) {
    int a = 50;
    float b = 10.2;
    double c = 123.456789;
    printf("a = %d, b = %f, c = %lf", a, b, c); // 変換指定子に注意
    return 0;
}
a = 50, b = 10.200000, c = 123.456789

int型には修飾子をつけることができます。

修飾子 説明 備考
signed 符号つき 省略可
unsigned 符号なし
short 2バイトにする
long 4バイトにする
#include <stdio.h>
int main(void) {
    // 変換指定子に注意
    int i = 100;
    printf("%d\n", i);
    unsigned int ui = 200;
    printf("%u\n", ui);
    short s = 300;
    printf("%d\n", s);
    unsigned short us = 400;
    printf("%u\n", us);
    long l = 500;
    printf("%ld\n", l);
    unsigned long ul = 600;
    printf("%lu\n", ul);
    return 0;
}
100
200
300
400
500
600

バイト数は表現できる数の大きさに影響します。
バイト数が大きいほど格納できる数値の最大値が大きくなります。

1ビットは0,1の2通り。1バイト = 8ビットです。

整数の場合、1バイトで $2^8$ 通りの数を表現できます。
例えば、int(4バイト)であれば $2^{32} = 4294967296$ なので、

  • signedなら -2,147,483,648 ~ 2,147,483,647
  • unsignedなら 0 ~ 4,294,967,295

となります。

小数の場合、 浮動小数点 という方式で管理されています。
詳細は省略しますが、おおよそ下記の範囲の値を扱うことができます。

  • float

    • $-3.4 \times 10^{38} ~~~ 3.4 \times 10^{38}$
    • 有効数字6桁

  • double

    • $-1.7 \times 10^{308} ~~~ 1.7 \times 10^{308}$
    • 有効数字14桁

計算しよう

演算子を使用して様々な計算をすることができます。

演算子 用途
+ 足し算
- 引き算
* 掛け算
/ 割り算
% 割り算のあまり(整数のみ)
#include <stdio.h>
int main(void) {
    int a = 100, b = 3;
    printf("%d + %d = %d\n", a, b, a + b);
    printf("%d - %d = %d\n", a, b, a - b);
    printf("%d * %d = %d\n", a, b, a * b);
    printf("%d / %d = %d\n", a, b, a / b);
    printf("%d %% %d = %d\n", a, b, a % b);
    float c = 100.0, d = 3.0;
    printf("%f + %f = %f\n", c, d, c + d);
    printf("%f - %f = %f\n", c, d, c - d);
    printf("%f * %f = %f\n", c, d, c * d);
    printf("%f / %f = %f\n", c, d, c / d);
    return 0;
}
100 + 3 = 103
100 - 3 = 97
100 * 3 = 300
100 / 3 = 33
100 % 3 = 1
100.000000 + 3.000000 = 103.000000
100.000000 - 3.000000 = 97.000000
100.000000 * 3.000000 = 300.000000
100.000000 / 3.000000 = 33.333332

上で確認した通り、下記の点に注意が必要です。

  • 整数の割り算は切り捨てになる

  • 小数の計算結果には誤差が含まれることがある

練習

1. 増量された商品の内容量は?

aグラムの商品がb%増量されている。内容量は何キログラムだろうか?

1200(g)の20%増量は1.440000(kg)です!

ポイント

整数と小数の演算は小数になります。

解答例

#include <stdio.h>
int main(void) {
    int a = 1200;
    int b = 20;
    float result = a * (b / 100.0 + 1) / 1000;
    printf("%d(g)の%d%%増量は%f(kg)です!", a, b, result);
    return 0;
}
1200(g)の20%増量は1.440000(kg)です!

2. 利益を求めよう

会社Aの今年の売上は40億2500万円、仕入は16億7380万円でした。利益を求めよう。

会社Aの今年の利益は2351200000円です!

ポイント

最大値を超える値を扱うとオーバーフローを起こし不正な値になってしまいます。

解答例

#include <stdio.h>
int main(void) {
    unsigned int uriage = 4025000000;
    unsigned int siire = 1673800000;
    unsigned int rieki = uriage - siire;
    printf("会社Aの今年の利益は%u円です!", rieki);
    return 0;
}
会社Aの今年の利益は2351200000円です!

「万円」を基準にしてもよさそうです。

#include <stdio.h>
int main(void) {
    int uriage = 402500;
    int siire = 167380;
    int rieki = uriage - siire;
    printf("会社Aの今年の利益は%d0000円です!", rieki);
    return 0;
}
会社Aの今年の利益は2351200000円です!

3. おつかいに行こう

a円渡された。b円の商品を買えるだけ買うとき、いくつ買えていくら余るだろうか?

10000円で398円の商品を25個買えて50円余ります!

ポイント

整数同士の割り算の特性が便利です。

解答例

#include <stdio.h>
int main(void) {
    int a = 10000;
    int b = 398;
    printf("%d円で%d円の商品を%d個買えて%d円余ります!", a, b, a / b, a % b);
    return 0;
}
10000円で398円の商品を25個買えて50円余ります!

おわりに

想定外のバグは整数同士の演算だったりオーバーフローだったりします。 % の演算は便利で好きですが、日常生活であまり伝わらないのがちょっと悲しいですね。

参考文献

↓↓↓ はじめてプログラミングを学んだときに読んだ本です ↓↓↓
詳細(プログラミング入門 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?