0
0

More than 1 year has passed since last update.

c言語で%を表示するには

Last updated at Posted at 2023-07-31

湿度を表示してみましょう。

#include <stdio.h>

int main(void) {
    
    int x = 10;

    printf("今日の湿度は%d%です\n", x);

    return 0;
}

このように書くと%d%のところでエラーが出てしまいます。

このエラーの解決策は下記のように%を%%とすることです。

#include <stdio.h>

int main(void) {
    
    int x = 10;

    printf("今日の湿度は%d%%です\n",x);

    return 0;
}

基本ではありますが、しばらくc言語から離れていると忘れがちな記法だと思います。
似たような例題とその解答を下に置いておきます。
間違いがあればコメントしていただけると嬉しいです。

<例題1>
二つの整数値を読み込んで、前者の値が後者の値の何%であるかを整数で表示するプログラムを作成せよ。

#include <stdio.h>

int main(void) {
    
    int x, y;

    puts("二つの整数を入力してください。");
    printf("整数x:"); scanf("%d", &x);
    printf("整数y:"); scanf("%d", &y);

    printf("xの値はyの%d%%です\n", x * 100/ y);

    return 0;
}
0
0
1

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