様々な実現 〜道は沢山有る〜

アステリスク「*」を10個表示する(縦編へ

1.まずはオーソドックスに


/* アステリスク「*」を10個表示する */
#include <stdio.h>	/* printfを使うためのおまじない */

main()
{
	printf("**********");	/* printfは文字列を表示する関数 */
}				/* 文字列は " で囲む */
/* end of ways11.c */

2.行数を減らしてみる


/* アステリスク「*」を10個表示する:一行版 */
#include <stdio.h>main(){printf("**********");}
/* end of ways12.c */

3.一個ずつ表示してみる


/* アステリスク「*」を10個表示する:一文字ずつ */
#include <stdio.h>	/* putcharを使うためのおまじない */

main()
{
	putchar('*');	/* putcharは文字を表示する関数 */
	putchar('*');	/* 文字は ' で囲む */
	putchar('*');
	putchar('*');
	putchar('*');
	putchar('*');
	putchar('*');
	putchar('*');
	putchar('*');
	putchar('*');
}
/* end of ways13.c */

4.記号定数(#define)を使ってみる


/* MOJI(プラス「+」)を10個表示する:記号定数版 */
#include <stdio.h>
#define	MOJI	'+'	/* 以下の MOJI という識別子(名前)を '+' で置き換える */

main()
{
	putchar(MOJI);
	putchar(MOJI);
	putchar(MOJI);
	putchar(MOJI);
	putchar(MOJI);
	putchar(MOJI);
	putchar(MOJI);
	putchar(MOJI);
	putchar(MOJI);
	putchar(MOJI);
}
/* end of ways14.c */

5.変数を使ってみる


/* moji(プラス「+」)を10個表示する:変数版 */
#include <stdio.h>

main()
{
	char	moji = '+';	/* 表示する文字を保持する変数 */

	putchar(moji);
	putchar(moji);
	putchar(moji);
	putchar(moji);
	putchar(moji);
	putchar(moji);
	putchar(moji);
	putchar(moji);
	putchar(moji);
	putchar(moji);
}
/* end of ways15.c */

6.ループ構造(for)を使ってみる(縦版との比較


/* アステリスク「*」を10個表示する:ループ版 */
#include <stdio.h>

main()
{
	int	i;	/* 回数を数えるカウンタ */

	for( i = 0; i < 10; i++ ) /* i が 0 から 9 までの 10 回繰り返す */
		putchar('*');
}
/* end of ways16.c */

/* アステリスク「*」を NUM 個表示する:ループ版 */
#include <stdio.h>
#define	NUM	10	/* この 10 を変更すれば良い */

main()
{
	int	i;	/* 回数を数えるカウンタ */

	for( i = 0; i < NUM; i++ ) /* i が 0 から NUM-1 までの NUM 回繰り返す */
		putchar('*');
}
/* end of ways17.c */

今度はアステリスク「*」を縦に10個表示する

1.まずはオーソドックスに


/* アステリスク「*」を縦に10個表示する */
#include <stdio.h>

main()
{
	printf("*\n*\n*\n*\n*\n*\n*\n*\n*\n*\n");
}
/* end of ways21.c */

2.一文字ずつ表示してみる


/* アステリスク「*」を縦に10個表示する:一文字ずつ */
#include <stdio.h>

main()
{
	putchar('*');
	putchar('\n');
	putchar('*');
	putchar('\n');
	putchar('*');
	putchar('\n');
	putchar('*');
	putchar('\n');
	putchar('*');
	putchar('\n');
	putchar('*');
	putchar('\n');
	putchar('*');
	putchar('\n');
	putchar('*');
	putchar('\n');
	putchar('*');
	putchar('\n');
	putchar('*');
	putchar('\n');
}
/* end of ways22.c */

3.一行ずつ表示してみる


/* アステリスク「*」を縦に10個表示する:一行ずつ */
#include <stdio.h>	/* putsを使うためのおまじない */

main()
{
	puts("*");
	puts("*");
	puts("*");
	puts("*");
	puts("*");
	puts("*");
	puts("*");
	puts("*");
	puts("*");
	puts("*");
}
/* end of ways23.c */

4.記号定数(#define)を使ってみる


/* MSG("+")を10回表示する:記号定数版 */
#include <stdio.h>
#define	MSG	"+"	/* 以下の MSG という識別子(名前)を "+" で置き換える */

main()
{
	puts(MSG);
	puts(MSG);
	puts(MSG);
	puts(MSG);
	puts(MSG);
	puts(MSG);
	puts(MSG);
	puts(MSG);
	puts(MSG);
	puts(MSG);
}
/* end of ways24.c */

5.変数を使ってみる


/* msg("+")を10回表示する:配列変数版 */
#include <stdio.h>

main()
{
	char	msg[] = "+";	/* 表示内容を保持する配列 */

	puts(msg);
	puts(msg);
	puts(msg);
	puts(msg);
	puts(msg);
	puts(msg);
	puts(msg);
	puts(msg);
	puts(msg);
	puts(msg);
}
/* end of ways25.c */

5'.変数を使ってみる


/* msgp("+")を10回表示する:ポインタ変数版 */
#include <stdio.h>

main()
{
	char	*msgp = "+";	/* 表示内容を指すポインタ */

	puts(msgp);
	puts(msgp);
	puts(msgp);
	puts(msgp);
	puts(msgp);
	puts(msgp);
	puts(msgp);
	puts(msgp);
	puts(msgp);
	puts(msgp);
}
/* end of ways26.c */

6.ループ構造(for)を使ってみる(横版との比較


/* アステリスク「*」を10個表示する:ループ版 */
#include <stdio.h>

main()
{
	int	i;	/* 回数を数えるカウンタ */

	for( i = 0; i < 10; i++ ) /* i が 0 から 9 までの 10 回繰り返す */
	{	/* 複数行になるときは { } で囲んでブロックにする */
		putchar('*');
		putchar('\n');
	}
}
/* end of ways27.c */

/* アステリスク「*」を NUM 個表示する:ループ版 */
#include <stdio.h>
#define	NUM	10	/* この 10 を変更すれば良い */

main()
{
	int	i;	/* 回数を数えるカウンタ */

	for( i = 0; i < NUM; i++ ) /* i が 0 から NUM-1 までの NUM 回繰り返す */
	{	/* 複数行になるときは { } で囲んでブロックにする */
		putchar('*');
		putchar('\n');
	}
}
/* end of ways28.c */

今度は斜めにチャレンジ!