Download : sample-001.c
/*
* 2017/09/22 sample-001.c
*/
/*
* Hello, World
*
* 何時も、始まりはここから...
*
* 利用方法
* コンパイル
* cc -o sample-001.exe sample-001.c
* 実行
* ./sample-001.exe
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
printf ( "Hello, World\n" );
return 0;
}
$ ./sample-001.exe Hello, World $
Download : sample-002.c
/*
* 2017/09/22 sample-002.c
*/
/*
* n^2 の計算を足し算だけで実現する
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-002.exe sample-002.c
* 実行
* ./sample-002.exe
*/
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
/*
* 四角数 http://ja.wikipedia.org/wiki/%E5%B9%B3%E6%96%B9%E6%95%B0
*/
/*
* main
*/
int main ( int argc, char *argv[] ) {
int n = 13; /* n=13 として n^2 = n*n = 13*13 = 169 を計算する */
int i = 0; /* i は、0 から n-1 まで動き、足し算の回数を示す */
int sum = 0; /* sum には、個々の odd の値が足しこまれる。最初は 0 */
int odd = 1; /* odd は、最初は 1 で、以後 2 ずつふえる(奇数) */
while ( i < n ) { /* i が n より小さい間 */
sum = sum + odd; /* sum に現在の奇数を加える */
odd = odd + 2; /* 次の奇数は、今の奇数に 2 を加えればよい */
i = i + 1; /* 何回加えたかを計数し、繰返し回数を確認する */
}
s_print_int ( sum ); /* 加えた結果が表示される n^2 になっているはず.. */
s_print_newline();
return 0;
}
$ ./sample-002.exe 169 $
Download : sample-003.c
/*
* 2017/09/22 sample-003.c
*/
/*
* printf の書式付き出力
*
* 利用方法
* コンパイル
* cc -o sample-003.exe sample-003.c
* 実行
* ./sample-003.exe
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
printf ( "5 + 4 = %d\n", 5 + 4 );
/* 文字列の中の「%d」の所に 9 ( = 5 + 4 ) が入る */
return 0;
}
$ ./sample-003.exe 5 + 4 = 9 $
Download : sample-004.c
/*
* 2017/09/22 sample-004.c
*/
/*
* printf の書式付き出力(その他色々)
*
* 利用方法
* コンパイル
* cc -o sample-004.exe sample-004.c
* 実行
* ./sample-004.exe
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
printf ( "%d + %d = %d\n", 5, 4, 5 + 4 );
/*
文字列の中の「%d」の所に
順番に数値を表す文字列が埋め込まれる */
return 0;
}
$ ./sample-004.exe 5 + 4 = 9 $
Download : sample-005.c
/*
* 2017/09/22 sample-005.c
*/
/*
* printf の書式付き出力(その他色々)
*
* 利用方法
* コンパイル
* cc -o sample-005.exe sample-005.c
* 実行
* ./sample-005.exe
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
printf ( "整数型の出力は %%d を使って「%d」と出せる。\n",
123 );
printf ( "浮動小数点数型の出力は %%f を使って「%f」と出せる。\n",
123.456 );
printf ( "文字型の出力は %%c を使って「%c」と出せる。\n",
'Z' );
printf ( "文字列の出力は %%s を使って「%s」と出せる。\n",
"pqr" );
return 0;
}
$ ./sample-005.exe 整数型の出力は %d を使って「123」と出せる。 浮動小数点数型の出力は %f を使って「123.456000」と出せる。 文字型の出力は %c を使って「Z」と出せる。 文字列の出力は %s を使って「pqr」と出せる。 $
Download : sample-006.c
/*
* 2017/09/22 sample-006.c
*/
/*
* printf の書式付き出力(その他色々)
*
* 利用方法
* コンパイル
* cc -o sample-006.exe sample-006.c
* 実行
* ./sample-006.exe
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
printf ( "書式は、色々な形の物のを混在させても良い。\n" );
printf ( "%d, %f, %c, %s\n", 10, 2.3, 'x', "abc" );
return 0;
}
$ ./sample-006.exe 書式は、色々な形の物のを混在させても良い。 10, 2.300000, x, abc $
Download : sample-007.c
/*
* 2017/09/22 sample-007.c
*/
/*
* scanf の書式付き入力
*
* 利用方法
* コンパイル
* cc -o sample-007.exe sample-007.c
* 実行
* ./sample-007.exe
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
int i;
printf ( "整数値を入力してください : " );
scanf ( "%d", &i );
/* 整数型なので「%d」の書式を付ける */
/* 変数 i の前に「&」を付ける(今回は「お呪い」考えてください)*/
printf ( "整数値 %d が入力されました。\n", i );
return 0;
}
123
$ ./sample-007.exe < sample-007.in 整数値を入力してください : 123 整数値 123 が入力されました。 $
Download : sample-008.c
/*
* 2017/09/22 sample-008.c
*/
/*
* scanf の書式付き入力(色々)
*
* 利用方法
* コンパイル
* cc -o sample-008.exe sample-008.c
* 実行
* ./sample-008.exe
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
int i;
double x;
/* 整数 */
printf ( "整数値を入力してください : " );
scanf ( "%d", &i );
/* 整数型なので「%d」の書式を付ける */
/* 変数 i の前に「&」を付ける(今回は「お呪い」と考える)*/
printf ( "整数値 %d が入力されました。\n", i );
/* 浮動小数点数 */
printf ( "浮動小数点数値を入力してください : " );
scanf ( "%lf", &x );
/* 浮動小数点数型なので「%lf」の書式を付ける */
/* 変数 x の前に「&」を付ける(今回は「お呪い」と考える)*/
printf ( "浮動小数点数値 %f が入力されました。\n", x );
return 0;
}
123 456.789
$ ./sample-008.exe < sample-008.in 整数値を入力してください : 123 整数値 123 が入力されました。 浮動小数点数値を入力してください : 456.789000 浮動小数点数値 456.789000 が入力されました。 $
Download : sample-009.c
/*
* 2017/09/22 sample-009.c
*/
/*
* 一文字鸚鵡返しをするプログラム
* キーボードから文字を入力するには、「Enter」を押す必要がある
* # つまり、「'A'の一文字」を入力する場合も「'A'」と[ENTER]のニ文字の入力が必要って事
* # 厄介な事に、もちろん [ENTER] も一文字になる。
*
* 利用方法
* コンパイル
* cc -o sample-009.exe sample-009.c
* 実行
* ./sample-009.exe
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
int ch; /* 文字を扱うのだが、都合により整数型(int 型)を使う */
ch = getchar(); /* 一文字入力する */
putchar ( ch ); /* その文字を出力する */
return 0;
}
A
$ ./sample-009.exe < sample-009.in A A$
Download : sample-010.c
/*
* 2017/09/22 sample-010.c
*/
/*
* 一文字と改行を鸚鵡返しをするプログラム
*
* 利用方法
* コンパイル
* cc -o sample-010.exe sample-010.c
* 実行
* ./sample-010.exe
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
int ch; /* 文字を扱うのだが、都合により整数型(int 型)を使う */
ch = getchar(); /* 一文字入力する */
putchar ( ch ); /* その文字を出力する */
ch = getchar(); /* ニ文字目(恐らく [Enter]) を入力する */
putchar ( ch ); /* その文字を出力する */
return 0;
}
A
$ ./sample-010.exe < sample-010.in A A $
Download : sample-011.c
/*
* 2017/09/22 sample-011.c
*/
/*
* 「終わり」のマークが来るまで鸚鵡返し
*
* 利用方法
* コンパイル
* cc -o sample-011.exe sample-011.c
* 実行
* ./sample-011.exe
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
int ch;
ch = getchar(); /* 最初の一文字入力する */
while ( ch != EOF ) { /* 「入力」が EOF でなければ.. 以下を繰り返す */
/* EOF は「End Of File(ファイルの終わり)」を示す */
/* !! 何故、「キーボードから入力」が「ファイル」なのかは後日 */
/* EOF は文字(コード)ではなく、整数型 */
/* ch を整数型(int 型)にしたのは EOF が入る可能性があるので.. */
/* 間違えて ch を char で宣言すると EOF の判定ができない */
putchar ( ch ); /* 入力された文字を出力する */
ch = getchar(); /* 次の文字を入力する (EOF の可能性もある..) */
}
/* 「ubuntu」で、「キーボード」から「EOF」を入力する場合は
Ctrol-D ( ^d : [Ctrl] キーを押しながら 「D」をポンと押す)
を入力する ( OS や入力先により、操作が異る事が多い .. )
*/
return 0;
}
This is first line. And, it is second line. Last, it is third line.
$ ./sample-011.exe < sample-011.in This is first line. This is first line. And, it is second line. And, it is second line. Last, it is third line. Last, it is third line. $
Download : sample-012.c
/*
* 2017/09/22 sample-012.c
*/
/*
* 鸚鵡返しの fgtec, fputc 版
*
* 利用方法
* コンパイル
* cc -o sample-012.exe sample-012.c
* 実行
* ./sample-012.exe
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
int ch;
ch = fgetc ( stdin ); /* 入力元を stdin (標準入力) にする */
while ( ch != EOF ) { /* 「入力データ」が EOF になるまで */
fputc ( ch, stdout ); /* 出力先を stdout (標準出力) にする */
ch = fgetc ( stdin );
}
return 0;
}
This is first line. And, it is second line. Last, it is third line.
$ ./sample-012.exe < sample-012.in This is first line. And, it is second line. Last, it is third line. $
Download : sample-013.c
/*
* 2017/09/22 sample-013.c
*/
/*
* stdout と stderr の違い
*
* 利用方法
* コンパイル
* cc -o sample-013.exe sample-013.c
* 実行
* ./sample-013.exe
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
printf ( "O: これは標準出力\n" );
/* printf は標準出力に出す */
fprintf ( stderr, "E: これは標準エラー出力\n" );
/* 標準エラー出力に出す場合は、fprintf を用い、stderr を指定する */
fprintf ( stdout, "O: これも標準出力\n" );
/* fprintf で stdout を指定すれば、標準出力に出せる */
fprintf ( stderr, "E: 再び標準エラー出力\n" );
/*
だまっていると、混在して画面に表示される
標準出力をリダイレクトすると、この二つが分離できる
*/
return 0;
}
$ ./sample-013.exe O: これは標準出力 O: これも標準出力 $
Download : sample-014.c
/*
* 2017/09/22 sample-014.c
*/
/*
* fopen の利用
*
* 利用方法
* コンパイル
* cc -o sample-014.exe sample-014.c
* 実行
* ./sample-014.exe
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
FILE *ifp; /* ファイルポインタ変数の宣言 */
ifp = fopen ( "sample-014.c", "r" );
/* sample-014.c を 読み出しモードで開く */
if ( ifp == NULL ) { /* Open に失敗した場合は.. */
fprintf ( stderr, "ファイルのオープンに失敗しました。\n" );
/* エラーメッセージを出力 */
/* 後は何もしない(クローズもしない) */
} else { /* Open に成功した場合のみ利用してよい */
int ch;
ch = fgetc ( ifp ); /* fgetc で、ファイルから読込み */
while ( ch != EOF ) { /* ファイルからでも EOF は「入力終わり」*/
putchar ( ch ); /* 結果は標準出力へ.. */
ch = fgetc ( ifp );
}
fclose ( ifp ); /* 最後に、オープンできた場合はクローズする */
}
return 0;
}
$ ./sample-014.exe
/*
* 2017/09/22 sample-014.c
*/
/*
* fopen の利用
*
* 利用方法
* コンパイル
* cc -o sample-014.exe sample-014.c
* 実行
* ./sample-014.exe
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
FILE *ifp; /* ファイルポインタ変数の宣言 */
ifp = fopen ( "sample-014.c", "r" );
/* sample-014.c を 読み出しモードで開く */
if ( ifp == NULL ) { /* Open に失敗した場合は.. */
fprintf ( stderr, "ファイルのオープンに失敗しました。\n" );
/* エラーメッセージを出力 */
/* 後は何もしない(クローズもしない) */
} else { /* Open に成功した場合のみ利用してよい */
int ch;
ch = fgetc ( ifp ); /* fgetc で、ファイルから読込み */
while ( ch != EOF ) { /* ファイルからでも EOF は「入力終わり」*/
putchar ( ch ); /* 結果は標準出力へ.. */
ch = fgetc ( ifp );
}
fclose ( ifp ); /* 最後に、オープンできた場合はクローズする */
}
return 0;
}
$
Download : sample-015.c
/*
* 2017/09/22 sample-015.c
*/
/*
* fopen の利用
*
* 利用方法
* コンパイル
* cc -o sample-015.exe sample-015.c
* 実行
* ./sample-015.exe
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
FILE *ofp; /* ファイルポインタ変数の宣言 */
ofp = fopen ( "sample-015.txt", "w" );
/* sample-015.txt を 読み出しモードで開く */
/* 元々あった sample-015.txt の内容は上書きされる事に注意 */
if ( ofp == NULL ) { /* Open に失敗した場合は.. */
fprintf ( stderr, "ファイルのオープンに失敗しました。\n" );
/* エラーメッセージを出力 */
/* 後は何もしない(クローズもしない) */
} else { /* Open に成功した場合のみ利用してよい */
int ch;
ch = getchar (); /* キーボード入力 */
while ( ch != EOF ) {
fputc ( ch, ofp ); /* 結果はファイルへ.. */
ch = getchar ();
}
fclose ( ofp ); /* 最後に、オープンできた場合はクローズする */
/*
結果は、画面でなく、sample-015.txt に保存される
*/
}
return 0;
}
Fist Line Sencod Line Last Line
$ ./sample-015.exe < sample-015.in Fist Line Sencod Line Last Line $
#include <stdio.h>
int main(int argc, char *argv[]) {
printf ( "Hello, World\n" );
return 0;
}
/*
* 2017/09/22 20170922-04.c
*
* 変数宣言と代入文
* 整数型変数 ( a, b, c ) を三つ宣言する
* 整数型変数 a, b にそれぞれ、123, 4 を代入する
* 整数型変数 c に a と b の和を代入する
* 整数型変数 c の値を画面に出力する
*/
#include <stdio.h>
#include "s_print.h" /* ソフトウェア概論 A で導入 */
/* 色々な型の値を出力する */
/*
* main
*/
int main( int argc, char *argv[] )
{
/* 整数型変数 a の宣言 */
int a; /* 整数型なので int を先行して、次に変数名 a を書く */
/* 整数型変数 b の宣言 */
int b; /* 変数の型と名前が決まれば、変数宣言できる */
/* 整数型変数 c の宣言 */
int c;
/* 整数型変数 a に 123 を代入 */
a = 123;
/* 整数型変数 b に 4 を代入 */
b = 4; /* 変数と代入したい値が決まれば、
代入文が作れる */
/* 整数型変数 c に、変数 a と 変数 b の和を代入 */
c = a + b; /* 代入文の右辺には、「式」が書ける
まず、この「式」の値が計算(評価)されて、
それが、左辺の変数に「代入」される
*/
/* 整数型変数 c の値を画面に出力 */
s_print_string ( "変数 c の値は " );
s_print_int ( c );
s_print_string ( " です。\n" );
return 0;
}
#include <stdio.h>
int main(int argc, char *argv[]) {
printf ( "Hello, World\n" );
return 0;
}
#include <stdio.h>
int main(int argc, char *argv[]) {
printf ( "Hello, World\n" );
printf ( "Hello, World\n" );
printf ( "Hello, World\n" );
printf ( "Hello, World\n" );
return 0;
}
#include <stdio.h>
#define EOS '\0'
/*
文字列で三角形の形を表示する
*/
void triangle ( char *text ) {
if ( *text != EOS ) { /* 文字列が空でなければ.. */
printf ( text ); /* 文字列を出力する */
triangle ( text + 1 ); /* 文字列を一文字短くして、.. */
} else {
/* なにもしない */
}
}
int main(int argc, char *argv[] ) {
triangle ( "abc\n" );
printf ( "\n" );
triangle ( "123456789\n" );
return 0;
}
#include <stdio.h>
/*
課題:
Hello, World
ello, World
llo, World
という三行を表示したい
*/
int main(int argc, char *argv[]) {
/* 第一案 */
printf ( "Hello, World\n" );
printf ( "ello, World\n" );
printf ( "llo, World\n" );
return 0;
}
#include <stdio.h>
/*
課題:
Hello, World
ello, World
llo, World
という三行を表示したい
*/
int main(int argc, char *argv[]) {
/* 第二案 */
printf ( "Hello, World\n" );
printf ( "Hello, World\n" + 1 ); /* 文字列に 1 を加えると先頭の文字がなくなって一文字分短くなる */
printf ( "Hello, World\n" + 2 );
return 0;
}
#include <stdio.h>
/*
課題:
Hello, World
ello, World
llo, World
という三行を表示したい
*/
/* 第三案 */
void print_three_times ( char *text ) {
printf ( text );
printf ( text + 1 );
printf ( text + 2 );
}
int main(int argc, char *argv[]) {
print_three_times ( "Hello, World\n" );
return 0;
}
#include <stdio.h>
/*
課題:
Hello, World
ello, World
llo, World
という三行を表示したい
*/
/* 第四案 */
void print_two_times ( char *text ) {
/*
text = "Hello, World\n" + 1
= "ello, World\n"
*/
printf ( text );
printf ( text + 1 );
}
void print_three_times ( char *text ) {
/*
text = "Hello, World\n"
*/
printf ( text );
print_two_times ( text + 1 );
}
int main(int argc, char *argv[]) {
print_three_times ( "Hello, World\n" );
/*
print_three_times ( "Hello, World\n" );
=>
print_three_times ( "Hello, World\n" );
void print_three_times ( char *text ) {
printf ( text );
print_two_times ( text + 1 );
}
=>
text = "Hello, World\n"
{
printf ( text );
print_two_times ( text + 1 );
}
=>
{
printf ( "Hello, World\n" );
print_two_times ( "Hello, World\n" + 1 );
}
*/
return 0;
}
/*
目的は、次の三つの文字列を出力(関数 printf に渡す)
"Hello, World\n"
"Hello, World\n" + 1
"Hello, World\n" + 2
手段は、
1. (print_three_timesの)text という変数に "Hello, World\n" をいれて、
2. まずは、text そのものを出力
3. (print_two_timesの)text に "Hello, World\n" + 1 をいれる
4. それを出力したあと、さらに + 1 (合計 +2 ) したものを出す
概観
変数 text に色々な値を入れながらプログラムが実行されている
最初は "Hello, World\n"
次は "Hello, World\n" + 1
[ポイント]
関数の仮引数変数は、関数が呼び出されるときにはじめて意味を持つ
異なる関数で同じ変数があっても違うもの
同じ関数でも、別の呼び出しでは、違うもの
になる
きもち
text, text + 1, text + 2
を出力したい。
一方、
printf ( text )
だけで、処理したい
p-008.c では、同じ変数名(text)に異なる値
( "Hel..", "Hel.." + 1, "Hel.." + 2 )
を指定するために、関数を三つ作った
同じ関数内の、同じ変数は、同じ値
=> 関数が異なれば、同じ名前でも異なる値が持てる
text に割り当てた複数の値は、同時には利用されない
変数 text の値は、一度に一つしか利用されていない
かつ、複数の値を利用したい場合
「代入」を使う。
*/
#include <stdio.h>
/*
課題:
Hello, World
ello, World
llo, World
という三行を表示したい
*/
/* 第四案 */
void print_one_times ( char *text ) {
printf ( text );
}
void print_two_times ( char *text ) {
printf ( text );
print_one_times ( text + 1 );
}
void print_three_times ( char *text ) {
printf ( text );
print_two_times ( text + 1 );
}
int main(int argc, char *argv[]) {
print_three_times ( "Hello, World\n" );
return 0;
}
#include <stdio.h>
void print_three_times ( char *text ) {
printf ( text );
/* 代入前の時点 : text には "Hello, World\n" が入っている */
text = text + 1; /* 代入 !! */
/* 変数 text の内容が、+1 したものに
上書きされる */
/* 代入後の時点 : text には "Hello, World\n" +1 が入っている */
printf ( text );
text = text + 1;
printf ( text );
}
int main(int argc, char *argv[] ) {
print_three_times ( "Hello, World\n" );
return 0;
}
#include <stdio.h>
void function ( char *argument ) {
/*
argument : 関数の引数を値とする変数
仮引数変数
argument の値は、関数を呼び出す度に異なる
*/
printf ( argument ); /* 引数の値を表示する */
}
int main(int argc, char *argv[]) {
function ( "Hello\n" ); /* 関数呼び出しの実引数の値が.. */
/*
実引数に値を指定すると、
関数の仮引数変数に代入されていた
仮引数変数は、関数呼び出しをするときに作られる
!! 関数呼び出しの前は存在しないし、値もない
!! 一度、設定されたら、変わらない
!! 変数の癖に、「変わらない」もの
!! 変数の値は変化してなかった
代入
同じ変数の内容を変更する
*/
function ( "World\n" );
return 0;
}
#include <stdio.h>
void ntimes (char *text) {
printf ( text ); /* "Hello, World\n" */
text = text + 1; /* text <- "Hello, World\n" + 1 */
/* <- "ello, World\n" */
/* 代入文の右側の式内の変数の値は、
代入文の実行前の変数の値が利用される */
printf ( text ); /* "ello, World\n" */
text = text + 1; /* text <- "ello, World\n" + 1 */
/* <- "llo, World\n" */
printf ( text ); /* "llo, World\n" */
text = text + 1; /* text <- "llo, World\n" + 1 */
/* <- "lo, World\n" */
printf ( text );
text = text + 1;
printf ( text );
text = text + 1;
}
int main(int argc, char *argv[] ) {
ntimes ( "Hello, World\n" );
return 0;
}
#include <stdio.h>
int main(int argc, char *argv[] ) {
char *text; /* 仮引数変数の宣言を、
main 関数の中に移動
-> 変数宣言
text という「変数」を宣言
この時に、必ず、
変数(が記録する情報の)型を記述する
今回は char * : 文字列
# 変数を利用するためだけに
# 関数を作るのは馬鹿らしいので、
# 関数を作らず、変数を利用する方法
*/
text = "Hello, World\n"; /* 変数に対する最初の代入 */
/* -> 変数の初期化 */
/* !!! 変数の初期化を忘れるバグ */
/* => 変数を宣言したらすぐ初期化 */
printf ( text ); /* "Hello, World\n" */
text = text + 1; /* text <- "Hello, World\n" + 1 */
/* <- "ello, World\n" */
/* 代入文の右側の式内の変数の値は、
代入文の実行前の変数の値が利用される */
printf ( text ); /* "ello, World\n" */
text = text + 1; /* text <- "ello, World\n" + 1 */
/* <- "llo, World\n" */
printf ( text ); /* "llo, World\n" */
text = text + 1; /* text <- "llo, World\n" + 1 */
/* <- "lo, World\n" */
printf ( text );
text = text + 1;
printf ( text );
text = text + 1;
return 0;
}
#include <stdio.h>
int main(int argc, char *argv[]) {
char *text;
printf ( text ); /* ?????? text ????????Y??o?O */
return 0;
}
Download : 20170922-01.c
/*
* 2017/09/22 20170922-01.c
*
* Hello, World (3 度目)
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
/*
** この部分を完成させなさい
*/
return 0;
}
$ ./20170922-01-QQQQ.exe Hello, World $
Download : 20170922-02.c
/*
* 2017/09/22 20170922-02.c
*
* printf による書式指定出力
*
*/
#include <stdio.h>
/*
*
*/
/*
* main
*/
int main( int argc, char *argv[] )
{
int ia = 7;
int ib = 15;
double da = 3.14;
double db = 1.59;
printf ( "/* o:ここ */ + /* o:ここ */ = /* o:ここ */\n", /* p:ここ */, /* q:ここ */, /* p:ここ */ + /* q:ここ */ ); /* 整数型の和 */
printf ( "/* r:ここ */ / /* r:ここ */ = /* r:ここ */\n", /* s:ここ */, /* u:ここ */, /* s:ここ */ / /* u:ここ */ ); /* 浮動小数点数の商 */
return 0;
}
$ ./20170922-02-QQQQ.exe 7 + 15 = 22 3.140000 / 1.590000 = 1.974843 $
Download : 20170922-03.c
/*
* 2017/09/22 20170922-03.c
*
* scanf による書式指定入力
*
*/
#include <stdio.h>
/*
*
*/
/*
* main
*/
int main( int argc, char *argv[] )
{
int i; /* キーボードから入力された整数値を保持する整数型変数 */
double d; /* キーボードから入力された浮動小数点数値を保持する浮動小数点数型変数 */
printf ( "整数値をキーボードから入力します : " );
scanf ( "/* p:ここ */", /* q:ここ */ );
printf ( "入力された整数値は %d でした。\n", i );
printf ( "浮動小数点数値をキーボードから入力します : " );
scanf ( "/* r:ここ */", /* s:ここ */ );
printf ( "入力された浮動小数点数値は %f でした。\n", d );
return 0;
}
123 456.789
$ ./20170922-03-QQQQ.exe 整数値をキーボードから入力します : 123 入力された整数値は 123 でした。 浮動小数点数値をキーボードから入力します : 456.789000 入力された浮動小数点数値は 456.789000 でした。 $
Download : 20170922-04.c
/*
* 2017/09/22 20170922-04.c
*
* 変数宣言と代入文
* 整数型変数 ( a, b, c ) を三つ宣言する
* 整数型変数 a, b にそれぞれ、123, 4 を代入する
* 整数型変数 c に a と b の和を代入する
* 整数型変数 c の値を画面に出力する
*/
#include <stdio.h>
#include "s_print.h"
/*
* main
*/
int main( int argc, char *argv[] )
{
/* 整数型変数 a の宣言 */
int a;
/* 整数型変数 b の宣言 */
/*
** この部分を完成させなさい
*/
/* 整数型変数 c の宣言 */
int c;
/* 整数型変数 a に 123 を代入 */
a = 123;
/* 整数型変数 b に 4 を代入 */
/*
** この部分を完成させなさい
*/
/* 整数型変数 c に、変数 a と 変数 b の和を代入 */
/*
** この部分を完成させなさい
*/
/* 整数型変数 c の値を画面に出力 */
s_print_string ( "変数 c の値は " );
s_print_int ( c );
s_print_string ( " です。\n" );
return 0;
}
$ ./20170922-04-QQQQ.exe 変数 c の値は 127 です。 $
Download : 20170922-05.c
/*
* 2017/09/22 20170922-05.c
*
* while 文
*/
#include <stdio.h>
#include "s_print.h"
/*
* main
*/
int main( int argc, char *argv[] )
{
/* 整数型変数 odd の宣言とその初期化 ( 初期値は 1 を指定 ) */
int odd = 1;
/* 整数型変数 sum の宣言とその初期化 ( 初期値は 0 を指定 ) */
int sum = 0;
/*
sum = 1 + 3 + .. + 9 = 25 = 5^2
*/
/* odd の値が 9 以下の間は繰り返す */
while ( /* q:ここ */ ) {
/* sum に odd の値を「追加」する */
/*
** この部分を完成させなさい
*/
/* odd の値を次の奇数にする */
odd = odd + 2;
}
/* 整数型変数 sum の値を画面に出力 */
s_print_string ( "1 から 9 までの奇数の和は " );
s_print_int ( sum );
s_print_string ( " です。\n" );
return 0;
}
$ ./20170922-05-QQQQ.exe 1 から 9 までの奇数の和は 25 です。 $