21
VIP 大学オンライン プログラミング入門 C 言語編

Introduction to C Language

  • Upload
    pikavip

  • View
    956

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to C Language

VIP 大学オンライン

プログラミング入門C 言語編

Page 2: Introduction to C Language

2

目標

プログラミング言語を使って実際に動くプログラムを作ってみる

C 言語を使用

C 言語の解説ではない→マクロ等は説明しない

3回(3時間)で一通り説明する予定

1回目の最後で C 言語の環境をインストール

内要1回:変数、算術演算、フロー制御2回:配列、ポインタ、関数3回:メモリ動的確保、関数ポインタ

Page 3: Introduction to C Language

3

開発環境インストール

C 言語の開発環境がない人はインストールしよう

Visual C++ Express Edition無料 & インストールが簡単http://www.microsoft.com/japan/msdn/vstudio/Express/DL に時間がかかるので、先に DL しておこう

Page 4: Introduction to C Language

4

プログラミング言語とは?

プログラムメモリ上の命令+データ手作業で作るのは大変

入力支援ツール=プログラミング言語

010010111...............................10100101010

Page 5: Introduction to C Language

5

C言語

C 言語は

もっとも有名なプログラミング言語の1つシンプルで使いやすいシンプルで学習に向いている

Page 6: Introduction to C Language

6

プログラムの流れ

上から順番に実行していきます

int main(void){

int i=0;for(int i=0;i<10;i++){

printf("%d\n",i);}i=0;while(i<10){

i=i+1;printf("%d\n",i);

}return 0;

}

Page 7: Introduction to C Language

7

変数

変数:データを記憶する場所

010010111...............................10100101010

int main(void){

int i=0;for(int i=0;i<10;i++){

printf("%d\n",i);}i=0;

}

i

Page 8: Introduction to C Language

8

変数のデータ型

整数 : int, long浮動小数点: double, float文字: char

int i=0;long abc=5;double def=0.5;float flo=0.3;char moji='a';

010010111...............................10100101010

Page 9: Introduction to C Language

9

値の代入

変数に値を入れる書式:変数名=値

int a=0;int b=1;int c=2;c=a+b;

Page 10: Introduction to C Language

10

算術演算

和: a=b+c差: a=b-c積: a=b*c商: a=b/c余り a=b%c符合反転 a=-b

問題

a=a+b はどうなるか?

Page 11: Introduction to C Language

11

フロー制御

プログラムは上から下へ実行するたまには戻ったり、移動したい

3つのフロー制御方法 ( ジャンプ命令 )forwhileif

Page 12: Introduction to C Language

12

for文

同じ命令を N 回繰り返したい場合for(A:B:C){ D}A :最初に1回だけ実行されるB : B が真なら D を実行、偽なら for から抜けるC : D の実行後に実行される

int k=1;for(int i=0 ; i<10 ; i++){

printf("%d+%d=%d\n",k,i,k+i);k=k+i;

}

Page 13: Introduction to C Language

13

while文

条件が満たされるまで処理を実行したい場合while (条件) {

命令}

int i=1;double k=1;

while(0.00001<k){printf("%d %f \n", i,k);

k=k/2;i=i+1;

}

Page 14: Introduction to C Language

14

BreakFor 文や while 文を終了させる→各自、自分で調べること

Page 15: Introduction to C Language

15

if文条件が成り立つときだけ実行if( 条件 ){ 命令}else{ 命令 }

double a=10,b=20;double answer=0;if(a<b){

answer=b-a;}else{

answer=a-b;}

printf("%f\n",answer);

Page 16: Introduction to C Language

16

条件に関する演算

>>=<<=== 等しい!= 等しくない

&& AND|| OR! NOT

Page 17: Introduction to C Language

17

その他の算術演算

a+=ba-=ba*=ba/=ba%=b

Page 18: Introduction to C Language

18

実践タイム

VC++ の場合

ファイル>新規作成>プロジェクト

Page 19: Introduction to C Language

19

実践タイム

Win32 コンソールアプリケーションを選択

プロジェクト名をつける

Page 20: Introduction to C Language

20

main関数

C 言語はメイン関数からはじまる

VC++ の場合、 _tmain 関数からはじまる

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[]){

return 0;}

ここにプログラムを書く

Page 21: Introduction to C Language

21

printf画面に変数の中身を表示

整数の場合printf(“%d\n”, 変数 )小数の場合printf(“%f\n”, 変数 )

%d や %f の部分が、変数の値と置き換わります

\n は改行です