%2.f が何を表しているのか教えてください。

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
hanya841

%2.f が何を表しているのか教えてください。

#1

投稿記事 by hanya841 » 8年前

#include <stdio.h>
#define NINZU 5 //NINZU=5に置換
#define KYOKA 3 //KYOKA=3に置換
int main(void)
{
int score[NINZU][KYOKA]={{87,78,91} //{}の数=NINZU=5
,{86,67,89},{78,82,90},{65,55,71},{69,82,88}
}; //{}内の要素数=KYOKA=3
int i,j,sum; //変数 i,j,sumの宣言
//(1)学生ごとの平均点を求める
for(i=0;i<NINZU;i++){
sum=0;
for(j=0;j<KYOKA;j++)
sum+=score[j];
printf("%d番目の学生の平均点は%2.f\n",i+1,(double)sum/KYOKA);
}
//(2)教科ごとの平均点を求める
for(j=0;j<KYOKA;j++){
sum=0;
for(i=0;i<NINZU;i++)
sum+=score[j];
printf("%d番目の教科の平均点は%.2f\n",j+1,(double)sum/KYOKA);
}
}

    -実行結果-
1番目の学生の平均点は85
2番目の学生の平均点は81
3番目の学生の平均点は83
4番目の学生の平均点は64
5番目の学生の平均点は80
1番目の教科の平均点は128.33
2番目の教科の平均点は121.33
3番目の教科の平均点は143.00

超初級者
記事: 56
登録日時: 9年前

Re: %2.f が何を表しているのか教えてください。

#2

投稿記事 by 超初級者 » 8年前

私もわかりません。
%.2f
ではないか、と。

アバター
びす
記事: 31
登録日時: 12年前

Re: %2.f が何を表しているのか教えてください。

#3

投稿記事 by びす » 8年前

※コードを貼り付ける場合はcodeタグで囲ってください。

それはフォーマット指定子と呼ばれるもので
%2.f はfloatを2桁まで表示する、という意味だと思います。
%.2f はfloatを小数点以下2桁まで表示する、みたいです。。

参考:フォーマット指定子一覧

hanya841

Re: %2.f が何を表しているのか教えてください。

#4

投稿記事 by hanya841 » 8年前

回答してくださってありがとうございます。 プログラミング学習の手助けとなりました。

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: %2.f が何を表しているのか教えてください。

#5

投稿記事 by みけCAT » 8年前

% : 変換指定子の始まり
2 : フィールド幅 (少なくとも2文字を使って出力する)
. : 精度の指定 (整数が省略されているので0とみなされ、0なので小数点と小数点以下の桁は出力しない)
f : double型のデータを十進数で出力する

という意味です。

N1570 7.21.6.1 The fprintf functionより引用
4 Each conversion specification is introduced by the character %. After the %, the following
appear in sequence:

— Zero or more flags (in any order) that modify the meaning of the conversion
specification.

— An optional minimum field width. If the converted value has fewer characters than the
field width, it is padded with spaces (by default) on the left (or right, if the left
adjustment flag, described later, has been given) to the field width. The field width
takes the form of an asterisk * (described later) or a nonnegative decimal integer. 275)

— An optional precision that gives the minimum number of digits to appear for the d, i,
o, u, x, and X conversions, the number of digits to appear after the decimal-point
character for a, A, e, E, f, and F conversions, the maximum number of significant
digits for the g and G conversions, or the maximum number of bytes to be written for
s conversions. The precision takes the form of a period (.) followed either by an
asterisk * (described later) or by an optional decimal integer; if only the period is
specified, the precision is taken as zero. If a precision appears with any other
conversion specifier, the behavior is undefined.

— An optional length modifier that specifies the size of the argument.

— A conversion specifier character that specifies the type of conversion to be applied.

(中略)

8 The conversion specifiers and their meanings are:

(中略)

f,F A double argument representing a floating-point number is converted to
decimal notation in the style [−]ddd.ddd, where the number of digits after
the decimal-point character is equal to the precision specification. If the
precision is missing, it is taken as 6; if the precision is zero and the # flag is
not specified, no decimal-point character appears. If a decimal-point
character appears, at least one digit appears before it. The value is rounded to
the appropriate number of digits.
N1570 7.21.6.3 The printf functionより引用
2 The printf function is equivalent to fprintf with the argument stdout interposed
before the arguments to printf.
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

box
記事: 2002
登録日時: 13年前

Re: %2.f が何を表しているのか教えてください。

#6

投稿記事 by box » 8年前

hanya841 さんが書きました: #define NINZU 5 //NINZU=5に置換
#define KYOKA 3 //KYOKA=3に置換
int i,j,sum; //変数 i,j,sumの宣言
これらのような、ソースコードを見ればだれでもわかるようなコメントは不要だと思います。
バグのないプログラムはない。
プログラムは思ったとおりには動かない。書いたとおりに動く。

閉鎖

“C言語何でも質問掲示板” へ戻る